|
| 1 | +import * as fs from "fs-extra"; |
| 2 | +import * as os from "os"; |
| 3 | +import * as path from "path"; |
| 4 | +import { locateConfigFile } from "../../src/CommandLineParser"; |
| 5 | +import { normalizeSlashes } from "../../src/utils"; |
| 6 | + |
| 7 | +let temp: string; |
| 8 | +beforeEach(async () => { |
| 9 | + temp = await fs.mkdtemp(path.join(os.tmpdir(), "tstl-test-")); |
| 10 | + process.chdir(temp); |
| 11 | +}); |
| 12 | + |
| 13 | +afterEach(async () => { |
| 14 | + // TODO [node@12]: `rmdir` has `recursive` option |
| 15 | + await fs.remove(temp); |
| 16 | +}); |
| 17 | + |
| 18 | +const locate = (project: string | undefined, fileNames: string[] = []) => |
| 19 | + locateConfigFile({ errors: [], fileNames, options: { project } }); |
| 20 | + |
| 21 | +const normalize = (name: string) => normalizeSlashes(path.resolve(temp, name)); |
| 22 | + |
| 23 | +describe("specified", () => { |
| 24 | + for (const separator of process.platform === "win32" ? ["/", "\\"] : ["/"]) { |
| 25 | + for (const pointsTo of ["file", "directory"] as const) { |
| 26 | + const findAndExpect = (project: string, expected: string) => { |
| 27 | + project = project.replace(/[\\/]/g, separator); |
| 28 | + if (pointsTo === "directory") { |
| 29 | + project = path.dirname(project); |
| 30 | + } |
| 31 | + |
| 32 | + expect(locate(project)).toBe(normalize(expected)); |
| 33 | + }; |
| 34 | + |
| 35 | + test(`relative to ${pointsTo} separated with '${separator}'`, async () => { |
| 36 | + await fs.outputFile("tsconfig.json", ""); |
| 37 | + await fs.mkdir("src"); |
| 38 | + process.chdir("src"); |
| 39 | + findAndExpect("../tsconfig.json", "tsconfig.json"); |
| 40 | + }); |
| 41 | + |
| 42 | + test(`absolute to ${pointsTo} separated with '${separator}'`, async () => { |
| 43 | + await fs.outputFile("tsconfig.json", ""); |
| 44 | + findAndExpect(path.resolve("tsconfig.json"), "tsconfig.json"); |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + test.each(["", ".", "./"])("current directory (%p)", async () => { |
| 50 | + await fs.outputFile("tsconfig.json", ""); |
| 51 | + expect(locate(".")).toBe(normalize("tsconfig.json")); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe("inferred", () => { |
| 56 | + test("in current directory", async () => { |
| 57 | + await fs.outputFile("tsconfig.json", ""); |
| 58 | + expect(locate(undefined)).toBe(normalize("tsconfig.json")); |
| 59 | + }); |
| 60 | + |
| 61 | + test("in parent directory", async () => { |
| 62 | + await fs.outputFile("tsconfig.json", ""); |
| 63 | + await fs.mkdir("src"); |
| 64 | + process.chdir("src"); |
| 65 | + expect(locate(undefined)).toBe(normalize("tsconfig.json")); |
| 66 | + }); |
| 67 | + |
| 68 | + test("not found", () => { |
| 69 | + expect(locate(undefined)).toBe(undefined); |
| 70 | + }); |
| 71 | + |
| 72 | + test("does not attempt when has files", async () => { |
| 73 | + await fs.outputFile("tsconfig.json", ""); |
| 74 | + expect(locate(undefined, [""])).toBe(undefined); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("errors", () => { |
| 79 | + test("specified file does not exist", () => { |
| 80 | + expect([locate("tsconfig.json")]).toHaveDiagnostics(); |
| 81 | + }); |
| 82 | + |
| 83 | + test("specified directory does not exist", () => { |
| 84 | + expect([locate("project")]).toHaveDiagnostics(); |
| 85 | + }); |
| 86 | + |
| 87 | + test("cannot be mixed", async () => { |
| 88 | + await fs.outputFile("tsconfig.json", ""); |
| 89 | + expect([locate("tsconfig.json", [""])]).toHaveDiagnostics(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments