Skip to content

Commit 3c8c0ae

Browse files
committed
Remove unnecessary sinon restore calls
1 parent 93d215d commit 3c8c0ae

5 files changed

Lines changed: 38 additions & 121 deletions

File tree

src/diff-informed-analysis-utils.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ const testShouldPerformDiffInformedAnalysis = makeMacro({
7373
[Feature.DiffInformedQueries]: testCase.featureEnabled,
7474
});
7575

76-
const getGitHubVersionStub = sinon
76+
sinon
7777
.stub(apiClient, "getGitHubVersion")
7878
.resolves(testCase.gitHubVersion);
79-
const getPullRequestBranchesStub = sinon
79+
sinon
8080
.stub(actionsUtil, "getPullRequestBranches")
8181
.returns(testCase.pullRequestBranches);
8282

@@ -89,9 +89,6 @@ const testShouldPerformDiffInformedAnalysis = makeMacro({
8989
t.is(result, expectedResult);
9090

9191
delete process.env.CODEQL_ACTION_DIFF_INFORMED_QUERIES;
92-
93-
getGitHubVersionStub.restore();
94-
getPullRequestBranchesStub.restore();
9592
});
9693
},
9794
title: (title) => `shouldPerformDiffInformedAnalysis: ${title}`,

src/git-utils.test.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ test.serial(
3333

3434
const actualRef = await gitUtils.getRef();
3535
t.deepEqual(actualRef, expectedRef);
36-
callback.restore();
3736
});
3837
},
3938
);
@@ -54,7 +53,6 @@ test.serial(
5453

5554
const actualRef = await gitUtils.getRef();
5655
t.deepEqual(actualRef, expectedRef);
57-
callback.restore();
5856
});
5957
},
6058
);
@@ -73,7 +71,6 @@ test.serial(
7371

7472
const actualRef = await gitUtils.getRef();
7573
t.deepEqual(actualRef, "refs/pull/1/head");
76-
callback.restore();
7774
});
7875
},
7976
);
@@ -100,8 +97,6 @@ test.serial(
10097

10198
const actualRef = await gitUtils.getRef();
10299
t.deepEqual(actualRef, "refs/pull/2/merge");
103-
callback.restore();
104-
getAdditionalInputStub.restore();
105100
});
106101
},
107102
);
@@ -161,7 +156,6 @@ test.serial(
161156
"Both 'ref' and 'sha' are required if one of them is provided.",
162157
},
163158
);
164-
getAdditionalInputStub.restore();
165159
});
166160
},
167161
);
@@ -188,7 +182,6 @@ test.serial(
188182
"Both 'ref' and 'sha' are required if one of them is provided.",
189183
},
190184
);
191-
getAdditionalInputStub.restore();
192185
});
193186
},
194187
);
@@ -242,7 +235,6 @@ test.serial("isAnalyzingDefaultBranch()", async (t) => {
242235
process.env["GITHUB_EVENT_NAME"] = "schedule";
243236
process.env["GITHUB_REF"] = "refs/heads/main";
244237
t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), false);
245-
getAdditionalInputStub.restore();
246238
});
247239
});
248240

@@ -254,8 +246,6 @@ test.serial("determineBaseBranchHeadCommitOid non-pullrequest", async (t) => {
254246
const result = await gitUtils.determineBaseBranchHeadCommitOid(__dirname);
255247
t.deepEqual(result, undefined);
256248
t.deepEqual(0, infoStub.callCount);
257-
258-
infoStub.restore();
259249
});
260250

261251
test.serial(
@@ -276,8 +266,6 @@ test.serial(
276266
"git call failed. Will calculate the base branch SHA on the server. Error: " +
277267
"The checkout path provided to the action does not appear to be a git repository.",
278268
);
279-
280-
infoStub.restore();
281269
},
282270
);
283271

@@ -301,8 +289,6 @@ test.serial("determineBaseBranchHeadCommitOid other error", async (t) => {
301289
"The checkout path provided to the action does not appear to be a git repository.",
302290
),
303291
);
304-
305-
infoStub.restore();
306292
});
307293

308294
test.serial(
@@ -315,16 +301,12 @@ test.serial(
315301
process.env["GITHUB_EVENT_NAME"] = "pull_request";
316302
process.env["GITHUB_SHA"] = mergeSha;
317303

318-
const runGitCommandStub = sinon
304+
sinon
319305
.stub(gitUtils as any, "runGitCommand")
320306
.resolves(`commit ${mergeSha}\nparent ${baseOid}\nparent ${headOid}\n`);
321307

322-
try {
323-
const result = await gitUtils.determineBaseBranchHeadCommitOid(__dirname);
324-
t.deepEqual(result, baseOid);
325-
} finally {
326-
runGitCommandStub.restore();
327-
}
308+
const result = await gitUtils.determineBaseBranchHeadCommitOid(__dirname);
309+
t.deepEqual(result, baseOid);
328310
},
329311
);
330312

