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 <bnmvco@gmail.com> [matheus.bernardino: update to use test-tool and some minor aesthetics] Helped-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Ferreira committed
Jul 10, 2019 at 20:58 UTC
150791adbf1eb432af0b895d81eef5c2717aa6cc
5 files changed
+91
Makefile
+1
@@ -710,6 +710,7 @@ TEST_BUILTINS_OBJS += test-config.o
710
TEST_BUILTINS_OBJS += test-ctype.o
711
TEST_BUILTINS_OBJS += test-date.o
712
TEST_BUILTINS_OBJS += test-delta.o
713
+TEST_BUILTINS_OBJS += test-dir-iterator.o
714
TEST_BUILTINS_OBJS += test-drop-caches.o
715
TEST_BUILTINS_OBJS += test-dump-cache-tree.o
716
TEST_BUILTINS_OBJS += test-dump-fsmonitor.o
t/helper/test-dir-iterator.c
new
+33
@@ -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
+1
@@ -19,6 +19,7 @@ static struct test_cmd cmds[] = {
19
{ "ctype", cmd__ctype },
20
{ "date", cmd__date },
21
{ "delta", cmd__delta },
22
+ { "dir-iterator", cmd__dir_iterator },
23
{ "drop-caches", cmd__drop_caches },
24
{ "dump-cache-tree", cmd__dump_cache_tree },
25
{ "dump-fsmonitor", cmd__dump_fsmonitor },
t/helper/test-tool.h
+1
@@ -9,6 +9,7 @@ int cmd__config(int argc, const char **argv);
9
int cmd__ctype(int argc, const char **argv);
10
int cmd__date(int argc, const char **argv);
11
int cmd__delta(int argc, const char **argv);
12
+int cmd__dir_iterator(int argc, const char **argv);
13
int cmd__drop_caches(int argc, const char **argv);
14
int cmd__dump_cache_tree(int argc, const char **argv);
15
int cmd__dump_fsmonitor(int argc, const char **argv);
t/t0066-dir-iterator.sh
new
+55
@@ -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