Remove fs mocking from cli-integration tests

This commit is contained in:
Koen Vlaswinkel
2022-12-02 14:21:31 +01:00
committed by Koen Vlaswinkel
parent 1f9e28e09d
commit d79b105751
2 changed files with 70 additions and 125 deletions

View File

@@ -63,10 +63,6 @@ import { DbManager } from "../../../../src/databases/db-manager";
jest.setTimeout(3 * 60 * 1000); jest.setTimeout(3 * 60 * 1000);
describe("Variant Analysis Manager", () => { describe("Variant Analysis Manager", () => {
let pathExistsStub: jest.SpiedFunction<typeof fs.pathExists>;
let readJsonStub: jest.SpiedFunction<typeof fs.readJson>;
let outputJsonStub: jest.SpiedFunction<typeof fs.outputJson>;
let writeFileStub: jest.SpiedFunction<typeof fs.writeFile>;
let cli: CodeQLCliServer; let cli: CodeQLCliServer;
let cancellationTokenSource: CancellationTokenSource; let cancellationTokenSource: CancellationTokenSource;
let variantAnalysisManager: VariantAnalysisManager; let variantAnalysisManager: VariantAnalysisManager;
@@ -76,16 +72,10 @@ describe("Variant Analysis Manager", () => {
let scannedRepos: VariantAnalysisScannedRepository[]; let scannedRepos: VariantAnalysisScannedRepository[];
beforeEach(async () => { beforeEach(async () => {
pathExistsStub = jest.spyOn(fs, "pathExists");
readJsonStub = jest.spyOn(fs, "readJson");
outputJsonStub = jest.spyOn(fs, "outputJson").mockReturnValue(undefined);
writeFileStub = jest.spyOn(fs, "writeFile").mockReturnValue(undefined);
jest.spyOn(extLogger, "log").mockResolvedValue(undefined); jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
jest jest
.spyOn(config, "isVariantAnalysisLiveResultsEnabled") .spyOn(config, "isVariantAnalysisLiveResultsEnabled")
.mockReturnValue(false); .mockReturnValue(false);
jest.spyOn(fs, "mkdirSync").mockReturnValue(undefined);
cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource = new CancellationTokenSource();
@@ -137,8 +127,6 @@ describe("Variant Analysis Manager", () => {
} }
beforeEach(async () => { beforeEach(async () => {
writeFileStub.mockRestore();
const mockCredentials = { const mockCredentials = {
getOctokit: () => getOctokit: () =>
Promise.resolve({ Promise.resolve({
@@ -278,10 +266,6 @@ describe("Variant Analysis Manager", () => {
const variantAnalysis = createMockVariantAnalysis({}); const variantAnalysis = createMockVariantAnalysis({});
describe("when the directory does not exist", () => { describe("when the directory does not exist", () => {
beforeEach(() => {
pathExistsStub.mockImplementation(() => false);
});
it("should fire the removed event if the file does not exist", async () => { it("should fire the removed event if the file does not exist", async () => {
const stub = jest.fn(); const stub = jest.fn();
variantAnalysisManager.onVariantAnalysisRemoved(stub); variantAnalysisManager.onVariantAnalysisRemoved(stub);
@@ -289,16 +273,12 @@ describe("Variant Analysis Manager", () => {
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis); await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
expect(stub).toBeCalledTimes(1); expect(stub).toBeCalledTimes(1);
expect(pathExistsStub).toHaveBeenCalledTimes(1);
expect(pathExistsStub).toBeCalledWith(
join(storagePath, variantAnalysis.id.toString()),
);
}); });
}); });
describe("when the directory exists", () => { describe("when the directory exists", () => {
beforeEach(() => { beforeEach(async () => {
pathExistsStub.mockImplementation(() => true); await fs.ensureDir(join(storagePath, variantAnalysis.id.toString()));
}); });
it("should store the variant analysis", async () => { it("should store the variant analysis", async () => {
@@ -307,31 +287,20 @@ describe("Variant Analysis Manager", () => {
expect( expect(
await variantAnalysisManager.getVariantAnalysis(variantAnalysis.id), await variantAnalysisManager.getVariantAnalysis(variantAnalysis.id),
).toEqual(variantAnalysis); ).toEqual(variantAnalysis);
expect(pathExistsStub).toBeCalledWith(
join(storagePath, variantAnalysis.id.toString()),
);
}); });
it("should not error if the repo states file does not exist", async () => { it("should not error if the repo states file does not exist", async () => {
readJsonStub.mockImplementation(() =>
Promise.reject(new Error("File does not exist")),
);
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis); await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
expect(readJsonStub).toHaveBeenCalledTimes(1);
expect(readJsonStub).toHaveBeenCalledWith(
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
);
expect( expect(
await variantAnalysisManager.getRepoStates(variantAnalysis.id), await variantAnalysisManager.getRepoStates(variantAnalysis.id),
).toEqual([]); ).toEqual([]);
}); });
it("should read in the repo states if it exists", async () => { it("should read in the repo states if it exists", async () => {
readJsonStub.mockImplementation(() => await fs.writeJson(
Promise.resolve({ join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
{
[scannedRepos[0].repository.id]: { [scannedRepos[0].repository.id]: {
repositoryId: scannedRepos[0].repository.id, repositoryId: scannedRepos[0].repository.id,
downloadStatus: downloadStatus:
@@ -342,15 +311,11 @@ describe("Variant Analysis Manager", () => {
downloadStatus: downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.InProgress, VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
}, },
}), },
); );
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis); await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
expect(readJsonStub).toHaveBeenCalledTimes(1);
expect(readJsonStub).toHaveBeenCalledWith(
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
);
expect( expect(
await variantAnalysisManager.getRepoStates(variantAnalysis.id), await variantAnalysisManager.getRepoStates(variantAnalysis.id),
).toEqual( ).toEqual(
@@ -381,6 +346,8 @@ describe("Variant Analysis Manager", () => {
typeof ghApiClient.getVariantAnalysisRepoResult typeof ghApiClient.getVariantAnalysisRepoResult
>; >;
let repoStatesPath: string;
beforeEach(async () => { beforeEach(async () => {
const mockCredentials = { const mockCredentials = {
getOctokit: () => getOctokit: () =>
@@ -404,6 +371,12 @@ describe("Variant Analysis Manager", () => {
ghApiClient, ghApiClient,
"getVariantAnalysisRepoResult", "getVariantAnalysisRepoResult",
); );
repoStatesPath = join(
storagePath,
variantAnalysis.id.toString(),
"repo_states.json",
);
}); });
describe("when the artifact_url is missing", () => { describe("when the artifact_url is missing", () => {
@@ -494,16 +467,13 @@ describe("Variant Analysis Manager", () => {
cancellationTokenSource.token, cancellationTokenSource.token,
); );
expect(outputJsonStub).toHaveBeenCalledWith( await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"), [scannedRepos[0].repository.id]: {
{ repositoryId: scannedRepos[0].repository.id,
[scannedRepos[0].repository.id]: { downloadStatus:
repositoryId: scannedRepos[0].repository.id, VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
}, },
); });
}); });
it("should not write the repo state when the download fails", async () => { it("should not write the repo state when the download fails", async () => {
@@ -519,7 +489,7 @@ describe("Variant Analysis Manager", () => {
), ),
).rejects.toThrow(); ).rejects.toThrow();
expect(outputJsonStub).not.toHaveBeenCalled(); await expect(fs.pathExists(repoStatesPath)).resolves.toBe(false);
}); });
it("should have a failed repo state when the repo task API fails", async () => { it("should have a failed repo state when the repo task API fails", async () => {
@@ -535,7 +505,7 @@ describe("Variant Analysis Manager", () => {
), ),
).rejects.toThrow(); ).rejects.toThrow();
expect(outputJsonStub).not.toHaveBeenCalled(); await expect(fs.pathExists(repoStatesPath)).resolves.toBe(false);
await variantAnalysisManager.autoDownloadVariantAnalysisResult( await variantAnalysisManager.autoDownloadVariantAnalysisResult(
scannedRepos[1], scannedRepos[1],
@@ -543,21 +513,18 @@ describe("Variant Analysis Manager", () => {
cancellationTokenSource.token, cancellationTokenSource.token,
); );
expect(outputJsonStub).toHaveBeenCalledWith( await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"), [scannedRepos[0].repository.id]: {
{ repositoryId: scannedRepos[0].repository.id,
[scannedRepos[0].repository.id]: { downloadStatus:
repositoryId: scannedRepos[0].repository.id, VariantAnalysisScannedRepositoryDownloadStatus.Failed,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Failed,
},
[scannedRepos[1].repository.id]: {
repositoryId: scannedRepos[1].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
}, },
); [scannedRepos[1].repository.id]: {
repositoryId: scannedRepos[1].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
});
}); });
it("should have a failed repo state when the download fails", async () => { it("should have a failed repo state when the download fails", async () => {
@@ -573,7 +540,7 @@ describe("Variant Analysis Manager", () => {
), ),
).rejects.toThrow(); ).rejects.toThrow();
expect(outputJsonStub).not.toHaveBeenCalled(); await expect(fs.pathExists(repoStatesPath)).resolves.toBe(false);
await variantAnalysisManager.autoDownloadVariantAnalysisResult( await variantAnalysisManager.autoDownloadVariantAnalysisResult(
scannedRepos[1], scannedRepos[1],
@@ -581,25 +548,22 @@ describe("Variant Analysis Manager", () => {
cancellationTokenSource.token, cancellationTokenSource.token,
); );
expect(outputJsonStub).toHaveBeenCalledWith( await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"), [scannedRepos[0].repository.id]: {
{ repositoryId: scannedRepos[0].repository.id,
[scannedRepos[0].repository.id]: { downloadStatus:
repositoryId: scannedRepos[0].repository.id, VariantAnalysisScannedRepositoryDownloadStatus.Failed,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Failed,
},
[scannedRepos[1].repository.id]: {
repositoryId: scannedRepos[1].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
}, },
); [scannedRepos[1].repository.id]: {
repositoryId: scannedRepos[1].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
});
}); });
it("should update the repo state correctly", async () => { it("should update the repo state correctly", async () => {
mockRepoStates({ await mockRepoStates({
[scannedRepos[1].repository.id]: { [scannedRepos[1].repository.id]: {
repositoryId: scannedRepos[1].repository.id, repositoryId: scannedRepos[1].repository.id,
downloadStatus: downloadStatus:
@@ -614,50 +578,35 @@ describe("Variant Analysis Manager", () => {
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis); await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
expect(pathExistsStub).toBeCalledWith(
join(storagePath, variantAnalysis.id.toString()),
);
expect(readJsonStub).toHaveBeenCalledTimes(1);
expect(readJsonStub).toHaveBeenCalledWith(
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"),
);
pathExistsStub.mockRestore();
await variantAnalysisManager.autoDownloadVariantAnalysisResult( await variantAnalysisManager.autoDownloadVariantAnalysisResult(
scannedRepos[0], scannedRepos[0],
variantAnalysis, variantAnalysis,
cancellationTokenSource.token, cancellationTokenSource.token,
); );
expect(outputJsonStub).toHaveBeenCalledWith( await expect(fs.readJson(repoStatesPath)).resolves.toEqual({
join(storagePath, variantAnalysis.id.toString(), "repo_states.json"), [scannedRepos[1].repository.id]: {
{ repositoryId: scannedRepos[1].repository.id,
[scannedRepos[1].repository.id]: { downloadStatus:
repositoryId: scannedRepos[1].repository.id, VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
[scannedRepos[2].repository.id]: {
repositoryId: scannedRepos[2].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
},
[scannedRepos[0].repository.id]: {
repositoryId: scannedRepos[0].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
}, },
); [scannedRepos[2].repository.id]: {
repositoryId: scannedRepos[2].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
},
[scannedRepos[0].repository.id]: {
repositoryId: scannedRepos[0].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
},
});
}); });
function mockRepoStates( async function mockRepoStates(
repoStates: Record<number, VariantAnalysisScannedRepositoryState>, repoStates: Record<number, VariantAnalysisScannedRepositoryState>,
) { ) {
pathExistsStub.mockImplementation(() => true); await fs.outputJson(repoStatesPath, repoStates);
// This will read in the correct repo states
readJsonStub.mockImplementation(() => Promise.resolve(repoStates));
} }
}); });
}); });
@@ -703,7 +652,6 @@ describe("Variant Analysis Manager", () => {
let removeAnalysisResultsStub: jest.SpiedFunction< let removeAnalysisResultsStub: jest.SpiedFunction<
typeof variantAnalysisResultsManager.removeAnalysisResults typeof variantAnalysisResultsManager.removeAnalysisResults
>; >;
let removeStorageStub: jest.SpiedFunction<typeof fs.remove>;
let dummyVariantAnalysis: VariantAnalysis; let dummyVariantAnalysis: VariantAnalysis;
beforeEach(async () => { beforeEach(async () => {
@@ -712,25 +660,24 @@ describe("Variant Analysis Manager", () => {
removeAnalysisResultsStub = jest removeAnalysisResultsStub = jest
.spyOn(variantAnalysisResultsManager, "removeAnalysisResults") .spyOn(variantAnalysisResultsManager, "removeAnalysisResults")
.mockReturnValue(undefined); .mockReturnValue(undefined);
removeStorageStub = jest.spyOn(fs, "remove").mockReturnValue(undefined);
}); });
it("should remove variant analysis", async () => { it("should remove variant analysis", async () => {
pathExistsStub.mockImplementation(() => true); await fs.ensureDir(join(storagePath, dummyVariantAnalysis.id.toString()));
await variantAnalysisManager.rehydrateVariantAnalysis( await variantAnalysisManager.rehydrateVariantAnalysis(
dummyVariantAnalysis, dummyVariantAnalysis,
); );
expect(pathExistsStub).toBeCalledWith(
join(storagePath, dummyVariantAnalysis.id.toString()),
);
expect(variantAnalysisManager.variantAnalysesSize).toBe(1); expect(variantAnalysisManager.variantAnalysesSize).toBe(1);
await variantAnalysisManager.removeVariantAnalysis(dummyVariantAnalysis); await variantAnalysisManager.removeVariantAnalysis(dummyVariantAnalysis);
expect(removeAnalysisResultsStub).toBeCalledTimes(1); expect(removeAnalysisResultsStub).toBeCalledTimes(1);
expect(removeStorageStub).toBeCalledTimes(1);
expect(variantAnalysisManager.variantAnalysesSize).toBe(0); expect(variantAnalysisManager.variantAnalysesSize).toBe(0);
await expect(
fs.pathExists(join(storagePath, dummyVariantAnalysis.id.toString())),
).resolves.toBe(false);
}); });
}); });

View File

@@ -47,8 +47,6 @@ describe(VariantAnalysisResultsManager.name, () => {
beforeEach(async () => { beforeEach(async () => {
jest.spyOn(extLogger, "log").mockResolvedValue(undefined); jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
jest.spyOn(fs, "mkdirSync").mockReturnValue(undefined);
jest.spyOn(fs, "writeFile").mockReturnValue(undefined);
variantAnalysisResultsManager = new VariantAnalysisResultsManager( variantAnalysisResultsManager = new VariantAnalysisResultsManager(
cli, cli,