Re: [PATCH v9 1/5] dir_iterator: add tests for dir_iterator API

2017-04-17 Thread Junio C Hamano
Daniel Ferreira  writes:

> +cat >expect-sorted-output <<-\EOF &&
> +[d] (a) [a] ./dir/a
> +[d] (a/b) [b] ./dir/a/b
> +[d] (a/b/c) [c] ./dir/a/b/c
> +[d] (d) [d] ./dir/d
> +[d] (d/e) [e] ./dir/d/e
> +[d] (d/e/d) [d] ./dir/d/e/d
> +[f] (a/b/c/d) [d] ./dir/a/b/c/d
> +[f] (a/e) [e] ./dir/a/e
> +[f] (b) [b] ./dir/b
> +[f] (c) [c] ./dir/c
> +[f] (d/e/d/a) [a] ./dir/d/e/d/a
> +EOF
> +
> +test_expect_success 'dir-iterator should iterate through all files' '
> + test-dir-iterator ./dir >out &&
> + sort ./actual-pre-order-sorted-output &&
> +
> + test_cmp expect-sorted-output actual-pre-order-sorted-output
> +'

Modern tests create these test vectors inside test_expect_success
block, like so:

test_expect_success 'dir-iterator should iterate through all files' '
cat >expect-sorted-output <<-\EOF &&
[d] (a) [a] ./dir/a
[d] (a/b) [b] ./dir/a/b
[d] (a/b/c) [c] ./dir/a/b/c
[d] (d) [d] ./dir/d
[d] (d/e) [e] ./dir/d/e
[d] (d/e/d) [d] ./dir/d/e/d
[f] (a/b/c/d) [d] ./dir/a/b/c/d
[f] (a/e) [e] ./dir/a/e
[f] (b) [b] ./dir/b
[f] (c) [c] ./dir/c
[f] (d/e/d/a) [a] ./dir/d/e/d/a
EOF

test-dir-iterator ./dir >out &&
sort ./actual-pre-order-sorted-output &&

test_cmp expect-sorted-output actual-pre-order-sorted-output
'


Re: [PATCH v9 1/5] dir_iterator: add tests for dir_iterator API

2017-04-17 Thread Junio C Hamano
Daniel Ferreira  writes:

> +test_expect_success 'setup' '
> + mkdir -p dir &&
> + mkdir -p dir/a/b/c/ &&
> + >dir/b &&
> + >dir/c &&
> + mkdir -p dir/d/e/d/ &&
> + >dir/a/b/c/d &&
> + >dir/a/e &&
> + >dir/d/e/d/a &&
> +
> + mkdir -p dir2/a/b/c/ &&
> + >dir2/a/b/c/d
> +'
> +
> +cat >expect-sorted-output <<-\EOF &&
> +[d] (a) [a] ./dir/a
> +[d] (a/b) [b] ./dir/a/b
> +[d] (a/b/c) [c] ./dir/a/b/c
> +[d] (d) [d] ./dir/d
> +[d] (d/e) [e] ./dir/d/e
> +[d] (d/e/d) [d] ./dir/d/e/d
> +[f] (a/b/c/d) [d] ./dir/a/b/c/d
> +[f] (a/e) [e] ./dir/a/e
> +[f] (b) [b] ./dir/b
> +[f] (c) [c] ./dir/c
> +[f] (d/e/d/a) [a] ./dir/d/e/d/a
> +EOF
> +
> +test_expect_success 'dir-iterator should iterate through all files' '
> + test-dir-iterator ./dir >out &&
> + sort ./actual-pre-order-sorted-output &&
> +
> + test_cmp expect-sorted-output actual-pre-order-sorted-output
> +'

OK, if you can get multiple entries from a single directory, the
order of these entries are not known because they come in readdir()
order, and you'd need to sort the output to see if you got the same
thing.  Sort of makes sense.

> +
> +cat >expect-pre-order-output <<-\EOF &&
> +[d] (a) [a] ./dir2/a
> +[d] (a/b) [b] ./dir2/a/b
> +[d] (a/b/c) [c] ./dir2/a/b/c
> +[f] (a/b/c/d) [d] ./dir2/a/b/c/d
> +EOF
> +
> +test_expect_success 'dir-iterator should list files in the correct order' '
> + test-dir-iterator ./dir2 >actual-pre-order-output &&
> +
> + test_cmp expect-pre-order-output actual-pre-order-output
> +'

And this example has only one item per each recursion level, so the
order of output is predictable.  Again, makes sense.


[PATCH v9 1/5] dir_iterator: add tests for dir_iterator API

2017-04-17 Thread Daniel Ferreira
Create t/helper/test-dir-iterator.c, which prints relevant information
about a directory tree iterated over with dir_iterator.

Create t/t0065-dir-iterator.sh, which tests that dir_iterator does
iterate through a whole directory tree.

