blob: 63a1a45cd30921a5b0d28b8d5db63046aeb2a3ea [file] [log] [blame]
Daniel Ferreira150791a2019-07-10 20:58:57 -03001#!/bin/sh
2
3test_description='Test the dir-iterator functionality'
4
Ævar Arnfjörð Bjarmasonfdc8f792021-10-12 15:56:38 +02005TEST_PASSES_SANITIZE_LEAK=true
Daniel Ferreira150791a2019-07-10 20:58:57 -03006. ./test-lib.sh
7
8test_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
22test_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
43test_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 Tavares30123972019-07-10 20:58:59 -030056test_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 Hamano90421402019-07-30 10:45:48 -070059 echo "dir_iterator_begin failure: ENOENT" >expected-inexistent-path-output &&
Matheus Tavares30123972019-07-10 20:58:59 -030060 test_cmp expected-inexistent-path-output actual-inexistent-path-output
61'
62
63test_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 Hamano90421402019-07-30 10:45:48 -070065 echo "dir_iterator_begin failure: ENOTDIR" >expected-non-dir-output &&
Matheus Tavares30123972019-07-10 20:58:59 -030066 test_cmp expected-non-dir-output actual-non-dir-output
67'
68
Matheus Tavaresfa1da7d2019-07-10 20:59:00 -030069test_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
84test_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
102test_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
116test_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
132test_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 Ferreira150791a2019-07-10 20:58:57 -0300149test_done