Skip to content

Commit 2a17146

Browse files
committed
test(editor): add tests for multi workspace folder setup
1 parent 2113390 commit 2a17146

File tree

14 files changed

+521
-339
lines changed

14 files changed

+521
-339
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ target/
2424
/editors/vscode/out/
2525
/editors/vscode/*.vsix
2626
/editors/vscode/test_workspace/
27+
/editors/vscode/test_workspace_second/
28+
/editors/vscode/*.test.code-workspace
2729

2830
# Cloned conformance repos
2931
tasks/coverage/babel/

editors/vscode/.vscode-test.mjs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
11
import { defineConfig } from '@vscode/test-cli';
2-
import { existsSync, mkdirSync } from 'node:fs';
2+
import { mkdirSync, writeFileSync } from 'node:fs';
33
import path from 'node:path';
44

5-
// create dir if not exists
6-
if (!existsSync('./test_workspace')) {
7-
mkdirSync('./test_workspace');
8-
}
5+
const multiRootWorkspaceFile = './multi-root.test.code-workspace';
6+
7+
mkdirSync('./test_workspace', { recursive: true });
8+
mkdirSync('./test_workspace_second', { recursive: true });
9+
10+
const multiRootWorkspaceConfig = {
11+
'folders': [
12+
{ 'path': 'test_workspace' },
13+
{ 'path': 'test_workspace_second' },
14+
],
15+
};
16+
writeFileSync(multiRootWorkspaceFile, JSON.stringify(multiRootWorkspaceConfig, null, 2));
917

1018
const ext = process.platform === 'win32' ? '.exe' : '';
1119

1220
export default defineConfig({
13-
tests: [{
14-
files: 'out/**/*.spec.js',
15-
workspaceFolder: './test_workspace',
16-
launchArgs: [
17-
// This disables all extensions except the one being testing
18-
'--disable-extensions',
19-
],
20-
env: {
21-
SERVER_PATH_DEV: path.resolve(import.meta.dirname, `./target/debug/oxc_language_server${ext}`),
21+
tests: [
22+
{
23+
files: 'out/**/*.spec.js',
24+
workspaceFolder: './test_workspace',
25+
launchArgs: [
26+
// This disables all extensions except the one being testing
27+
'--disable-extensions',
28+
],
29+
env: {
30+
SINGLE_FOLDER_WORKSPACE: 'true',
31+
SERVER_PATH_DEV: path.resolve(
32+
import.meta.dirname,
33+
`./target/debug/oxc_language_server${ext}`,
34+
),
35+
},
36+
mocha: {
37+
timeout: 10_000,
38+
},
2239
},
23-
mocha: {
24-
timeout: 10_000,
40+
{
41+
files: 'out/**/*.spec.js',
42+
workspaceFolder: multiRootWorkspaceFile,
43+
launchArgs: [
44+
// This disables all extensions except the one being testing
45+
'--disable-extensions',
46+
],
47+
env: {
48+
MULTI_FOLDER_WORKSPACE: 'true',
49+
SERVER_PATH_DEV: path.resolve(
50+
import.meta.dirname,
51+
`./target/debug/oxc_language_server${ext}`,
52+
),
53+
},
54+
mocha: {
55+
timeout: 10_000,
56+
},
2557
},
26-
}],
58+
],
2759
});

editors/vscode/client/WorkspaceConfig.spec.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-debugger": "error"
4+
}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* 😊 */debugger;

