Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add test for uploadSarif with output directory
  • Loading branch information
mbg committed Oct 22, 2025
commit def04c1c0ec9589b3a2943faa40a555c80a7a1a7
86 changes: 54 additions & 32 deletions src/upload-sarif.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ interface UploadSarifExpectedResult {
expectedFiles?: string[];
}

function mockPostProcessSarifFiles() {
const postProcessSarifFiles = sinon.stub(uploadLib, "postProcessSarifFiles");

for (const analysisKind of Object.values(AnalysisKind)) {
const analysisConfig = getAnalysisConfig(analysisKind);
postProcessSarifFiles
.withArgs(
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
analysisConfig,
)
.resolves({ sarif: { runs: [] }, analysisKey: "", environment: "" });
}

return postProcessSarifFiles;
}

const uploadSarifMacro = test.macro({
exec: async (
t: ExecutionContext<unknown>,
Expand All @@ -33,27 +53,14 @@ const uploadSarifMacro = test.macro({

const toFullPath = (filename: string) => path.join(tempDir, filename);

const postProcessSarifFiles = sinon.stub(
uploadLib,
"postProcessSarifFiles",
);
const postProcessSarifFiles = mockPostProcessSarifFiles();
const uploadProcessedFiles = sinon.stub(
uploadLib,
"uploadProcessedFiles",
);

for (const analysisKind of Object.values(AnalysisKind)) {
const analysisConfig = getAnalysisConfig(analysisKind);
postProcessSarifFiles
.withArgs(
logger,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
analysisConfig,
)
.resolves({ sarif: { runs: [] }, analysisKey: "", environment: "" });
uploadProcessedFiles
.withArgs(logger, sinon.match.any, analysisConfig, sinon.match.any)
.resolves(expectedResult[analysisKind as AnalysisKind]?.uploadResult);
Expand Down Expand Up @@ -203,26 +210,9 @@ test("uploadSarif doesn't upload if `upload` != `always`", async (t) => {

const toFullPath = (filename: string) => path.join(tempDir, filename);

const postProcessSarifFiles = sinon.stub(
uploadLib,
"postProcessSarifFiles",
);
const postProcessSarifFiles = mockPostProcessSarifFiles();
const uploadProcessedFiles = sinon.stub(uploadLib, "uploadProcessedFiles");

for (const analysisKind of Object.values(AnalysisKind)) {
const analysisConfig = getAnalysisConfig(analysisKind);
postProcessSarifFiles
.withArgs(
logger,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
analysisConfig,
)
.resolves({ sarif: { runs: [] }, analysisKey: "", environment: "" });
}

fs.writeFileSync(toFullPath("test.sarif"), "");
fs.writeFileSync(toFullPath("test.quality.sarif"), "");

Expand All @@ -233,3 +223,35 @@ test("uploadSarif doesn't upload if `upload` != `always`", async (t) => {
t.assert(uploadProcessedFiles.notCalled);
});
});

test("uploadSarif writes processed SARIF files if output directory is provided", async (t) => {
await util.withTmpDir(async (tempDir) => {
const logger = getRunnerLogger(true);
const features = createFeatures([]);

const toFullPath = (filename: string) => path.join(tempDir, filename);

const postProcessSarifFiles = mockPostProcessSarifFiles();

fs.writeFileSync(toFullPath("test.sarif"), "");
fs.writeFileSync(toFullPath("test.quality.sarif"), "");

const processedOutPath = path.join(tempDir, "processed");
const actual = await uploadSarif(
logger,
features,
"never",
"",
tempDir,
"",
processedOutPath,
);

t.truthy(actual);
t.assert(postProcessSarifFiles.calledTwice);
t.assert(fs.existsSync(path.join(processedOutPath, "upload.sarif")));
t.assert(
fs.existsSync(path.join(processedOutPath, "upload.quality.sarif")),
);
});
});