Skip to content

Commit 58f1dd0

Browse files
committed
Remove unnecessary stub in tests
1 parent 8087fcc commit 58f1dd0

File tree

3 files changed

+22
-81
lines changed

3 files changed

+22
-81
lines changed

src/platform.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import vscode = require("vscode");
88
import { integer } from "vscode-languageserver-protocol";
99
import type { ILogger } from "./logging";
1010
import { changeSetting, getSettings, type PowerShellAdditionalExePathSettings } from "./settings";
11+
import * as utils from "./utils";
1112
import untildify from "untildify";
1213

13-
// This uses require so we can rewire it in unit tests!
14-
const utils = require("./utils");
15-
1614
const WindowsPowerShell64BitLabel = "Windows PowerShell (x64)";
1715
const WindowsPowerShell32BitLabel = "Windows PowerShell (x86)";
1816

@@ -91,7 +89,7 @@ export class PowerShellExeFinder {
9189
private platformDetails: IPlatformDetails,
9290
// Additional configured PowerShells
9391
private additionalPowerShellExes: PowerShellAdditionalExePathSettings,
94-
private logger: ILogger) { }
92+
private logger?: ILogger) { }
9593

9694
/**
9795
* Returns the first available PowerShell executable found in the search order.
@@ -156,7 +154,7 @@ export class PowerShellExeFinder {
156154
yield additionalPwsh;
157155
} else if (!additionalPwsh.suppressWarning) {
158156
const message = `Additional PowerShell '${additionalPwsh.displayName}' not found at '${additionalPwsh.exePath}'!`;
159-
this.logger.writeWarning(message);
157+
this.logger?.writeWarning(message);
160158

161159
if (!getSettings().suppressAdditionalExeNotFoundWarning) {
162160
const selection = await vscode.window.showWarningMessage(message, "Don't Show Again");
@@ -243,7 +241,7 @@ export class PowerShellExeFinder {
243241
* Iterates through the configured additional PowerShell executable locations,
244242
* without checking for their existence.
245243
*/
246-
private async *enumerateAdditionalPowerShellInstallations(): AsyncIterable<IPossiblePowerShellExe> {
244+
public async *enumerateAdditionalPowerShellInstallations(): AsyncIterable<IPossiblePowerShellExe> {
247245
for (const versionName in this.additionalPowerShellExes) {
248246
if (Object.prototype.hasOwnProperty.call(this.additionalPowerShellExes, versionName)) {
249247
let exePath: string | undefined = utils.stripQuotePair(this.additionalPowerShellExes[versionName]);

test/core/platform.test.ts

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,8 @@ import mockFS = require("mock-fs");
66
import FileSystem = require("mock-fs/lib/filesystem");
77
import * as os from "os";
88
import * as path from "path";
9-
import rewire = require("rewire");
109
import * as sinon from "sinon";
1110
import * as platform from "../../src/platform";
12-
import * as fs from "fs"; // NOTE: Necessary for mock-fs.
13-
import * as vscode from "vscode";
14-
import { stripQuotePair } from "../../src/utils";
15-
16-
// We have to rewire the platform module so that mock-fs can be used, as it
17-
// overrides the fs module but not the vscode.workspace.fs module.
18-
const platformMock = rewire("../../src/platform");
19-
20-
// eslint-disable-next-line @typescript-eslint/require-await
21-
async function fakeCheckIfFileExists(targetPath: string | vscode.Uri): Promise<boolean> {
22-
try {
23-
const stat = fs.lstatSync(targetPath instanceof vscode.Uri ? targetPath.fsPath : targetPath);
24-
return stat.isFile();
25-
} catch {
26-
return false;
27-
}
28-
}
29-
30-
// eslint-disable-next-line @typescript-eslint/require-await
31-
async function fakeCheckIfDirectoryExists(targetPath: string | vscode.Uri): Promise<boolean> {
32-
try {
33-
const stat = fs.lstatSync(targetPath instanceof vscode.Uri ? targetPath.fsPath : targetPath);
34-
return stat.isDirectory();
35-
} catch {
36-
return false;
37-
}
38-
}
39-
40-
// eslint-disable-next-line @typescript-eslint/require-await
41-
async function fakeReadDirectory(targetPath: string | vscode.Uri): Promise<string[]> {
42-
return fs.readdirSync(targetPath instanceof vscode.Uri ? targetPath.fsPath : targetPath);
43-
}
44-
45-
const utilsMock = {
46-
checkIfFileExists: fakeCheckIfFileExists,
47-
checkIfDirectoryExists: fakeCheckIfDirectoryExists,
48-
readDirectory: fakeReadDirectory,
49-
stripQuotePair: stripQuotePair
50-
};
51-
52-
platformMock.__set__("utils", utilsMock);
5311

5412
/**
5513
* Describes a platform on which the PowerShell extension should work,
@@ -847,8 +805,12 @@ function setupTestEnvironment(testPlatform: ITestPlatform): void {
847805
}
848806

849807
describe("Platform module", function () {
808+
afterEach(function () {
809+
mockFS.restore();
810+
});
811+
850812
it("Gets the correct platform details", function () {
851-
const platformDetails: platform.IPlatformDetails = platformMock.getPlatformDetails();
813+
const platformDetails: platform.IPlatformDetails = platform.getPlatformDetails();
852814
switch (process.platform) {
853815
case "darwin":
854816
assert.strictEqual(
@@ -901,31 +863,26 @@ describe("Platform module", function () {
901863
});
902864

903865
describe("Default PowerShell installation", function () {
904-
afterEach(function () {
905-
sinon.restore();
906-
mockFS.restore();
907-
});
908-
909866
for (const testPlatform of successTestCases) {
910867
it(`Finds it on ${testPlatform.name}`, async function () {
911868
setupTestEnvironment(testPlatform);
912869

913-
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
870+
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails, {});
914871

915872
const defaultPowerShell = await powerShellExeFinder.getFirstAvailablePowerShellInstallation();
916873
const expectedPowerShell = testPlatform.expectedPowerShellSequence[0];
917874

918-
assert.strictEqual(defaultPowerShell.exePath, expectedPowerShell.exePath);
919-
assert.strictEqual(defaultPowerShell.displayName, expectedPowerShell.displayName);
920-
assert.strictEqual(defaultPowerShell.supportsProperArguments, expectedPowerShell.supportsProperArguments);
875+
assert.strictEqual(defaultPowerShell!.exePath, expectedPowerShell.exePath);
876+
assert.strictEqual(defaultPowerShell!.displayName, expectedPowerShell.displayName);
877+
assert.strictEqual(defaultPowerShell!.supportsProperArguments, expectedPowerShell.supportsProperArguments);
921878
});
922879
}
923880

924881
for (const testPlatform of errorTestCases) {
925882
it(`Fails gracefully on ${testPlatform.name}`, async function () {
926883
setupTestEnvironment(testPlatform);
927884

928-
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
885+
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails, {});
929886

930887
const defaultPowerShell = await powerShellExeFinder.getFirstAvailablePowerShellInstallation();
931888
assert.strictEqual(defaultPowerShell, undefined);
@@ -934,26 +891,21 @@ describe("Platform module", function () {
934891
});
935892

936893
describe("Expected PowerShell installation list", function () {
937-
afterEach(function () {
938-
sinon.restore();
939-
mockFS.restore();
940-
});
941-
942894
for (const testPlatform of successTestCases) {
943895
it(`Finds them on ${testPlatform.name}`, async function () {
944896
setupTestEnvironment(testPlatform);
945897

946-
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
898+
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails, {});
947899

948900
const foundPowerShells = await powerShellExeFinder.getAllAvailablePowerShellInstallations();
949901

950902
for (let i = 0; i < testPlatform.expectedPowerShellSequence.length; i++) {
951903
const foundPowerShell = foundPowerShells[i];
952904
const expectedPowerShell = testPlatform.expectedPowerShellSequence[i];
953905

954-
assert.strictEqual(foundPowerShell?.exePath, expectedPowerShell.exePath);
955-
assert.strictEqual(foundPowerShell?.displayName, expectedPowerShell.displayName);
956-
assert.strictEqual(foundPowerShell?.supportsProperArguments, expectedPowerShell.supportsProperArguments);
906+
assert.strictEqual(foundPowerShell.exePath, expectedPowerShell.exePath);
907+
assert.strictEqual(foundPowerShell.displayName, expectedPowerShell.displayName);
908+
assert.strictEqual(foundPowerShell.supportsProperArguments, expectedPowerShell.supportsProperArguments);
957909
}
958910

959911
assert.strictEqual(
@@ -967,7 +919,7 @@ describe("Platform module", function () {
967919
it(`Fails gracefully on ${testPlatform.name}`, async function () {
968920
setupTestEnvironment(testPlatform);
969921

970-
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
922+
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails, {});
971923

972924
const foundPowerShells = await powerShellExeFinder.getAllAvailablePowerShellInstallations();
973925
assert.strictEqual(foundPowerShells.length, 0);
@@ -976,11 +928,6 @@ describe("Platform module", function () {
976928
});
977929

978930
describe("Windows PowerShell path fix", function () {
979-
afterEach(function () {
980-
sinon.restore();
981-
mockFS.restore();
982-
});
983-
984931
for (const testPlatform of successTestCases
985932
.filter((tp) => tp.platformDetails.operatingSystem === platform.OperatingSystem.Windows)) {
986933

@@ -1007,7 +954,7 @@ describe("Platform module", function () {
1007954
altWinPSPath = null;
1008955
}
1009956

1010-
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails);
957+
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails, {});
1011958

1012959
assert.strictEqual(powerShellExeFinder.fixWindowsPowerShellPath(winPSPath), winPSPath);
1013960

@@ -1019,16 +966,11 @@ describe("Platform module", function () {
1019966
});
1020967

1021968
describe("PowerShell executables from 'powerShellAdditionalExePaths' are found", function () {
1022-
afterEach(function () {
1023-
sinon.restore();
1024-
mockFS.restore();
1025-
});
1026-
1027969
for (const testPlatform of successAdditionalTestCases) {
1028970
it(`Guesses for ${testPlatform.name}`, async function () {
1029971
setupTestEnvironment(testPlatform);
1030972

1031-
const powerShellExeFinder = new platformMock.PowerShellExeFinder(testPlatform.platformDetails, additionalPowerShellExes);
973+
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails, additionalPowerShellExes);
1032974

1033975
let i = 0;
1034976
for await (const additionalPwsh of powerShellExeFinder.enumerateAdditionalPowerShellInstallations()) {

test/features/DebugSession.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ describe("DebugSessionFeature", () => {
191191

192192
assert.equal(actual, undefined);
193193
assert.match(logger.writeAndShowError.firstCall.args[0], /debugging this file type/);
194+
Sinon.restore();
194195
});
195196

196197
it("Prevents debugging untitled files in a temp console", async () => {

0 commit comments

Comments
 (0)