Skip to content

Commit 2d7fb0b

Browse files
committed
Add tests for validateCwdSetting
Those were overdue!
1 parent c6e5377 commit 2d7fb0b

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

src/settings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export async function changeSetting(
217217
let hasPrompted = false;
218218
export let chosenWorkspace: vscode.WorkspaceFolder | undefined = undefined;
219219

220-
export async function validateCwdSetting(logger: ILogger): Promise<string> {
220+
export async function validateCwdSetting(logger: ILogger | undefined): Promise<string> {
221221
let cwd: string | undefined = utils.stripQuotePair(
222222
vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd"));
223223

test/core/settings.test.ts

+64-17
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,79 @@
22
// Licensed under the MIT License.
33

44
import * as assert from "assert";
5+
import * as os from "os";
56
import * as vscode from "vscode";
6-
import { Settings, getSettings, getEffectiveConfigurationTarget, changeSetting, CommentType } from "../../src/settings";
7+
import {
8+
Settings,
9+
getSettings,
10+
getEffectiveConfigurationTarget,
11+
changeSetting,
12+
CommentType,
13+
validateCwdSetting
14+
} from "../../src/settings";
15+
import path from "path";
716

817
describe("Settings E2E", function () {
9-
it("Loads without error", function () {
10-
assert.doesNotThrow(getSettings);
18+
describe("The 'getSettings' method loads the 'Settings' class", function () {
19+
it("Loads without error", function () {
20+
assert.doesNotThrow(getSettings);
21+
});
22+
23+
it("Loads the correct defaults", function () {
24+
const testSettings = new Settings();
25+
const actualSettings = getSettings();
26+
assert.deepStrictEqual(actualSettings, testSettings);
27+
});
1128
});
1229

13-
it("Loads the correct defaults", function () {
14-
const testSettings = new Settings();
15-
const actualSettings = getSettings();
16-
assert.deepStrictEqual(actualSettings, testSettings);
30+
describe("The 'changeSetting' method", function () {
31+
it("Updates correctly", async function () {
32+
await changeSetting("helpCompletion", CommentType.LineComment, false, undefined);
33+
assert.strictEqual(getSettings().helpCompletion, CommentType.LineComment);
34+
});
1735
});
1836

19-
it("Updates correctly", async function () {
20-
await changeSetting("helpCompletion", CommentType.LineComment, false, undefined);
21-
assert.strictEqual(getSettings().helpCompletion, CommentType.LineComment);
37+
describe("The 'getEffectiveConfigurationTarget' method'", function () {
38+
it("Works for 'Workspace' target", async function () {
39+
await changeSetting("helpCompletion", CommentType.LineComment, false, undefined);
40+
const target = getEffectiveConfigurationTarget("helpCompletion");
41+
assert.strictEqual(target, vscode.ConfigurationTarget.Workspace);
42+
});
43+
44+
it("Works for 'undefined' target", async function () {
45+
await changeSetting("helpCompletion", undefined, false, undefined);
46+
const target = getEffectiveConfigurationTarget("helpCompletion");
47+
assert.strictEqual(target, undefined);
48+
});
2249
});
2350

24-
it("Gets the effective configuration target", async function () {
25-
await changeSetting("helpCompletion", CommentType.LineComment, false, undefined);
26-
let target = getEffectiveConfigurationTarget("helpCompletion");
27-
assert.strictEqual(target, vscode.ConfigurationTarget.Workspace);
51+
describe("The CWD setting", function () {
52+
beforeEach(async function () {
53+
await changeSetting("cwd", undefined, undefined, undefined);
54+
});
55+
56+
const workspace = vscode.workspace.workspaceFolders![0].uri.fsPath;
57+
58+
it("Defaults to the 'mocks' workspace folder", async function () {
59+
assert.strictEqual(await validateCwdSetting(undefined), workspace);
60+
});
61+
62+
it("Uses the default when given a non-existent folder", async function () {
63+
await changeSetting("cwd", "/a/totally/fake/folder", undefined, undefined);
64+
assert.strictEqual(await validateCwdSetting(undefined), workspace);
65+
});
66+
67+
it("Uses the given folder when it exists", async function () {
68+
// A different than default folder that definitely exists
69+
const cwd = path.resolve(path.join(process.cwd(), ".."));
70+
await changeSetting("cwd", cwd, undefined, undefined);
71+
assert.strictEqual(await validateCwdSetting(undefined), cwd);
72+
});
2873

29-
await changeSetting("helpCompletion", undefined, false, undefined);
30-
target = getEffectiveConfigurationTarget("helpCompletion");
31-
assert.strictEqual(target, undefined);
74+
it("Uses the home folder for ~ (tilde)", async function () {
75+
// A different than default folder that definitely exists
76+
await changeSetting("cwd", "~", undefined, undefined);
77+
assert.strictEqual(await validateCwdSetting(undefined), os.homedir());
78+
});
3279
});
3380
});

0 commit comments

Comments
 (0)