Skip to content

Commit 87cb667

Browse files
authored
test(oxfmt/lsp): add nested workspace folders test (oxc-project#19507)
Added test coverage for a bug a possible oxc-project/oxc-vscode#66 From the snapshots, I could not detect something wrong. Still worth to include it
1 parent b55727f commit 87cb667

10 files changed

Lines changed: 123 additions & 20 deletions

File tree

apps/oxfmt/test/lsp/format/__snapshots__/format.test.ts.snap

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,62 @@ if (true) {
197197
--------------------"
198198
`;
199199

200+
exports[`LSP formatting > config options in nested workspace folders > should respect nested oxfmt config with nested workspace folders nested-workspaces/test.ts 1`] = `
201+
"--- FILE -----------
202+
nested-workspaces/test.ts
203+
--- BEFORE ---------
204+
// { "semi": true, "singleQuote": true }
205+
const x = "1";
206+
207+
--- AFTER ----------
208+
// { "semi": true, "singleQuote": true }
209+
const x = '1';
210+
211+
--------------------"
212+
`;
213+
214+
exports[`LSP formatting > config options in nested workspace folders > should respect nested oxfmt config with nested workspace folders nested-workspaces/test.ts 2`] = `
215+
"--- FILE -----------
216+
nested-workspaces/second/test.ts
217+
--- BEFORE ---------
218+
// { "semi": true, "singleQuote": true }
219+
const x = "1";
220+
221+
--- AFTER ----------
222+
// { "semi": true, "singleQuote": true }
223+
const x = '1';
224+
225+
--------------------"
226+
`;
227+
228+
exports[`LSP formatting > config options in nested workspace folders > should respect nested oxfmt config with nested workspace folders nested-workspaces-with-config/test.ts 1`] = `
229+
"--- FILE -----------
230+
nested-workspaces-with-config/test.ts
231+
--- BEFORE ---------
232+
// { "semi": true, "singleQuote": true }
233+
const x = "1";
234+
235+
--- AFTER ----------
236+
// { "semi": true, "singleQuote": true }
237+
const x = '1';
238+
239+
--------------------"
240+
`;
241+
242+
exports[`LSP formatting > config options in nested workspace folders > should respect nested oxfmt config with nested workspace folders nested-workspaces-with-config/test.ts 2`] = `
243+
"--- FILE -----------
244+
nested-workspaces-with-config/second/test.ts
245+
--- BEFORE ---------
246+
// { "semi": false, "singleQuote": false }
247+
const x = "1";
248+
249+
--- AFTER ----------
250+
// { "semi": false, "singleQuote": false }
251+
const x = "1"
252+
253+
--------------------"
254+
`;
255+
200256
exports[`LSP formatting > ignore patterns > should handle ignore-config/file.generated.ts 1`] = `
201257
"--- FILE -----------
202258
ignore-config/file.generated.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "semi": true, "singleQuote": true }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "semi": false, "singleQuote": false }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// { "semi": false, "singleQuote": false }
2+
const x = "1";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// { "semi": true, "singleQuote": true }
2+
const x = "1";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "semi": true, "singleQuote": true }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// { "semi": true, "singleQuote": true }
2+
const x = "1";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// { "semi": true, "singleQuote": true }
2+
const x = "1";

apps/oxfmt/test/lsp/format/format.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { join } from "node:path";
1+
import { dirname, join } from "node:path";
22
import { describe, expect, it } from "vitest";
3-
import { formatFixture, formatFixtureContent } from "../utils";
3+
import { createLspConnection, formatFixture, formatFixtureContent } from "../utils";
4+
import { pathToFileURL } from "node:url";
45

56
const FIXTURES_DIR = join(import.meta.dirname, "fixtures");
67

@@ -33,6 +34,37 @@ describe("LSP formatting", () => {
3334
});
3435
});
3536

37+
describe("config options in nested workspace folders", () => {
38+
it.each([
39+
["nested-workspaces/test.ts", "nested-workspaces/second/test.ts"],
40+
["nested-workspaces-with-config/test.ts", "nested-workspaces-with-config/second/test.ts"],
41+
])("should respect nested oxfmt config with nested workspace folders %s", async (...paths) => {
42+
await using client = createLspConnection();
43+
const dirUris = paths.map((path) => pathToFileURL(dirname(join(FIXTURES_DIR, path))).href);
44+
await client.initialize(
45+
[
46+
{ uri: dirUris[0], name: "test" },
47+
{ uri: dirUris[1], name: "test-2" },
48+
],
49+
{},
50+
[
51+
{
52+
workspaceUri: dirUris[0],
53+
options: null,
54+
},
55+
{
56+
workspaceUri: dirUris[1],
57+
options: null,
58+
},
59+
],
60+
);
61+
for (const path of paths) {
62+
// oxlint-disable-next-line no-await-in-loop
63+
expect(await formatFixture(FIXTURES_DIR, path, "typescript", client)).toMatchSnapshot();
64+
}
65+
});
66+
});
67+
3668
describe("unsaved document", () => {
3769
it.each([
3870
["format/test.tsx", "typescriptreact"],

apps/oxfmt/test/lsp/utils.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,42 +106,46 @@ export async function formatFixture(
106106
fixturesDir: string,
107107
fixturePath: string,
108108
languageId: string,
109-
initializationOptions?: OxfmtLSPConfig,
109+
clientOrConfig?: OxfmtLSPConfig | ReturnType<typeof createLspConnection>,
110110
): Promise<string> {
111111
const filePath = join(fixturesDir, fixturePath);
112112
const fileUri = pathToFileURL(filePath).href;
113113

114-
return await formatFixtureContent(
115-
fixturesDir,
116-
fixturePath,
117-
fileUri,
118-
languageId,
119-
initializationOptions,
120-
);
114+
return await formatFixtureContent(fixturesDir, fixturePath, fileUri, languageId, clientOrConfig);
121115
}
122116

123117
export async function formatFixtureContent(
124118
fixturesDir: string,
125119
fixturePath: string,
126120
fileUri: string,
127121
languageId: string,
128-
initializationOptions?: OxfmtLSPConfig,
122+
clientOrConfig?: OxfmtLSPConfig | ReturnType<typeof createLspConnection>,
129123
): Promise<string> {
130124
const filePath = join(fixturesDir, fixturePath);
131125
const dirPath = dirname(filePath);
132126
const content = await fs.readFile(filePath, "utf-8");
133127

134-
await using client = createLspConnection();
128+
let innerClient: ReturnType<typeof createLspConnection> | undefined;
135129

136-
await client.initialize([{ uri: pathToFileURL(dirPath).href, name: "test" }], {}, [
137-
{
138-
workspaceUri: pathToFileURL(dirPath).href,
139-
options: initializationOptions,
140-
},
141-
]);
142-
await client.didOpen(fileUri, languageId, content);
130+
if (clientOrConfig === undefined || !("initialize" in clientOrConfig)) {
131+
innerClient = createLspConnection();
132+
133+
await innerClient.initialize([{ uri: pathToFileURL(dirPath).href, name: "test" }], {}, [
134+
{
135+
workspaceUri: pathToFileURL(dirPath).href,
136+
options: clientOrConfig,
137+
},
138+
]);
139+
140+
clientOrConfig = innerClient;
141+
}
142+
await clientOrConfig.didOpen(fileUri, languageId, content);
143+
144+
const edits = await clientOrConfig.format(fileUri);
143145

144-
const edits = await client.format(fileUri);
146+
if (innerClient) {
147+
await innerClient[Symbol.asyncDispose]();
148+
}
145149

146150
return `
147151
--- FILE -----------

0 commit comments

Comments
 (0)