[PATCH v7 11/14] t/unit-tests: implement test driver

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The test driver in "unit-test.c" is responsible for setting up our unit
tests and eventually running them. As such, it is also responsible for
parsing the command line arguments.

The clar unit testing framework provides function `clar_test()` that
parses command line arguments and then executes the tests for us. In
theory that would already be sufficient. We have the special requirement
to always generate TAP-formatted output though, so we'd have to always
pass the "-t" argument to clar. Furthermore, some of the options exposed
by clar are ineffective when "-t" is used, but they would still be shown
when the user passes the "-h" parameter to have the clar show its usage.

Implement our own option handling instead of using the one provided by
clar, which gives us greater flexibility in how exactly we set things
up.

We would ideally not use any "normal" code of ours for this such that
the unit testing framework doesn't depend on it working correctly. But
it is somewhat dubious whether we really want to reimplement all of the
option parsing. So for now, let's be pragmatic and reuse it until we
find a good reason in the future why we'd really want to avoid it.

Signed-off-by: Patrick Steinhardt <ps@xxxxxx>
---
 t/unit-tests/unit-test.c | 43 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/t/unit-tests/unit-test.c b/t/unit-tests/unit-test.c
index 3d12cde6dae..96fa64de71d 100644
--- a/t/unit-tests/unit-test.c
+++ b/t/unit-tests/unit-test.c
@@ -1,6 +1,45 @@
 #include "unit-test.h"
+#include "parse-options.h"
+#include "string-list.h"
+#include "strvec.h"
 
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
+static const char * const unit_test_usage[] = {
+	N_("unit-test [<options>]"),
+	NULL,
+};
+
+int cmd_main(int argc, const char **argv)
 {
-	return 0;
+	struct string_list run_args = STRING_LIST_INIT_NODUP;
+	struct string_list exclude_args = STRING_LIST_INIT_NODUP;
+	int immediate = 0;
+	struct option options[] = {
+		OPT_BOOL('i', "--immediate", &immediate,
+			 N_("immediately exit upon the first failed test")),
+		OPT_STRING_LIST('r', "run", &run_args, N_("name"),
+				N_("run only test suite or individual test <name>")),
+		OPT_STRING_LIST('x', "exclude", &exclude_args, N_("name"),
+				N_("exclude test suite <name>")),
+		OPT_END(),
+	};
+	struct strvec args = STRVEC_INIT;
+	int ret;
+
+	argc = parse_options(argc, argv, NULL, options,
+			     unit_test_usage, PARSE_OPT_KEEP_ARGV0);
+	if (argc > 1)
+		usagef(_("extra command line parameter '%s'"), argv[0]);
+
+	strvec_push(&args, argv[0]);
+	strvec_push(&args, "-t");
+	for (size_t i = 0; i < run_args.nr; i++)
+		strvec_pushf(&args, "-s%s", run_args.items[i].string);
+	for (size_t i = 0; i < exclude_args.nr; i++)
+		strvec_pushf(&args, "-x%s", exclude_args.items[i].string);
+
+	ret = clar_test(args.nr, (char **) args.v);
+
+	string_list_clear(&run_args, 0);
+	strvec_clear(&args);
+	return ret;
 }
-- 
2.46.0.421.g159f2d50e7.dirty





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux