Skip to content

Commit df84412

Browse files
derrickstoleedscho
authored andcommitted
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 7ff4fe5 commit df84412

File tree

8 files changed

+223
-1
lines changed

8 files changed

+223
-1
lines changed

Documentation/technical/api-path-walk.txt

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

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

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ TEST_BUILTINS_OBJS += test-parse-options.o
826826
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
827827
TEST_BUILTINS_OBJS += test-partial-clone.o
828828
TEST_BUILTINS_OBJS += test-path-utils.o
829+
TEST_BUILTINS_OBJS += test-path-walk.o
829830
TEST_BUILTINS_OBJS += test-pcre2-config.o
830831
TEST_BUILTINS_OBJS += test-pkt-line.o
831832
TEST_BUILTINS_OBJS += test-proc-receive.o

t/helper/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ test_tool_sources = [
4141
'test-parse-pathspec-file.c',
4242
'test-partial-clone.c',
4343
'test-path-utils.c',
44+
'test-path-walk.c',
4445
'test-pcre2-config.c',
4546
'test-pkt-line.c',
4647
'test-proc-receive.c',

t/helper/test-path-walk.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 "parse-options.h"
12+
#include "path-walk.h"
13+
#include "oid-array.h"
14+
15+
static const char * const path_walk_usage[] = {
16+
N_("test-tool path-walk <options> -- <revision-options>"),
17+
NULL
18+
};
19+
20+
struct path_walk_test_data {
21+
uintmax_t tree_nr;
22+
uintmax_t blob_nr;
23+
};
24+
25+
static int emit_block(const char *path, struct oid_array *oids,
26+
enum object_type type, void *data)
27+
{
28+
struct path_walk_test_data *tdata = data;
29+
const char *typestr;
30+
31+
switch (type) {
32+
case OBJ_TREE:
33+
typestr = "TREE";
34+
tdata->tree_nr += oids->nr;
35+
break;
36+
37+
case OBJ_BLOB:
38+
typestr = "BLOB";
39+
tdata->blob_nr += oids->nr;
40+
break;
41+
42+
default:
43+
BUG("we do not understand this type");
44+
}
45+
46+
for (size_t i = 0; i < oids->nr; i++)
47+
printf("%s:%s:%s\n", typestr, path, oid_to_hex(&oids->oid[i]));
48+
49+
return 0;
50+
}
51+
52+
int cmd__path_walk(int argc, const char **argv)
53+
{
54+
int res;
55+
struct rev_info revs = REV_INFO_INIT;
56+
struct path_walk_info info = PATH_WALK_INFO_INIT;
57+
struct path_walk_test_data data = { 0 };
58+
struct option options[] = {
59+
OPT_END(),
60+
};
61+
62+
setup_git_directory();
63+
revs.repo = the_repository;
64+
65+
argc = parse_options(argc, argv, NULL,
66+
options, path_walk_usage,
67+
PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_ARGV0);
68+
69+
if (argc > 1)
70+
setup_revisions(argc, argv, &revs, NULL);
71+
else
72+
usage(path_walk_usage[0]);
73+
74+
info.revs = &revs;
75+
info.path_fn = emit_block;
76+
info.path_fn_data = &data;
77+
78+
res = walk_objects_by_path(&info);
79+
80+
printf("trees:%" PRIuMAX "\n"
81+
"blobs:%" PRIuMAX "\n",
82+
data.tree_nr, data.blob_nr);
83+
84+
release_revisions(&revs);
85+
return res;
86+
}

t/helper/test-tool.c

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

t/helper/test-tool.h

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

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ integration_tests = [
829829
't6500-gc.sh',
830830
't6501-freshen-objects.sh',
831831
't6600-test-reach.sh',
832+
't6601-path-walk.sh',
832833
't6700-tree-depth.sh',
833834
't7001-mv.sh',
834835
't7002-mv-sparse-checkout.sh',

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)