|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import {spawnSync} from "node:child_process"; |
| 3 | +import {existsSync, mkdtempSync, mkdirSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync} from "node:fs"; |
| 4 | +import {tmpdir} from "node:os"; |
| 5 | +import {join} from "node:path"; |
| 6 | + |
| 7 | +const executable = process.env.REWATCH_EXECUTABLE; |
| 8 | +assert(executable, "REWATCH_EXECUTABLE must be set"); |
| 9 | + |
| 10 | +const executablePath = executable.endsWith(".exe") ? executable : `${executable}.exe`; |
| 11 | +const commandShim = executable.endsWith(".cmd") ? executable : `${executable}.cmd`; |
| 12 | +const resolvedExecutable = existsSync(executablePath) |
| 13 | + ? executablePath |
| 14 | + : existsSync(commandShim) |
| 15 | + ? commandShim |
| 16 | + : executable; |
| 17 | + |
| 18 | +function runBuild(buildDirectory) { |
| 19 | + if (process.platform !== "win32") { |
| 20 | + return spawnSync(executable, ["build", buildDirectory], { |
| 21 | + cwd: tmpdir(), |
| 22 | + encoding: "utf8", |
| 23 | + env: process.env, |
| 24 | + timeout: 30_000, |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + return spawnSync( |
| 29 | + process.env.ComSpec ?? "cmd.exe", |
| 30 | + [ |
| 31 | + "/d", |
| 32 | + "/c", |
| 33 | + 'cd /d "%REWATCH_TEST_PROJECT%" && "%REWATCH_TEST_EXECUTABLE%" build', |
| 34 | + ], |
| 35 | + { |
| 36 | + encoding: "utf8", |
| 37 | + env: { |
| 38 | + ...process.env, |
| 39 | + REWATCH_TEST_EXECUTABLE: resolvedExecutable, |
| 40 | + REWATCH_TEST_PROJECT: buildDirectory, |
| 41 | + }, |
| 42 | + timeout: 30_000, |
| 43 | + windowsVerbatimArguments: true, |
| 44 | + }, |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +function getBuildDirectory(projectDir) { |
| 49 | + if (process.platform !== "win32") { |
| 50 | + const symlink = `${projectDir}-link`; |
| 51 | + symlinkSync(projectDir, symlink, "dir"); |
| 52 | + return symlink; |
| 53 | + } |
| 54 | + |
| 55 | + const expandedPath = realpathSync.native(projectDir); |
| 56 | + const result = spawnSync( |
| 57 | + process.env.ComSpec ?? "cmd.exe", |
| 58 | + ["/d", "/c", 'for %I in ("%REWATCH_TEST_PROJECT%") do @echo %~sI'], |
| 59 | + { |
| 60 | + encoding: "utf8", |
| 61 | + env: {...process.env, REWATCH_TEST_PROJECT: expandedPath}, |
| 62 | + windowsVerbatimArguments: true, |
| 63 | + }, |
| 64 | + ); |
| 65 | + assert.ifError(result.error); |
| 66 | + assert.equal(result.status, 0, result.stderr); |
| 67 | + |
| 68 | + const shortPath = result.stdout.trim(); |
| 69 | + assert(shortPath, "Windows did not return a short path for the temporary project"); |
| 70 | + if (shortPath.toLowerCase() === expandedPath.toLowerCase()) { |
| 71 | + console.log("SKIP: Windows 8.3 short names are unavailable on this volume"); |
| 72 | + return null; |
| 73 | + } |
| 74 | + return shortPath; |
| 75 | +} |
| 76 | + |
| 77 | +function checkEditorCompilation(buildDirectory) { |
| 78 | + const options = {encoding: "utf8", timeout: 30_000}; |
| 79 | + const result = spawnSync( |
| 80 | + resolvedExecutable, |
| 81 | + ["compiler-args", join(buildDirectory, "src-a", "A.res")], |
| 82 | + {...options, shell: resolvedExecutable.endsWith(".cmd")}, |
| 83 | + ); |
| 84 | + assert.ifError(result.error); |
| 85 | + assert.equal(result.status, 0, result.stderr); |
| 86 | + const {compiler_args: args} = JSON.parse(result.stdout); |
| 87 | + assert.equal(args[args.indexOf("-bs-project-root") + 1], buildDirectory); |
| 88 | + |
| 89 | + // The editor compiles an absolute incremental source instead of the AST, |
| 90 | + // and omits generated-JavaScript output while retaining the GenType flags. |
| 91 | + args.splice(args.indexOf("-bs-package-output"), 2); |
| 92 | + args.pop(); |
| 93 | + const incrementalDir = join(buildDirectory, "lib", "bs", "___incremental"); |
| 94 | + mkdirSync(incrementalDir); |
| 95 | + const source = join(incrementalDir, "Editor.res"); |
| 96 | + writeFileSync(source, "@genType\nlet value = 42\n"); |
| 97 | + const compiled = spawnSync(process.env.RESCRIPT_BSC_EXE, [...args, source], { |
| 98 | + ...options, |
| 99 | + cwd: join(buildDirectory, "lib", "bs"), |
| 100 | + }); |
| 101 | + assert.ifError(compiled.error); |
| 102 | + assert.equal(compiled.status, 0, `${compiled.stdout}\n${compiled.stderr}`); |
| 103 | + assert(existsSync(join(incrementalDir, "Editor.gen.tsx"))); |
| 104 | +} |
| 105 | + |
| 106 | +const projectDir = mkdtempSync(join(tmpdir(), "rescript-gentype-root-")); |
| 107 | + |
| 108 | +try { |
| 109 | + mkdirSync(join(projectDir, "src-a")); |
| 110 | + mkdirSync(join(projectDir, "src-b", "nested"), {recursive: true}); |
| 111 | + writeFileSync( |
| 112 | + join(projectDir, "rescript.json"), |
| 113 | + `${JSON.stringify( |
| 114 | + { |
| 115 | + name: "gentype-project-root", |
| 116 | + sources: ["src-a", {dir: "src-b", subdirs: true}], |
| 117 | + gentypeconfig: {}, |
| 118 | + }, |
| 119 | + null, |
| 120 | + 2, |
| 121 | + )}\n`, |
| 122 | + ); |
| 123 | + writeFileSync(join(projectDir, "src-a", "A.res"), "@genType\ntype t = {value: int}\n"); |
| 124 | + writeFileSync( |
| 125 | + join(projectDir, "src-b", "nested", "B.res"), |
| 126 | + "@genType\nlet getValue = (record: A.t) => record.value\n", |
| 127 | + ); |
| 128 | + |
| 129 | + const buildDirectory = getBuildDirectory(projectDir); |
| 130 | + if (buildDirectory !== null) { |
| 131 | + assert(existsSync(buildDirectory), `Build directory does not exist: ${buildDirectory}`); |
| 132 | + const result = runBuild(buildDirectory); |
| 133 | + |
| 134 | + assert.ifError(result.error); |
| 135 | + assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`); |
| 136 | + assert.match( |
| 137 | + readFileSync(join(projectDir, "src-b", "nested", "B.gen.tsx"), "utf8"), |
| 138 | + /from '\.\.\/\.\.\/src-a\/A\.gen'/, |
| 139 | + ); |
| 140 | + checkEditorCompilation(buildDirectory); |
| 141 | + } |
| 142 | +} finally { |
| 143 | + rmSync(`${projectDir}-link`, {force: true}); |
| 144 | + rmSync(projectDir, {recursive: true, force: true}); |
| 145 | +} |
0 commit comments