Skip to content

Commit 28e8f0d

Browse files
avargitster
authored andcommitted
userdiff tests: list builtin drivers via test-tool
Change the userdiff test to list the builtin drivers via the test-tool, using the new for_each_userdiff_driver() API function. This gets rid of the need to modify this part of the test every time a new pattern is added, see 2ff6c34 (userdiff: support Bash, 2020-10-22) and 09dad92 (userdiff: support Markdown, 2020-05-02) for two recent examples. I only need the "list-builtin-drivers "argument here, but let's add "list-custom-drivers" and "list-drivers" too, just because it's easy. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 132bf25 commit 28e8f0d

File tree

5 files changed

+68
-27
lines changed

5 files changed

+68
-27
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
744744
TEST_BUILTINS_OBJS += test-subprocess.o
745745
TEST_BUILTINS_OBJS += test-trace2.o
746746
TEST_BUILTINS_OBJS += test-urlmatch-normalization.o
747+
TEST_BUILTINS_OBJS += test-userdiff.o
747748
TEST_BUILTINS_OBJS += test-wildmatch.o
748749
TEST_BUILTINS_OBJS += test-windows-named-pipe.o
749750
TEST_BUILTINS_OBJS += test-write-cache.o

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static struct test_cmd cmds[] = {
7171
{ "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
7272
{ "subprocess", cmd__subprocess },
7373
{ "trace2", cmd__trace2 },
74+
{ "userdiff", cmd__userdiff },
7475
{ "urlmatch-normalization", cmd__urlmatch_normalization },
7576
{ "xml-encode", cmd__xml_encode },
7677
{ "wildmatch", cmd__wildmatch },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ int cmd__submodule_config(int argc, const char **argv);
6161
int cmd__submodule_nested_repo_config(int argc, const char **argv);
6262
int cmd__subprocess(int argc, const char **argv);
6363
int cmd__trace2(int argc, const char **argv);
64+
int cmd__userdiff(int argc, const char **argv);
6465
int cmd__urlmatch_normalization(int argc, const char **argv);
6566
int cmd__xml_encode(int argc, const char **argv);
6667
int cmd__wildmatch(int argc, const char **argv);

t/helper/test-userdiff.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "test-tool.h"
2+
#include "cache.h"
3+
#include "userdiff.h"
4+
#include "config.h"
5+
6+
static int driver_cb(struct userdiff_driver *driver,
7+
enum userdiff_driver_type type, void *priv)
8+
{
9+
enum userdiff_driver_type *want_type = priv;
10+
if (type & *want_type && driver->funcname.pattern)
11+
puts(driver->name);
12+
return 0;
13+
}
14+
15+
static int cmd__userdiff_config(const char *var, const char *value, void *cb)
16+
{
17+
if (userdiff_config(var, value) < 0)
18+
return -1;
19+
return 0;
20+
}
21+
22+
int cmd__userdiff(int argc, const char **argv)
23+
{
24+
enum userdiff_driver_type want = 0;
25+
if (argc != 2)
26+
return 1;
27+
28+
if (!strcmp(argv[1], "list-drivers"))
29+
want = (USERDIFF_DRIVER_TYPE_BUILTIN |
30+
USERDIFF_DRIVER_TYPE_CUSTOM);
31+
else if (!strcmp(argv[1], "list-builtin-drivers"))
32+
want = USERDIFF_DRIVER_TYPE_BUILTIN;
33+
else if (!strcmp(argv[1], "list-custom-drivers"))
34+
want = USERDIFF_DRIVER_TYPE_CUSTOM;
35+
else
36+
return error("unknown argument %s", argv[1]);
37+
38+
if (want & USERDIFF_DRIVER_TYPE_CUSTOM) {
39+
setup_git_directory();
40+
git_config(cmd__userdiff_config, NULL);
41+
}
42+
43+
for_each_userdiff_driver(driver_cb, &want);
44+
45+
return 0;
46+
}

t/t4018-diff-funcname.sh

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,26 @@ test_expect_success 'setup' '
2525
echo B >B.java
2626
'
2727

28+
test_expect_success 'setup: test-tool userdiff' '
29+
# Make sure additions to builtin_drivers are sorted
30+
test_when_finished "rm builtin-drivers.sorted" &&
31+
test-tool userdiff list-builtin-drivers >builtin-drivers &&
32+
test_file_not_empty builtin-drivers &&
33+
sort <builtin-drivers >builtin-drivers.sorted &&
34+
test_cmp builtin-drivers.sorted builtin-drivers &&
35+
36+
# Ditto, but "custom" requires the .git directory and config
37+
# to be setup and read.
38+
test_when_finished "rm custom-drivers.sorted" &&
39+
test-tool userdiff list-custom-drivers >custom-drivers &&
40+
test_file_not_empty custom-drivers &&
41+
sort <custom-drivers >custom-drivers.sorted &&
42+
test_cmp custom-drivers.sorted custom-drivers
43+
'
44+
2845
diffpatterns="
29-
ada
30-
bash
31-
bibtex
32-
cpp
33-
csharp
34-
css
35-
dts
36-
elixir
37-
fortran
38-
fountain
39-
golang
40-
html
41-
java
42-
markdown
43-
matlab
44-
objc
45-
pascal
46-
perl
47-
php
48-
python
49-
ruby
50-
rust
51-
tex
52-
default
53-
custom1
54-
custom2
55-
custom3
46+
$(cat builtin-drivers)
47+
$(cat custom-drivers)
5648
"
5749

5850
for p in $diffpatterns

0 commit comments

Comments
 (0)