src/overlay/caching.test.ts

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,65 +80,46 @@ const testDownloadOverlayBaseDatabaseFromCache = makeMacro({
8080
await fs.promises.writeFile(baseDatabaseOidsFile, JSON.stringify({}));
8181
}
8282

83-
const stubs: sinon.SinonStub[] = [];
83+
sinon.stub(apiClient, "getAutomationID").resolves("test-automation-id/");
8484

85-
const getAutomationIDStub = sinon
86-
.stub(apiClient, "getAutomationID")
87-
.resolves("test-automation-id/");
88-
stubs.push(getAutomationIDStub);
89-
90-
const isInTestModeStub = sinon
91-
.stub(utils, "isInTestMode")
92-
.returns(testCase.isInTestMode);
93-
stubs.push(isInTestModeStub);
85+
sinon.stub(utils, "isInTestMode").returns(testCase.isInTestMode);
9486

9587
if (testCase.restoreCacheResult instanceof Error) {
96-
const restoreCacheStub = sinon
88+
sinon
9789
.stub(actionsCache, "restoreCache")
9890
.rejects(testCase.restoreCacheResult);
99-
stubs.push(restoreCacheStub);
10091
} else {
101-
const restoreCacheStub = sinon
92+
sinon
10293
.stub(actionsCache, "restoreCache")
10394
.resolves(testCase.restoreCacheResult);
104-
stubs.push(restoreCacheStub);
10595
}
10696

107-
const tryGetFolderBytesStub = sinon
97+
sinon
10898
.stub(utils, "tryGetFolderBytes")
10999
.resolves(testCase.tryGetFolderBytesSucceeds ? 1024 * 1024 : undefined);
110-
stubs.push(tryGetFolderBytesStub);
111100

112101
const codeql = mockCodeQLVersion(testCase.codeQLVersion);
113102

114103
if (testCase.resolveDatabaseOutput instanceof Error) {
115-
const resolveDatabaseStub = sinon
104+
sinon
116105
.stub(codeql, "resolveDatabase")
117106
.rejects(testCase.resolveDatabaseOutput);
118-
stubs.push(resolveDatabaseStub);
119107
} else {
120-
const resolveDatabaseStub = sinon
108+
sinon
121109
.stub(codeql, "resolveDatabase")
122110
.resolves(testCase.resolveDatabaseOutput);
123-
stubs.push(resolveDatabaseStub);
124111
}
125112

126-
try {
127-
const result = await downloadOverlayBaseDatabaseFromCache(
128-
codeql,
129-
config,
130-
logger,
131-
);
113+
const result = await downloadOverlayBaseDatabaseFromCache(
114+
codeql,
115+
config,
116+
logger,
117+
);
132118

133-
if (expectDownloadSuccess) {
134-
t.truthy(result);
135-
} else {
136-
t.is(result, undefined);
137-
}
138-
} finally {
139-
for (const stub of stubs) {
140-
stub.restore();
141-
}
119+
if (expectDownloadSuccess) {
120+
t.truthy(result);
121+
} else {
122+
t.is(result, undefined);
142123
}
143124
});
144125
},

src/overlay/index.test.ts

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,21 @@ test.serial(
5050
"modified.js": "ddd444", // Changed OID
5151
"added.js": "eee555", // New file
5252
};
53-
const getFileOidsStubForOverlay = sinon
54-
.stub(gitUtils, "getFileOidsUnderPath")
55-
.resolves(currentOids);
53+
sinon.stub(gitUtils, "getFileOidsUnderPath").resolves(currentOids);
5654

5755
// Write the overlay changes file, which uses the mocked overlay OIDs
5856
// and the base database OIDs file
5957
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
60-
const getTempDirStub = sinon
61-
.stub(actionsUtil, "getTemporaryDirectory")
62-
.returns(tempDir);
63-
const getDiffRangesStub = sinon
58+
sinon.stub(actionsUtil, "getTemporaryDirectory").returns(tempDir);
59+
sinon
6460
.stub(actionsUtil, "getDiffRangesJsonFilePath")
6561
.returns(diffRangeFilePath);
66-
const getGitRootStub = sinon
67-
.stub(gitUtils, "getGitRoot")
68-
.resolves(sourceRoot);
62+
sinon.stub(gitUtils, "getGitRoot").resolves(sourceRoot);
6963
const changesFilePath = await writeOverlayChangesFile(
7064
config,
7165
sourceRoot,
7266
logger,
7367
);
74-
getFileOidsStubForOverlay.restore();
75-
getTempDirStub.restore();
76-
getDiffRangesStub.restore();
77-
getGitRootStub.restore();
7868

7969
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");
8070
const parsedContent = JSON.parse(fileContent) as { changes: string[] };
@@ -128,20 +118,14 @@ test.serial(
128118
"modified.js": "ddd444", // Changed OID
129119
"reverted.js": "eee555", // Same OID as base -- not detected by OID comparison
130120
};
131-
const getFileOidsStubForOverlay = sinon
132-
.stub(gitUtils, "getFileOidsUnderPath")
133-
.resolves(currentOids);
121+
sinon.stub(gitUtils, "getFileOidsUnderPath").resolves(currentOids);
134122

