Daniel Ferreira | 150791a | 2019-07-10 20:58:57 -0300 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Test the dir-iterator functionality' |
| 4 | |
Ævar Arnfjörð Bjarmason | fdc8f79 | 2021-10-12 15:56:38 +0200 | [diff] [blame] | 5 | TEST_PASSES_SANITIZE_LEAK=true |
Daniel Ferreira | 150791a | 2019-07-10 20:58:57 -0300 | [diff] [blame] | 6 | . ./test-lib.sh |
| 7 | |
| 8 | test_expect_success 'setup' ' |
| 9 | mkdir -p dir && |
| 10 | mkdir -p dir/a/b/c/ && |
| 11 | >dir/b && |
| 12 | >dir/c && |
| 13 | mkdir -p dir/d/e/d/ && |
| 14 | >dir/a/b/c/d && |
| 15 | >dir/a/e && |
| 16 | >dir/d/e/d/a && |
| 17 | |
| 18 | mkdir -p dir2/a/b/c/ && |
| 19 | >dir2/a/b/c/d |
| 20 | ' |
| 21 | |
| 22 | test_expect_success 'dir-iterator should iterate through all files' ' |
| 23 | cat >expected-iteration-sorted-output <<-EOF && |
| 24 | [d] (a) [a] ./dir/a |
| 25 | [d] (a/b) [b] ./dir/a/b |
| 26 | [d] (a/b/c) [c] ./dir/a/b/c |
| 27 | [d] (d) [d] ./dir/d |
| 28 | [d] (d/e) [e] ./dir/d/e |
| 29 | [d] (d/e/d) [d] ./dir/d/e/d |
| 30 | [f] (a/b/c/d) [d] ./dir/a/b/c/d |
| 31 | [f] (a/e) [e] ./dir/a/e |
| 32 | [f] (b) [b] ./dir/b |
| 33 | [f] (c) [c] ./dir/c |
| 34 | [f] (d/e/d/a) [a] ./dir/d/e/d/a |
| 35 | EOF |
| 36 | |
| 37 | test-tool dir-iterator ./dir >out && |
| 38 | sort out >./actual-iteration-sorted-output && |
| 39 | |
| 40 | test_cmp expected-iteration-sorted-output actual-iteration-sorted-output |
| 41 | ' |
| 42 | |
| 43 | test_expect_success 'dir-iterator should list files in the correct order' ' |
| 44 | cat >expected-pre-order-output <<-EOF && |
| 45 | [d] (a) [a] ./dir2/a |
| 46 | [d] (a/b) [b] ./dir2/a/b |
| 47 | [d] (a/b/c) [c] ./dir2/a/b/c |
| 48 | [f] (a/b/c/d) [d] ./dir2/a/b/c/d |
| 49 | EOF |
| 50 | |
| 51 | test-tool dir-iterator ./dir2 >actual-pre-order-output && |
| 52 | |
| 53 | test_cmp expected-pre-order-output actual-pre-order-output |
| 54 | ' |
| 55 | |
Matheus Tavares | 3012397 | 2019-07-10 20:58:59 -0300 | [diff] [blame] | 56 | test_expect_success 'begin should fail upon inexistent paths' ' |
| 57 | test_must_fail test-tool dir-iterator ./inexistent-path \ |
| 58 | >actual-inexistent-path-output && |
Junio C Hamano | 9042140 | 2019-07-30 10:45:48 -0700 | [diff] [blame] | 59 | echo "dir_iterator_begin failure: ENOENT" >expected-inexistent-path-output && |
Matheus Tavares | 3012397 | 2019-07-10 20:58:59 -0300 | [diff] [blame] | 60 | test_cmp expected-inexistent-path-output actual-inexistent-path-output |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'begin should fail upon non directory paths' ' |
| 64 | test_must_fail test-tool dir-iterator ./dir/b >actual-non-dir-output && |
Junio C Hamano | 9042140 | 2019-07-30 10:45:48 -0700 | [diff] [blame] | 65 | echo "dir_iterator_begin failure: ENOTDIR" >expected-non-dir-output && |
Matheus Tavares | 3012397 | 2019-07-10 20:58:59 -0300 | [diff] [blame] | 66 | test_cmp expected-non-dir-output actual-non-dir-output |
| 67 | ' |
| 68 | |
Matheus Tavares | fa1da7d | 2019-07-10 20:59:00 -0300 | [diff] [blame] | 69 | test_expect_success POSIXPERM,SANITY 'advance should not fail on errors by default' ' |
| 70 | cat >expected-no-permissions-output <<-EOF && |
| 71 | [d] (a) [a] ./dir3/a |
| 72 | EOF |
| 73 | |
| 74 | mkdir -p dir3/a && |
| 75 | >dir3/a/b && |
| 76 | chmod 0 dir3/a && |
| 77 | |
| 78 | test-tool dir-iterator ./dir3 >actual-no-permissions-output && |
| 79 | test_cmp expected-no-permissions-output actual-no-permissions-output && |
| 80 | chmod 755 dir3/a && |
| 81 | rm -rf dir3 |
| 82 | ' |
| 83 | |
| 84 | test_expect_success POSIXPERM,SANITY 'advance should fail on errors, w/ pedantic flag' ' |
| 85 | cat >expected-no-permissions-pedantic-output <<-EOF && |
| 86 | [d] (a) [a] ./dir3/a |
| 87 | dir_iterator_advance failure |
| 88 | EOF |
| 89 | |
| 90 | mkdir -p dir3/a && |
| 91 | >dir3/a/b && |
| 92 | chmod 0 dir3/a && |
| 93 | |
| 94 | test_must_fail test-tool dir-iterator --pedantic ./dir3 \ |
| 95 | >actual-no-permissions-pedantic-output && |
| 96 | test_cmp expected-no-permissions-pedantic-output \ |
| 97 | actual-no-permissions-pedantic-output && |
| 98 | chmod 755 dir3/a && |
| 99 | rm -rf dir3 |
| 100 | ' |
| 101 | |
| 102 | test_expect_success SYMLINKS 'setup dirs with symlinks' ' |
| 103 | mkdir -p dir4/a && |
| 104 | mkdir -p dir4/b/c && |
| 105 | >dir4/a/d && |
| 106 | ln -s d dir4/a/e && |
| 107 | ln -s ../b dir4/a/f && |
| 108 | |
| 109 | mkdir -p dir5/a/b && |
| 110 | mkdir -p dir5/a/c && |
| 111 | ln -s ../c dir5/a/b/d && |
| 112 | ln -s ../ dir5/a/b/e && |
| 113 | ln -s ../../ dir5/a/b/f |
| 114 | ' |
| 115 | |
| 116 | test_expect_success SYMLINKS 'dir-iterator should not follow symlinks by default' ' |
| 117 | cat >expected-no-follow-sorted-output <<-EOF && |
| 118 | [d] (a) [a] ./dir4/a |
| 119 | [d] (b) [b] ./dir4/b |
| 120 | [d] (b/c) [c] ./dir4/b/c |
| 121 | [f] (a/d) [d] ./dir4/a/d |
| 122 | [s] (a/e) [e] ./dir4/a/e |
| 123 | [s] (a/f) [f] ./dir4/a/f |
| 124 | EOF |
| 125 | |
| 126 | test-tool dir-iterator ./dir4 >out && |
| 127 | sort out >actual-no-follow-sorted-output && |
| 128 | |
| 129 | test_cmp expected-no-follow-sorted-output actual-no-follow-sorted-output |
| 130 | ' |
| 131 | |
| 132 | test_expect_success SYMLINKS 'dir-iterator should follow symlinks w/ follow flag' ' |
| 133 | cat >expected-follow-sorted-output <<-EOF && |
| 134 | [d] (a) [a] ./dir4/a |
| 135 | [d] (a/f) [f] ./dir4/a/f |
| 136 | [d] (a/f/c) [c] ./dir4/a/f/c |
| 137 | [d] (b) [b] ./dir4/b |
| 138 | [d] (b/c) [c] ./dir4/b/c |
| 139 | [f] (a/d) [d] ./dir4/a/d |
| 140 | [f] (a/e) [e] ./dir4/a/e |
| 141 | EOF |
| 142 | |
| 143 | test-tool dir-iterator --follow-symlinks ./dir4 >out && |
| 144 | sort out >actual-follow-sorted-output && |
| 145 | |
| 146 | test_cmp expected-follow-sorted-output actual-follow-sorted-output |
| 147 | ' |
| 148 | |
Daniel Ferreira | 150791a | 2019-07-10 20:58:57 -0300 | [diff] [blame] | 149 | test_done |