| 179 | replacer: ((content: Buffer) => Buffer) | ((content: string) => string), |
| 180 | ): void |
| 181 | export function editFile( |
| 182 | filename: string, |
| 183 | encodingOrReplacer: BufferEncoding | null | ((content: string) => string), |
| 184 | maybeReplacer?: ((content: Buffer) => Buffer) | ((content: string) => string), |
| 185 | ): void { |
| 186 | filename = path.resolve(testDir, filename) |
| 187 | const [encoding, replacer] = maybeReplacer |
| 188 | ? [encodingOrReplacer as BufferEncoding | null, maybeReplacer] |
| 189 | : [class="st">'utf-8' as const, encodingOrReplacer as (content: string) => string] |
| 190 | const content: string | Buffer = fs.readFileSync(filename, encoding) |
| 191 | const modified = (replacer as (content: string | Buffer) => string | Buffer)( |
| 192 | content, |
| 193 | ) |
| 194 | if (Buffer.byteLength(modified) === Buffer.byteLength(content)) { |
| 195 | const e = new Error( |
| 196 | `editFile(class="st">"${filename}") did not change the file size. The polling ` + |
| 197 | `watcher used in tests may miss same-length edits and cause flaky ` + |
| 198 | `failures; change the edit so the file's byte length changes. See ` + |
| 199 | `https:class="cm">//github.com/vitejs/vite/blob/main/CONTRIBUTING.md#test-env-and-helpers`, |
| 200 | ) |
| 201 | Error.captureStackTrace(e, editFile) |
| 202 | throw e |
| 203 | } |
| 204 | fs.writeFileSync(filename, modified) |
| 205 | } |
| 206 | |
| 207 | export function addFile(filename: string, content: string): void { |
| 208 | const resolvedFilename = path.resolve(testDir, filename) |