135123
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
136-
const getTempDirStub = sinon
137-
.stub(actionsUtil, "getTemporaryDirectory")
138-
.returns(tempDir);
139-
const getDiffRangesStub = sinon
124+
sinon.stub(actionsUtil, "getTemporaryDirectory").returns(tempDir);
125+
sinon
140126
.stub(actionsUtil, "getDiffRangesJsonFilePath")
141127
.returns(diffRangeFilePath);
142-
const getGitRootStub = sinon
143-
.stub(gitUtils, "getGitRoot")
144-
.resolves(sourceRoot);
128+
sinon.stub(gitUtils, "getGitRoot").resolves(sourceRoot);
145129

146130
// Write a pr-diff-range.json file with diff ranges including
147131
// "reverted.js" (unchanged OIDs) and "modified.js" (already in OID changes)
@@ -159,10 +143,6 @@ test.serial(
159143
sourceRoot,
160144
logger,
161145
);
162-
getFileOidsStubForOverlay.restore();
163-
getTempDirStub.restore();
164-
getDiffRangesStub.restore();
165-
getGitRootStub.restore();
166146

167147
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");
168148
const parsedContent = JSON.parse(fileContent) as { changes: string[] };
@@ -208,31 +188,21 @@ test.serial(
208188
"unchanged.js": "aaa111",
209189
"modified.js": "ddd444",
210190
};
211-
const getFileOidsStubForOverlay = sinon
212-
.stub(gitUtils, "getFileOidsUnderPath")
213-
.resolves(currentOids);
191+
sinon.stub(gitUtils, "getFileOidsUnderPath").resolves(currentOids);
214192

215193
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
216-
const getTempDirStub = sinon
217-
.stub(actionsUtil, "getTemporaryDirectory")
218-
.returns(tempDir);
219-
const getDiffRangesStub = sinon
194+
sinon.stub(actionsUtil, "getTemporaryDirectory").returns(tempDir);
195+
sinon
220196
.stub(actionsUtil, "getDiffRangesJsonFilePath")
221197
.returns(diffRangeFilePath);
222-
const getGitRootStub = sinon
223-
.stub(gitUtils, "getGitRoot")
224-
.resolves(sourceRoot);
198+
sinon.stub(gitUtils, "getGitRoot").resolves(sourceRoot);
225199

226200
// No pr-diff-range.json file exists - should work the same as before
227201
const changesFilePath = await writeOverlayChangesFile(
228202
config,
229203
sourceRoot,
230204
logger,
231205
);
232-
getFileOidsStubForOverlay.restore();
233-
getTempDirStub.restore();
234-
getDiffRangesStub.restore();
235-
getGitRootStub.restore();
236206

237207
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");
238208
const parsedContent = JSON.parse(fileContent) as { changes: string[] };
@@ -281,21 +251,15 @@ test.serial(
281251
"app.js": "aaa111",
282252
"lib/util.js": "bbb222",
283253
};
284-
const getFileOidsStubForOverlay = sinon
285-
.stub(gitUtils, "getFileOidsUnderPath")
286-
.resolves(currentOids);
254+
sinon.stub(gitUtils, "getFileOidsUnderPath").resolves(currentOids);
287255

288256
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
289-
const getTempDirStub = sinon
290-
.stub(actionsUtil, "getTemporaryDirectory")
291-
.returns(tempDir);
292-
const getDiffRangesStub = sinon
257+
sinon.stub(actionsUtil, "getTemporaryDirectory").returns(tempDir);
258+
sinon
293259
.stub(actionsUtil, "getDiffRangesJsonFilePath")
294260
.returns(diffRangeFilePath);
295261
// getGitRoot returns the repo root (parent of sourceRoot)
296-
const getGitRootStub = sinon
297-
.stub(gitUtils, "getGitRoot")
298-
.resolves(repoRoot);
262+
sinon.stub(gitUtils, "getGitRoot").resolves(repoRoot);
299263

300264
// Diff ranges use repo-root-relative paths (as returned by the GitHub compare API)
301265
await fs.promises.writeFile(
@@ -312,10 +276,6 @@ test.serial(
312276
sourceRoot,
313277
logger,
314278
);
315-
getFileOidsStubForOverlay.restore();
316-
getTempDirStub.restore();
317-
getDiffRangesStub.restore();
318-
getGitRootStub.restore();
319279

320280
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");
321281
const parsedContent = JSON.parse(fileContent) as { changes: string[] };

src/util.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,7 @@ for (const [
418418
`checkActionVersion ${reportErrorDescription} for ${versionsDescription}`,
419419
async (t) => {
420420
const warningSpy = sinon.spy(core, "warning");
421-
const versionStub = sinon
422-
.stub(api, "getGitHubVersion")
423-
.resolves(githubVersion);
421+
sinon.stub(api, "getGitHubVersion").resolves(githubVersion);
424422

425423
// call checkActionVersion twice and assert below that warning is reported only once
426424
util.checkActionVersion(version, await api.getGitHubVersion());
@@ -437,7 +435,6 @@ for (const [
437435
} else {
438436
t.false(warningSpy.called);
439437
}
440-
versionStub.restore();
441438
},
442439
);
443440
}

0 commit comments

Comments
 (0)