|
| 1 | +import * as assert from 'node:assert' |
| 2 | +import * as path from 'node:path' |
| 3 | +import type { Subscription } from 'rxjs' |
| 4 | +import { workspace } from 'vscode' |
| 5 | +import { URI } from 'vscode-uri' |
| 6 | +import { getWatcher$ } from '../../watcher' |
| 7 | +import { TEST_ASSETS_PATH, waitFor } from './test-utils' |
| 8 | + |
| 9 | +suite('watcher', () => { |
| 10 | + suite('getWatcher$', () => { |
| 11 | + const test_folder = path.resolve(TEST_ASSETS_PATH, 'watcher-test') |
| 12 | + const watcher$ = getWatcher$(`${test_folder}/**/*.txt`) |
| 13 | + |
| 14 | + let subscription: Subscription |
| 15 | + let results: URI[] |
| 16 | + |
| 17 | + suite('when file is created', () => { |
| 18 | + const filename = path.resolve(test_folder, 'create.txt') |
| 19 | + const content = 'test' |
| 20 | + |
| 21 | + setup(async () => { |
| 22 | + results = [] |
| 23 | + await forceDeleteFile(filename) |
| 24 | + |
| 25 | + subscription = watcher$.subscribe(v => results.push(v)) |
| 26 | + await workspace.fs.writeFile(URI.file(filename), Buffer.from(content)) |
| 27 | + }) |
| 28 | + |
| 29 | + teardown(async () => { |
| 30 | + subscription.unsubscribe() |
| 31 | + await forceDeleteFile(filename) |
| 32 | + }) |
| 33 | + |
| 34 | + test('emits a value', async () => |
| 35 | + waitFor(async () => { |
| 36 | + assert.ok(results[0]) |
| 37 | + const readResult = await workspace.fs.readFile(results[0]) |
| 38 | + assert.strictEqual(readResult.toString(), content) |
| 39 | + })) |
| 40 | + }) |
| 41 | + |
| 42 | + suite('when file is changed', () => { |
| 43 | + const filename = path.resolve(test_folder, 'change.txt') |
| 44 | + const newContent = getRandomString() |
| 45 | + |
| 46 | + let originalContent: string |
| 47 | + |
| 48 | + setup(async () => { |
| 49 | + originalContent = ( |
| 50 | + await workspace.fs.readFile(URI.file(filename)) |
| 51 | + ).toString() |
| 52 | + results = [] |
| 53 | + subscription = watcher$.subscribe(v => results.push(v)) |
| 54 | + await workspace.fs.writeFile( |
| 55 | + URI.file(filename), |
| 56 | + Buffer.from(newContent), |
| 57 | + ) |
| 58 | + }) |
| 59 | + |
| 60 | + teardown(async () => { |
| 61 | + subscription.unsubscribe() |
| 62 | + await workspace.fs.writeFile( |
| 63 | + URI.file(filename), |
| 64 | + Buffer.from(originalContent), |
| 65 | + ) |
| 66 | + }) |
| 67 | + |
| 68 | + test('emits a value', async () => |
| 69 | + waitFor(async () => { |
| 70 | + assert.ok(results[0]) |
| 71 | + const readResult = await workspace.fs.readFile(results[0]) |
| 72 | + assert.strictEqual(readResult.toString(), newContent) |
| 73 | + })) |
| 74 | + }) |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +async function forceDeleteFile(filename: string) { |
| 79 | + try { |
| 80 | + await workspace.fs.delete(URI.file(filename)) |
| 81 | + } catch { |
| 82 | + // ignore error |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function getRandomString() { |
| 87 | + // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 88 | + return Math.random().toString(36).slice(2) |
| 89 | +} |
0 commit comments