|
1 | 1 | import * as assert from "assert"; |
2 | 2 | import * as vscode from "vscode"; |
3 | 3 | import { ensureExtensionActivated } from "./testUtils.js"; |
| 4 | +import { getWorkspaceFolderUri } from "./formatTestUtils.js"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Helper to wait for a specified number of milliseconds |
| 8 | + */ |
| 9 | +function delay(ms: number): Promise<void> { |
| 10 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 11 | +} |
4 | 12 |
|
5 | 13 | describe("Test Prettier Code Actions", () => { |
6 | 14 | const unformattedCode = `const x = { a: 1, b: 2 }`; |
@@ -79,4 +87,73 @@ describe("Test Prettier Code Actions", () => { |
79 | 87 | "Document should be formatted correctly after applying code action", |
80 | 88 | ); |
81 | 89 | }); |
| 90 | + |
| 91 | + it("does NOT provide code action when defaultFormatter is not prettier", async function () { |
| 92 | + // Increase timeout for this test as it involves workspace folder switching |
| 93 | + this.timeout(10000); |
| 94 | + |
| 95 | + // The 'workspace' folder has editor.defaultFormatter set to |
| 96 | + // vscode.typescript-language-features for JavaScript files |
| 97 | + const base = getWorkspaceFolderUri("workspace"); |
| 98 | + const testFilePath = vscode.Uri.joinPath(base, "test.js"); |
| 99 | + const doc = await vscode.workspace.openTextDocument(testFilePath); |
| 100 | + await vscode.window.showTextDocument(doc); |
| 101 | + |
| 102 | + // Wait for formatter registration to complete |
| 103 | + await delay(1000); |
| 104 | + |
| 105 | + // Get code actions for the document |
| 106 | + const codeActions = await vscode.commands.executeCommand< |
| 107 | + vscode.CodeAction[] |
| 108 | + >( |
| 109 | + "vscode.executeCodeActionProvider", |
| 110 | + doc.uri, |
| 111 | + new vscode.Range(0, 0, doc.lineCount, 0), |
| 112 | + ); |
| 113 | + |
| 114 | + // Prettier code action should NOT be available |
| 115 | + const prettierAction = codeActions?.find( |
| 116 | + (action) => action.kind?.value === "source.fixAll.prettier", |
| 117 | + ); |
| 118 | + |
| 119 | + assert.ok( |
| 120 | + !prettierAction, |
| 121 | + "Prettier code action should NOT be available when defaultFormatter is not prettier", |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it("provides code action when source.fixAll.prettier is explicitly enabled", async function () { |
| 126 | + // Increase timeout for this test as it involves workspace folder switching |
| 127 | + this.timeout(10000); |
| 128 | + |
| 129 | + // The 'workspace-explicit-prettier' folder has: |
| 130 | + // - editor.defaultFormatter set to a different formatter |
| 131 | + // - BUT source.fixAll.prettier is explicitly set to "always" |
| 132 | + const base = getWorkspaceFolderUri("workspace-explicit-prettier"); |
| 133 | + const testFilePath = vscode.Uri.joinPath(base, "test.js"); |
| 134 | + const doc = await vscode.workspace.openTextDocument(testFilePath); |
| 135 | + await vscode.window.showTextDocument(doc); |
| 136 | + |
| 137 | + // Wait for formatter registration to complete |
| 138 | + await delay(1000); |
| 139 | + |
| 140 | + // Get code actions for the document |
| 141 | + const codeActions = await vscode.commands.executeCommand< |
| 142 | + vscode.CodeAction[] |
| 143 | + >( |
| 144 | + "vscode.executeCodeActionProvider", |
| 145 | + doc.uri, |
| 146 | + new vscode.Range(0, 0, doc.lineCount, 0), |
| 147 | + ); |
| 148 | + |
| 149 | + // Prettier code action SHOULD be available because explicitly enabled |
| 150 | + const prettierAction = codeActions?.find( |
| 151 | + (action) => action.kind?.value === "source.fixAll.prettier", |
| 152 | + ); |
| 153 | + |
| 154 | + assert.ok( |
| 155 | + prettierAction, |
| 156 | + "Prettier code action should be available when source.fixAll.prettier is explicitly enabled", |
| 157 | + ); |
| 158 | + }); |
82 | 159 | }); |
0 commit comments