Skip to content

Commit 1f9e28e

Browse files
committed
Remove fs mocking from minimal-workspace tests
1 parent 0d8c90a commit 1f9e28e

3 files changed

Lines changed: 91 additions & 85 deletions

File tree

extensions/ql-vscode/src/databases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ export class DatabaseManager extends DisposableObject {
924924
// Delete folder from file system only if it is controlled by the extension
925925
if (this.isExtensionControlledLocation(item.databaseUri)) {
926926
void extLogger.log("Deleting database from filesystem.");
927-
remove(item.databaseUri.fsPath).then(
927+
await remove(item.databaseUri.fsPath).then(
928928
() => void extLogger.log(`Deleted '${item.databaseUri.fsPath}'`),
929929
(e) =>
930930
void extLogger.log(

extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases.test.ts

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ describe("databases", () => {
200200

201201
it("should remove a database item", async () => {
202202
const mockDbItem = createMockDB();
203-
const removeMock = jest
204-
.spyOn(fs, "remove")
205-
.mockImplementation(() => Promise.resolve());
203+
await fs.ensureDir(mockDbItem.databaseUri.fsPath);
206204

207205
// pretend that this item is the first workspace folder in the list
208206
jest
@@ -229,13 +227,14 @@ describe("databases", () => {
229227
expect(workspace.updateWorkspaceFolders).toBeCalledWith(0, 1);
230228

231229
// should also delete the db contents
232-
expect(removeMock).toBeCalledWith(mockDbItem.databaseUri.fsPath);
230+
await expect(fs.pathExists(mockDbItem.databaseUri.fsPath)).resolves.toBe(
231+
false,
232+
);
233233
});
234234

235235
it("should remove a database item outside of the extension controlled area", async () => {
236236
const mockDbItem = createMockDB();
237-
const removeMock = jest.spyOn(fs, "remove");
238-
removeMock.mockReset().mockImplementation(() => Promise.resolve());
237+
await fs.ensureDir(mockDbItem.databaseUri.fsPath);
239238

240239
// pretend that this item is the first workspace folder in the list
241240
jest
@@ -263,16 +262,16 @@ describe("databases", () => {
263262
expect(workspace.updateWorkspaceFolders).toBeCalledWith(0, 1);
264263

265264
// should NOT delete the db contents
266-
expect(removeMock).not.toBeCalled();
265+
await expect(fs.pathExists(mockDbItem.databaseUri.fsPath)).resolves.toBe(
266+
true,
267+
);
267268
});
268269

269270
it("should register and deregister a database when adding and removing it", async () => {
270271
// similar test as above, but also check the call to sendRequestSpy to make sure they send the
271272
// registration messages.
272273
const mockDbItem = createMockDB();
273274

274-
jest.spyOn(fs, "remove").mockImplementation(() => Promise.resolve());
275-
276275
await (databaseManager as any).addDatabaseItem(
277276
{} as ProgressCallback,
278277
{} as CancellationToken,
@@ -411,98 +410,93 @@ describe("databases", () => {
411410
});
412411

413412
describe("isAffectedByTest", () => {
414-
const directoryStats = new fs.Stats();
415-
const fileStats = new fs.Stats();
416-
beforeEach(() => {
417-
jest.spyOn(directoryStats, "isDirectory").mockReturnValue(true);
418-
jest.spyOn(fileStats, "isDirectory").mockReturnValue(false);
413+
let directoryPath: string;
414+
let projectPath: string;
415+
let qlFilePath: string;
416+
417+
beforeEach(async () => {
418+
directoryPath = join(dir.name, "dir");
419+
await fs.ensureDir(directoryPath);
420+
projectPath = join(directoryPath, "dir.testproj");
421+
await fs.writeFile(projectPath, "");
422+
qlFilePath = join(directoryPath, "test.ql");
423+
await fs.writeFile(qlFilePath, "");
419424
});
420425

421426
it("should return true for testproj database in test directory", async () => {
422-
jest.spyOn(fs, "stat").mockResolvedValue(directoryStats);
423-
const db = createMockDB(
424-
sourceLocationUri(),
425-
Uri.file("/path/to/dir/dir.testproj"),
426-
);
427-
expect(await db.isAffectedByTest("/path/to/dir")).toBe(true);
427+
const db = createMockDB(sourceLocationUri(), Uri.file(projectPath));
428+
expect(await db.isAffectedByTest(directoryPath)).toBe(true);
428429
});
429430

430431
it("should return false for non-existent test directory", async () => {
431-
jest.spyOn(fs, "stat").mockImplementation(() => {
432-
throw new Error("Simulated Error: ENOENT");
433-
});
434432
const db = createMockDB(
435433
sourceLocationUri(),
436-
Uri.file("/path/to/dir/dir.testproj"),
434+
Uri.file(join(dir.name, "non-existent/non-existent.testproj")),
435+
);
436+
expect(await db.isAffectedByTest(join(dir.name, "non-existent"))).toBe(
437+
false,
437438
);
438-
expect(await db.isAffectedByTest("/path/to/dir")).toBe(false);
439439
});
440440

441441
it("should return false for non-testproj database in test directory", async () => {
442-
jest.spyOn(fs, "stat").mockResolvedValue(directoryStats);
442+
const anotherProjectPath = join(directoryPath, "dir.proj");
443+
await fs.writeFile(anotherProjectPath, "");
444+
443445
const db = createMockDB(
444446
sourceLocationUri(),
445-
Uri.file("/path/to/dir/dir.proj"),
447+
Uri.file(anotherProjectPath),
446448
);
447-
expect(await db.isAffectedByTest("/path/to/dir")).toBe(false);
449+
expect(await db.isAffectedByTest(directoryPath)).toBe(false);
448450
});
449451

450452
it("should return false for testproj database outside test directory", async () => {
451-
jest.spyOn(fs, "stat").mockResolvedValue(directoryStats);
453+
const anotherProjectDir = join(dir.name, "other");
454+
await fs.ensureDir(anotherProjectDir);
455+
const anotherProjectPath = join(anotherProjectDir, "other.testproj");
456+
await fs.writeFile(anotherProjectPath, "");
457+
452458
const db = createMockDB(
453459
sourceLocationUri(),
454-
Uri.file("/path/to/other/dir.testproj"),
460+
Uri.file(anotherProjectPath),
455461
);
456-
expect(await db.isAffectedByTest("/path/to/dir")).toBe(false);
462+
expect(await db.isAffectedByTest(directoryPath)).toBe(false);
457463
});
458464

459465
it("should return false for testproj database for prefix directory", async () => {
460-
jest.spyOn(fs, "stat").mockResolvedValue(directoryStats);
461-
const db = createMockDB(
462-
sourceLocationUri(),
463-
Uri.file("/path/to/dir/dir.testproj"),
464-
);
465-
// /path/to/d is a prefix of /path/to/dir/dir.testproj, but
466-
// /path/to/dir/dir.testproj is not under /path/to/d
467-
expect(await db.isAffectedByTest("/path/to/d")).toBe(false);
466+
const db = createMockDB(sourceLocationUri(), Uri.file(projectPath));
467+
// /d is a prefix of /dir/dir.testproj, but
468+
// /dir/dir.testproj is not under /d
469+
expect(await db.isAffectedByTest(join(directoryPath, "d"))).toBe(false);
468470
});
469471

470472
it("should return true for testproj database for test file", async () => {
471-
jest.spyOn(fs, "stat").mockResolvedValue(fileStats);
472-
const db = createMockDB(
473-
sourceLocationUri(),
474-
Uri.file("/path/to/dir/dir.testproj"),
475-
);
476-
expect(await db.isAffectedByTest("/path/to/dir/test.ql")).toBe(true);
473+
const db = createMockDB(sourceLocationUri(), Uri.file(projectPath));
474+
expect(await db.isAffectedByTest(qlFilePath)).toBe(true);
477475
});
478476

479477
it("should return false for non-existent test file", async () => {
480-
jest.spyOn(fs, "stat").mockImplementation(() => {
481-
throw new Error("Simulated Error: ENOENT");
482-
});
483-
const db = createMockDB(
484-
sourceLocationUri(),
485-
Uri.file("/path/to/dir/dir.testproj"),
486-
);
487-
expect(await db.isAffectedByTest("/path/to/dir/test.ql")).toBe(false);
478+
const otherTestFile = join(directoryPath, "other-test.ql");
479+
const db = createMockDB(sourceLocationUri(), Uri.file(projectPath));
480+
expect(await db.isAffectedByTest(otherTestFile)).toBe(false);
488481
});
489482

490483
it("should return false for non-testproj database for test file", async () => {
491-
jest.spyOn(fs, "stat").mockResolvedValue(fileStats);
484+
const anotherProjectPath = join(directoryPath, "dir.proj");
485+
await fs.writeFile(anotherProjectPath, "");
486+
492487
const db = createMockDB(
493488
sourceLocationUri(),
494-
Uri.file("/path/to/dir/dir.proj"),
489+
Uri.file(anotherProjectPath),
495490
);
496-
expect(await db.isAffectedByTest("/path/to/dir/test.ql")).toBe(false);
491+
expect(await db.isAffectedByTest(qlFilePath)).toBe(false);
497492
});
498493

499494
it("should return false for testproj database not matching test file", async () => {
500-
jest.spyOn(fs, "stat").mockResolvedValue(fileStats);
501-
const db = createMockDB(
502-
sourceLocationUri(),
503-
Uri.file("/path/to/dir/dir.testproj"),
504-
);
505-
expect(await db.isAffectedByTest("/path/to/test.ql")).toBe(false);
495+
const otherTestFile = join(dir.name, "test.ql");
496+
await fs.writeFile(otherTestFile, "");
497+
498+
const db = createMockDB(sourceLocationUri(), Uri.file(projectPath));
499+
expect(await db.isAffectedByTest(otherTestFile)).toBe(false);
506500
});
507501
});
508502

extensions/ql-vscode/test/vscode-tests/minimal-workspace/qltest-discovery.test.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
11
import { Uri, WorkspaceFolder } from "vscode";
22
import * as fs from "fs-extra";
3+
import { join } from "path";
34

45
import { QLTestDiscovery } from "../../../src/qltest-discovery";
6+
import { DirectoryResult } from "tmp-promise";
7+
import * as tmp from "tmp-promise";
58

69
describe("qltest-discovery", () => {
710
describe("discoverTests", () => {
8-
const baseUri = Uri.parse("file:/a/b");
9-
const baseDir = baseUri.fsPath;
10-
const cDir = Uri.parse("file:/a/b/c").fsPath;
11-
const dFile = Uri.parse("file:/a/b/c/d.ql").fsPath;
12-
const eFile = Uri.parse("file:/a/b/c/e.ql").fsPath;
13-
const hDir = Uri.parse("file:/a/b/c/f/g/h").fsPath;
14-
const iFile = Uri.parse("file:/a/b/c/f/g/h/i.ql").fsPath;
11+
let directory: DirectoryResult;
12+
13+
let baseDir: string;
14+
let cDir: string;
15+
let dFile: string;
16+
let eFile: string;
17+
let hDir: string;
18+
let iFile: string;
1519
let qlTestDiscover: QLTestDiscovery;
1620

17-
beforeEach(() => {
21+
beforeEach(async () => {
22+
directory = await tmp.dir({
23+
unsafeCleanup: true,
24+
});
25+
26+
const baseUri = Uri.file(directory.path);
27+
baseDir = directory.path;
28+
cDir = join(baseDir, "c");
29+
dFile = join(cDir, "d.ql");
30+
eFile = join(cDir, "e.ql");
31+
hDir = join(cDir, "f/g/h");
32+
iFile = join(hDir, "i.ql");
33+
1834
qlTestDiscover = new QLTestDiscovery(
1935
{
2036
uri: baseUri,
2137
name: "My tests",
2238
} as unknown as WorkspaceFolder,
2339
{
2440
resolveTests() {
25-
return [
26-
Uri.parse("file:/a/b/c/d.ql").fsPath,
27-
Uri.parse("file:/a/b/c/e.ql").fsPath,
28-
Uri.parse("file:/a/b/c/f/g/h/i.ql").fsPath,
29-
];
41+
return [dFile, eFile, iFile];
3042
},
3143
} as any,
3244
);
3345
});
3446

35-
it("should run discovery", async () => {
36-
jest
37-
.spyOn(fs, "pathExists")
38-
.mockImplementation(() => Promise.resolve(true));
47+
afterEach(async () => {
48+
await directory.cleanup();
49+
});
3950

51+
it("should run discovery", async () => {
4052
const result = await (qlTestDiscover as any).discover();
4153
expect(result.watchPath).toBe(baseDir);
4254
expect(result.testDirectory.path).toBe(baseDir);
4355
expect(result.testDirectory.name).toBe("My tests");
4456

4557
let children = result.testDirectory.children;
58+
expect(children.length).toBe(1);
59+
4660
expect(children[0].path).toBe(cDir);
4761
expect(children[0].name).toBe("c");
48-
expect(children.length).toBe(1);
4962

5063
children = children[0].children;
64+
expect(children.length).toBe(3);
65+
5166
expect(children[0].path).toBe(dFile);
5267
expect(children[0].name).toBe("d.ql");
5368
expect(children[1].path).toBe(eFile);
@@ -56,17 +71,14 @@ describe("qltest-discovery", () => {
5671
// A merged foler
5772
expect(children[2].path).toBe(hDir);
5873
expect(children[2].name).toBe("f / g / h");
59-
expect(children.length).toBe(3);
6074

6175
children = children[2].children;
6276
expect(children[0].path).toBe(iFile);
6377
expect(children[0].name).toBe("i.ql");
6478
});
6579

6680
it("should avoid discovery if a folder does not exist", async () => {
67-
jest
68-
.spyOn(fs, "pathExists")
69-
.mockImplementation(() => Promise.resolve(false));
81+
await fs.remove(baseDir);
7082

7183
const result = await (qlTestDiscover as any).discover();
7284
expect(result.watchPath).toBe(baseDir);

0 commit comments

Comments
 (0)