|
| 1 | +import { test } from "node:test"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { glob } from "glob"; |
| 5 | +import { spawn, spawnSync } from "node:child_process"; |
| 6 | + |
| 7 | +// Get the current ReScript version |
| 8 | +const rescriptVersion = spawnSync("./node_modules/.bin/bsc", ["-v"]) |
| 9 | + .stdout.toString() |
| 10 | + .trim() |
| 11 | + .replace("ReScript ", ""); |
| 12 | + |
| 13 | +const testFilesDir = path.join(import.meta.dirname, "./test_files"); |
| 14 | +const buildDir = path.join(import.meta.dirname, "./test_files/.build"); |
| 15 | +const incrementalDir = path.join( |
| 16 | + import.meta.dirname, |
| 17 | + "./lib/bs/___incremental" |
| 18 | +); |
| 19 | + |
| 20 | +// Recreate directories needed |
| 21 | +try { |
| 22 | + await fs.access(incrementalDir); |
| 23 | + await fs.rm(incrementalDir, { recursive: true }); |
| 24 | +} catch (_) {} |
| 25 | +await fs.mkdir(incrementalDir, { recursive: true }); |
| 26 | + |
| 27 | +try { |
| 28 | + await fs.access(buildDir); |
| 29 | + await fs.rm(buildDir, { recursive: true }); |
| 30 | +} catch (_) {} |
| 31 | +await fs.mkdir(buildDir, { recursive: true }); |
| 32 | + |
| 33 | +const resFiles = await glob("**/*.res", { |
| 34 | + cwd: testFilesDir, |
| 35 | + absolute: true, |
| 36 | +}).then((files) => |
| 37 | + files.map((file) => ({ |
| 38 | + absolutePath: file, |
| 39 | + relativePath: path.relative(testFilesDir, file), |
| 40 | + })) |
| 41 | +); |
| 42 | + |
| 43 | +// Function to split test file contents into blocks |
| 44 | +const splitTestBlocks = (contents) => { |
| 45 | + const testBlocks = contents.split(/\/\/ == TEST:/); |
| 46 | + // Skip the first empty block if it exists |
| 47 | + return testBlocks.slice(1).map((block) => { |
| 48 | + const [description, ...rest] = block.split("\n"); |
| 49 | + return { |
| 50 | + description: description.trim(), |
| 51 | + content: rest.join("\n").trim(), |
| 52 | + }; |
| 53 | + }); |
| 54 | +}; |
| 55 | + |
| 56 | +const testBlocksPerFile = new Map(); |
| 57 | + |
| 58 | +const baseCommand = `./node_modules/.bin/bsc -I lib/bs/support_files -I node_modules/@rescript/react/lib/ocaml -editor-mode -ignore-parse-errors -color never -bs-package-name test -bs-package-output esmodule:test.res`; |
| 59 | + |
| 60 | +// Compile all files and move incremental cmt's |
| 61 | +await Promise.all( |
| 62 | + resFiles.map(async (file) => { |
| 63 | + const contents = await fs.readFile(file.absolutePath, "utf-8"); |
| 64 | + const testBlocks = splitTestBlocks(contents); |
| 65 | + |
| 66 | + let blockIndex = 1; |
| 67 | + const blockData = []; |
| 68 | + |
| 69 | + for (const block of testBlocks) { |
| 70 | + const { description, content } = block; |
| 71 | + const filePath = path.join( |
| 72 | + buildDir, |
| 73 | + `${file.relativePath.slice(0, -4)}_${blockIndex}.res` |
| 74 | + ); |
| 75 | + |
| 76 | + const fileContent = `// ${description}\n${content}`; |
| 77 | + |
| 78 | + await fs.writeFile(filePath, fileContent); |
| 79 | + |
| 80 | + const command = `${baseCommand} ${filePath}`; |
| 81 | + const [cmd, ...args] = command.split(" "); |
| 82 | + |
| 83 | + const _debugData = await new Promise((resolve) => { |
| 84 | + const child = spawn(cmd, args); |
| 85 | + |
| 86 | + let stdout = ""; |
| 87 | + let stderr = ""; |
| 88 | + |
| 89 | + child.stdout.on("data", (chunk) => { |
| 90 | + stdout += chunk; |
| 91 | + }); |
| 92 | + |
| 93 | + child.stderr.on("data", (chunk) => { |
| 94 | + stderr += chunk; |
| 95 | + }); |
| 96 | + |
| 97 | + child.on("close", () => { |
| 98 | + resolve({ stdout, stderr }); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + // Move .cmt file to incremental directory |
| 103 | + const cmtPath = filePath.replace(".res", ".cmt"); |
| 104 | + const cmtFileName = path.basename(cmtPath); |
| 105 | + const targetPath = path.join(incrementalDir, cmtFileName); |
| 106 | + await fs.rename(cmtPath, targetPath); |
| 107 | + |
| 108 | + blockData.push({ filePath, description, fileContent }); |
| 109 | + blockIndex++; |
| 110 | + } |
| 111 | + |
| 112 | + testBlocksPerFile.set(file.relativePath, blockData); |
| 113 | + }) |
| 114 | +); |
| 115 | + |
| 116 | +resFiles.forEach((file) => { |
| 117 | + const blockData = testBlocksPerFile.get(file.relativePath); |
| 118 | + for (const block of blockData) { |
| 119 | + test(`${file.relativePath} - ${block.description}`, async (t) => { |
| 120 | + // Run rescript-editor-analysis and capture output |
| 121 | + const analysisOutput = await new Promise((resolve, reject) => { |
| 122 | + const analysisCmd = spawn( |
| 123 | + "../../../_build/install/default/bin/rescript-editor-analysis", |
| 124 | + ["test_revamped", block.filePath, "rescript.test.json"], |
| 125 | + { |
| 126 | + stdio: "pipe", |
| 127 | + env: { |
| 128 | + RESCRIPT_INCREMENTAL_TYPECHECKING: "true", |
| 129 | + RESCRIPT_PROJECT_CONFIG_CACHE: "true", |
| 130 | + RESCRIPT_VERSION: rescriptVersion, |
| 131 | + }, |
| 132 | + } |
| 133 | + ); |
| 134 | + |
| 135 | + let stdout = ""; |
| 136 | + let stderr = ""; |
| 137 | + |
| 138 | + analysisCmd.stdout.on("data", (data) => { |
| 139 | + stdout += data.toString(); |
| 140 | + }); |
| 141 | + |
| 142 | + analysisCmd.stderr.on("data", (data) => { |
| 143 | + stderr += data.toString(); |
| 144 | + }); |
| 145 | + |
| 146 | + analysisCmd.on("close", (code) => { |
| 147 | + if (code === 0) { |
| 148 | + resolve({ stdout, stderr }); |
| 149 | + } else { |
| 150 | + reject(new Error(`Analysis command failed with code ${code}`)); |
| 151 | + console.error(stderr); |
| 152 | + } |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + t.assert.snapshot(analysisOutput.stdout); |
| 157 | + }); |
| 158 | + } |
| 159 | +}); |
0 commit comments