|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import { promisify } from "util"; |
| 5 | +import { |
| 6 | + format, |
| 7 | + getWorkspaceFolderUri, |
| 8 | + moveRootPrettierRC, |
| 9 | + putBackPrettierRC, |
| 10 | +} from "./formatTestUtils.js"; |
| 11 | +import { ensureExtensionActivated } from "./testUtils.js"; |
| 12 | + |
| 13 | +const writeFileAsync = promisify(fs.writeFile); |
| 14 | +const readFileAsync = promisify(fs.readFile); |
| 15 | + |
| 16 | +/** |
| 17 | + * Helper to wait for a specified number of milliseconds |
| 18 | + */ |
| 19 | +function delay(ms: number): Promise<void> { |
| 20 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 21 | +} |
| 22 | + |
| 23 | +describe("Test config file watcher", () => { |
| 24 | + const configDir = "config-watcher"; |
| 25 | + const configFile = ".prettierrc"; |
| 26 | + let configPath: string; |
| 27 | + let originalConfig: string; |
| 28 | + |
| 29 | + before(async () => { |
| 30 | + await ensureExtensionActivated(); |
| 31 | + await moveRootPrettierRC(); |
| 32 | + |
| 33 | + // Store original config |
| 34 | + const base = getWorkspaceFolderUri("config"); |
| 35 | + configPath = path.join(base.fsPath, configDir, configFile); |
| 36 | + originalConfig = await readFileAsync(configPath, "utf8"); |
| 37 | + }); |
| 38 | + |
| 39 | + after(async () => { |
| 40 | + // Restore original config |
| 41 | + await writeFileAsync(configPath, originalConfig, "utf8"); |
| 42 | + await putBackPrettierRC(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("detects config file changes and applies new formatting", async function () { |
| 46 | + // This test may need more time due to file watcher delays |
| 47 | + this.timeout(30000); |
| 48 | + |
| 49 | + // Format with initial config (tabWidth: 2) |
| 50 | + const { actual: initialFormat } = await format( |
| 51 | + "config", |
| 52 | + `${configDir}/test.js`, |
| 53 | + ); |
| 54 | + |
| 55 | + // Verify initial format uses tabWidth: 2 (2-space indentation) |
| 56 | + assert.ok( |
| 57 | + initialFormat.includes(" console.log"), |
| 58 | + `Initial format should use 2-space indentation, got:\n${initialFormat}`, |
| 59 | + ); |
| 60 | + |
| 61 | + // Change config to tabWidth: 4 |
| 62 | + const newConfig = JSON.stringify({ tabWidth: 4 }, null, 2) + "\n"; |
| 63 | + await writeFileAsync(configPath, newConfig, "utf8"); |
| 64 | + |
| 65 | + // Wait for file watcher to detect the change |
| 66 | + // The watcher should clear the formatter cache |
| 67 | + await delay(2000); |
| 68 | + |
| 69 | + // Format again - should now use tabWidth: 4 |
| 70 | + const { actual: newFormat } = await format( |
| 71 | + "config", |
| 72 | + `${configDir}/test.js`, |
| 73 | + ); |
| 74 | + |
| 75 | + // Verify new format uses tabWidth: 4 (4-space indentation) |
| 76 | + assert.ok( |
| 77 | + newFormat.includes(" console.log"), |
| 78 | + `After config change, format should use 4-space indentation, got:\n${newFormat}`, |
| 79 | + ); |
| 80 | + assert.ok( |
| 81 | + !newFormat.includes(" console.log(") || |
| 82 | + newFormat.includes(" console.log"), |
| 83 | + "Should not have 2-space indentation after config change", |
| 84 | + ); |
| 85 | + }); |
| 86 | +}); |
0 commit comments