Skip to content

Commit 591438e

Browse files
committed
t6601: add helper for testing path-walk API
Add some tests based on the current behavior, doing interesting checks for different sets of branches, ranges, and the --boundary option. This sets a baseline for the behavior and we can extend it as new options are introduced. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 8b6ba93 commit 591438e

File tree

6 files changed

+212
-1
lines changed

6 files changed

+212
-1
lines changed

Documentation/technical/api-path-walk.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ commits are emitted.
4949
Examples
5050
--------
5151

52-
See example usages in future changes.
52+
See example usages in:
53+
`t/helper/test-path-walk.c`

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ TEST_BUILTINS_OBJS += test-parse-options.o
822822
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
823823
TEST_BUILTINS_OBJS += test-partial-clone.o
824824
TEST_BUILTINS_OBJS += test-path-utils.o
825+
TEST_BUILTINS_OBJS += test-path-walk.o
825826
TEST_BUILTINS_OBJS += test-pcre2-config.o
826827
TEST_BUILTINS_OBJS += test-pkt-line.o
827828
TEST_BUILTINS_OBJS += test-proc-receive.o

t/helper/test-path-walk.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
3+
#include "test-tool.h"
4+
#include "environment.h"
5+
#include "hex.h"
6+
#include "object-name.h"
7+
#include "object.h"
8+
#include "pretty.h"
9+
#include "revision.h"
10+
#include "setup.h"
11+
#include "path-walk.h"
12+
#include "oid-array.h"
13+
14+
struct path_walk_test_data {
15+
uint32_t tree_nr;
16+
uint32_t blob_nr;
17+
};
18+
19+
static int emit_block(const char *path, struct oid_array *oids,
20+
enum object_type type, void *data)
21+
{
22+
struct path_walk_test_data *tdata = data;
23+
const char *typestr;
24+
25+
switch (type) {
26+
case OBJ_TREE:
27+
typestr = "TREE";
28+
tdata->tree_nr += oids->nr;
29+
break;
30+
31+
case OBJ_BLOB:
32+
typestr = "BLOB";
33+
tdata->blob_nr += oids->nr;
34+
break;
35+
36+
default:
37+
BUG("we do not understand this type");
38+
}
39+
40+
for (size_t i = 0; i < oids->nr; i++)
41+
printf("%s:%s:%s\n", typestr, path, oid_to_hex(&oids->oid[i]));
42+
43+
return 0;
44+
}
45+
46+
int cmd__path_walk(int argc, const char **argv)
47+
{
48+
int argi, res;
49+
struct rev_info revs = REV_INFO_INIT;
50+
struct path_walk_info info = PATH_WALK_INFO_INIT;
51+
struct path_walk_test_data data = { 0 };
52+
53+
initialize_repository(the_repository);
54+
setup_git_directory();
55+
revs.repo = the_repository;
56+
57+
for (argi = 0; argi < argc; argi++) {
58+
if (!strcmp(argv[argi], "--"))
59+
break;
60+
}
61+
62+
if (argi < argc)
63+
setup_revisions(argc - argi, argv + argi, &revs, NULL);
64+
else
65+
die("usage: test-tool path-walk <options> -- <rev opts>");
66+
67+
info.revs = &revs;
68+
info.path_fn = emit_block;
69+
info.path_fn_data = &data;
70+
71+
res = walk_objects_by_path(&info);
72+
73+
printf("trees:%d\nblobs:%d\n",
74+
data.tree_nr, data.blob_nr);
75+
76+
return res;
77+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static struct test_cmd cmds[] = {
5454
{ "parse-subcommand", cmd__parse_subcommand },
5555
{ "partial-clone", cmd__partial_clone },
5656
{ "path-utils", cmd__path_utils },
57+
{ "path-walk", cmd__path_walk },
5758
{ "pcre2-config", cmd__pcre2_config },
5859
{ "pkt-line", cmd__pkt_line },
5960
{ "proc-receive", cmd__proc_receive },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);
4747
int cmd__parse_subcommand(int argc, const char **argv);
4848
int cmd__partial_clone(int argc, const char **argv);
4949
int cmd__path_utils(int argc, const char **argv);
50+
int cmd__path_walk(int argc, const char **argv);
5051
int cmd__pcre2_config(int argc, const char **argv);
5152
int cmd__pkt_line(int argc, const char **argv);
5253
int cmd__proc_receive(int argc, const char **argv);

t/t6601-path-walk.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/sh
2+
3+
test_description='direct path-walk API tests'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup test repository' '
8+
git checkout -b base &&
9+
10+
mkdir left &&
11+
mkdir right &&
12+
echo a >a &&
13+
echo b >left/b &&
14+
echo c >right/c &&
15+
git add . &&
16+
git commit -m "first" &&
17+
18+
echo d >right/d &&
19+
git add right &&
20+
git commit -m "second" &&
21+
22+
echo bb >left/b &&
23+
git commit -a -m "third" &&
24+
25+
git checkout -b topic HEAD~1 &&
26+
echo cc >right/c &&
27+
git commit -a -m "topic"
28+
'
29+
30+
test_expect_success 'all' '
31+
test-tool path-walk -- --all >out &&
32+
33+
cat >expect <<-EOF &&
34+
TREE::$(git rev-parse topic^{tree})
35+
TREE::$(git rev-parse base^{tree})
36+
TREE::$(git rev-parse base~1^{tree})
37+
TREE::$(git rev-parse base~2^{tree})
38+
TREE:left/:$(git rev-parse base:left)
39+
TREE:left/:$(git rev-parse base~2:left)
40+
TREE:right/:$(git rev-parse topic:right)
41+
TREE:right/:$(git rev-parse base~1:right)
42+
TREE:right/:$(git rev-parse base~2:right)
43+
trees:9
44+
BLOB:a:$(git rev-parse base~2:a)
45+
BLOB:left/b:$(git rev-parse base~2:left/b)
46+
BLOB:left/b:$(git rev-parse base:left/b)
47+
BLOB:right/c:$(git rev-parse base~2:right/c)
48+
BLOB:right/c:$(git rev-parse topic:right/c)
49+
BLOB:right/d:$(git rev-parse base~1:right/d)
50+
blobs:6
51+
EOF
52+
53+
sort expect >expect.sorted &&
54+
sort out >out.sorted &&
55+
56+
test_cmp expect.sorted out.sorted
57+
'
58+
59+
test_expect_success 'topic only' '
60+
test-tool path-walk -- topic >out &&
61+
62+
cat >expect <<-EOF &&
63+
TREE::$(git rev-parse topic^{tree})
64+
TREE::$(git rev-parse base~1^{tree})
65+
TREE::$(git rev-parse base~2^{tree})
66+
TREE:left/:$(git rev-parse base~2:left)
67+
TREE:right/:$(git rev-parse topic:right)
68+
TREE:right/:$(git rev-parse base~1:right)
69+
TREE:right/:$(git rev-parse base~2:right)
70+
trees:7
71+
BLOB:a:$(git rev-parse base~2:a)
72+
BLOB:left/b:$(git rev-parse base~2:left/b)
73+
BLOB:right/c:$(git rev-parse base~2:right/c)
74+
BLOB:right/c:$(git rev-parse topic:right/c)
75+
BLOB:right/d:$(git rev-parse base~1:right/d)
76+
blobs:5
77+
EOF
78+
79+
sort expect >expect.sorted &&
80+
sort out >out.sorted &&
81+
82+
test_cmp expect.sorted out.sorted
83+
'
84+
85+
test_expect_success 'topic, not base' '
86+
test-tool path-walk -- topic --not base >out &&
87+
88+
cat >expect <<-EOF &&
89+
TREE::$(git rev-parse topic^{tree})
90+
TREE:left/:$(git rev-parse topic:left)
91+
TREE:right/:$(git rev-parse topic:right)
92+
trees:3
93+
BLOB:a:$(git rev-parse topic:a)
94+
BLOB:left/b:$(git rev-parse topic:left/b)
95+
BLOB:right/c:$(git rev-parse topic:right/c)
96+
BLOB:right/d:$(git rev-parse topic:right/d)
97+
blobs:4
98+
EOF
99+
100+
sort expect >expect.sorted &&
101+
sort out >out.sorted &&
102+
103+
test_cmp expect.sorted out.sorted
104+
'
105+
106+
test_expect_success 'topic, not base, boundary' '
107+
test-tool path-walk -- --boundary topic --not base >out &&
108+
109+
cat >expect <<-EOF &&
110+
TREE::$(git rev-parse topic^{tree})
111+
TREE::$(git rev-parse base~1^{tree})
112+
TREE:left/:$(git rev-parse base~1:left)
113+
TREE:right/:$(git rev-parse topic:right)
114+
TREE:right/:$(git rev-parse base~1:right)
115+
trees:5
116+
BLOB:a:$(git rev-parse base~1:a)
117+
BLOB:left/b:$(git rev-parse base~1:left/b)
118+
BLOB:right/c:$(git rev-parse base~1:right/c)
119+
BLOB:right/c:$(git rev-parse topic:right/c)
120+
BLOB:right/d:$(git rev-parse base~1:right/d)
121+
blobs:5
122+
EOF
123+
124+
sort expect >expect.sorted &&
125+
sort out >out.sorted &&
126+
127+
test_cmp expect.sorted out.sorted
128+
'
129+
130+
test_done

0 commit comments

Comments
 (0)