|
| 1 | +import * as path from "path"; |
| 2 | +import * as ts from "typescript"; |
| 3 | +import { getSourceDir } from "../../src"; |
| 4 | +import * as util from "../util"; |
| 5 | + |
| 6 | +const cwd = process.cwd(); |
| 7 | + |
| 8 | +// Path for project tsconfig.json to resolve for |
| 9 | +const configFilePath = path.join(cwd, "tsconfig.json"); |
| 10 | + |
| 11 | +describe("getSourceDir", () => { |
| 12 | + test("with rootDir", () => { |
| 13 | + const program = ts.createProgram(["main.ts", "src/otherfile.ts"], { configFilePath, rootDir: "src" }); |
| 14 | + |
| 15 | + // getCommonSourceDirectory does not work right so mock it |
| 16 | + jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd); |
| 17 | + |
| 18 | + expect(getSourceDir(program)).toBe(path.join(cwd, "src")); |
| 19 | + }); |
| 20 | + |
| 21 | + test("without rootDir", () => { |
| 22 | + const program = ts.createProgram(["main.ts", "src/otherfile.ts"], { configFilePath }); |
| 23 | + |
| 24 | + // getCommonSourceDirectory does not work right so mock it |
| 25 | + jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(cwd); |
| 26 | + |
| 27 | + // Common sources directory is project root |
| 28 | + expect(normalize(getSourceDir(program))).toBe(cwd); |
| 29 | + }); |
| 30 | + |
| 31 | + test("without rootDir in src dir", () => { |
| 32 | + const program = ts.createProgram([path.join(cwd, "src", "main.ts"), path.join(cwd, "src", "otherfile.ts")], { |
| 33 | + configFilePath, |
| 34 | + }); |
| 35 | + |
| 36 | + // getCommonSourceDirectory does not work right so mock it |
| 37 | + jest.spyOn(program, "getCommonSourceDirectory").mockReturnValue(path.join(cwd, "src")); |
| 38 | + |
| 39 | + // Common sources directory is src |
| 40 | + expect(normalize(getSourceDir(program))).toBe(path.join(cwd, "src")); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe("getEmitPath", () => { |
| 45 | + test("puts files next to input without options", () => { |
| 46 | + const { transpiledFiles } = util.testModule`` |
| 47 | + .setMainFileName("main.ts") |
| 48 | + .addExtraFile("dir/extra.ts", "") |
| 49 | + .expectToHaveNoDiagnostics() |
| 50 | + .getLuaResult(); |
| 51 | + |
| 52 | + const fileNames = transpiledFiles.map(f => f.outPath); |
| 53 | + expect(fileNames).toContain("main.lua"); |
| 54 | + expect(fileNames).toContain(path.join("dir", "extra.lua")); |
| 55 | + }); |
| 56 | + |
| 57 | + test("puts files in outdir", () => { |
| 58 | + const outDir = path.join(cwd, "tstl-out"); |
| 59 | + const { transpiledFiles } = util.testModule`` |
| 60 | + .setMainFileName("main.ts") |
| 61 | + .addExtraFile("dir/extra.ts", "") |
| 62 | + .setOptions({ outDir }) |
| 63 | + .expectToHaveNoDiagnostics() |
| 64 | + .getLuaResult(); |
| 65 | + |
| 66 | + const fileNames = transpiledFiles.map(f => f.outPath); |
| 67 | + expect(fileNames).toContain(path.join(outDir, "main.lua")); |
| 68 | + expect(fileNames).toContain(path.join(outDir, "dir", "extra.lua")); |
| 69 | + }); |
| 70 | + |
| 71 | + test("puts files from rootDir in outdir", () => { |
| 72 | + const outDir = path.join(cwd, "tstl-out"); |
| 73 | + const { transpiledFiles } = util.testModule`` |
| 74 | + .setMainFileName("src/main.ts") |
| 75 | + .addExtraFile("src/extra.ts", "") |
| 76 | + .setOptions({ rootDir: "src", outDir }) |
| 77 | + .expectToHaveNoDiagnostics() |
| 78 | + .getLuaResult(); |
| 79 | + |
| 80 | + const fileNames = transpiledFiles.map(f => f.outPath); |
| 81 | + expect(fileNames).toContain(path.join(outDir, "main.lua")); |
| 82 | + expect(fileNames).toContain(path.join(outDir, "extra.lua")); |
| 83 | + }); |
| 84 | + |
| 85 | + test("puts bundle relative to project root", () => { |
| 86 | + const { transpiledFiles } = util.testModule`` |
| 87 | + .setMainFileName("src/main.ts") |
| 88 | + .addExtraFile("src/extra.ts", "") |
| 89 | + .setOptions({ configFilePath, rootDir: "src", luaBundle: "out/bundle.lua", luaBundleEntry: "src/main.ts" }) |
| 90 | + .expectToHaveNoDiagnostics() |
| 91 | + .getLuaResult(); |
| 92 | + |
| 93 | + const fileNames = transpiledFiles.map(f => f.outPath); |
| 94 | + expect(fileNames).toHaveLength(1); |
| 95 | + expect(fileNames).toContain(path.join(cwd, "out", "bundle.lua")); |
| 96 | + }); |
| 97 | + |
| 98 | + test("puts bundle relative to outdir", () => { |
| 99 | + const { transpiledFiles } = util.testModule`` |
| 100 | + .setMainFileName("src/main.ts") |
| 101 | + .addExtraFile("src/extra.ts", "") |
| 102 | + .setOptions({ |
| 103 | + configFilePath, |
| 104 | + rootDir: "src", |
| 105 | + outDir: "out1", |
| 106 | + luaBundle: "out2/bundle.lua", |
| 107 | + luaBundleEntry: "src/main.ts", |
| 108 | + }) |
| 109 | + .expectToHaveNoDiagnostics() |
| 110 | + .getLuaResult(); |
| 111 | + |
| 112 | + const fileNames = transpiledFiles.map(f => f.outPath); |
| 113 | + expect(fileNames).toHaveLength(1); |
| 114 | + expect(fileNames).toContain(path.join(cwd, "out1", "out2", "bundle.lua")); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +function normalize(path: string) { |
| 119 | + return path.endsWith("/") ? path.slice(0, path.length - 1) : path; |
| 120 | +} |
0 commit comments