|
1 | 1 | import { deepEqual, equal, match, notEqual } from 'node:assert/strict'; |
2 | 2 | import { EventEmitter, once } from 'node:events'; |
3 | 3 | import { WriteStream, createWriteStream } from 'node:fs'; |
4 | | -import { mkdtemp, rm, unlink, writeFile } from 'node:fs/promises'; |
| 4 | +import { mkdtemp, rm, unlink, writeFile, truncate } from 'node:fs/promises'; |
5 | 5 | import { EOL, platform, tmpdir } from 'node:os'; |
6 | 6 | import { join } from 'node:path'; |
7 | 7 | import { setTimeout } from 'node:timers/promises'; |
@@ -177,4 +177,41 @@ suite('WatchFileCommand', function () { |
177 | 177 | const actual = getFilenames(); |
178 | 178 | deepEqual(actual, expected); |
179 | 179 | }); |
| 180 | + |
| 181 | + |
| 182 | + test('file truncation', async function () { |
| 183 | + const fname = join(tmpDir, '0006.txt'); |
| 184 | + |
| 185 | + const expectedContent = '0123456789'; |
| 186 | + await writeFile(fname, expectedContent); |
| 187 | + |
| 188 | + const [editor, emitter] = await Promise.all([ |
| 189 | + waitForOutputWindow(`extension-output-`), |
| 190 | + commands.executeCommand<EventEmitter>('logwatcher.watchFile', fname), |
| 191 | + ]); |
| 192 | + |
| 193 | + notEqual(editor, undefined); |
| 194 | + notEqual(emitter, undefined); |
| 195 | + |
| 196 | + if (!editor.document.getText()) { |
| 197 | + await waitForVisibleRangesChange(editor); |
| 198 | + } |
| 199 | + |
| 200 | + let actual = editor.document.getText(); |
| 201 | + equal(actual, expectedContent); |
| 202 | + |
| 203 | + const waitForVisibleRangesChangePromise = waitForVisibleRangesChange(editor); |
| 204 | + const onceFileChangedPromise = once(emitter, 'fileChanged'); |
| 205 | + |
| 206 | + const expectedSize = 5; |
| 207 | + await truncate(fname, expectedSize); |
| 208 | + |
| 209 | + await Promise.all([ |
| 210 | + waitForVisibleRangesChangePromise, |
| 211 | + onceFileChangedPromise, |
| 212 | + ]); |
| 213 | + |
| 214 | + actual = editor.document.getText(); |
| 215 | + equal(actual, expectedContent + expectedContent.slice(0, expectedSize)); |
| 216 | + }); |
180 | 217 | }); |
0 commit comments