From 320fdb37738bd4752e64aed181ce42af6e813449 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Fri, 26 Sep 2025 15:32:57 +0100 Subject: [PATCH] Tests: ensure `uploadSpecifiedFiles` wasn't called if we don't expect it to be --- src/upload-sarif.test.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/upload-sarif.test.ts b/src/upload-sarif.test.ts index d2d662b0e..d46443aff 100644 --- a/src/upload-sarif.test.ts +++ b/src/upload-sarif.test.ts @@ -109,7 +109,7 @@ const uploadSarifMacro = test.macro({ "uploadSpecifiedFiles", ); - for (const analysisKind of Object.keys(expectedResult)) { + for (const analysisKind of Object.values(AnalysisKind)) { uploadSpecifiedFiles .withArgs( sinon.match.any, @@ -135,8 +135,10 @@ const uploadSarifMacro = test.macro({ for (const analysisKind of Object.values(AnalysisKind)) { const analyisKindResult = expectedResult[analysisKind]; if (analyisKindResult) { + // We are expecting a result for this analysis kind, check that we have it. t.deepEqual(actual[analysisKind], analyisKindResult.uploadResult); - + // Additionally, check that the mocked `uploadSpecifiedFiles` was called with only the file paths + // that we expected it to be called with. t.assert( uploadSpecifiedFiles.calledWith( analyisKindResult.expectedFiles?.map(toFullPath) ?? @@ -151,7 +153,24 @@ const uploadSarifMacro = test.macro({ ), ); } else { + // Otherwise, we are not expecting a result for this analysis kind. However, note that `undefined` + // is also returned by our mocked `uploadSpecifiedFiles` when there is no expected result for this + // analysis kind. t.is(actual[analysisKind], undefined); + // Therefore, we also check that the mocked `uploadSpecifiedFiles` was not called for this analysis kind. + t.assert( + !uploadSpecifiedFiles.calledWith( + sinon.match.any, + sinon.match.any, + sinon.match.any, + features, + logger, + analysisKind === AnalysisKind.CodeScanning + ? CodeScanning + : CodeQuality, + ), + `uploadSpecifiedFiles was called for ${analysisKind}, but should not have been.`, + ); } } });