|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import * as assert from "node:assert/strict"; |
| 4 | +import * as fs from "node:fs/promises"; |
| 5 | +import * as path from "node:path"; |
| 6 | +import { pathToFileURL } from "node:url"; |
| 7 | +import { setup } from "#dev/process"; |
| 8 | + |
| 9 | +const root = import.meta.dirname; |
| 10 | +const { bsc } = setup(root); |
| 11 | +const outputDir = path.join(root, "lib"); |
| 12 | + |
| 13 | +/** |
| 14 | + * @param {string} directory |
| 15 | + * @param {string} source |
| 16 | + * @param {string[]} includes |
| 17 | + * @param {string[]} [extraFlags] |
| 18 | + */ |
| 19 | +async function compile(directory, source, includes, extraFlags = []) { |
| 20 | + const output = path.join(outputDir, directory); |
| 21 | + const stem = path.basename(source, path.extname(source)); |
| 22 | + await fs.mkdir(output, { recursive: true }); |
| 23 | + const result = await bsc([ |
| 24 | + "-bs-project-root", |
| 25 | + root, |
| 26 | + "-bs-package-name", |
| 27 | + "option_transitive_dependency", |
| 28 | + "-bs-package-output", |
| 29 | + `esmodule:lib/${directory}:.mjs`, |
| 30 | + ...includes.flatMap(include => ["-I", path.join(outputDir, include)]), |
| 31 | + ...extraFlags, |
| 32 | + "-o", |
| 33 | + path.join(output, `${stem}.${source.endsWith(".resi") ? "cmi" : "cmj"}`), |
| 34 | + path.join(root, directory, source), |
| 35 | + ]); |
| 36 | + assert.equal(result.status, 0, `${source}: ${result.stdout}${result.stderr}`); |
| 37 | +} |
| 38 | + |
| 39 | +await fs.rm(outputDir, { recursive: true, force: true }); |
| 40 | + |
| 41 | +try { |
| 42 | + await compile("dep", "A.resi", ["dep"]); |
| 43 | + await compile("dep", "A.res", ["dep"], ["-bs-read-cmi"]); |
| 44 | + await compile("facade", "Facade.res", ["dep", "facade"]); |
| 45 | + // The consumer can load Facade, but not the dependency declaring its types. |
| 46 | + await compile("app", "Main.res", ["facade", "app"]); |
| 47 | + |
| 48 | + const main = await import( |
| 49 | + pathToFileURL(path.join(outputDir, "app", "Main.mjs")).href |
| 50 | + ); |
| 51 | + const facade = await import( |
| 52 | + pathToFileURL(path.join(outputDir, "facade", "Facade.mjs")).href |
| 53 | + ); |
| 54 | + assert.equal(main.variant, facade.variant); |
| 55 | + assert.deepEqual(main.record, { value: 42 }); |
| 56 | + assert.equal(main.alias, 42); |
| 57 | + |
| 58 | + // Some(undefined) must remain distinct from None, including optional args. |
| 59 | + for (const field of [ |
| 60 | + "abstractIsSome", |
| 61 | + "unboxedIsSome", |
| 62 | + "abstractRoundTrip", |
| 63 | + "unboxedRoundTrip", |
| 64 | + "optionalAbstractIsSome", |
| 65 | + "optionalUnboxedIsSome", |
| 66 | + "optionalAbstractRoundTrip", |
| 67 | + "optionalUnboxedRoundTrip", |
| 68 | + ]) { |
| 69 | + assert.equal(main[field], true, field); |
| 70 | + } |
| 71 | + assert.equal(main.omittedAbstract, undefined); |
| 72 | + assert.equal(main.omittedUnboxed, undefined); |
| 73 | +} finally { |
| 74 | + await fs.rm(outputDir, { recursive: true, force: true }); |
| 75 | +} |
0 commit comments