editors/vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
"server:build:debug": "cross-env CARGO_TARGET_DIR=./target cargo build -p oxc_language_server",
152152
"server:build:release": "cross-env CARGO_TARGET_DIR=./target cargo build -p oxc_language_server --release",
153153
"lint": "npx oxlint --tsconfig=tsconfig.json",
154-
"test": "esbuild client/*.spec.ts --bundle --outdir=out --external:vscode --format=cjs --platform=node --target=node16 --minify --sourcemap && vscode-test",
154+
"test": "esbuild tests/*.spec.ts --bundle --outdir=out --external:vscode --format=cjs --platform=node --target=node16 --sourcemap && vscode-test",
155155
"type-check": "tsc --noEmit"
156156
},
157157
"devDependencies": {

editors/vscode/client/VSCodeConfig.spec.ts renamed to editors/vscode/tests/VSCodeConfig.spec.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { strictEqual } from 'assert';
2-
import { Uri, workspace, WorkspaceEdit } from 'vscode';
3-
import { VSCodeConfig } from './VSCodeConfig.js';
2+
import { workspace } from 'vscode';
3+
import { VSCodeConfig } from '../client/VSCodeConfig.js';
4+
import { testSingleFolderMode } from './test-helpers.js';
45

56
const conf = workspace.getConfiguration('oxc');
67

@@ -11,23 +12,21 @@ suite('VSCodeConfig', () => {
1112
await Promise.all(keys.map(key => conf.update(key, undefined)));
1213
});
1314

14-
suiteTeardown(async () => {
15-
const WORKSPACE_DIR = workspace.workspaceFolders![0].uri.toString();
16-
const file = Uri.parse(WORKSPACE_DIR + '/.vscode/settings.json');
17-
const edit = new WorkspaceEdit();
18-
edit.deleteFile(file);
19-
await workspace.applyEdit(edit);
15+
teardown(async () => {
16+
const keys = ['enable', 'trace.server', 'path.server'];
17+
18+
await Promise.all(keys.map(key => conf.update(key, undefined)));
2019
});
2120

22-
test('default values on initialization', () => {
21+
testSingleFolderMode('default values on initialization', () => {
2322
const config = new VSCodeConfig();
2423

2524
strictEqual(config.enable, true);
2625
strictEqual(config.trace, 'off');
2726
strictEqual(config.binPath, '');
2827
});
2928

30-
test('updating values updates the workspace configuration', async () => {
29+
testSingleFolderMode('updating values updates the workspace configuration', async () => {
3130
const config = new VSCodeConfig();
3231

3332
await Promise.all([
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { deepStrictEqual, strictEqual } from 'assert';
2+
import { ConfigurationTarget, workspace } from 'vscode';
3+
import { WorkspaceConfig } from '../client/WorkspaceConfig.js';
4+
import { WORKSPACE_FOLDER } from './test-helpers.js';
5+
6+
suite('WorkspaceConfig', () => {
7+
setup(async () => {
8+
const workspaceConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER);
9+
const globalConfig = workspace.getConfiguration('oxc');
10+
const keys = ['lint.run', 'configPath', 'flags'];
11+
12+
await Promise.all(keys.map(key => workspaceConfig.update(key, undefined, ConfigurationTarget.WorkspaceFolder)));
13+
// VSCode will not save different workspace configuration inside a `.code-workspace` file.
14+
// Do not fail, we will make sure the global config is empty too.
15+
await Promise.all(keys.map(key => globalConfig.update(key, undefined)));
16+
});
17+
18+
teardown(async () => {
19+
const workspaceConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER);
20+
const globalConfig = workspace.getConfiguration('oxc');
21+
const keys = ['lint.run', 'configPath', 'flags'];
22+
23+
await Promise.all(keys.map(key => workspaceConfig.update(key, undefined, ConfigurationTarget.WorkspaceFolder)));
24+
// VSCode will not save different workspace configuration inside a `.code-workspace` file.
25+
// Do not fail, we will make sure the global config is empty too.
26+
await Promise.all(keys.map(key => globalConfig.update(key, undefined)));
27+
});
28+
29+
test('default values on initialization', () => {
30+
const config = new WorkspaceConfig(WORKSPACE_FOLDER);
31+
strictEqual(config.runTrigger, 'onType');
32+
strictEqual(config.configPath, null);
33+
deepStrictEqual(config.flags, {});
34+
});
35+
36+
test('configPath defaults to null when using nested configs and configPath is empty', async () => {
37+
const wsConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER);
38+
await wsConfig.update('configPath', '', ConfigurationTarget.WorkspaceFolder);
39+
await wsConfig.update('flags', {}, ConfigurationTarget.WorkspaceFolder);
40+
41+
const config = new WorkspaceConfig(WORKSPACE_FOLDER);
42+
43+
deepStrictEqual(config.flags, {});
44+
strictEqual(config.configPath, null);
45+
});
46+
47+
test('configPath defaults to .oxlintrc.json when not using nested configs and configPath is empty', async () => {
48+
const wsConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER);
49+
await wsConfig.update('configPath', undefined, ConfigurationTarget.WorkspaceFolder);
50+
await wsConfig.update('flags', { disable_nested_config: '' }, ConfigurationTarget.WorkspaceFolder);
51+
52+
const config = new WorkspaceConfig(WORKSPACE_FOLDER);
53+
54+
deepStrictEqual(config.flags, { disable_nested_config: '' });
55+
strictEqual(config.configPath, '.oxlintrc.json');
56+
});
57+
58+
test('updating values updates the workspace configuration', async () => {
59+
const config = new WorkspaceConfig(WORKSPACE_FOLDER);
60+
61+
await Promise.all([
62+
config.updateRunTrigger('onSave'),
63+
config.updateConfigPath('./somewhere'),
64+
config.updateFlags({ test: 'value' }),
65+
]);
66+
67+
const wsConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER);
68+
69+
strictEqual(wsConfig.get('lint.run'), 'onSave');
70+
strictEqual(wsConfig.get('configPath'), './somewhere');
71+
deepStrictEqual(wsConfig.get('flags'), { test: 'value' });
72+
});
73+
});

0 commit comments

Comments
 (0)