test-dir-iterator: do not assume errno values
authorJunio C Hamano <gitster@pobox.com>
Tue, 30 Jul 2019 17:45:48 +0000 (10:45 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 Jul 2019 17:45:48 +0000 (10:45 -0700)
A few tests printed 'errno' as an integer and compared with
hardcoded integers; this is obviously not portable.

A two things to note are:

- the string obtained by strerror() is not portable, and cannot be
used for the purpose of these tests.

- there unfortunately isn't a portable way to map error numbers to
error names.

As we only care about a few selected errors, just map the error
number to the name before emitting for comparison.

Reported-by: Randall S. Becker <rsbecker@nexbridge.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/helper/test-dir-iterator.c
t/t0066-dir-iterator.sh
index a5b96cb0dcfd0e5b493638bc5d6a310ffbf87db3..c7c30664dad2a027c4cf6b0bac7ecb78080cbcf1 100644 (file)
@@ -4,6 +4,15 @@
 #include "iterator.h"
 #include "dir-iterator.h"
 
+static const char *error_name(int error_number)
+{
+       switch (error_number) {
+       case ENOENT: return "ENOENT";
+       case ENOTDIR: return "ENOTDIR";
+       default: return "ESOMETHINGELSE";
+       }
+}
+
 /*
  * usage:
  * tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path
@@ -31,7 +40,7 @@ int cmd__dir_iterator(int argc, const char **argv)
        diter = dir_iterator_begin(path.buf, flags);
 
        if (!diter) {
-               printf("dir_iterator_begin failure: %d\n", errno);
+               printf("dir_iterator_begin failure: %s\n", error_name(errno));
                exit(EXIT_FAILURE);
        }
 
index 9354d3f1ed42aef99f6f7c3d27c572903c3617da..92910e4e6c13ce381dddefcf197dea26a07efd88 100755 (executable)
@@ -55,13 +55,13 @@ test_expect_success 'dir-iterator should list files in the correct order' '
 test_expect_success 'begin should fail upon inexistent paths' '
        test_must_fail test-tool dir-iterator ./inexistent-path \
                >actual-inexistent-path-output &&
-       echo "dir_iterator_begin failure: 2" >expected-inexistent-path-output &&
+       echo "dir_iterator_begin failure: ENOENT" >expected-inexistent-path-output &&
        test_cmp expected-inexistent-path-output actual-inexistent-path-output
 '
 
 test_expect_success 'begin should fail upon non directory paths' '
        test_must_fail test-tool dir-iterator ./dir/b >actual-non-dir-output &&
-       echo "dir_iterator_begin failure: 20" >expected-non-dir-output &&
+       echo "dir_iterator_begin failure: ENOTDIR" >expected-non-dir-output &&
        test_cmp expected-non-dir-output actual-non-dir-output
 '