[Asterisk-code-review] test.c: Make output jUnit compatible (asterisk[master])

Jenkins2 asteriskteam at digium.com
Mon Jul 9 06:33:59 CDT 2018


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/9364 )

Change subject: test.c:  Make output jUnit compatible
......................................................................

test.c:  Make output jUnit compatible

Separate "name" into "classname" and "name".
Use '.' for classname separator instead of '/'.
Prefix reserved words with '_'.
Wrap output with a top-level "testsuites" element.

Change-Id: Iec1a985eba1c478e5c1d65d5dfd95cb708442099
---
M main/test.c
1 file changed, 71 insertions(+), 10 deletions(-)

Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/main/test.c b/main/test.c
index b117c08..2abe698 100644
--- a/main/test.c
+++ b/main/test.c
@@ -267,21 +267,80 @@
 	test->state = state;
 }
 
+/*
+ * These are the Java reserved words we need to munge so Jenkins
+ * doesn't barf on them.
+ */
+static char *reserved_words[] = {
+	"abstract", "arguments", "as", "assert", "await",
+	"boolean", "break", "byte", "case", "catch", "char", "class",
+	"const", "continue", "debugger", "def", "default", "delete", "do",
+	"double", "else", "enum", "eval", "export", "extends", "false",
+	"final", "finally", "float", "for", "function", "goto", "if",
+	"implements", "import", "in", "instanceof", "int", "interface",
+	"let", "long", "native", "new", "null", "package", "private",
+	"protected", "public", "return", "short", "static", "strictfp",
+	"string", "super", "switch", "synchronized", "this", "throw", "throws",
+	"trait", "transient", "true", "try", "typeof", "var", "void",
+	"volatile", "while", "with", "yield" };
+
+static int is_reserved_word(const char *word)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_LEN(reserved_words); i++) {
+		if (strcmp(word, reserved_words[i]) == 0) {
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
 static void test_xml_entry(struct ast_test *test, FILE *f)
 {
-	if (!f || !test || test->state == AST_TEST_NOT_RUN) {
+	/* We need a copy of the category skipping past the initial '/' */
+	char *test_cat = ast_strdupa(test->info.category + 1);
+	char *next_cat;
+	char *test_name = (char *)test->info.name;
+	struct ast_str *category = ast_str_create(strlen(test->info.category) + 32);
+
+	if (!category || test->state == AST_TEST_NOT_RUN) {
+		ast_free(category);
+
 		return;
 	}
 
-	fprintf(f, "\t<testcase time=\"%u.%u\" name=\"%s%s\"%s>\n",
+	while ((next_cat = ast_strsep(&test_cat, '/', AST_STRSEP_TRIM))) {
+		char *prefix = "";
+
+		if (is_reserved_word(next_cat)) {
+			prefix = "_";
+		}
+		ast_str_append(&category, 0, ".%s%s", prefix, next_cat);
+	}
+	test_cat = ast_str_buffer(category);
+	/* Skip past the initial '.' */
+	test_cat++;
+
+	if (is_reserved_word(test->info.name)) {
+		size_t name_length = strlen(test->info.name) + 2;
+
+		test_name = ast_alloca(name_length);
+		snprintf(test_name, name_length, "_%s", test->info.name);
+	}
+
+	fprintf(f, "\t\t<testcase time=\"%u.%u\" classname=\"%s\" name=\"%s\"%s>\n",
 			test->time / 1000, test->time % 1000,
-			test->info.category, test->info.name,
+			test_cat, test_name,
 			test->state == AST_TEST_PASS ? "/" : "");
 
+	ast_free(category);
+
 	if (test->state == AST_TEST_FAIL) {
-		fprintf(f, "\t\t<failure><![CDATA[\n%s\n\t\t]]></failure>\n",
+		fprintf(f, "\t\t\t<failure><![CDATA[\n%s\n\t\t]]></failure>\n",
 				S_OR(ast_str_buffer(test->status_str), "NA"));
-		fprintf(f, "\t</testcase>\n");
+		fprintf(f, "\t\t</testcase>\n");
 	}
 
 }
@@ -473,13 +532,14 @@
 		 * http://confluence.atlassian.com/display/BAMBOO/JUnit+parsing+in+Bamboo
 		 */
 		fprintf(f_xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
-		fprintf(f_xml, "<testsuite errors=\"0\" time=\"%u.%u\" tests=\"%u\" "
+		fprintf(f_xml, "<testsuites>\n");
+		fprintf(f_xml, "\t<testsuite errors=\"0\" time=\"%u.%u\" tests=\"%u\" "
 				"name=\"AsteriskUnitTests\">\n",
 				last_results.total_time / 1000, last_results.total_time % 1000,
 				last_results.total_tests);
-		fprintf(f_xml, "\t<properties>\n");
-		fprintf(f_xml, "\t\t<property name=\"version\" value=\"%s\"/>\n", ast_get_version());
-		fprintf(f_xml, "\t</properties>\n");
+		fprintf(f_xml, "\t\t<properties>\n");
+		fprintf(f_xml, "\t\t\t<property name=\"version\" value=\"%s\"/>\n", ast_get_version());
+		fprintf(f_xml, "\t\t</properties>\n");
 	}
 
 	/* txt header information */
@@ -517,7 +577,8 @@
 
 done:
 	if (f_xml) {
-		fprintf(f_xml, "</testsuite>\n");
+		fprintf(f_xml, "\t</testsuite>\n");
+		fprintf(f_xml, "</testsuites>\n");
 		fclose(f_xml);
 	}
 	if (f_txt) {

-- 
To view, visit https://gerrit.asterisk.org/9364
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Iec1a985eba1c478e5c1d65d5dfd95cb708442099
Gerrit-Change-Number: 9364
Gerrit-PatchSet: 3
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180709/f1a4975e/attachment.html>


More information about the asterisk-code-review mailing list