[PATCH 1/2] metadata: add tests grouping support

Andrea Cervesato <[email protected]>
Newsgroups gmane.linux.ltp
Message-ID <[email protected]>
From: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>

Add groups field to metaparse JSON output, so we can filter out tests
in kirk. Groups are derived from:

1. Source file path - the two nearest parent directories (immediate
   parent first), skipping 'kernel' as too generic. For example:
   - testcases/kernel/syscalls/clone/clone01.c -> clone, syscalls
   - testcases/kernel/kvm/kvm_pagefault01.c -> kvm
   - testcases/cve/cve-2017-16939.c -> cve

2. @group tags in the doc comment block, e.g.:
   /*    * Test description.
    *
    * @group stress
    */

Add test case for @group tag parsing.

Signed-off-by: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>
---
 metadata/metaparse.c         | 88 ++++++++++++++++++++++++++++++++++++++++++++
 metadata/tests/groups.c      | 11 ++++++
 metadata/tests/groups.c.json | 13 +++++++
 3 files changed, 112 insertions(+)

diff --git a/metadata/metaparse.c b/metadata/metaparse.c
index 561cbb9d2d54689988c9aa49d591628696bcf847..6bc4b7af60c7449d4b60a1252fa58fed77e03066 100644
--- a/metadata/metaparse.c
+++ b/metadata/metaparse.c
@@ -1168,6 +1168,92 @@ static void print_help(const char *prgname)
 	exit(0);
 }
 
+/*
+ * Add groups derived from the source file path.
+ *
+ * Groups are the two nearest parent directories (immediate parent
+ * first), skipping 'kernel' as it's too generic:
+ *
+ *   testcases/kernel/syscalls/clone/clone01.c  -> clone, syscalls
+ *   testcases/kernel/kvm/kvm_pagefault01.c     -> kvm
+ *   testcases/cve/cve-2017-16939.c             -> cve
+ */
+static void add_path_groups(struct data_node *groups, const char *fname)
+{
+	char buf[256];
+	int offsets[8];
+	int ndirs = 0;
+	int ngroups = 0;
+	char *p;
+
+	if (strncmp(fname, "testcases/", 10))
+		return;
+
+	snprintf(buf, sizeof(buf), "%s", fname + 10);
+
+	p = strtok(buf, "/");
+	while (p && ndirs < 8) {
+		offsets[ndirs++] = p - buf;
+		p = strtok(NULL, "/");
+	}
+
+	/* Last element is the filename, skip it */
+	ndirs--;
+
+	for (int j = ndirs - 1; j >= 0 && ngroups < 2; j--) {
+		if (!strcmp(buf + offsets[j], "kernel"))
+			continue;
+
+		data_node_array_add(groups, data_node_string(buf + offsets[j]));
+		ngroups++;
+	}
+}
+
+/*
+ * Add groups from @group tags in the doc comment block.
+ */
+static void add_doc_groups(struct data_node *groups, struct data_node *doc)
+{
+	if (!doc || doc->type != DATA_ARRAY)
+		return;
+
+	for (unsigned int i = 0; i < data_node_array_len(doc); i++) {
+		struct data_node *line = doc->array.array[i];
+		const char *s;
+
+		if (line->type != DATA_STRING)
+			continue;
+
+		s = line->string.val;
+
+		while (*s && (*s == ' ' || *s == '\t'))
+			s++;
+
+		if (strncmp(s, "@group ", 7))
+			continue;
+
+		s += 7;
+		while (*s && (*s == ' ' || *s == '\t'))
+			s++;
+
+		if (*s)
+			data_node_array_add(groups, data_node_string(s));
+	}
+}
+
+static void build_groups(struct data_node *res, const char *fname)
+{
+	struct data_node *groups = data_node_array();
+
+	add_path_groups(groups, fname);
+	add_doc_groups(groups, data_node_hash_get(res, "doc"));
+
+	if (data_node_array_len(groups))
+		data_node_hash_add(res, "groups", groups);
+	else
+		data_node_free(groups);
+}
+
 int main(int argc, char *argv[])
 {
 	unsigned int i, j;
@@ -1238,6 +1324,8 @@ int main(int argc, char *argv[])
 	}
 
 	data_node_hash_add(res, "fname", data_node_string(argv[optind]));
+	build_groups(res, argv[optind]);
+
 	printf("  \"%s\": ", strip_name(argv[optind]));
 	data_to_json(res, stdout, 2);
 	data_node_free(res);
diff --git a/metadata/tests/groups.c b/metadata/tests/groups.c
new file mode 100644
index 0000000000000000000000000000000000000000..82f07111c1506c634f13822ee6aa95f574eb19a5
--- /dev/null
+++ b/metadata/tests/groups.c
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/*\
+ * Test for @group tag parsing.
+ *
+ * @group stress
+ * @group regression
+ */
+
+static struct tst_test test = {
+};
diff --git a/metadata/tests/groups.c.json b/metadata/tests/groups.c.json
new file mode 100644
index 0000000000000000000000000000000000000000..4683e6cf07eeebc60faefb9aead9370dc3f631aa
--- /dev/null
+++ b/metadata/tests/groups.c.json
@@ -0,0 +1,13 @@
+  "groups": {
+   "doc": [
+    "Test for @group tag parsing.",
+    "",
+    "@group stress",
+    "@group regression"
+   ],
+   "fname": "groups.c",
+   "groups": [
+    "stress",
+    "regression"
+   ]
+  }
\ No newline at end of file

-- 
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.