]> git.ipfire.org Git - thirdparty/git.git/commitdiff
userdiff tests: list builtin drivers via test-tool
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 8 Apr 2021 15:04:21 +0000 (17:04 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 8 Apr 2021 19:19:10 +0000 (12:19 -0700)
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>
Makefile
t/helper/test-tool.c
t/helper/test-tool.h
t/helper/test-userdiff.c [new file with mode: 0644]
t/t4018-diff-funcname.sh

index f3dc2178324e4f7270c053a7fad070ead9f0c21e..3e2568d68dabeb3932294920e702a7dd3ee3529c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -744,6 +744,7 @@ TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
 TEST_BUILTINS_OBJS += test-subprocess.o
 TEST_BUILTINS_OBJS += test-trace2.o
 TEST_BUILTINS_OBJS += test-urlmatch-normalization.o
+TEST_BUILTINS_OBJS += test-userdiff.o
 TEST_BUILTINS_OBJS += test-wildmatch.o
 TEST_BUILTINS_OBJS += test-windows-named-pipe.o
 TEST_BUILTINS_OBJS += test-write-cache.o
index f97cd9f48a69048db519ffc0519a99559f543b31..dcb05ca6e5e2bbd8bff61a5f9cbedf452b39d27f 100644 (file)
@@ -71,6 +71,7 @@ static struct test_cmd cmds[] = {
        { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
        { "subprocess", cmd__subprocess },
        { "trace2", cmd__trace2 },
+       { "userdiff", cmd__userdiff },
        { "urlmatch-normalization", cmd__urlmatch_normalization },
        { "xml-encode", cmd__xml_encode },
        { "wildmatch", cmd__wildmatch },
index 28072c0ad5ab446eab4b37d915222b6768dc2e66..589f2e8ac6726a039dd6401c2540ced8ebc1abdc 100644 (file)
@@ -61,6 +61,7 @@ int cmd__submodule_config(int argc, const char **argv);
 int cmd__submodule_nested_repo_config(int argc, const char **argv);
 int cmd__subprocess(int argc, const char **argv);
 int cmd__trace2(int argc, const char **argv);
+int cmd__userdiff(int argc, const char **argv);
 int cmd__urlmatch_normalization(int argc, const char **argv);
 int cmd__xml_encode(int argc, const char **argv);
 int cmd__wildmatch(int argc, const char **argv);
diff --git a/t/helper/test-userdiff.c b/t/helper/test-userdiff.c
new file mode 100644 (file)
index 0000000..f013f8a
--- /dev/null
@@ -0,0 +1,46 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "userdiff.h"
+#include "config.h"
+
+static int driver_cb(struct userdiff_driver *driver,
+                    enum userdiff_driver_type type, void *priv)
+{
+       enum userdiff_driver_type *want_type = priv;
+       if (type & *want_type && driver->funcname.pattern)
+               puts(driver->name);
+       return 0;
+}
+
+static int cmd__userdiff_config(const char *var, const char *value, void *cb)
+{
+       if (userdiff_config(var, value) < 0)
+               return -1;
+       return 0;
+}
+
+int cmd__userdiff(int argc, const char **argv)
+{
+       enum userdiff_driver_type want = 0;
+       if (argc != 2)
+               return 1;
+
+       if (!strcmp(argv[1], "list-drivers"))
+               want = (USERDIFF_DRIVER_TYPE_BUILTIN |
+                       USERDIFF_DRIVER_TYPE_CUSTOM);
+       else if (!strcmp(argv[1], "list-builtin-drivers"))
+               want = USERDIFF_DRIVER_TYPE_BUILTIN;
+       else if (!strcmp(argv[1], "list-custom-drivers"))
+               want = USERDIFF_DRIVER_TYPE_CUSTOM;
+       else
+               return error("unknown argument %s", argv[1]);
+
+       if (want & USERDIFF_DRIVER_TYPE_CUSTOM) {
+               setup_git_directory();
+               git_config(cmd__userdiff_config, NULL);
+       }
+
+       for_each_userdiff_driver(driver_cb, &want);
+
+       return 0;
+}
index cefe329aea7702120036a485c0072acab96e5d11..409372f3a48cf2781f314ffec7cb8a32ea15f71d 100755 (executable)
@@ -25,34 +25,26 @@ test_expect_success 'setup' '
        echo B >B.java
 '
 
+test_expect_success 'setup: test-tool userdiff' '
+       # Make sure additions to builtin_drivers are sorted
+       test_when_finished "rm builtin-drivers.sorted" &&
+       test-tool userdiff list-builtin-drivers >builtin-drivers &&
+       test_file_not_empty builtin-drivers &&
+       sort <builtin-drivers >builtin-drivers.sorted &&
+       test_cmp builtin-drivers.sorted builtin-drivers &&
+
+       # Ditto, but "custom" requires the .git directory and config
+       # to be setup and read.
+       test_when_finished "rm custom-drivers.sorted" &&
+       test-tool userdiff list-custom-drivers >custom-drivers &&
+       test_file_not_empty custom-drivers &&
+       sort <custom-drivers >custom-drivers.sorted &&
+       test_cmp custom-drivers.sorted custom-drivers
+'
+
 diffpatterns="
-       ada
-       bash
-       bibtex
-       cpp
-       csharp
-       css
-       dts
-       elixir
-       fortran
-       fountain
-       golang
-       html
-       java
-       markdown
-       matlab
-       objc
-       pascal
-       perl
-       php
-       python
-       ruby
-       rust
-       tex
-       default
-       custom1
-       custom2
-       custom3
+       $(cat builtin-drivers)
+       $(cat custom-drivers)
 "
 
 for p in $diffpatterns