Skip to content

Commit d79b105

Browse files
committed
Remove fs mocking from cli-integration tests
1 parent 1f9e28e commit d79b105

2 files changed

Lines changed: 70 additions & 125 deletions

File tree

extensions/ql-vscode/test/vscode-tests/cli-integration/remote-queries/variant-analysis-manager.test.ts

Lines changed: 70 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ import { DbManager } from "../../../../src/databases/db-manager";
6363
jest.setTimeout(3 * 60 * 1000);
6464

6565
describe("Variant Analysis Manager", () => {
66-
let pathExistsStub: jest.SpiedFunction<typeof fs.pathExists>;
67-
let readJsonStub: jest.SpiedFunction<typeof fs.readJson>;
68-
let outputJsonStub: jest.SpiedFunction<typeof fs.outputJson>;
69-
let writeFileStub: jest.SpiedFunction<typeof fs.writeFile>;
7066
let cli: CodeQLCliServer;
7167
let cancellationTokenSource: CancellationTokenSource;
7268
let variantAnalysisManager: VariantAnalysisManager;
@@ -76,16 +72,10 @@ describe("Variant Analysis Manager", () => {
7672
let scannedRepos: VariantAnalysisScannedRepository[];
7773

7874
beforeEach(async () => {
79-
pathExistsStub = jest.spyOn(fs, "pathExists");
80-
readJsonStub = jest.spyOn(fs, "readJson");
81-
outputJsonStub = jest.spyOn(fs, "outputJson").mockReturnValue(undefined);
82-
writeFileStub = jest.spyOn(fs, "writeFile").mockReturnValue(undefined);
83-
8475
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
8576
jest
8677
.spyOn(config, "isVariantAnalysisLiveResultsEnabled")
8778
.mockReturnValue(false);
88-
jest.spyOn(fs, "mkdirSync").mockReturnValue(undefined);
8979

9080
cancellationTokenSource = new CancellationTokenSource();
9181

@@ -137,8 +127,6 @@ describe("Variant Analysis Manager", () => {
137127
}
138128

139129
beforeEach(async () => {
140-
writeFileStub.mockRestore();
141-
142130
const mockCredentials = {
143131
getOctokit: () =>
144132
Promise.resolve({
@@ -278,27 +266,19 @@ describe("Variant Analysis Manager", () => {
278266
const variantAnalysis = createMockVariantAnalysis({});
279267

280268
describe("when the directory does not exist", () => {
281-
beforeEach(() => {
282-
pathExistsStub.mockImplementation(() => false);
283-
});
284-
285269
it("should fire the removed event if the file does not exist", async () => {
286270
const stub = jest.fn();
287271
variantAnalysisManager.onVariantAnalysisRemoved(stub);
288272

289273
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
290274

291275
expect(stub).toBeCalledTimes(1);
292-
expect(pathExistsStub).toHaveBeenCalledTimes(1);
293-
expect(pathExistsStub).toBeCalledWith(
294-
join(storagePath, variantAnalysis.id.toString()),
295-
);
296276
});
297277
});
298278

299279
describe("when the directory exists", () => {
300-
beforeEach(() => {
301-
pathExistsStub.mockImplementation(() => true);
280+
beforeEach(async () => {
281+
await fs.ensureDir(join(storagePath, variantAnalysis.id.toString()));
302282
});
303283

304284
it("should store the variant analysis", async () => {
@@ -307,31 +287,20 @@ describe("Variant Analysis Manager", () => {
307287
expect(
308288
await variantAnalysisManager.getVariantAnalysis(variantAnalysis.id),
309289
).toEqual(variantAnalysis);
310-
311-
expect(pathExistsStub).toBeCalledWith(
312-
join(storagePath, variantAnalysis.id.toString()),
313-
);
314290
});
315291

316292
it("should not error if the repo states file does not exist", async () => {
317-
readJsonStub.mockImplementation(() =>
318-
Promise.reject(new Error("File does not exist")),
319-
);
320-
321293
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
322294

323-
expect(readJsonStub).toHaveBeenCalledTimes(1);
324-
expect(readJsonStub).toHaveBeenCalledWith(
325-
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
326-
);
327295
expect(
328296
await variantAnalysisManager.getRepoStates(variantAnalysis.id),
329297
).toEqual([]);
330298
});
331299

332300
it("should read in the repo states if it exists", async () => {
333-
readJsonStub.mockImplementation(() =>
334-
Promise.resolve({
301+
await fs.writeJson(
302+
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
303+
{
335304
[scannedRepos[0].repository.id]: {
336305
repositoryId: scannedRepos[0].repository.id,
337306
downloadStatus:
@@ -342,15 +311,11 @@ describe("Variant Analysis Manager", () => {
342311
downloadStatus:
343312
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
344313
},
345-
}),
314+
},
346315
);
347316

348317
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
349318

350-
expect(readJsonStub).toHaveBeenCalledTimes(1);
351-
expect(readJsonStub).toHaveBeenCalledWith(
352-
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
353-
);
354319
expect(
355320
await variantAnalysisManager.getRepoStates(variantAnalysis.id),
356321
).toEqual(
@@ -381,6 +346,8 @@ describe("Variant Analysis Manager", () => {
381346
typeof ghApiClient.getVariantAnalysisRepoResult
382347
>;
383348

349+
let repoStatesPath: string;
350+
384351
beforeEach(async () => {
385352
const mockCredentials = {
386353
getOctokit: () =>
@@ -404,6 +371,12 @@ describe("Variant Analysis Manager", () => {
404371
ghApiClient,
405372
"getVariantAnalysisRepoResult",
406373
);
374+
375+
repoStatesPath = join(
376+
storagePath,
377+
variantAnalysis.id.toString(),
378+
"repo_states.json",
379+
);
407380
});
408381

409382
describe("when the artifact_url is missing", () => {
@@ -494,16 +467,13 @@ describe("Variant Analysis Manager", () => {
494467
cancellationTokenSource.token,
495468
);
496469

497-
expect(outputJsonStub).toHaveBeenCalledWith(
498-
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
499-
{
500-
[scannedRepos[0].repository.id]: {
501-
repositoryId: scannedRepos[0].repository.id,
502-
downloadStatus:
503-
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
504-
},
470+
await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
471+
[scannedRepos[0].repository.id]: {
472+
repositoryId: scannedRepos[0].repository.id,
473+
downloadStatus:
474+
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
505475
},
506-
);
476+
});
507477
});
508478

509479
it("should not write the repo state when the download fails", async () => {
@@ -519,7 +489,7 @@ describe("Variant Analysis Manager", () => {
519489
),
520490
).rejects.toThrow();
521491

522-
expect(outputJsonStub).not.toHaveBeenCalled();
492+
await expect(fs.pathExists(repoStatesPath)).resolves.toBe(false);
523493
});
524494

525495
it("should have a failed repo state when the repo task API fails", async () => {
@@ -535,29 +505,26 @@ describe("Variant Analysis Manager", () => {
535505
),
536506
).rejects.toThrow();
537507

538-
expect(outputJsonStub).not.toHaveBeenCalled();
508+
await expect(fs.pathExists(repoStatesPath)).resolves.toBe(false);
539509

540510
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
541511
scannedRepos[1],
542512
variantAnalysis,
543513
cancellationTokenSource.token,
544514
);
545515

546-
expect(outputJsonStub).toHaveBeenCalledWith(
547-
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
548-
{
549-
[scannedRepos[0].repository.id]: {
550-
repositoryId: scannedRepos[0].repository.id,
551-
downloadStatus:
552-
VariantAnalysisScannedRepositoryDownloadStatus.Failed,
553-
},
554-
[scannedRepos[1].repository.id]: {
555-
repositoryId: scannedRepos[1].repository.id,
556-
downloadStatus:
557-
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
558-
},
516+
await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
517+
[scannedRepos[0].repository.id]: {
518+
repositoryId: scannedRepos[0].repository.id,
519+
downloadStatus:
520+
VariantAnalysisScannedRepositoryDownloadStatus.Failed,
559521
},
560-
);
522+
[scannedRepos[1].repository.id]: {
523+
repositoryId: scannedRepos[1].repository.id,
524+
downloadStatus:
525+
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
526+
},
527+
});
561528
});
562529

563530
it("should have a failed repo state when the download fails", async () => {
@@ -573,33 +540,30 @@ describe("Variant Analysis Manager", () => {
573540
),
574541
).rejects.toThrow();
575542

576-
expect(outputJsonStub).not.toHaveBeenCalled();
543+
await expect(fs.pathExists(repoStatesPath)).resolves.toBe(false);
577544

578545
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
579546
scannedRepos[1],
580547
variantAnalysis,
581548
cancellationTokenSource.token,
582549
);
583550

584-
expect(outputJsonStub).toHaveBeenCalledWith(
585-
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
586-
{
587-
[scannedRepos[0].repository.id]: {
588-
repositoryId: scannedRepos[0].repository.id,
589-
downloadStatus:
590-
VariantAnalysisScannedRepositoryDownloadStatus.Failed,
591-
},
592-
[scannedRepos[1].repository.id]: {
593-
repositoryId: scannedRepos[1].repository.id,
594-
downloadStatus:
595-
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
596-
},
551+
await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
552+
[scannedRepos[0].repository.id]: {
553+
repositoryId: scannedRepos[0].repository.id,
554+
downloadStatus:
555+
VariantAnalysisScannedRepositoryDownloadStatus.Failed,
597556
},
598-
);
557+
[scannedRepos[1].repository.id]: {
558+
repositoryId: scannedRepos[1].repository.id,
559+
downloadStatus:
560+
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
561+
},
562+
});
599563
});
600564

601565
it("should update the repo state correctly", async () => {
602-
mockRepoStates({
566+
await mockRepoStates({
603567
[scannedRepos[1].repository.id]: {
604568
repositoryId: scannedRepos[1].repository.id,
605569
downloadStatus:
@@ -614,50 +578,35 @@ describe("Variant Analysis Manager", () => {
614578

615579
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
616580

617-
expect(pathExistsStub).toBeCalledWith(
618-
join(storagePath, variantAnalysis.id.toString()),
619-
);
620-
expect(readJsonStub).toHaveBeenCalledTimes(1);
621-
expect(readJsonStub).toHaveBeenCalledWith(
622-
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
623-
);
624-
625-
pathExistsStub.mockRestore();
626-
627581
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
628582
scannedRepos[0],
629583
variantAnalysis,
630584
cancellationTokenSource.token,
631585
);
632586

633-
expect(outputJsonStub).toHaveBeenCalledWith(
634-
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
635-
{
636-
[scannedRepos[1].repository.id]: {
637-
repositoryId: scannedRepos[1].repository.id,
638-
downloadStatus:
639-
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
640-
},
641-
[scannedRepos[2].repository.id]: {
642-
repositoryId: scannedRepos[2].repository.id,
643-
downloadStatus:
644-
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
645-
},
646-
[scannedRepos[0].repository.id]: {
647-
repositoryId: scannedRepos[0].repository.id,
648-
downloadStatus:
649-
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
650-
},
587+
await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
588+
[scannedRepos[1].repository.id]: {
589+
repositoryId: scannedRepos[1].repository.id,
590+
downloadStatus:
591+
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
651592
},
652-
);
593+
[scannedRepos[2].repository.id]: {
594+
repositoryId: scannedRepos[2].repository.id,
595+
downloadStatus:
596+
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
597+
},
598+
[scannedRepos[0].repository.id]: {
599+
repositoryId: scannedRepos[0].repository.id,
600+
downloadStatus:
601+
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
602+
},
603+
});
653604
});
654605

655-
function mockRepoStates(
606+
async function mockRepoStates(
656607
repoStates: Record<number, VariantAnalysisScannedRepositoryState>,
657608
) {
658-
pathExistsStub.mockImplementation(() => true);
659-
// This will read in the correct repo states
660-
readJsonStub.mockImplementation(() => Promise.resolve(repoStates));
609+
await fs.outputJson(repoStatesPath, repoStates);
661610
}
662611
});
663612
});
@@ -703,7 +652,6 @@ describe("Variant Analysis Manager", () => {
703652
let removeAnalysisResultsStub: jest.SpiedFunction<
704653
typeof variantAnalysisResultsManager.removeAnalysisResults
705654
>;
706-
let removeStorageStub: jest.SpiedFunction<typeof fs.remove>;
707655
let dummyVariantAnalysis: VariantAnalysis;
708656

709657
beforeEach(async () => {
@@ -712,25 +660,24 @@ describe("Variant Analysis Manager", () => {
712660
removeAnalysisResultsStub = jest
713661
.spyOn(variantAnalysisResultsManager, "removeAnalysisResults")
714662
.mockReturnValue(undefined);
715-
716-
removeStorageStub = jest.spyOn(fs, "remove").mockReturnValue(undefined);
717663
});
718664

719665
it("should remove variant analysis", async () => {
720-
pathExistsStub.mockImplementation(() => true);
666+
await fs.ensureDir(join(storagePath, dummyVariantAnalysis.id.toString()));
667+
721668
await variantAnalysisManager.rehydrateVariantAnalysis(
722669
dummyVariantAnalysis,
723670
);
724-
expect(pathExistsStub).toBeCalledWith(
725-
join(storagePath, dummyVariantAnalysis.id.toString()),
726-
);
727671
expect(variantAnalysisManager.variantAnalysesSize).toBe(1);
728672

729673
await variantAnalysisManager.removeVariantAnalysis(dummyVariantAnalysis);
730674

731675
expect(removeAnalysisResultsStub).toBeCalledTimes(1);
732-
expect(removeStorageStub).toBeCalledTimes(1);
733676
expect(variantAnalysisManager.variantAnalysesSize).toBe(0);
677+
678+
await expect(
679+
fs.pathExists(join(storagePath, dummyVariantAnalysis.id.toString())),
680+
).resolves.toBe(false);
734681
});
735682
});
736683

extensions/ql-vscode/test/vscode-tests/cli-integration/remote-queries/variant-analysis-results-manager.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ describe(VariantAnalysisResultsManager.name, () => {
4747

4848
beforeEach(async () => {
4949
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
50-
jest.spyOn(fs, "mkdirSync").mockReturnValue(undefined);
51-
jest.spyOn(fs, "writeFile").mockReturnValue(undefined);
5250

5351
variantAnalysisResultsManager = new VariantAnalysisResultsManager(
5452
cli,

0 commit comments

Comments
 (0)