Skip to content

Commit

Permalink
feat: refactor tags and meta_data (#76)
Browse files Browse the repository at this point in the history
* feat: refactor tags and meta_data

* feat: rename metadata
  • Loading branch information
ASaiAnudeep authored Jul 13, 2024
1 parent a6936d2 commit 5f5d98c
Show file tree
Hide file tree
Showing 20 changed files with 682 additions and 413 deletions.
8 changes: 5 additions & 3 deletions src/models/TestCase.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as TestStep from './TestStep';
import * as TestAttachment from './TestAttachment';
import * as TestStep from './TestStep';

declare class TestCase {
name: string;
Expand All @@ -12,10 +12,12 @@ declare class TestCase {
status: string;
failure: string;
stack_trace: string;
tags: string[];
metadata: object;

steps: TestStep[];
attachments: TestAttachment[];
meta_data: Map<string,string>;


setFailure: SetFailureFunction
}
export type SetFailureFunction = (value: string) => string;
Expand Down
8 changes: 5 additions & 3 deletions src/models/TestCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ class TestCase {
this.status = 'NA';
this.failure = '';
this.stack_trace = '';
this.tags = [];
this.metadata = {};

this.steps = [];
this.attachments = [];
this.meta_data = new Map();
}

setFailure(value) {
this.failure = value ? unescape(value) : value;
}

}

module.exports = TestCase;
3 changes: 3 additions & 0 deletions src/models/TestResult.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ declare class TestResult {
retried: number;
duration: number;
status: string;
tags: string[];
metadata: object;

suites: TestSuite[];
}

Expand Down
3 changes: 3 additions & 0 deletions src/models/TestResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class TestResult {
this.retried = 0;
this.duration = 0;
this.status = 'NA';
this.tags = [];
this.metadata = {};

this.suites = [];
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/models/TestSuite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ declare class TestSuite {
skipped: number;
duration: number;
status: string;
meta_data: Map<string,string>;
tags: string[];
metadata: object;

cases: TestCase[];
}

Expand Down
4 changes: 3 additions & 1 deletion src/models/TestSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class TestSuite {
this.skipped = 0;
this.duration = 0;
this.status = 'NA';
this.meta_data = new Map();
this.tags = [];
this.metadata = {};

this.cases = [];
}

Expand Down
17 changes: 6 additions & 11 deletions src/parsers/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getTestCase(rawCase) {
setMetaData(rawCase, test_case);
if (rawCase.state && rawCase.state === "failed") {
test_case.status = 'FAIL';
set_error_and_stack_trace(test_case, rawCase.errorStack);
setErrorAndStackTrace(test_case, rawCase.errorStack);
}
else {
test_case.status = 'PASS';
Expand All @@ -23,7 +23,7 @@ function getTestCase(rawCase) {
* @param {TestCase} test_case
* @param {string?} message
*/
function set_error_and_stack_trace(test_case, message) {
function setErrorAndStackTrace(test_case, message) {
if (message) {
const stack_trace_start_index = message.indexOf(' at ');
if (stack_trace_start_index) {
Expand All @@ -43,21 +43,16 @@ function set_error_and_stack_trace(test_case, message) {
* @param {TestCase | TestSuite} test_element
*/
function setMetaData(element, test_element) {
const meta_tags = [];
const meta_raw_tags = [];
const tags = element.tags;
if (tags && tags.length > 0) {
for (const tag of tags) {
const [name, value] = tag["name"].substring(1).split("=");
if (value) {
test_element.meta_data.set(name, value);
if (tag["name"].includes("=")) {
const [name, value] = tag["name"].substring(1).split("=");
test_element.metadata[name] = value;
} else {
meta_tags.push(name);
meta_raw_tags.push(tag["name"]);
test_element.tags.push(tag["name"]);
}
}
test_element.meta_data.set("tags", meta_tags.join(","));
test_element.meta_data.set("tagsRaw", meta_raw_tags.join(","));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/parsers/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function getTestCase(rawCase, suite_meta) {
const test_case = new TestCase();
test_case.name = rawCase["@_name"];
test_case.duration = rawCase["@_time"] * 1000;
test_case.meta_data = new Map(suite_meta);
test_case.metadata = Object.assign({}, suite_meta);
setAttachments(rawCase, test_case);
setMetaData(rawCase, test_case);
if (rawCase.failure && rawCase.failure.length > 0) {
Expand Down Expand Up @@ -67,7 +67,7 @@ function getTestSuite(rawSuite) {
const raw_test_cases = rawSuite.testcase;
if (raw_test_cases) {
for (let i = 0; i < raw_test_cases.length; i++) {
suite.cases.push(getTestCase(raw_test_cases[i], suite.meta_data));
suite.cases.push(getTestCase(raw_test_cases[i], suite.metadata));
}
}
return suite;
Expand All @@ -81,13 +81,13 @@ function setMetaData(rawElement, test_element) {
if (rawElement.properties && rawElement.properties.property.length > 0) {
const raw_properties = rawElement.properties.property;
for (const raw_property of raw_properties) {
test_element.meta_data.set(raw_property["@_name"], raw_property["@_value"]);
test_element.metadata[raw_property["@_name"]] = raw_property["@_value"];
}
}
// handle testsuite specific attributes
if (test_element instanceof TestSuite) {
if (rawElement["@_hostname"]) {
test_element.meta_data.set("hostname", rawElement["@_hostname"]);
test_element.metadata["hostname"] = rawElement["@_hostname"];
}
}
}
Expand Down
25 changes: 10 additions & 15 deletions src/parsers/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function getTestSuite(rawSuite) {

/**
* Function to format the mocha raw json report
* @param {import("./mocha.result").MochaJsonData} raw_json
* @param {import("./mocha.result").MochaJsonData} raw_json
*/
function getTestResult(raw_json) {
const result = new TestResult();
Expand Down Expand Up @@ -82,7 +82,7 @@ function getTestResult(raw_json) {

/**
* Function to format the mocha raw json report
* @param {import("./mocha.result").MochaJsonData} raw_json
* @param {import("./mocha.result").MochaJsonData} raw_json
* @returns formatted json object
*/
function formatMochaJsonReport(raw_json) {
Expand Down Expand Up @@ -125,8 +125,8 @@ function formatMochaJsonReport(raw_json) {
}

/**
*
* @param {import("./mocha.result").MochaSuite} suite
*
* @param {import("./mocha.result").MochaSuite} suite
*/
function flattenTestSuite(suite) {
if (!suite.suites) {
Expand All @@ -144,27 +144,22 @@ function flattenTestSuite(suite) {
}

/**
*
* @param {TestCase | TestSuite} test_element
*
* @param {TestCase | TestSuite} test_element
*/
function setMetaData(test_element) {
const regexp = /([\@\#][^\s]*)/gm; // match @tag or #tag
const matches = [...test_element.name.matchAll(regexp)];
if (matches.length > 0) {
const meta_tags = [];
const meta_raw_tags = [];
for (const match of matches) {
const rawTag = match[0];
const [name, value] = rawTag.substring(1).split("=");
if (value) {
test_element.meta_data.set(name, value);
if (rawTag.includes("=")) {
const [name, value] = rawTag.substring(1).split("=");
test_element.metadata[name] = value;
} else {
meta_tags.push(name);
meta_raw_tags.push(rawTag);
test_element.tags.push(rawTag);
}
}
test_element.meta_data.set("tags", meta_tags.join(","));
test_element.meta_data.set("tagsRaw", meta_raw_tags.join(","));
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/parsers/mstest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ const RESULT_MAP = {
NotExecuted: "SKIP",
}

function populateMetaData(rawElement, map) {
/**
*
* @param {*} rawElement
* @param {TestCase | TestSuite} test_element
*/
function populateMetaData(rawElement, test_element) {
if (rawElement.TestCategory && rawElement.TestCategory.TestCategoryItem) {
let rawCategories = rawElement.TestCategory.TestCategoryItem;
for (let i = 0; i < rawCategories.length; i++) {
let categoryName = rawCategories[i]["@_TestCategory"];
map.set(categoryName, "");
test_element.tags.push(categoryName);

// create comma-delimited list of categories
if (map.has("Categories")) {
map.set("Categories", map.get("Categories").concat(",", categoryName));
if (test_element.metadata["Categories"]) {
test_element.metadata["Categories"] = test_element.metadata["Categories"].concat(",", categoryName)
} else {
map.set("Categories", categoryName);
test_element.metadata["Categories"] = categoryName;
}
}
}
Expand All @@ -36,7 +41,7 @@ function populateMetaData(rawElement, map) {
for (let i = 0; i < rawProperties.length; i++) {
let key = rawProperties[i].Key ?? "not-set";
let val = rawProperties[i].Value ?? "";
map.set(key, val);
map[key] = val;
}
}
}
Expand Down Expand Up @@ -120,7 +125,7 @@ function getTestCase(rawTestResult, definitionMap, testRunName) {
// populate attachments
populateAttachments(rawTestResult, testCase.attachments, testRunName);
// populate meta
populateMetaData(rawDefinition, testCase.meta_data);
populateMetaData(rawDefinition, testCase);

return testCase;
} else {
Expand Down
43 changes: 23 additions & 20 deletions src/parsers/nunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,30 @@ function populateAttachments(rawCase, attachments) {
}

function mergeMeta(map1, map2) {
for (let kvp of map1) {
map2.set(kvp[0], kvp[1]);
for (let kvp of Object.entries(map1)) {
map2[kvp[0]] = kvp[1];
}
}

function populateMetaData(raw, map) {
/**
*
* @param {*} raw
* @param {TestCase | TestSuite} test_element
*/
function populateMetaData(raw, test_element) {

// v2 supports categories
if (raw.categories) {
let categories = raw.categories.category;
for (let i = 0; i < categories.length; i++) {
let categoryName = categories[i]["@_name"];
map.set(categoryName, "");
test_element.tags.push(categoryName);

// create comma-delimited list of categories
if (map.has("Categories")) {
map.set("Categories", map.get("Categories").concat(",", categoryName));
if (test_element.metadata["Categories"]) {
test_element.metadata["Categories"] = test_element.metadata["Categories"].concat(",", categoryName);
} else {
map.set("Categories", categoryName);
test_element.metadata["Categories"] = categoryName;
}
}
}
Expand All @@ -74,15 +79,14 @@ function populateMetaData(raw, map) {
// v3 treats 'Categories' as property "Category"
if (propName == "Category") {

if (map.has("Categories")) {
map.set("Categories", map.get("Categories").concat(",", propValue));
if (test_element.metadata["Categories"]) {
test_element.metadata["Categories"] = test_element.metadata["Categories"].concat(",", propValue);
} else {
map.set("Categories", propValue);
test_element.metadata["Categories"] = propValue;
}
map.set(propValue, "");

} else {
map.set(propName, propValue);
test_element.metadata[propName] = propValue;
}
}
}
Expand Down Expand Up @@ -148,8 +152,8 @@ function getTestCases(rawSuite, parent_meta) {
// populate attachments
populateAttachments(rawCase, testCase.attachments);
// copy parent_meta data to test case
mergeMeta(parent_meta, testCase.meta_data);
populateMetaData(rawCase, testCase.meta_data);
mergeMeta(parent_meta, testCase.metadata);
populateMetaData(rawCase, testCase);

cases.push(testCase);
}
Expand All @@ -165,8 +169,8 @@ function getTestSuites(rawSuites, assembly_meta) {
let rawSuite = rawSuites[i];

if (rawSuite["@_type"] == "Assembly") {
assembly_meta = new Map();
populateMetaData(rawSuite, assembly_meta);
assembly_meta = {};
populateMetaData(rawSuite, { tags: [], metadata: assembly_meta });
}

if (hasNestedSuite(rawSuite)) {
Expand All @@ -180,10 +184,9 @@ function getTestSuites(rawSuites, assembly_meta) {
suite.duration = (rawSuite["@_time"] ?? rawSuite["@_duration"]) * 1000; // in milliseconds
suite.status = RESULT_MAP[rawSuite["@_result"]];

const meta_data = new Map();
mergeMeta(assembly_meta, meta_data);
populateMetaData(rawSuite, meta_data);
suite.cases.push(...getTestCases(rawSuite, meta_data));
mergeMeta(assembly_meta, suite.metadata);
populateMetaData(rawSuite, suite);
suite.cases.push(...getTestCases(rawSuite, suite.metadata));

// calculate totals
suite.total = suite.cases.length;
Expand Down
5 changes: 1 addition & 4 deletions src/parsers/testng.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ function getTestCase(rawCase, testCaseToGroupMap) {
const key = getFullTestName(rawCase);
if (testCaseToGroupMap.has(key)) {
let groups = testCaseToGroupMap.get(key);
test_case.meta_data.set("groups", groups.join(","));
groups.forEach(group => {
test_case.meta_data.set(group, "");
})
test_case.tags = groups;
}
if (rawCase.exception) {
test_case.setFailure(rawCase.exception[0].message);
Expand Down
Loading

0 comments on commit 5f5d98c

Please sign in to comment.