Skip to content

Commit 6f93dff

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. It is important to mention that the behavior of the API will change soon as we start to handle UNINTERESTING objects differently, but these tests will demonstrate the change in behavior. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 4f9f898 commit 6f93dff

File tree

6 files changed

+209
-1
lines changed

6 files changed

+209
-1
lines changed

Documentation/technical/api-path-walk.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ commits.
4242
Examples
4343
--------
4444

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

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ TEST_BUILTINS_OBJS += test-parse-options.o
818818
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
819819
TEST_BUILTINS_OBJS += test-partial-clone.o
820820
TEST_BUILTINS_OBJS += test-path-utils.o
821+
TEST_BUILTINS_OBJS += test-path-walk.o
821822
TEST_BUILTINS_OBJS += test-pcre2-config.o
822823
TEST_BUILTINS_OBJS += test-pkt-line.o
823824
TEST_BUILTINS_OBJS += test-proc-receive.o

t/helper/test-path-walk.c

+86
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+
initialize_repository(the_repository);
63+
setup_git_directory();
64+
revs.repo = the_repository;
65+
66+
argc = parse_options(argc, argv, NULL,
67+
options, path_walk_usage,
68+
PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_ARGV0);
69+
70+
if (argc > 1)
71+
setup_revisions(argc, argv, &revs, NULL);
72+
else
73+
usage(path_walk_usage[0]);
74+
75+
info.revs = &revs;
76+
info.path_fn = emit_block;
77+
info.path_fn_data = &data;
78+
79+
res = walk_objects_by_path(&info);
80+
81+
printf("trees:%" PRIuMAX "\n"
82+
"blobs:%" PRIuMAX "\n",
83+
data.tree_nr, data.blob_nr);
84+
85+
return res;
86+
}

t/helper/test-tool.c

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static struct test_cmd cmds[] = {
5252
{ "parse-subcommand", cmd__parse_subcommand },
5353
{ "partial-clone", cmd__partial_clone },
5454
{ "path-utils", cmd__path_utils },
55+
{ "path-walk", cmd__path_walk },
5556
{ "pcre2-config", cmd__pcre2_config },
5657
{ "pkt-line", cmd__pkt_line },
5758
{ "proc-receive", cmd__proc_receive },

t/helper/test-tool.h

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);
4545
int cmd__parse_subcommand(int argc, const char **argv);
4646
int cmd__partial_clone(int argc, const char **argv);
4747
int cmd__path_utils(int argc, const char **argv);
48+
int cmd__path_walk(int argc, const char **argv);
4849
int cmd__pcre2_config(int argc, const char **argv);
4950
int cmd__pkt_line(int argc, const char **argv);
5051
int cmd__proc_receive(int argc, const char **argv);

t/t6601-path-walk.sh

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
test_cmp_sorted expect out
54+
'
55+
56+
test_expect_success 'topic only' '
57+
test-tool path-walk -- topic >out &&
58+
59+
cat >expect <<-EOF &&
60+
TREE::$(git rev-parse topic^{tree})
61+
TREE::$(git rev-parse base~1^{tree})
62+
TREE::$(git rev-parse base~2^{tree})
63+
TREE:left/:$(git rev-parse base~2:left)
64+
TREE:right/:$(git rev-parse topic:right)
65+
TREE:right/:$(git rev-parse base~1:right)
66+
TREE:right/:$(git rev-parse base~2:right)
67+
trees:7
68+
BLOB:a:$(git rev-parse base~2:a)
69+
BLOB:left/b:$(git rev-parse base~2:left/b)
70+
BLOB:right/c:$(git rev-parse base~2:right/c)
71+
BLOB:right/c:$(git rev-parse topic:right/c)
72+
BLOB:right/d:$(git rev-parse base~1:right/d)
73+
blobs:5
74+
EOF
75+
76+
test_cmp_sorted expect out
77+
'
78+
79+
test_expect_success 'topic, not base' '
80+
test-tool path-walk -- topic --not base >out &&
81+
82+
cat >expect <<-EOF &&
83+
TREE::$(git rev-parse topic^{tree})
84+
TREE:left/:$(git rev-parse topic:left)
85+
TREE:right/:$(git rev-parse topic:right)
86+
trees:3
87+
BLOB:a:$(git rev-parse topic:a)
88+
BLOB:left/b:$(git rev-parse topic:left/b)
89+
BLOB:right/c:$(git rev-parse topic:right/c)
90+
BLOB:right/d:$(git rev-parse topic:right/d)
91+
blobs:4
92+
EOF
93+
94+
test_cmp_sorted expect out
95+
'
96+
97+
test_expect_success 'topic, not base, boundary' '
98+
test-tool path-walk -- --boundary topic --not base >out &&
99+
100+
cat >expect <<-EOF &&
101+
TREE::$(git rev-parse topic^{tree})
102+
TREE::$(git rev-parse base~1^{tree})
103+
TREE:left/:$(git rev-parse base~1:left)
104+
TREE:right/:$(git rev-parse topic:right)
105+
TREE:right/:$(git rev-parse base~1:right)
106+
trees:5
107+
BLOB:a:$(git rev-parse base~1:a)
108+
BLOB:left/b:$(git rev-parse base~1:left/b)
109+
BLOB:right/c:$(git rev-parse base~1:right/c)
110+
BLOB:right/c:$(git rev-parse topic:right/c)
111+
BLOB:right/d:$(git rev-parse base~1:right/d)
112+
blobs:5
113+
EOF
114+
115+
test_cmp_sorted expect out
116+
'
117+
118+
test_done

0 commit comments

Comments
 (0)