t: directly test parse_pathspec_file()
Previously, `parse_pathspec_file()` was tested indirectly by invoking git commands with properly crafted inputs. As demonstrated by the previous bugfix, testing complicated black boxes indirectly can lead to tests that silently test the wrong thing. Introduce direct tests for `parse_pathspec_file()`. Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alexandr Miloslavskiy committed
Dec 31, 2019 at 10:15 UTC
d0d0a357a18c5ce2e9586db28de753309e19f282
5 files changed
+144
Makefile
+1
@@ -721,6 +721,7 @@ TEST_BUILTINS_OBJS += test-mktemp.o
721
TEST_BUILTINS_OBJS += test-oidmap.o
722
TEST_BUILTINS_OBJS += test-online-cpus.o
723
TEST_BUILTINS_OBJS += test-parse-options.o
724
+TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
725
TEST_BUILTINS_OBJS += test-path-utils.o
726
TEST_BUILTINS_OBJS += test-pkt-line.o
727
TEST_BUILTINS_OBJS += test-prio-queue.o
t/helper/test-parse-pathspec-file.c
new
+33
@@ -0,0 +1,33 @@
1
+#include "test-tool.h"
2
+#include "parse-options.h"
3
+#include "pathspec.h"
4
+#include "gettext.h"
5
+
6
+int cmd__parse_pathspec_file(int argc, const char **argv)
7
+{
8
+ struct pathspec pathspec;
9
+ const char *pathspec_from_file = 0;
10
+ int pathspec_file_nul = 0, i;
11
+
12
+ static const char *const usage[] = {
13
+ "test-tool parse-pathspec-file --pathspec-from-file [--pathspec-file-nul]",
14
+ NULL
15
+ };
16
+
17
+ struct option options[] = {
18
+ OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
19
+ OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
20
+ OPT_END()
21
+ };
22
+
23
+ parse_options(argc, argv, 0, options, usage, 0);
24
+
25
+ parse_pathspec_file(&pathspec, 0, 0, 0, pathspec_from_file,
26
+ pathspec_file_nul);
27
+
28
+ for (i = 0; i < pathspec.nr; i++)
29
+ printf("%s\n", pathspec.items[i].original);
30
+
31
+ clear_pathspec(&pathspec);
32
+ return 0;
33
+}
t/helper/test-tool.c
+1
@@ -39,6 +39,7 @@ static struct test_cmd cmds[] = {
39
{ "oidmap", cmd__oidmap },
40
{ "online-cpus", cmd__online_cpus },
41
{ "parse-options", cmd__parse_options },
42
+ { "parse-pathspec-file", cmd__parse_pathspec_file },
43
{ "path-utils", cmd__path_utils },
44
{ "pkt-line", cmd__pkt_line },
45
{ "prio-queue", cmd__prio_queue },
t/helper/test-tool.h
+1
@@ -29,6 +29,7 @@ int cmd__mktemp(int argc, const char **argv);
29
int cmd__oidmap(int argc, const char **argv);
30
int cmd__online_cpus(int argc, const char **argv);
31
int cmd__parse_options(int argc, const char **argv);
32
+int cmd__parse_pathspec_file(int argc, const char** argv);
33
int cmd__path_utils(int argc, const char **argv);
34
int cmd__pkt_line(int argc, const char **argv);
35
int cmd__prio_queue(int argc, const char **argv);
t/t0067-parse_pathspec_file.sh
new
+108
@@ -0,0 +1,108 @@
1
+#!/bin/sh
2
+
3
+test_description='Test parse_pathspec_file()'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success 'one item from stdin' '
8
+ cat >expect <<-\EOF &&
9
+ fileA.t
10
+ EOF
11
+
12
+ echo fileA.t |
13
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
14
+
15
+ test_cmp expect actual
16
+'
17
+
18
+test_expect_success 'one item from file' '
19
+ cat >expect <<-\EOF &&
20
+ fileA.t
21
+ EOF
22
+
23
+ echo fileA.t >list &&
24
+ test-tool parse-pathspec-file --pathspec-from-file=list >actual &&
25
+
26
+ test_cmp expect actual
27
+'
28
+
29
+test_expect_success 'NUL delimiters' '
30
+ cat >expect <<-\EOF &&
31
+ fileA.t
32
+ fileB.t
33
+ EOF
34
+
35
+ printf "fileA.t\0fileB.t\0" |
36
+ test-tool parse-pathspec-file --pathspec-from-file=- --pathspec-file-nul >actual &&
37
+
38
+ test_cmp expect actual
39
+'
40
+
41
+test_expect_success 'LF delimiters' '
42
+ cat >expect <<-\EOF &&
43
+ fileA.t
44
+ fileB.t
45
+ EOF
46
+
47
+ printf "fileA.t\nfileB.t\n" |
48
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
49
+
50
+ test_cmp expect actual
51
+'
52
+
53
+test_expect_success 'no trailing delimiter' '
54
+ cat >expect <<-\EOF &&
55
+ fileA.t
56
+ fileB.t
57
+ EOF
58
+
59
+ printf "fileA.t\nfileB.t" |
60
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
61
+
62
+ test_cmp expect actual
63
+'
64
+
65
+test_expect_success 'CRLF delimiters' '
66
+ cat >expect <<-\EOF &&
67
+ fileA.t
68
+ fileB.t
69
+ EOF
70
+
71
+ printf "fileA.t\r\nfileB.t\r\n" |
72
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
73
+
74
+ test_cmp expect actual
75
+'
76
+
77
+test_expect_success 'quotes' '
78
+ cat >expect <<-\EOF &&
79
+ fileA.t
80
+ EOF
81
+
82
+ cat >list <<-\EOF &&
83
+ "file\101.t"
84
+ EOF
85
+
86
+ test-tool parse-pathspec-file --pathspec-from-file=list >actual &&
87
+
88
+ test_cmp expect actual
89
+'
90
+
91
+test_expect_success '--pathspec-file-nul takes quotes literally' '
92
+ # Note: there is an extra newline because --pathspec-file-nul takes
93
+ # input \n literally, too
94
+ cat >expect <<-\EOF &&
95
+ "file\101.t"
96
+
97
+ EOF
98
+
99
+ cat >list <<-\EOF &&
100
+ "file\101.t"
101
+ EOF
102
+
103
+ test-tool parse-pathspec-file --pathspec-from-file=list --pathspec-file-nul >actual &&
104
+
105
+ test_cmp expect actual
106
+'
107
+
108
+test_done