|
| 1 | +import { AsyncTest, Expect, Setup, Timeout } from "alsatian"; |
| 2 | +import { fork } from "child_process"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as path from "path"; |
| 5 | + |
| 6 | +export class CompilerWatchModeTest { |
| 7 | + |
| 8 | + private singleFilePath: string; |
| 9 | + private singleFilePathOut: string; |
| 10 | + |
| 11 | + @AsyncTest("Watch single File") |
| 12 | + @Timeout(10000) |
| 13 | + public async testSingle(): Promise<void> { |
| 14 | + // spawn watcher in different thread, that way we can just terminate it after test are completed |
| 15 | + const child = fork(path.join(__dirname, "watcher_proccess.ts")); |
| 16 | + child.send(["-w", this.singleFilePath]); |
| 17 | + |
| 18 | + await this.waitForFileExists(this.singleFilePathOut, 4000) |
| 19 | + .catch(err => console.error(err)); |
| 20 | + |
| 21 | + Expect(fs.existsSync(this.singleFilePathOut)).toBe(true); |
| 22 | + |
| 23 | + const initialResultLua = fs.readFileSync(this.singleFilePathOut); |
| 24 | + const originalTS = fs.readFileSync(this.singleFilePath); |
| 25 | + |
| 26 | + fs.unlinkSync(this.singleFilePathOut); |
| 27 | + |
| 28 | + fs.writeFileSync(this.singleFilePath, "class MyTest2 {}"); |
| 29 | + |
| 30 | + await this.waitForFileExists(this.singleFilePathOut) |
| 31 | + .catch(err => console.error(err)); |
| 32 | + |
| 33 | + const updatedResultLua = fs.readFileSync(this.singleFilePathOut).toString(); |
| 34 | + |
| 35 | + Expect(initialResultLua).not.toEqual(updatedResultLua); |
| 36 | + |
| 37 | + fs.writeFileSync(this.singleFilePath, originalTS); |
| 38 | + |
| 39 | + fs.unlinkSync(this.singleFilePathOut); |
| 40 | + |
| 41 | + child.kill(); |
| 42 | + } |
| 43 | + |
| 44 | + @Setup |
| 45 | + private setup(): void { |
| 46 | + this.singleFilePath = path.join(__dirname, "./testfiles/watch_single.ts"); |
| 47 | + this.singleFilePathOut = path.join(__dirname, "./testfiles/watch_single.lua"); |
| 48 | + } |
| 49 | + |
| 50 | + private waitForFileExists(filepath: string, timeout: number = 3000): Promise<void> { |
| 51 | + const interval = 200; |
| 52 | + return new Promise((resolve, reject) => { |
| 53 | + const intervalTimerId = setInterval( |
| 54 | + () => { |
| 55 | + if (fs.existsSync(filepath)) { |
| 56 | + clearTimeout(timeoutId); |
| 57 | + clearInterval(intervalTimerId); |
| 58 | + resolve(); |
| 59 | + } |
| 60 | + }, |
| 61 | + interval); |
| 62 | + |
| 63 | + const timeoutId = setTimeout( |
| 64 | + () => { |
| 65 | + clearInterval(intervalTimerId); |
| 66 | + reject(new Error("Wating for file timed out!")); |
| 67 | + }, |
| 68 | + timeout); |
| 69 | + }); |
| 70 | + } |
| 71 | +} |
0 commit comments