Skip to content

Commit 7e2fc39

Browse files
vdyegitster
authored andcommitted
t7450: test submodule urls
Add tests to 't7450-bad-git-dotfiles.sh' to check the validity of different submodule URLs. To verify this directly (without setting up test repositories & submodules), add a 'check-url' subcommand to 'test-tool submodule' that calls 'check_submodule_url' in the same way that 'check-name' calls 'check_submodule_name'. Add two tests to separately address cases where the URL check correctly filters out invalid URLs and cases where the check misses invalid URLs. Mark the latter ("url check misses invalid cases") with 'test_expect_failure' to indicate that this is currently broken, which will be fixed in the next step. Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6af2c4a commit 7e2fc39

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

t/helper/test-submodule.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ static const char *submodule_check_name_usage[] = {
1414
NULL
1515
};
1616

17+
#define TEST_TOOL_CHECK_URL_USAGE \
18+
"test-tool submodule check-url"
19+
static const char *submodule_check_url_usage[] = {
20+
TEST_TOOL_CHECK_URL_USAGE,
21+
NULL
22+
};
23+
1724
#define TEST_TOOL_IS_ACTIVE_USAGE \
1825
"test-tool submodule is-active <name>"
1926
static const char *submodule_is_active_usage[] = {
@@ -30,17 +37,23 @@ static const char *submodule_resolve_relative_url_usage[] = {
3037

3138
static const char *submodule_usage[] = {
3239
TEST_TOOL_CHECK_NAME_USAGE,
40+
TEST_TOOL_CHECK_URL_USAGE,
3341
TEST_TOOL_IS_ACTIVE_USAGE,
3442
TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
3543
NULL
3644
};
3745

38-
/* Filter stdin to print only valid names. */
39-
static int check_name(void)
46+
typedef int (*check_fn_t)(const char *);
47+
48+
/*
49+
* Apply 'check_fn' to each line of stdin, printing values that pass the check
50+
* to stdout.
51+
*/
52+
static int check_submodule(check_fn_t check_fn)
4053
{
4154
struct strbuf buf = STRBUF_INIT;
4255
while (strbuf_getline(&buf, stdin) != EOF) {
43-
if (!check_submodule_name(buf.buf))
56+
if (!check_fn(buf.buf))
4457
printf("%s\n", buf.buf);
4558
}
4659
strbuf_release(&buf);
@@ -57,7 +70,20 @@ static int cmd__submodule_check_name(int argc, const char **argv)
5770
if (argc)
5871
usage_with_options(submodule_check_name_usage, options);
5972

60-
return check_name();
73+
return check_submodule(check_submodule_name);
74+
}
75+
76+
static int cmd__submodule_check_url(int argc, const char **argv)
77+
{
78+
struct option options[] = {
79+
OPT_END()
80+
};
81+
argc = parse_options(argc, argv, "test-tools", options,
82+
submodule_check_url_usage, 0);
83+
if (argc)
84+
usage_with_options(submodule_check_url_usage, options);
85+
86+
return check_submodule(check_submodule_url);
6187
}
6288

6389
static int cmd__submodule_is_active(int argc, const char **argv)
@@ -183,6 +209,7 @@ static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
183209

184210
static struct test_cmd cmds[] = {
185211
{ "check-name", cmd__submodule_check_name },
212+
{ "check-url", cmd__submodule_check_url },
186213
{ "is-active", cmd__submodule_is_active },
187214
{ "resolve-relative-url", cmd__submodule_resolve_relative_url},
188215
{ "config-list", cmd__submodule_config_list },

t/t7450-bad-git-dotfiles.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,41 @@ test_expect_success 'check names' '
4545
test_cmp expect actual
4646
'
4747

48+
test_expect_success 'check urls' '
49+
cat >expect <<-\EOF &&
50+
./bar/baz/foo.git
51+
https://example.com/foo.git
52+
http://example.com:80/deeper/foo.git
53+
EOF
54+
55+
test-tool submodule check-url >actual <<-\EOF &&
56+
./bar/baz/foo.git
57+
https://example.com/foo.git
58+
http://example.com:80/deeper/foo.git
59+
-a./foo
60+
../../..//test/foo.git
61+
../../../../../:localhost:8080/foo.git
62+
..\../.\../:example.com/foo.git
63+
./%0ahost=example.com/foo.git
64+
https://one.example.com/evil?%0ahost=two.example.com
65+
https:///example.com/foo.git
66+
https::example.com/foo.git
67+
http:::example.com/foo.git
68+
EOF
69+
70+
test_cmp expect actual
71+
'
72+
73+
# NEEDSWORK: the URL checked here is not valid (and will not work as a remote if
74+
# a user attempts to clone it), but the fsck check passes.
75+
test_expect_failure 'url check misses invalid cases' '
76+
test-tool submodule check-url >actual <<-\EOF &&
77+
http://example.com:test/foo.git
78+
EOF
79+
80+
test_must_be_empty actual
81+
'
82+
4883
test_expect_success 'create innocent subrepo' '
4984
git init innocent &&
5085
git -C innocent commit --allow-empty -m foo

0 commit comments

Comments
 (0)