Skip to content

Commit

Permalink
feat: parentSuite by junit reader
Browse files Browse the repository at this point in the history
  • Loading branch information
delatrie committed Jan 16, 2025
1 parent 02796e6 commit c0053f5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/reader/src/junitxml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const STDOUT_ATTACHMENT_NAME = "System output";
const STDERR_ATTACHMENT_NAME = "System error";

const SUITE_PACKAGE_NAME = "package";
const SUITE_PARENT_LABEL_NAME = "parentSuite";
const SUITE_LABEL_NAME = "suite";
const TEST_CLASS_LABEL_NAME = "testClass";

Expand Down Expand Up @@ -90,7 +91,7 @@ const parseRootElement = async (visitor: ResultsVisitor, xml: Record<string, any
}

for (const testSuitesArrayElement of testSuitesArray) {
await parseTestSuite(visitor, testSuitesArrayElement);
await parseTestSuite(visitor, testSuitesArrayElement, true);
}
return true;
}
Expand All @@ -99,11 +100,11 @@ const parseRootElement = async (visitor: ResultsVisitor, xml: Record<string, any
return false;
}

await parseTestSuite(visitor, testSuite);
await parseTestSuite(visitor, testSuite, false);
return true;
};

const parseTestSuite = async (visitor: ResultsVisitor, testSuite: Record<string, any>) => {
const parseTestSuite = async (visitor: ResultsVisitor, testSuite: Record<string, any>, isAggregated: boolean) => {
const { name, package: packageAttribute, testcase } = testSuite;

if (!isStringAnyRecordArray(testcase)) {
Expand All @@ -115,6 +116,7 @@ const parseTestSuite = async (visitor: ResultsVisitor, testSuite: Record<string,
visitor,
{ name: ensureString(name), suitePackage: ensureString(packageAttribute) },
testcaseElement,
isAggregated,
);
}
};
Expand All @@ -123,6 +125,7 @@ const parseTestCase = async (
visitor: ResultsVisitor,
{ name: suiteName, suitePackage }: { name?: string; suitePackage?: string },
testCase: Record<string, any>,
isAggregated: boolean,
) => {
const {
"name": nameAttribute,
Expand Down Expand Up @@ -151,7 +154,7 @@ const parseTestCase = async (
message,
trace,
steps: await parseAttachments(visitor, systemOut, systemErr),
labels: convertLabels(suitePackage, suiteName, className),
labels: convertLabels({ suitePackage, suiteName, className, isAggregated }),
},
{ readerId },
);
Expand Down Expand Up @@ -188,11 +191,24 @@ const visitPlainTextAttachment = async (
};
};

const convertLabels = (suitePackage?: string, suiteName?: string, className?: string) => {
const convertLabels = ({
suitePackage,
suiteName,
className,
isAggregated,
}: {
suitePackage: string | undefined;
suiteName: string | undefined;
className: string | undefined;
isAggregated: boolean;
}) => {
const labels: RawTestLabel[] = [];

if (suitePackage) {
labels.push({ name: SUITE_PACKAGE_NAME, value: suitePackage });
if (isAggregated) {
labels.push({ name: SUITE_PARENT_LABEL_NAME, value: suitePackage });
}
}

if (suiteName) {
Expand Down
37 changes: 37 additions & 0 deletions packages/reader/test/junitxml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,43 @@ describe("junit xml reader", () => {

expect(trs).toMatchObject([{ labels: expect.not.arrayContaining([{ name: "suite" }]) }]);
});

// See https://github.com/windyroad/JUnit-Schema/blob/cfa434d4b8e102a8f55b8727b552a0063ee9044e/JUnit.xsd#L173
it("should add package as parent suite for an aggregated document", async () => {
const visitor = await readResults(junitXml, {
"junitxmldata/labels/suites/wellDefinedAggregated.xml": randomTestsuiteFileName(),
});

expect(visitor.visitTestResult).toHaveBeenCalledTimes(1);

const trs = visitor.visitTestResult.mock.calls.map((c) => c[0]);

expect(trs).toMatchObject([{ labels: expect.arrayContaining([{ name: "parentSuite", value: "foo" }]) }]);
});

it("should ignore a missing package of an aggregated document", async () => {
const visitor = await readResults(junitXml, {
"junitxmldata/labels/suites/aggregatedPackageMissing.xml": randomTestsuiteFileName(),
});

expect(visitor.visitTestResult).toHaveBeenCalledTimes(1);

const trs = visitor.visitTestResult.mock.calls.map((c) => c[0]);

expect(trs).toMatchObject([{ labels: expect.not.arrayContaining([{ name: "parentSuite" }]) }]);
});

it("should ignore an ill-formed package of an aggregated document", async () => {
const visitor = await readResults(junitXml, {
"junitxmldata/labels/suites/aggregatedPackageInvalid.xml": randomTestsuiteFileName(),
});

expect(visitor.visitTestResult).toHaveBeenCalledTimes(1);

const trs = visitor.visitTestResult.mock.calls.map((c) => c[0]);

expect(trs).toMatchObject([{ labels: expect.not.arrayContaining([{ name: "parentSuite" }]) }]);
});
});

describe("testClass", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="foo">
<package>
<foo>bar</foo>
</package>
<testcase name="baz" />
</testsuite>
</testsuites>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="foo">
<testcase name="bar" />
</testsuite>
</testsuites>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite package="foo" name="bar">
<testcase name="baz" />
</testsuite>
</testsuites>

0 comments on commit c0053f5

Please sign in to comment.