Skip to content

Commit ff2fc66

Browse files
committed
Simplify uploadPayload tests
1 parent a841c54 commit ff2fc66

File tree

1 file changed

+50
-51
lines changed

1 file changed

+50
-51
lines changed

src/upload-lib.test.ts

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -878,15 +878,14 @@ const uploadPayloadMacro = test.macro({
878878
t: ExecutionContext<unknown>,
879879
options: {
880880
analysis: analyses.AnalysisConfig;
881-
envVars?: Record<string, string>;
882881
body: (
883882
t: ExecutionContext<unknown>,
884883
upload: () => Promise<string>,
885-
tmpDir: string,
886884
requestStub: sinon.SinonStub,
887885
mockData: {
888886
payload: { sarif: string; commit_sha: string };
889-
repositoryNwo: { owner: string; repo: string };
887+
owner: string;
888+
repo: string;
890889
response: {
891890
status: number;
892891
data: { id: string };
@@ -897,50 +896,47 @@ const uploadPayloadMacro = test.macro({
897896
) => void | Promise<void>;
898897
},
899898
) => {
900-
await withTmpDir(async (tmpDir) => {
901-
process.env.RUNNER_TEMP = tmpDir;
902-
for (const [key, value] of Object.entries(options.envVars ?? {})) {
903-
process.env[key] = value;
904-
}
905-
906-
const mockData = {
907-
payload: { sarif: "base64data", commit_sha: "abc123" },
908-
repositoryNwo: { owner: "test-owner", repo: "test-repo" },
909-
response: {
910-
status: 200,
911-
data: { id: "uploaded-sarif-id" },
912-
headers: {},
913-
url: options.analysis.target,
899+
const mockData = {
900+
payload: { sarif: "base64data", commit_sha: "abc123" },
901+
owner: "test-owner",
902+
repo: "test-repo",
903+
response: {
904+
status: 200,
905+
data: { id: "uploaded-sarif-id" },
906+
headers: {},
907+
url: options.analysis.target,
908+
},
909+
};
910+
911+
const client = github.getOctokit("123");
912+
sinon.stub(api, "getApiClient").value(() => client);
913+
const requestStub = sinon.stub(client, "request");
914+
915+
const upload = async () =>
916+
uploadLib.uploadPayload(
917+
mockData.payload,
918+
{
919+
owner: mockData.owner,
920+
repo: mockData.repo,
914921
},
915-
};
916-
917-
const client = github.getOctokit("123");
918-
sinon.stub(api, "getApiClient").value(() => client);
919-
const requestStub = sinon.stub(client, "request");
920-
921-
const upload = async () =>
922-
uploadLib.uploadPayload(
923-
mockData.payload,
924-
mockData.repositoryNwo,
925-
getRunnerLogger(true),
926-
options.analysis,
927-
);
922+
getRunnerLogger(true),
923+
options.analysis,
924+
);
928925

929-
await options.body(t, upload, tmpDir, requestStub, mockData);
930-
});
926+
await options.body(t, upload, requestStub, mockData);
931927
},
932928
title: (providedTitle = "", options: { analysis: analyses.AnalysisConfig }) =>
933929
`uploadPayload - ${options.analysis.name} - ${providedTitle}`,
934930
});
935931

936932
for (const analysis of [CodeScanning, CodeQuality]) {
937-
test("successful upload", uploadPayloadMacro, {
933+
test("uploads successfully", uploadPayloadMacro, {
938934
analysis,
939-
body: async (t, upload, _tmpDir, requestStub, mockData) => {
935+
body: async (t, upload, requestStub, mockData) => {
940936
requestStub
941937
.withArgs(analysis.target, {
942-
owner: mockData.repositoryNwo.owner,
943-
repo: mockData.repositoryNwo.repo,
938+
owner: mockData.owner,
939+
repo: mockData.repo,
944940
data: mockData.payload,
945941
})
946942
.onFirstCall()
@@ -957,26 +953,29 @@ for (const analysis of [CodeScanning, CodeQuality]) {
957953
]) {
958954
test(`skips upload when ${envVar} is set`, uploadPayloadMacro, {
959955
analysis,
960-
envVars: {
961-
[envVar]: "true",
962-
},
963-
body: async (t, upload, tmpDir, requestStub, mockData) => {
964-
const result = await upload();
965-
t.is(result, "dummy-sarif-id");
966-
t.false(requestStub.called);
967-
968-
const payloadFile = path.join(tmpDir, `payload-${analysis.kind}.json`);
969-
t.true(fs.existsSync(payloadFile));
970-
971-
const savedPayload = JSON.parse(fs.readFileSync(payloadFile, "utf8"));
972-
t.deepEqual(savedPayload, mockData.payload);
973-
},
956+
body: async (t, upload, requestStub, mockData) =>
957+
withTmpDir(async (tmpDir) => {
958+
process.env.RUNNER_TEMP = tmpDir;
959+
process.env[envVar] = "true";
960+
const result = await upload();
961+
t.is(result, "dummy-sarif-id");
962+
t.false(requestStub.called);
963+
964+
const payloadFile = path.join(
965+
tmpDir,
966+
`payload-${analysis.kind}.json`,
967+
);
968+
t.true(fs.existsSync(payloadFile));
969+
970+
const savedPayload = JSON.parse(fs.readFileSync(payloadFile, "utf8"));
971+
t.deepEqual(savedPayload, mockData.payload);
972+
}),
974973
});
975974
}
976975

977976
test("handles error", uploadPayloadMacro, {
978977
analysis,
979-
body: async (t, upload, _tmpDir, requestStub, _mockData) => {
978+
body: async (t, upload, requestStub) => {
980979
const wrapApiConfigurationErrorStub = sinon.stub(
981980
api,
982981
"wrapApiConfigurationError",

0 commit comments

Comments
 (0)