|
| 1 | +import { Expect, Setup, Teardown, Test, TestCase } from "alsatian"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import { execCommandLine } from "../../src/Compiler"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Find all files inside a dir, recursively. |
| 8 | + */ |
| 9 | +function getAllFiles(dir: string): string[] { |
| 10 | + return fs.readdirSync(dir).reduce( |
| 11 | + (files, file) => { |
| 12 | + const name = path.join(dir, file); |
| 13 | + const isDirectory = fs.statSync(name).isDirectory(); |
| 14 | + return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name]; |
| 15 | + }, |
| 16 | + [] |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +export class CompilerTests { |
| 21 | + |
| 22 | + private existingFiles: string[]; |
| 23 | + private filesAfterCompile: string[]; |
| 24 | + |
| 25 | + @Setup |
| 26 | + public setup() { |
| 27 | + this.existingFiles = getAllFiles(path.resolve(__dirname, "./project")); |
| 28 | + this.filesAfterCompile = []; |
| 29 | + } |
| 30 | + |
| 31 | + @TestCase("tsconfig.default.json", |
| 32 | + "typescript_lualib.lua", |
| 33 | + "test_src/test_lib/file.lua", |
| 34 | + "test_src/main.lua") |
| 35 | + @TestCase("tsconfig.outDir.json", |
| 36 | + "out_dir/typescript_lualib.lua", |
| 37 | + "out_dir/test_src/test_lib/file.lua", |
| 38 | + "out_dir/test_src/main.lua") |
| 39 | + @TestCase("tsconfig.rootDir.json", |
| 40 | + "test_src/typescript_lualib.lua", |
| 41 | + "test_src/test_lib/file.lua", |
| 42 | + "test_src/main.lua") |
| 43 | + @TestCase("tsconfig.bothDirOptions.json", |
| 44 | + "out_dir/typescript_lualib.lua", |
| 45 | + "out_dir/test_lib/file.lua", |
| 46 | + "out_dir/main.lua") |
| 47 | + @Test("Compile project") |
| 48 | + public compileProject(tsconfig: string, ...expectedFiles: string[]) { |
| 49 | + const tsconfigPath = path.resolve(__dirname, "project", tsconfig); |
| 50 | + |
| 51 | + // Compile project |
| 52 | + execCommandLine(["-p", tsconfigPath]); |
| 53 | + |
| 54 | + this.filesAfterCompile = getAllFiles(path.resolve(__dirname, "project")); |
| 55 | + expectedFiles = expectedFiles.map((relPath) => path.resolve(__dirname, "project", relPath)); |
| 56 | + expectedFiles.push(...this.existingFiles); |
| 57 | + |
| 58 | + for (const existingFile of this.filesAfterCompile) { |
| 59 | + Expect(expectedFiles).toContain(existingFile); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Teardown |
| 64 | + public teardown() { |
| 65 | + // Remove files taht were created by the test |
| 66 | + const createdFiles = this.filesAfterCompile.filter((v) => this.existingFiles.indexOf(v) < 0); |
| 67 | + for (const file of createdFiles) { |
| 68 | + fs.unlinkSync(file); |
| 69 | + } |
| 70 | + this.existingFiles = []; |
| 71 | + this.filesAfterCompile = []; |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments