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

George Joseph asteriskteam at digium.com
Fri Jul 6 09:06:21 CDT 2018


George Joseph has uploaded this change for review. ( https://gerrit.asterisk.org/9361


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, 62 insertions(+), 10 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/61/9361/1

diff --git a/main/test.c b/main/test.c
index 3fb1074..d3688b6 100644
--- a/main/test.c
+++ b/main/test.c
@@ -269,21 +269,71 @@
 	test->state = state;
 }
 
+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 *tmp_cat = ast_strdupa(test->info.category + 1);
+	char *next_cat = NULL;
+	char *tmp_name = (char *)test->info.name;
+	struct ast_str *category = ast_str_create(strlen(test->info.category) + 32);
+
+	if (!category || !f || !test || test->state == AST_TEST_NOT_RUN) {
 		return;
 	}
 
-	fprintf(f, "\t<testcase time=\"%u.%u\" name=\"%s%s\"%s>\n",
+	while((next_cat = ast_strsep(&tmp_cat, '/', AST_STRSEP_TRIM))) {
+		char *prefix = "";
+
+		if (is_reserved_word(next_cat)) {
+			prefix = "_";
+		}
+		ast_str_append(&category, 0, ".%s%s", prefix, next_cat);
+	}
+	tmp_cat = ast_str_buffer(category);
+	/* Skip past the initial '.' */
+	tmp_cat++;
+
+	if (is_reserved_word(test->info.name)) {
+		size_t name_length = strlen(test->info.name) + 2;
+
+		tmp_name = ast_alloca(name_length);
+		snprintf(tmp_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,
+			tmp_cat, tmp_name,
 			test->state == AST_TEST_PASS ? "/" : "");
 
 	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");
 	}
 
 }
@@ -475,13 +525,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 */
@@ -519,7 +570,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/9361
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec1a985eba1c478e5c1d65d5dfd95cb708442099
Gerrit-Change-Number: 9361
Gerrit-PatchSet: 1
Gerrit-Owner: George Joseph <gjoseph at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180706/6bb9d001/attachment-0001.html>


More information about the asterisk-code-review mailing list