submodule--helper: move "is-active" to a test-tool
Create a new "test-tool submodule" and move the "is-active" subcommand over to it. It was added in 5c2bd8b77ae (submodule--helper: add is-active subcommand, 2017-03-16), since a452128a36c (submodule--helper: introduce add-config subcommand, 2021-08-06) it hasn't been used by git-submodule.sh. Since we're creating a command dispatch similar to test-tool.c itself let's split out the "struct test_cmd" into a new test-tool-utils.h, which both this new code and test-tool.c itself can use. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Reviewed-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Sep 1, 2022 at 01:17 UTC
9fb2a970e9ffe5e37637f25b700e8bc09789fbf2
7 files changed
+90
-30
Makefile
+1
@@ -785,6 +785,7 @@ TEST_BUILTINS_OBJS += test-strcmp-offset.o
785
TEST_BUILTINS_OBJS += test-string-list.o
786
TEST_BUILTINS_OBJS += test-submodule-config.o
787
TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
788
+TEST_BUILTINS_OBJS += test-submodule.o
789
TEST_BUILTINS_OBJS += test-subprocess.o
790
TEST_BUILTINS_OBJS += test-trace2.o
791
TEST_BUILTINS_OBJS += test-urlmatch-normalization.o
builtin/submodule--helper.c
-9
@@ -2728,14 +2728,6 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2728
return 0;
2729
}
2730
2731
-static int is_active(int argc, const char **argv, const char *prefix)
2732
-{
2733
- if (argc != 2)
2734
- die("submodule--helper is-active takes exactly 1 argument");
2735
-
2736
- return !is_submodule_active(the_repository, argv[1]);
2737
-}
2738
-
2731
/*
2732
* Exit non-zero if any of the submodule names given on the command line is
2733
* invalid. If no names are given, filter stdin to print only valid names
@@ -3313,7 +3305,6 @@ static struct cmd_struct commands[] = {
3305
{"summary", module_summary, 0},
3306
{"push-check", push_check, 0},
3307
{"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3316
- {"is-active", is_active, 0},
3308
{"check-name", check_name, 0},
3309
{"config", module_config, 0},
3310
{"set-url", module_set_url, 0},
t/helper/test-submodule.c
new
+58
@@ -0,0 +1,58 @@
1
+#include "test-tool.h"
2
+#include "test-tool-utils.h"
3
+#include "cache.h"
4
+#include "parse-options.h"
5
+#include "submodule.h"
6
+
7
+#define TEST_TOOL_IS_ACTIVE_USAGE \
8
+ "test-tool submodule is-active <name>"
9
+static const char *submodule_is_active_usage[] = {
10
+ TEST_TOOL_IS_ACTIVE_USAGE,
11
+ NULL
12
+};
13
+
14
+static const char *submodule_usage[] = {
15
+ TEST_TOOL_IS_ACTIVE_USAGE,
16
+ NULL
17
+};
18
+
19
+static int cmd__submodule_is_active(int argc, const char **argv)
20
+{
21
+ struct option options[] = {
22
+ OPT_END()
23
+ };
24
+ argc = parse_options(argc, argv, "test-tools", options,
25
+ submodule_is_active_usage, 0);
26
+ if (argc != 1)
27
+ usage_with_options(submodule_is_active_usage, options);
28
+
29
+ setup_git_directory();
30
+
31
+ return !is_submodule_active(the_repository, argv[0]);
32
+}
33
+
34
+static struct test_cmd cmds[] = {
35
+ { "is-active", cmd__submodule_is_active },
36
+};
37
+
38
+int cmd__submodule(int argc, const char **argv)
39
+{
40
+ struct option options[] = {
41
+ OPT_END()
42
+ };
43
+ size_t i;
44
+
45
+ argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
46
+ PARSE_OPT_STOP_AT_NON_OPTION);
47
+ if (argc < 1)
48
+ usage_with_options(submodule_usage, options);
49
+
50
+ for (i = 0; i < ARRAY_SIZE(cmds); i++)
51
+ if (!strcmp(cmds[i].name, argv[0]))
52
+ return cmds[i].fn(argc, argv);
53
+
54
+ usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
55
+ argv[0]);
56
+
57
+ return 0;
58
+}
t/helper/test-tool-utils.h
new
+9
@@ -0,0 +1,9 @@
1
+#ifndef TEST_TOOL_UTILS_H
2
+#define TEST_TOOL_UTILS_H
3
+
4
+struct test_cmd {
5
+ const char *name;
6
+ int (*fn)(int argc, const char **argv);
7
+};
8
+
9
+#endif
t/helper/test-tool.c
+2
-5
@@ -1,5 +1,6 @@
1
#include "git-compat-util.h"
2
#include "test-tool.h"
3
+#include "test-tool-utils.h"
4
#include "trace2.h"
5
#include "parse-options.h"
6
@@ -8,11 +9,6 @@ static const char * const test_tool_usage[] = {
9
NULL
10
};
11
11
-struct test_cmd {
12
- const char *name;
13
- int (*fn)(int argc, const char **argv);
14
-};
15
-
12
static struct test_cmd cmds[] = {
13
{ "advise", cmd__advise_if_enabled },
14
{ "bitmap", cmd__bitmap },
@@ -78,6 +74,7 @@ static struct test_cmd cmds[] = {
74
{ "simple-ipc", cmd__simple_ipc },
75
{ "strcmp-offset", cmd__strcmp_offset },
76
{ "string-list", cmd__string_list },
77
+ { "submodule", cmd__submodule },
78
{ "submodule-config", cmd__submodule_config },
79
{ "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
80
{ "subprocess", cmd__subprocess },
t/helper/test-tool.h
+1
@@ -68,6 +68,7 @@ int cmd__sigchain(int argc, const char **argv);
68
int cmd__simple_ipc(int argc, const char **argv);
69
int cmd__strcmp_offset(int argc, const char **argv);
70
int cmd__string_list(int argc, const char **argv);
71
+int cmd__submodule(int argc, const char **argv);
72
int cmd__submodule_config(int argc, const char **argv);
73
int cmd__submodule_nested_repo_config(int argc, const char **argv);
74
int cmd__subprocess(int argc, const char **argv);
t/t7413-submodule-is-active.sh
+19
-16
@@ -1,9 +1,12 @@
1
#!/bin/sh
2
3
-test_description='Test submodule--helper is-active
3
+test_description='Test with test-tool submodule is-active
4
5
-This test verifies that `git submodue--helper is-active` correctly identifies
5
+This test verifies that `test-tool submodule is-active` correctly identifies
6
submodules which are "active" and interesting to the user.
7
+
8
+This is a unit test of the submodule.c is_submodule_active() function,
9
+which is also indirectly tested elsewhere.
10
'
11
12
. ./test-lib.sh
@@ -25,13 +28,13 @@ test_expect_success 'setup' '
28
'
29
30
test_expect_success 'is-active works with urls' '
28
- git -C super submodule--helper is-active sub1 &&
29
- git -C super submodule--helper is-active sub2 &&
31
+ test-tool -C super submodule is-active sub1 &&
32
+ test-tool -C super submodule is-active sub2 &&
33
34
git -C super config --unset submodule.sub1.URL &&
32
- test_must_fail git -C super submodule--helper is-active sub1 &&
35
+ test_must_fail test-tool -C super submodule is-active sub1 &&
36
git -C super config submodule.sub1.URL ../sub &&
34
- git -C super submodule--helper is-active sub1
37
+ test-tool -C super submodule is-active sub1
38
'
39
40
test_expect_success 'is-active works with submodule.<name>.active config' '
@@ -39,11 +42,11 @@ test_expect_success 'is-active works with submodule.<name>.active config' '
42
test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
43
44
git -C super config --bool submodule.sub1.active "false" &&
42
- test_must_fail git -C super submodule--helper is-active sub1 &&
45
+ test_must_fail test-tool -C super submodule is-active sub1 &&
46
47
git -C super config --bool submodule.sub1.active "true" &&
48
git -C super config --unset submodule.sub1.URL &&
46
- git -C super submodule--helper is-active sub1
49
+ test-tool -C super submodule is-active sub1
50
'
51
52
test_expect_success 'is-active works with basic submodule.active config' '
@@ -53,17 +56,17 @@ test_expect_success 'is-active works with basic submodule.active config' '
56
git -C super config --add submodule.active "." &&
57
git -C super config --unset submodule.sub1.URL &&
58
56
- git -C super submodule--helper is-active sub1 &&
57
- git -C super submodule--helper is-active sub2
59
+ test-tool -C super submodule is-active sub1 &&
60
+ test-tool -C super submodule is-active sub2
61
'
62
63
test_expect_success 'is-active correctly works with paths that are not submodules' '
64
test_when_finished "git -C super config --unset-all submodule.active" &&
65
63
- test_must_fail git -C super submodule--helper is-active not-a-submodule &&
66
+ test_must_fail test-tool -C super submodule is-active not-a-submodule &&
67
68
git -C super config --add submodule.active "." &&
66
- test_must_fail git -C super submodule--helper is-active not-a-submodule
69
+ test_must_fail test-tool -C super submodule is-active not-a-submodule
70
'
71
72
test_expect_success 'is-active works with exclusions in submodule.active config' '
@@ -72,8 +75,8 @@ test_expect_success 'is-active works with exclusions in submodule.active config'
75
git -C super config --add submodule.active "." &&
76
git -C super config --add submodule.active ":(exclude)sub1" &&
77
75
- test_must_fail git -C super submodule--helper is-active sub1 &&
76
- git -C super submodule--helper is-active sub2
78
+ test_must_fail test-tool -C super submodule is-active sub1 &&
79
+ test-tool -C super submodule is-active sub2
80
'
81
82
test_expect_success 'is-active with submodule.active and submodule.<name>.active' '
@@ -85,8 +88,8 @@ test_expect_success 'is-active with submodule.active and submodule.<name>.active
88
git -C super config --bool submodule.sub1.active "false" &&
89
git -C super config --bool submodule.sub2.active "true" &&
90
88
- test_must_fail git -C super submodule--helper is-active sub1 &&
89
- git -C super submodule--helper is-active sub2
91
+ test_must_fail test-tool -C super submodule is-active sub1 &&
92
+ test-tool -C super submodule is-active sub2
93
'
94
95
test_expect_success 'is-active, submodule.active and submodule add' '