Skip to content

Commit 75e2460

Browse files
committed
Fix platform unit tests by rewiring to support mock-fs
1 parent 6ce772b commit 75e2460

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

src/platform.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import * as os from "os";
66
import * as path from "path";
77
import * as process from "process";
88
import { IPowerShellAdditionalExePathSettings } from "./settings";
9-
import { checkIfFileExists, checkIfDirectoryExists } from "./utils";
9+
// This uses require so we can rewire it in unit tests!
10+
// tslint:disable-next-line:no-var-requires
11+
const utils = require("./utils")
1012

1113
const WindowsPowerShell64BitLabel = "Windows PowerShell (x64)";
1214
const WindowsPowerShell32BitLabel = "Windows PowerShell (x86)";
@@ -276,7 +278,7 @@ export class PowerShellExeFinder {
276278
// Find the base directory for MSIX application exe shortcuts
277279
const msixAppDir = path.join(process.env.LOCALAPPDATA, "Microsoft", "WindowsApps");
278280

279-
if (!await checkIfDirectoryExists(msixAppDir)) {
281+
if (!await utils.checkIfDirectoryExists(msixAppDir)) {
280282
return null;
281283
}
282284

@@ -319,7 +321,7 @@ export class PowerShellExeFinder {
319321
const powerShellInstallBaseDir = path.join(programFilesPath, "PowerShell");
320322

321323
// Ensure the base directory exists
322-
if (!await checkIfDirectoryExists(powerShellInstallBaseDir)) {
324+
if (!await utils.checkIfDirectoryExists(powerShellInstallBaseDir)) {
323325
return null;
324326
}
325327

@@ -365,7 +367,7 @@ export class PowerShellExeFinder {
365367

366368
// Now look for the file
367369
const exePath = path.join(powerShellInstallBaseDir, item, "pwsh.exe");
368-
if (!await checkIfFileExists(exePath)) {
370+
if (!await utils.checkIfFileExists(exePath)) {
369371
continue;
370372
}
371373

@@ -491,7 +493,7 @@ class PossiblePowerShellExe implements IPossiblePowerShellExe {
491493

492494
public async exists(): Promise<boolean> {
493495
if (this.knownToExist === undefined) {
494-
this.knownToExist = await checkIfFileExists(this.exePath);
496+
this.knownToExist = await utils.checkIfFileExists(this.exePath);
495497
}
496498
return this.knownToExist;
497499
}

test/core/paths.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import * as assert from "assert";
55
import * as fs from "fs";
6-
import * as path from "path";
76
import * as vscode from "vscode";
87
import { IPowerShellExtensionClient } from "../../src/features/ExternalApi";
98
import utils = require("../utils");

test/core/platform.test.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,29 @@ import * as assert from "assert";
55
import mockFS = require("mock-fs");
66
import FileSystem = require("mock-fs/lib/filesystem");
77
import * as path from "path";
8+
import rewire = require("rewire");
89
import * as sinon from "sinon";
910
import * as platform from "../../src/platform";
11+
import * as fs from "fs";
12+
import * as vscode from "vscode";
13+
14+
// We have to rewire the platform module so that mock-fs can be used, as it
15+
// overrides the fs module but not the vscode.workspace.fs module.
16+
const platformMock = rewire("../../src/platform");
17+
18+
async function fakeCheckIfFileOrDirectoryExists(targetPath: string | vscode.Uri): Promise<boolean> {
19+
try {
20+
fs.lstatSync(targetPath instanceof vscode.Uri ? targetPath.fsPath : targetPath);
21+
return true;
22+
} catch {
23+
return false;
24+
}
25+
}
26+
const utilsMock = {
27+
checkIfFileExists: fakeCheckIfFileOrDirectoryExists,
28+
checkIfDirectoryExists: fakeCheckIfFileOrDirectoryExists
29+
}
30+
platformMock.__set__("utils", utilsMock);
1031

1132
/**
1233
* Describes a platform on which the PowerShell extension should work,
@@ -469,7 +490,7 @@ function setupTestEnvironment(testPlatform: ITestPlatform) {
469490

470491
describe("Platform module", function () {
471492
it("Gets the correct platform details", function () {
472-
const platformDetails: platform.IPlatformDetails = platform.getPlatformDetails();
493+
const platformDetails: platform.IPlatformDetails = platformMock.getPlatformDetails();
473494
switch (process.platform) {
474495
case "darwin":
475496
assert.strictEqual(
@@ -521,7 +542,7 @@ describe("Platform module", function () {
521542
}
522543
});
523544

524-
describe("Default PowerShell installation", async function () {
545+
describe("Default PowerShell installation", function () {
525546
afterEach(function () {
526547
sinon.restore();
527548
mockFS.restore();
@@ -531,7 +552,7 @@ describe("Platform module", function () {
531552
it(`Finds it on ${testPlatform.name}`, async function () {
532553
setupTestEnvironment(testPlatform);
533554

534-
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
555+
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
535556

536557
const defaultPowerShell = await powerShellExeFinder.getFirstAvailablePowerShellInstallation();
537558
const expectedPowerShell = testPlatform.expectedPowerShellSequence[0];
@@ -545,7 +566,7 @@ describe("Platform module", function () {
545566
it(`Fails gracefully on ${testPlatform.name}`, async function () {
546567
setupTestEnvironment(testPlatform);
547568

548-
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
569+
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
549570

550571
const defaultPowerShell = await powerShellExeFinder.getFirstAvailablePowerShellInstallation();
551572
assert.strictEqual(defaultPowerShell, undefined);
@@ -563,7 +584,7 @@ describe("Platform module", function () {
563584
it(`Finds them on ${testPlatform.name}`, async function () {
564585
setupTestEnvironment(testPlatform);
565586

566-
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
587+
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
567588

568589
const foundPowerShells = await powerShellExeFinder.getAllAvailablePowerShellInstallations();
569590

@@ -586,7 +607,7 @@ describe("Platform module", function () {
586607
it(`Fails gracefully on ${testPlatform.name}`, async function () {
587608
setupTestEnvironment(testPlatform);
588609

589-
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
610+
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
590611

591612
const foundPowerShells = await powerShellExeFinder.getAllAvailablePowerShellInstallations();
592613
assert.strictEqual(foundPowerShells.length, 0);
@@ -626,7 +647,7 @@ describe("Platform module", function () {
626647
altWinPSPath = null;
627648
}
628649

629-
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
650+
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
630651

631652
assert.strictEqual(powerShellExeFinder.fixWindowsPowerShellPath(winPSPath), winPSPath);
632653

test/features/RunCode.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as path from "path";
77
import rewire = require("rewire");
88
import vscode = require("vscode");
99
import utils = require("../utils");
10-
import { sleep } from "../../src/utils";
1110

1211
// Setup function that is not exported.
1312
const customViews = rewire("../../src/features/RunCode");

0 commit comments

Comments
 (0)