Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ctrf validate-strict <file-path> [--spec-version <version>]
**Parameters:**

- `file-path`: Path to CTRF report file (required)
- `--spec-version`: CTRF specification version to validate against. Supports `0.0.1`, `0.0.2`, `0.0.3`, `0.0.4`, `0.1.0`, `0.2.0`, and `latest` (default).
- `--spec-version`: CTRF specification version to validate against. Supports `0.0.1`, `0.0.2`, `0.0.3`, `0.0.4`, `0.1.0`, `0.2.0`, `0.3.0`, and `latest` (default).

**Modes:**

Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/2.6.4/schema.json",
"$schema": "https://biomejs.dev/schemas/2.6.8/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
Expand Down
518 changes: 307 additions & 211 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "ctrf-cli",
"version": "0.5.0",
"version": "0.6.0",
"description": "Various CTRF utilities available from the command line",
"main": "dist/index.js",
"bin": {
Expand Down Expand Up @@ -60,14 +60,14 @@
"yargs": "18.2.0"
},
"devDependencies": {
"@biomejs/biome": "2.6.4",
"@biomejs/biome": "2.6.8",
"@d2t/vitest-ctrf-json-reporter": "1.3.0",
"@types/node": "26.6.6",
"@types/yargs": "17.0.35",
"@vitest/coverage-v8": "5.0.5",
"ctrf": "0.5.0",
"@vitest/coverage-v8": "5.1.1",
"ctrf": "0.6.0",
"esbuild": "0.28.2",
"typescript": "7.0.2",
"vitest": "5.0.5"
"vitest": "5.1.1"
}
}
75 changes: 75 additions & 0 deletions scripts/package-smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const supportedSpecVersions = [
"0.0.4",
"0.1.0",
"0.2.0",
"0.3.0",
];

