Turn `git serve` into a test helper

The `git serve` built-in was introduced in ed10cb952d31 (serve: introduce git-serve, 2018-03-15) as a backend to serve Git protocol v2, probably originally intended to be spawned by `git upload-pack`. However, in the version that the protocol v2 patches made it into core Git, `git upload-pack` calls the `serve()` function directly instead of spawning `git serve`; The only reason in life for `git serve` to survive as a built-in command is to provide a way to test the protocol v2 functionality. Meaning that it does not even have to be a built-in that is installed with end-user facing Git installations, but it can be a test helper instead. Let's make it so. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 18, 2019 at 06:16 UTC b7ce24d09526d4e181920ee029c25438196c2847
9 files changed +36 -30
Makefile
+1 -1
@@ -758,6 +758,7 @@ TEST_BUILTINS_OBJS += test-repository.o
758 TEST_BUILTINS_OBJS += test-revision-walking.o
759 TEST_BUILTINS_OBJS += test-run-command.o
760 TEST_BUILTINS_OBJS += test-scrap-cache-tree.o
761 +TEST_BUILTINS_OBJS += test-serve-v2.o
762 TEST_BUILTINS_OBJS += test-sha1.o
763 TEST_BUILTINS_OBJS += test-sha1-array.o
764 TEST_BUILTINS_OBJS += test-sha256.o
@@ -1127,7 +1128,6 @@ BUILTIN_OBJS += builtin/rev-parse.o
1128 BUILTIN_OBJS += builtin/revert.o
1129 BUILTIN_OBJS += builtin/rm.o
1130 BUILTIN_OBJS += builtin/send-pack.o
1130 -BUILTIN_OBJS += builtin/serve.o
1131 BUILTIN_OBJS += builtin/shortlog.o
1132 BUILTIN_OBJS += builtin/show-branch.o
1133 BUILTIN_OBJS += builtin/show-index.o
builtin.h
-1
@@ -219,7 +219,6 @@ extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
219 extern int cmd_revert(int argc, const char **argv, const char *prefix);
220 extern int cmd_rm(int argc, const char **argv, const char *prefix);
221 extern int cmd_send_pack(int argc, const char **argv, const char *prefix);
222 -extern int cmd_serve(int argc, const char **argv, const char *prefix);
222 extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
223 extern int cmd_show(int argc, const char **argv, const char *prefix);
224 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
git.c
-1
@@ -548,7 +548,6 @@ static struct cmd_struct commands[] = {
548 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
549 { "rm", cmd_rm, RUN_SETUP },
550 { "send-pack", cmd_send_pack, RUN_SETUP },
551 - { "serve", cmd_serve, RUN_SETUP },
551 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
552 { "show", cmd_show, RUN_SETUP },
553 { "show-branch", cmd_show_branch, RUN_SETUP },
t/helper/test-serve-v2.c renamed
+4 -3
@@ -1,14 +1,14 @@
1 +#include "test-tool.h"
2 #include "cache.h"
2 -#include "builtin.h"
3 #include "parse-options.h"
4 #include "serve.h"
5
6 static char const * const serve_usage[] = {
7 - N_("git serve [<options>]"),
7 + N_("test-tool serve-v2 [<options>]"),
8 NULL
9 };
10
11 -int cmd_serve(int argc, const char **argv, const char *prefix)
11 +int cmd__serve_v2(int argc, const char **argv)
12 {
13 struct serve_options opts = SERVE_OPTIONS_INIT;
14
@@ -19,6 +19,7 @@ int cmd_serve(int argc, const char **argv, const char *prefix)
19 N_("exit immediately after advertising capabilities")),
20 OPT_END()
21 };
22 + const char *prefix = setup_git_directory();
23
24 /* ignore all unknown cmdline switches for now */
25 argc = parse_options(argc, argv, prefix, options, serve_usage,
t/helper/test-tool.c
+1
@@ -48,6 +48,7 @@ static struct test_cmd cmds[] = {
48 { "revision-walking", cmd__revision_walking },
49 { "run-command", cmd__run_command },
50 { "scrap-cache-tree", cmd__scrap_cache_tree },
51 + { "serve-v2", cmd__serve_v2 },
52 { "sha1", cmd__sha1 },
53 { "sha1-array", cmd__sha1_array },
54 { "sha256", cmd__sha256 },
t/helper/test-tool.h
+1
@@ -39,6 +39,7 @@ int cmd__repository(int argc, const char **argv);
39 int cmd__revision_walking(int argc, const char **argv);
40 int cmd__run_command(int argc, const char **argv);
41 int cmd__scrap_cache_tree(int argc, const char **argv);
42 +int cmd__serve_v2(int argc, const char **argv);
43 int cmd__sha1(int argc, const char **argv);
44 int cmd__sha1_array(int argc, const char **argv);
45 int cmd__sha256(int argc, const char **argv);
t/t5701-git-serve.sh
+18 -14
@@ -1,6 +1,6 @@
1 #!/bin/sh
2
3 -test_description='test git-serve and server commands'
3 +test_description='test protocol v2 server commands'
4
5 . ./test-lib.sh
6
@@ -14,7 +14,8 @@ test_expect_success 'test capability advertisement' '
14 0000
15 EOF
16
17 - GIT_TEST_SIDEBAND_ALL=0 git serve --advertise-capabilities >out &&
17 + GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
18 + --advertise-capabilities >out &&
19 test-tool pkt-line unpack <out >actual &&
20 test_cmp expect actual
21 '
@@ -24,11 +25,11 @@ test_expect_success 'stateless-rpc flag does not list capabilities' '
25 test-tool pkt-line pack >in <<-EOF &&
26 0000
27 EOF
27 - git serve --stateless-rpc >out <in &&
28 + test-tool serve-v2 --stateless-rpc >out <in &&
29 test_must_be_empty out &&
30
31 # EOF
31 - git serve --stateless-rpc >out &&
32 + test-tool serve-v2 --stateless-rpc >out &&
33 test_must_be_empty out
34 '
35
@@ -37,7 +38,7 @@ test_expect_success 'request invalid capability' '
38 foobar
39 0000
40 EOF
40 - test_must_fail git serve --stateless-rpc 2>err <in &&
41 + test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
42 test_i18ngrep "unknown capability" err
43 '
44
@@ -46,7 +47,7 @@ test_expect_success 'request with no command' '
47 agent=git/test
48 0000
49 EOF
49 - test_must_fail git serve --stateless-rpc 2>err <in &&
50 + test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
51 test_i18ngrep "no command requested" err
52 '
53
@@ -56,7 +57,7 @@ test_expect_success 'request invalid command' '
57 agent=git/test
58 0000
59 EOF
59 - test_must_fail git serve --stateless-rpc 2>err <in &&
60 + test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
61 test_i18ngrep "invalid command" err
62 '
63
@@ -87,7 +88,7 @@ test_expect_success 'basics of ls-refs' '
88 0000
89 EOF
90
90 - git serve --stateless-rpc <in >out &&
91 + test-tool serve-v2 --stateless-rpc <in >out &&
92 test-tool pkt-line unpack <out >actual &&
93 test_cmp expect actual
94 '
@@ -107,7 +108,7 @@ test_expect_success 'basic ref-prefixes' '
108 0000
109 EOF
110
110 - git serve --stateless-rpc <in >out &&
111 + test-tool serve-v2 --stateless-rpc <in >out &&
112 test-tool pkt-line unpack <out >actual &&
113 test_cmp expect actual
114 '
@@ -127,7 +128,7 @@ test_expect_success 'refs/heads prefix' '
128 0000
129 EOF
130
130 - git serve --stateless-rpc <in >out &&
131 + test-tool serve-v2 --stateless-rpc <in >out &&
132 test-tool pkt-line unpack <out >actual &&
133 test_cmp expect actual
134 '
@@ -148,7 +149,7 @@ test_expect_success 'peel parameter' '
149 0000
150 EOF
151
151 - git serve --stateless-rpc <in >out &&
152 + test-tool serve-v2 --stateless-rpc <in >out &&
153 test-tool pkt-line unpack <out >actual &&
154 test_cmp expect actual
155 '
@@ -169,7 +170,7 @@ test_expect_success 'symrefs parameter' '
170 0000
171 EOF
172
172 - git serve --stateless-rpc <in >out &&
173 + test-tool serve-v2 --stateless-rpc <in >out &&
174 test-tool pkt-line unpack <out >actual &&
175 test_cmp expect actual
176 '
@@ -189,7 +190,7 @@ test_expect_success 'sending server-options' '
190 0000
191 EOF
192
192 - git serve --stateless-rpc <in >out &&
193 + test-tool serve-v2 --stateless-rpc <in >out &&
194 test-tool pkt-line unpack <out >actual &&
195 test_cmp expect actual
196 '
@@ -204,7 +205,10 @@ test_expect_success 'unexpected lines are not allowed in fetch request' '
205 0000
206 EOF
207
207 - test_must_fail git -C server serve --stateless-rpc <in >/dev/null 2>err &&
208 + (
209 + cd server &&
210 + test_must_fail test-tool serve-v2 --stateless-rpc
211 + ) <in >/dev/null 2>err &&
212 grep "unexpected line: .this-is-not-a-command." err
213 '
214
t/t5702-protocol-v2.sh
+3 -2
@@ -359,12 +359,13 @@ test_expect_success 'even with handcrafted request, filter does not work if not
359 0000
360 EOF
361
362 - test_must_fail git -C server serve --stateless-rpc <in >/dev/null 2>err &&
362 + test_must_fail test-tool -C server serve-v2 --stateless-rpc \
363 + <in >/dev/null 2>err &&
364 grep "unexpected line: .filter blob:none." err &&
365
366 # Exercise to ensure that if advertised, filter works
367 git -C server config uploadpack.allowfilter 1 &&
367 - git -C server serve --stateless-rpc <in >/dev/null
368 + test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
369 '
370
371 test_expect_success 'default refspec is used to filter ref when fetchcing' '
t/t5703-upload-pack-ref-in-want.sh
+8 -8
@@ -48,15 +48,15 @@ test_expect_success 'setup repository' '
48 '
49
50 test_expect_success 'config controls ref-in-want advertisement' '
51 - git serve --advertise-capabilities >out &&
51 + test-tool serve-v2 --advertise-capabilities >out &&
52 ! grep -a ref-in-want out &&
53
54 git config uploadpack.allowRefInWant false &&
55 - git serve --advertise-capabilities >out &&
55 + test-tool serve-v2 --advertise-capabilities >out &&
56 ! grep -a ref-in-want out &&
57
58 git config uploadpack.allowRefInWant true &&
59 - git serve --advertise-capabilities >out &&
59 + test-tool serve-v2 --advertise-capabilities >out &&
60 grep -a ref-in-want out
61 '
62
@@ -70,7 +70,7 @@ test_expect_success 'invalid want-ref line' '
70 0000
71 EOF
72
73 - test_must_fail git serve --stateless-rpc 2>out <in &&
73 + test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
74 grep "unknown ref" out
75 '
76
@@ -90,7 +90,7 @@ test_expect_success 'basic want-ref' '
90 0000
91 EOF
92
93 - git serve --stateless-rpc >out <in &&
93 + test-tool serve-v2 --stateless-rpc >out <in &&
94 check_output
95 '
96
@@ -112,7 +112,7 @@ test_expect_success 'multiple want-ref lines' '
112 0000
113 EOF
114
115 - git serve --stateless-rpc >out <in &&
115 + test-tool serve-v2 --stateless-rpc >out <in &&
116 check_output
117 '
118
@@ -133,7 +133,7 @@ test_expect_success 'mix want and want-ref' '
133 0000
134 EOF
135
136 - git serve --stateless-rpc >out <in &&
136 + test-tool serve-v2 --stateless-rpc >out <in &&
137 check_output
138 '
139
@@ -153,7 +153,7 @@ test_expect_success 'want-ref with ref we already have commit for' '
153 0000
154 EOF
155
156 - git serve --stateless-rpc >out <in &&
156 + test-tool serve-v2 --stateless-rpc >out <in &&
157 check_output
158 '
159