Signed-off-by: Daniel Ferreira 
---
 Makefile |  1 +
 t/helper/.gitignore  |  1 +
 t/helper/test-dir-iterator.c | 30 
 t/t0065-dir-iterator.sh  | 55 
 4 files changed, 87 insertions(+)
 create mode 100644 t/helper/test-dir-iterator.c
 create mode 100755 t/t0065-dir-iterator.sh

diff --git a/Makefile b/Makefile
index d595ea3..a5c1ac0 100644
--- a/Makefile
+++ b/Makefile
@@ -614,6 +614,7 @@ TEST_PROGRAMS_NEED_X += test-ctype
 TEST_PROGRAMS_NEED_X += test-config
 TEST_PROGRAMS_NEED_X += test-date
 TEST_PROGRAMS_NEED_X += test-delta
+TEST_PROGRAMS_NEED_X += test-dir-iterator
 TEST_PROGRAMS_NEED_X += test-dump-cache-tree
 TEST_PROGRAMS_NEED_X += test-dump-split-index
 TEST_PROGRAMS_NEED_X += test-dump-untracked-cache
diff --git a/t/helper/.gitignore b/t/helper/.gitignore
index 758ed2e..a7d74d3 100644
--- a/t/helper/.gitignore
+++ b/t/helper/.gitignore
@@ -3,6 +3,7 @@
 /test-config
 /test-date
 /test-delta
+/test-dir-iterator
 /test-dump-cache-tree
 /test-dump-split-index
 /test-dump-untracked-cache
diff --git a/t/helper/test-dir-iterator.c b/t/helper/test-dir-iterator.c
new file mode 100644
index 000..a7d1470
--- /dev/null
+++ b/t/helper/test-dir-iterator.c
@@ -0,0 +1,30 @@
+#include "git-compat-util.h"
+#include "strbuf.h"
+#include "iterator.h"
+#include "dir-iterator.h"
+
+int cmd_main(int argc, const char **argv)
+{
+   struct strbuf path = STRBUF_INIT;
+   struct dir_iterator *diter;
+
+   if (argc < 2)
+   die("BUG: test-dir-iterator needs one argument");
+
+   strbuf_add(, argv[1], strlen(argv[1]));
+
+   diter = dir_iterator_begin(path.buf);
+
+   while (dir_iterator_advance(diter) == ITER_OK) {
+   if (S_ISDIR(diter->st.st_mode))
+   printf("[d] ");
+   else if (S_ISREG(diter->st.st_mode))
+   printf("[f] ");
+   else
+   printf("[?] ");
+
+   printf("(%s) [%s] %s\n", diter->relative_path, diter->basename, 
diter->path.buf);
+   }
+
+   return 0;
+}
diff --git a/t/t0065-dir-iterator.sh b/t/t0065-dir-iterator.sh
new file mode 100755
index 000..b6d76dd
--- /dev/null
+++ b/t/t0065-dir-iterator.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+test_description='Test directory iteration.'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+   mkdir -p dir &&
+   mkdir -p dir/a/b/c/ &&
+   >dir/b &&
+   >dir/c &&
+   mkdir -p dir/d/e/d/ &&
+   >dir/a/b/c/d &&
+   >dir/a/e &&
+   >dir/d/e/d/a &&
+
+   mkdir -p dir2/a/b/c/ &&
+   >dir2/a/b/c/d
+'
+
+cat >expect-sorted-output <<-\EOF &&
+[d] (a) [a] ./dir/a
+[d] (a/b) [b] ./dir/a/b
+[d] (a/b/c) [c] ./dir/a/b/c
+[d] (d) [d] ./dir/d
+[d] (d/e) [e] ./dir/d/e
+[d] (d/e/d) [d] ./dir/d/e/d
+[f] (a/b/c/d) [d] ./dir/a/b/c/d
+[f] (a/e) [e] ./dir/a/e
+[f] (b) [b] ./dir/b
+[f] (c) [c] ./dir/c
+[f] (d/e/d/a) [a] ./dir/d/e/d/a
+EOF
+
+test_expect_success 'dir-iterator should iterate through all files' '
+   test-dir-iterator ./dir >out &&
+   sort ./actual-pre-order-sorted-output &&
+
+   test_cmp expect-sorted-output actual-pre-order-sorted-output
+'
+
+cat >expect-pre-order-output <<-\EOF &&
+[d] (a) [a] ./dir2/a
+[d] (a/b) [b] ./dir2/a/b
+[d] (a/b/c) [c] ./dir2/a/b/c
+[f] (a/b/c/d) [d] ./dir2/a/b/c/d
+EOF
+
+test_expect_success 'dir-iterator should list files in the correct order' '
+   test-dir-iterator ./dir2 >actual-pre-order-output &&
+
+   test_cmp expect-pre-order-output actual-pre-order-output
+'
+
+test_done
-- 
2.7.4 (Apple Git-66)