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 2ff6c34612 (userdiff: support Bash, 2020-10-22) and 09dad9256a (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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Apr 8, 2021 at 17:04 UTC
28e8f0d5e5a6db005fdb22c65ee0d43cf15c4b17
5 files changed
+68
-27
Makefile
+1
@@ -744,6 +744,7 @@ TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
744
TEST_BUILTINS_OBJS += test-subprocess.o
745
TEST_BUILTINS_OBJS += test-trace2.o
746
TEST_BUILTINS_OBJS += test-urlmatch-normalization.o
747
+TEST_BUILTINS_OBJS += test-userdiff.o
748
TEST_BUILTINS_OBJS += test-wildmatch.o
749
TEST_BUILTINS_OBJS += test-windows-named-pipe.o
750
TEST_BUILTINS_OBJS += test-write-cache.o
t/helper/test-tool.c
+1
@@ -71,6 +71,7 @@ static struct test_cmd cmds[] = {
71
{ "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
72
{ "subprocess", cmd__subprocess },
73
{ "trace2", cmd__trace2 },
74
+ { "userdiff", cmd__userdiff },
75
{ "urlmatch-normalization", cmd__urlmatch_normalization },
76
{ "xml-encode", cmd__xml_encode },
77
{ "wildmatch", cmd__wildmatch },
t/helper/test-tool.h
+1
@@ -61,6 +61,7 @@ int cmd__submodule_config(int argc, const char **argv);
61
int cmd__submodule_nested_repo_config(int argc, const char **argv);
62
int cmd__subprocess(int argc, const char **argv);
63
int cmd__trace2(int argc, const char **argv);
64
+int cmd__userdiff(int argc, const char **argv);
65
int cmd__urlmatch_normalization(int argc, const char **argv);
66
int cmd__xml_encode(int argc, const char **argv);
67
int cmd__wildmatch(int argc, const char **argv);
t/helper/test-userdiff.c
new
+46
@@ -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
+19
-27
@@ -25,34 +25,26 @@ test_expect_success 'setup' '
25
echo B >B.java
26
'
27
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
+
45
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)
48
"
49
50
for p in $diffpatterns