| 1 |
set -e |
| 2 |
|
| 3 |
if [ -d debian/tests/test_modules ] && command -v pkgjs-utils >/dev/null; then |
| 4 |
pkgjs-utils link_test_modules |
| 5 |
trap 'pkgjs-utils clean_test_modules' EXIT |
| 6 |
fi |
| 7 |
|
| 8 |
node --input-type=module <<'EOF' |
| 9 |
import assert from "node:assert/strict"; |
| 10 |
import fs from "node:fs"; |
| 11 |
import fsp from "node:fs/promises"; |
| 12 |
import path from "node:path"; |
| 13 |
import { rollup } from "rollup"; |
| 14 |
import { VERSION as rollupVersionMajorMinorPatch } from "rollup"; |
| 15 |
import dts from "rollup-plugin-dts"; |
| 16 |
import ts from "typescript"; |
| 17 |
|
| 18 |
function exists(file) { |
| 19 |
return fs.existsSync(file); |
| 20 |
} |
| 21 |
|
| 22 |
function clean(code = "") { |
| 23 |
return code.trim().replace(/\n+/gm, "\n") + "\n"; |
| 24 |
} |
| 25 |
|
| 26 |
function withInput(dir, input) { |
| 27 |
if (typeof input === "string") { |
| 28 |
return path.join(dir, input); |
| 29 |
} |
| 30 |
if (Array.isArray(input)) { |
| 31 |
return input.map((entry) => path.join(dir, entry)); |
| 32 |
} |
| 33 |
return Object.fromEntries( |
| 34 |
Object.entries(input).map(([alias, entry]) => [alias, path.join(dir, entry)]), |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
function versionIsSupported(current, required) { |
| 39 |
const cur = current.split(".").map(Number); |
| 40 |
const req = required.split(".").map(Number); |
| 41 |
for (let i = 0; i < req.length; i += 1) { |
| 42 |
if ((cur[i] || 0) > req[i]) return true; |
| 43 |
if ((cur[i] || 0) < req[i]) return false; |
| 44 |
} |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
async function createBundle(options, rollupOptions) { |
| 49 |
const bundle = await rollup({ |
| 50 |
...rollupOptions, |
| 51 |
plugins: [dts(options)], |
| 52 |
onwarn() {}, |
| 53 |
}); |
| 54 |
try { |
| 55 |
return await bundle.generate({ |
| 56 |
...rollupOptions.output, |
| 57 |
format: "es", |
| 58 |
sourcemap: false, |
| 59 |
sourcemapExcludeSources: true, |
| 60 |
}); |
| 61 |
} finally { |
| 62 |
await bundle.close(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
const fixturesRoot = path.resolve("tests/testcases"); |
| 67 |
let failures = 0; |
| 68 |
|
| 69 |
for (const name of fs.readdirSync(fixturesRoot).sort()) { |
| 70 |
const dir = path.join(fixturesRoot, name); |
| 71 |
if (!fs.statSync(dir).isDirectory()) continue; |
| 72 |
|
| 73 |
const rollupOptions = { |
| 74 |
input: exists(path.join(dir, "index.d.ts")) ? "index.d.ts" : "index.ts", |
| 75 |
}; |
| 76 |
const meta = { |
| 77 |
options: {}, |
| 78 |
skip: false, |
| 79 |
rollupOptions, |
| 80 |
}; |
| 81 |
|
| 82 |
try { |
| 83 |
Object.assign(meta, (await import(`file://${path.join(dir, "meta.js")}`)).default); |
| 84 |
meta.rollupOptions = { ...rollupOptions, ...meta.rollupOptions }; |
| 85 |
} catch {} |
| 86 |
|
| 87 |
if (meta.skip) continue; |
| 88 |
if (meta.tsVersion && !versionIsSupported(ts.versionMajorMinor, meta.tsVersion)) continue; |
| 89 |
if (meta.rollupVersion && !versionIsSupported(rollupVersionMajorMinorPatch, meta.rollupVersion)) continue; |
| 90 |
|
| 91 |
process.stdout.write(`testcases/${name}... `); |
| 92 |
try { |
| 93 |
const input = withInput(dir, meta.rollupOptions.input); |
| 94 |
let output; |
| 95 |
let error; |
| 96 |
|
| 97 |
try { |
| 98 |
({ output } = await createBundle(meta.options, { ...meta.rollupOptions, input })); |
| 99 |
} catch (e) { |
| 100 |
error = e; |
| 101 |
if (!meta.expectedError) throw e; |
| 102 |
} |
| 103 |
|
| 104 |
if (meta.expectedError) { |
| 105 |
assert.equal(error.message, meta.expectedError); |
| 106 |
console.log("ok"); |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
const hasMultipleOutputs = output.length > 1; |
| 111 |
let code = clean(output[0].code); |
| 112 |
if (hasMultipleOutputs) { |
| 113 |
code = ""; |
| 114 |
for (const file of output) { |
| 115 |
if (file.type === "chunk") { |
| 116 |
code += `// ${file.fileName}\n${clean(file.code)}`; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
assert.equal(code, await fsp.readFile(path.join(dir, "expected.d.ts"), "utf8")); |
| 122 |
console.log("ok"); |
| 123 |
} catch (error) { |
| 124 |
failures += 1; |
| 125 |
console.log("failed"); |
| 126 |
console.error(error.stack || error); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (failures) { |
| 131 |
throw new Error(`${failures} test case(s) failed`); |
| 132 |
} |
| 133 |
EOF |