Skip to content

Commit 7e8e3fc

Browse files
authored
Add unit test for getting workspace settings (#2062)
* Add unit test for getting workspace settings * Add unit test for getting workspace settings * Update * Update
1 parent c1fb251 commit 7e8e3fc

5 files changed

Lines changed: 77 additions & 9 deletions

File tree

src/extension/rn-extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ export function getCountOfWorkspaceFolders(): number {
284284
}
285285

286286
export async function onFolderAdded(folder: vscode.WorkspaceFolder): Promise<void> {
287-
const excludeFolders = await SettingsHelper.getWorkspaceFileExcludeFolder();
287+
const workspacePath = vscode.workspace.workspaceFile?.fsPath;
288+
const excludeFolders = await SettingsHelper.getWorkspaceFileExcludeFolder(workspacePath);
288289
let isExclude = false;
289290
if (excludeFolders.length != 0) {
290291
for (let i = 0; i < excludeFolders.length; i++) {

src/extension/settingsHelper.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,20 @@ export class SettingsHelper {
206206
}
207207
}
208208

209-
public static async getWorkspaceFileExcludeFolder(): Promise<any> {
210-
const workspacePath = vscode.workspace.workspaceFile?.fsPath;
211-
const workspaceSettingsContent = workspacePath
212-
? JSON.parse(fs.readFileSync(workspacePath, "utf-8"))
209+
public static async getWorkspaceFileExcludeFolder(
210+
settingsPath: string | undefined,
211+
): Promise<any> {
212+
const workspaceSettingsContent = settingsPath
213+
? JSON.parse(fs.readFileSync(settingsPath, "utf-8"))
213214
: null;
214215
if (workspaceSettingsContent) {
215-
const exclude = workspaceSettingsContent.settings["react-native.workspace.exclude"];
216-
return exclude ? exclude : [];
217-
} else {
218-
return [];
216+
if (workspaceSettingsContent.settings) {
217+
const exclude = workspaceSettingsContent.settings["react-native.workspace.exclude"];
218+
return exclude ? exclude : [];
219+
} else {
220+
return [];
221+
}
219222
}
223+
return [];
220224
}
221225
}

test/extension/workspace.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for details.
3+
4+
import * as assert from "assert";
5+
import * as path from "path";
6+
import { SettingsHelper } from "../../src/extension/settingsHelper";
7+
8+
suite("workspace", function () {
9+
suite("extensionContext", function () {
10+
test("Should get react-native.workspace.exclude correctly from workspace settings file", async function () {
11+
const noWorkspacePath = undefined;
12+
const exclude1 = await SettingsHelper.getWorkspaceFileExcludeFolder(noWorkspacePath);
13+
assert.strictEqual(exclude1.length, 0);
14+
15+
const noExcludeWorkspacePath = path.resolve(
16+
__dirname,
17+
"..",
18+
"resources",
19+
"workspaceSettingsSample",
20+
"noExcludeSetting.json",
21+
);
22+
const exclude2 = await SettingsHelper.getWorkspaceFileExcludeFolder(
23+
noExcludeWorkspacePath,
24+
);
25+
assert.strictEqual(exclude2.length, 0);
26+
27+
const excludeWorkspacePath = path.resolve(
28+
__dirname,
29+
"..",
30+
"resources",
31+
"workspaceSettingsSample",
32+
"excludeSetting.json",
33+
);
34+
const exclude3 = await SettingsHelper.getWorkspaceFileExcludeFolder(
35+
excludeWorkspacePath,
36+
);
37+
assert.strictEqual(exclude3.length, 1);
38+
assert.strictEqual(exclude3[0], "testProject2");
39+
});
40+
});
41+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "testProject1"
5+
},
6+
{
7+
"path": "testProject2"
8+
}
9+
],
10+
"settings": {
11+
"react-native.workspace.exclude": [
12+
"testProject2"
13+
]
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "testProject"
5+
}
6+
]
7+
}

0 commit comments

Comments
 (0)