function run(command, args, options = {}) {
Expand Down Expand Up @@ -185,6 +186,80 @@ try {
assert.match(historical.stderr, /must NOT have additional properties/);
}

const currentReport = path.join(temporaryRoot, "current.json");
const current = {
reportFormat: "CTRF",
specVersion: "0.3.0",
reportId: "abcdefab-e29b-41d4-a716-446655440001",
provenance: {
inputs: [{ reportId: "abcdefab-e29b-41d4-a716-446655440002" }],
},
results: {
tool: { name: "package-control" },
summary: {
tests: 1,
passed: 1,
failed: 0,
skipped: 0,
pending: 0,
other: 0,
start: 100,
stop: 120,
},
errors: [{ message: "worker setup" }],
tests: [
{
name: "case",
description: "",
status: "passed",
duration: 1,
retries: 1,
retryAttempts: [
{
attempt: 1,
status: "failed",
threadId: "worker-1",
assertionFailures: [{ expected: "200", actual: "500" }],
},
],
},
],
},
};
writeFileSync(currentReport, JSON.stringify(current));
for (const binary of ["ctrf", "ctrf-cli"]) {
const accepted = run(
"npx",
[
"--yes",
`--package=${tarballPath}`,
binary,
"validate-strict",
currentReport,
"--spec-version",
"0.3.0",
],
{ cwd: temporaryRoot },
);
assert.match(accepted.stdout, /is valid CTRF/);
}
current.provenance.inputs[0].reportId = current.reportId;
writeFileSync(currentReport, JSON.stringify(current));
const selfReference = run(
"npx",
[
"--yes",
`--package=${tarballPath}`,
"ctrf",
"validate-strict",
currentReport,
"--spec-version",
"0.3.0",
],
{ cwd: temporaryRoot, expectedStatus: 2 },
);
assert.match(selfReference.stderr, /must not reference the current report/);

const invalidReport = path.join(temporaryRoot, "invalid.json");
writeFileSync(
invalidReport,
Expand Down
68 changes: 68 additions & 0 deletions src/merge.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { validateStrict } from "ctrf";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import fs from "node:fs";
import path from "node:path";
Expand Down Expand Up @@ -77,6 +78,73 @@ describe("mergeReports", () => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});

describe("specification 0.3.0", () => {
function configureInputs(versions = ["0.3.0", "0.3.0"]) {
return [testReport1, testReport2].map((file, index) => {
const report = JSON.parse(fs.readFileSync(file, "utf8"));
report.reportFormat = "CTRF";
report.specVersion = versions[index];
report.reportId =
index === 0
? "abcdefab-e29b-41d4-a716-446655440001"
: "abcdefab-e29b-41d4-a716-446655440002";
fs.writeFileSync(file, JSON.stringify(report));
return report;
});
}

it("preserves later errors and duplicates without inflating counts, and records direct sources", async () => {
const reports = configureInputs();
reports[1].results.errors = [
{ message: "worker setup", extra: { vendor: { attempt: 2 } } },
{ message: "worker setup", extra: { vendor: { attempt: 2 } } },
];
reports[0].provenance = {
inputs: [{ reportId: "abcdefab-e29b-41d4-a716-446655440003" }],
};
fs.writeFileSync(testReport1, JSON.stringify(reports[0]));
fs.writeFileSync(testReport2, JSON.stringify(reports[1]));
const output = path.join(tmpDir, "merged.json");
await mergeReports(testReportDir, output, true);
const merged = JSON.parse(fs.readFileSync(output, "utf8"));
validateStrict(merged, { specVersion: "0.3.0" });
expect(merged.results.errors).toEqual(reports[1].results.errors);
expect(merged.results.summary.tests).toBe(3);
expect(merged.provenance?.inputs).toEqual(
reports.map(({ reportId }) => ({ reportId })),
);
expect(reports.map(({ reportId }) => reportId)).not.toContain(
merged.reportId,
);
expect(JSON.parse(fs.readFileSync(testReport1, "utf8"))).toEqual(
reports[0],
);
});

it("omits provenance when a direct source has no report ID", async () => {
const reports = configureInputs();
delete reports[1].reportId;
fs.writeFileSync(testReport2, JSON.stringify(reports[1]));
const output = path.join(tmpDir, "merged.json");
await mergeReports(testReportDir, output, true);
const merged = JSON.parse(fs.readFileSync(output, "utf8"));
expect(merged).not.toHaveProperty("provenance");
validateStrict(merged, { specVersion: "0.3.0" });
});

it.each([
["0.2.0", "0.3.0"],
["0.3.0", "0.2.0"],
])("keeps accepting mixed %s and %s inputs", async (first, second) => {
configureInputs([first, second]);
const output = path.join(tmpDir, "merged.json");
await mergeReports(testReportDir, output, true);
const merged = JSON.parse(fs.readFileSync(output, "utf8"));
expect(merged.specVersion).toBe(first);
expect(merged.results.summary.tests).toBe(3);
});
});

describe("output path handling", () => {
it("should save with custom filename in input directory", async () => {
const outputFilename = "my-merged-report.json";
Expand Down
90 changes: 56 additions & 34 deletions src/merge.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { generateReportId, type CTRFReport, validateStrict } from "ctrf";
import fs from "node:fs";
import path from "node:path";

Expand Down Expand Up @@ -75,42 +76,63 @@ export async function mergeReports(
console.log(`Created output directory: ${outputDirPath}`);
}

const mergedReport = ctrfReportFiles
.map((file) => {
console.log("Merging report:", file);
const filePath = path.join(directoryPath, file);
const fileContent = fs.readFileSync(filePath, "utf8");
return JSON.parse(fileContent);
})
.reduce(
(acc, curr) => {
if (!acc.results) {
return curr;
}

acc.results.summary.tests += curr.results.summary.tests;
acc.results.summary.passed += curr.results.summary.passed;
acc.results.summary.failed += curr.results.summary.failed;
acc.results.summary.skipped += curr.results.summary.skipped;
acc.results.summary.pending += curr.results.summary.pending;
acc.results.summary.other += curr.results.summary.other;

acc.results.tests.push(...curr.results.tests);

acc.results.summary.start = Math.min(
acc.results.summary.start,
curr.results.summary.start,
);
acc.results.summary.stop = Math.max(
acc.results.summary.stop,
curr.results.summary.stop,
);

return acc;
},
{ results: null },
const reports: CTRFReport[] = ctrfReportFiles.map((file) => {
console.log("Merging report:", file);
const filePath = path.join(directoryPath, file);
const fileContent = fs.readFileSync(filePath, "utf8");
return JSON.parse(fileContent);
});
const specVersion = reports[0].specVersion;
const currentInputs = reports.every(
(report) => report.specVersion === "0.3.0",
);
const inputs = new Map<string, { reportId: string }>();
if (specVersion === "0.3.0") {
for (const report of reports) {
if (currentInputs) validateStrict(report, { specVersion });
if (report.reportId) {
const id = report.reportId.toLowerCase();
if (!inputs.has(id)) inputs.set(id, { reportId: report.reportId });
}
}
}
const completeProvenance = reports.every((report) => report.reportId);
const mergedReport = reports.reduce((acc, curr) => {
acc.results.summary.tests += curr.results.summary.tests;
acc.results.summary.passed += curr.results.summary.passed;
acc.results.summary.failed += curr.results.summary.failed;
acc.results.summary.skipped += curr.results.summary.skipped;
acc.results.summary.pending += curr.results.summary.pending;
acc.results.summary.other += curr.results.summary.other;

acc.results.tests.push(...curr.results.tests);
if (curr.results.errors?.length) {
acc.results.errors = [
...(acc.results.errors ?? []),
...curr.results.errors,
];
}

acc.results.summary.start = Math.min(
acc.results.summary.start,
curr.results.summary.start,
);
acc.results.summary.stop = Math.max(
acc.results.summary.stop,
curr.results.summary.stop,
);

return acc;
}, reports.shift() as CTRFReport);

if (specVersion === "0.3.0" && ctrfReportFiles.length > 1) {
mergedReport.reportId = generateReportId();
delete mergedReport.provenance;
if (completeProvenance)
mergedReport.provenance = { inputs: [...inputs.values()] };
if (currentInputs) validateStrict(mergedReport, { specVersion });
}

fs.writeFileSync(outputPath, JSON.stringify(mergedReport, null, 2));

if (!keepReports) {
Expand Down
37 changes: 21 additions & 16 deletions src/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,27 @@ describe("validateReport", () => {
expect(validate(currentReport).valid).toBe(true);
});

it.each(["0.0.1", "0.0.2", "0.0.3", "0.0.4", "0.1.0", "0.2.0"] as const)(
"should validate against CTRF %s",
async (specVersion) => {
fs.writeFileSync(
validReportPath,
JSON.stringify({ ...validReport, specVersion }, null, 2),
);

await validateReport(validReportPath, false, specVersion);

expect(exitSpy).toHaveBeenCalledWith(0);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining("is valid CTRF"),
);
},
);
it.each([
"0.0.1",
"0.0.2",
"0.0.3",
"0.0.4",
"0.1.0",
"0.2.0",
"0.3.0",
] as const)("should validate against CTRF %s", async (specVersion) => {
fs.writeFileSync(
validReportPath,
JSON.stringify({ ...validReport, specVersion }, null, 2),
);

await validateReport(validReportPath, false, specVersion);

expect(exitSpy).toHaveBeenCalledWith(0);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining("is valid CTRF"),
);
});

it("should apply the selected historical schema", async () => {
const reportWithLabels = {
Expand Down