Skip to content

Commit d0d0a35

Browse files
SyntevoAlexgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 568cabb commit d0d0a35

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ TEST_BUILTINS_OBJS += test-mktemp.o
721721
TEST_BUILTINS_OBJS += test-oidmap.o
722722
TEST_BUILTINS_OBJS += test-online-cpus.o
723723
TEST_BUILTINS_OBJS += test-parse-options.o
724+
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
724725
TEST_BUILTINS_OBJS += test-path-utils.o
725726
TEST_BUILTINS_OBJS += test-pkt-line.o
726727
TEST_BUILTINS_OBJS += test-prio-queue.o

t/helper/test-parse-pathspec-file.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 "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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static struct test_cmd cmds[] = {
3939
{ "oidmap", cmd__oidmap },
4040
{ "online-cpus", cmd__online_cpus },
4141
{ "parse-options", cmd__parse_options },
42+
{ "parse-pathspec-file", cmd__parse_pathspec_file },
4243
{ "path-utils", cmd__path_utils },
4344
{ "pkt-line", cmd__pkt_line },
4445
{ "prio-queue", cmd__prio_queue },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int cmd__mktemp(int argc, const char **argv);
2929
int cmd__oidmap(int argc, const char **argv);
3030
int cmd__online_cpus(int argc, const char **argv);
3131
int cmd__parse_options(int argc, const char **argv);
32+
int cmd__parse_pathspec_file(int argc, const char** argv);
3233
int cmd__path_utils(int argc, const char **argv);
3334
int cmd__pkt_line(int argc, const char **argv);
3435
int cmd__prio_queue(int argc, const char **argv);

t/t0067-parse_pathspec_file.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)