Skip to content

Commit 150791a

Browse files
theiostreamgitster
authored andcommitted
dir-iterator: add tests for dir-iterator API
Create t/helper/test-dir-iterator.c, which prints relevant information about a directory tree iterated over with dir-iterator. Create t/t0066-dir-iterator.sh, which tests that dir-iterator does iterate through a whole directory tree as expected. Signed-off-by: Daniel Ferreira <[email protected]> [matheus.bernardino: update to use test-tool and some minor aesthetics] Helped-by: Matheus Tavares <[email protected]> Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 36596fd commit 150791a

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ TEST_BUILTINS_OBJS += test-config.o
710710
TEST_BUILTINS_OBJS += test-ctype.o
711711
TEST_BUILTINS_OBJS += test-date.o
712712
TEST_BUILTINS_OBJS += test-delta.o
713+
TEST_BUILTINS_OBJS += test-dir-iterator.o
713714
TEST_BUILTINS_OBJS += test-drop-caches.o
714715
TEST_BUILTINS_OBJS += test-dump-cache-tree.o
715716
TEST_BUILTINS_OBJS += test-dump-fsmonitor.o

t/helper/test-dir-iterator.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "test-tool.h"
2+
#include "git-compat-util.h"
3+
#include "strbuf.h"
4+
#include "iterator.h"
5+
#include "dir-iterator.h"
6+
7+
/* Argument is a directory path to iterate over */
8+
int cmd__dir_iterator(int argc, const char **argv)
9+
{
10+
struct strbuf path = STRBUF_INIT;
11+
struct dir_iterator *diter;
12+
13+
if (argc < 2)
14+
die("BUG: test-dir-iterator needs one argument");
15+
16+
strbuf_add(&path, argv[1], strlen(argv[1]));
17+
18+
diter = dir_iterator_begin(path.buf);
19+
20+
while (dir_iterator_advance(diter) == ITER_OK) {
21+
if (S_ISDIR(diter->st.st_mode))
22+
printf("[d] ");
23+
else if (S_ISREG(diter->st.st_mode))
24+
printf("[f] ");
25+
else
26+
printf("[?] ");
27+
28+
printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
29+
diter->path.buf);
30+
}
31+
32+
return 0;
33+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static struct test_cmd cmds[] = {
1919
{ "ctype", cmd__ctype },
2020
{ "date", cmd__date },
2121
{ "delta", cmd__delta },
22+
{ "dir-iterator", cmd__dir_iterator },
2223
{ "drop-caches", cmd__drop_caches },
2324
{ "dump-cache-tree", cmd__dump_cache_tree },
2425
{ "dump-fsmonitor", cmd__dump_fsmonitor },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ int cmd__config(int argc, const char **argv);
99
int cmd__ctype(int argc, const char **argv);
1010
int cmd__date(int argc, const char **argv);
1111
int cmd__delta(int argc, const char **argv);
12+
int cmd__dir_iterator(int argc, const char **argv);
1213
int cmd__drop_caches(int argc, const char **argv);
1314
int cmd__dump_cache_tree(int argc, const char **argv);
1415
int cmd__dump_fsmonitor(int argc, const char **argv);

t/t0066-dir-iterator.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/sh
2+
3+
test_description='Test the dir-iterator functionality'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
mkdir -p dir &&
9+
mkdir -p dir/a/b/c/ &&
10+
>dir/b &&
11+
>dir/c &&
12+
mkdir -p dir/d/e/d/ &&
13+
>dir/a/b/c/d &&
14+
>dir/a/e &&
15+
>dir/d/e/d/a &&
16+
17+
mkdir -p dir2/a/b/c/ &&
18+
>dir2/a/b/c/d
19+
'
20+
21+
test_expect_success 'dir-iterator should iterate through all files' '
22+
cat >expected-iteration-sorted-output <<-EOF &&
23+
[d] (a) [a] ./dir/a
24+
[d] (a/b) [b] ./dir/a/b
25+
[d] (a/b/c) [c] ./dir/a/b/c
26+
[d] (d) [d] ./dir/d
27+
[d] (d/e) [e] ./dir/d/e
28+
[d] (d/e/d) [d] ./dir/d/e/d
29+
[f] (a/b/c/d) [d] ./dir/a/b/c/d
30+
[f] (a/e) [e] ./dir/a/e
31+
[f] (b) [b] ./dir/b
32+
[f] (c) [c] ./dir/c
33+
[f] (d/e/d/a) [a] ./dir/d/e/d/a
34+
EOF
35+
36+
test-tool dir-iterator ./dir >out &&
37+
sort out >./actual-iteration-sorted-output &&
38+
39+
test_cmp expected-iteration-sorted-output actual-iteration-sorted-output
40+
'
41+
42+
test_expect_success 'dir-iterator should list files in the correct order' '
43+
cat >expected-pre-order-output <<-EOF &&
44+
[d] (a) [a] ./dir2/a
45+
[d] (a/b) [b] ./dir2/a/b
46+
[d] (a/b/c) [c] ./dir2/a/b/c
47+
[f] (a/b/c/d) [d] ./dir2/a/b/c/d
48+
EOF
49+
50+
test-tool dir-iterator ./dir2 >actual-pre-order-output &&
51+
52+
test_cmp expected-pre-order-output actual-pre-order-output
53+
'
54+
55+
test_done

0 commit comments

Comments
 (0)