diff options
author | 2023-03-08 11:38:09 -0800 | |
---|---|---|
committer | 2023-03-08 11:38:09 -0800 | |
commit | ab02ab25b1ea37f2861099f91e3783591e08efdd (patch) | |
tree | b8a51acdc8ce63c01cdaa13d0a10b3c6d37ebb54 /test/js/deno/scripts/postinstall.ts | |
parent | deb7a2b19225265ad8f847da300f9f6db7c5e8b3 (diff) | |
download | bun-ab02ab25b1ea37f2861099f91e3783591e08efdd.tar.gz bun-ab02ab25b1ea37f2861099f91e3783591e08efdd.tar.zst bun-ab02ab25b1ea37f2861099f91e3783591e08efdd.zip |
Improve test harness
Diffstat (limited to 'test/js/deno/scripts/postinstall.ts')
-rw-r--r-- | test/js/deno/scripts/postinstall.ts | 96 |
1 files changed, 86 insertions, 10 deletions
diff --git a/test/js/deno/scripts/postinstall.ts b/test/js/deno/scripts/postinstall.ts index bca1e6582..65fd5a04e 100644 --- a/test/js/deno/scripts/postinstall.ts +++ b/test/js/deno/scripts/postinstall.ts @@ -1,26 +1,102 @@ import { mkdirSync } from "node:fs"; import { join, dirname } from "node:path"; +import { parse, print } from "@swc/core"; +import type { ImportDeclaration, ExpressionStatement, CallExpression } from "@swc/core"; import imports from "../resources/imports.json"; import tests from "../resources/tests.json"; +// FIXME: https://github.com/oven-sh/bun/issues/2350 +// import * as harness from "deno:harness"; + for (const test of tests) { const path = join(import.meta.dir, "..", test.path); const url = new URL(test.remotePath, "https://raw.githubusercontent.com/denoland/deno/main/cli/tests/"); const response = await fetch(url); console.log(response.status, url.toString(), "->", test.path); if (!response.ok) { - throw new Error(`Failed to download from GitHub: ${url} [status: ${response.status}]`); - } - let body = await response.text(); - for (const query of imports) { - const pattern = new RegExp(`"(.*${query})"`, "gmi"); - body = body.replace(pattern, '"deno:harness"'); + continue; } - const src = `// Updated: ${response.headers.get("Date")} -// URL: ${url} -${body}`; + const src = await response.text(); + const dst = await visit(src, test); try { mkdirSync(dirname(path)); } catch {} - await Bun.write(path, src); + await Bun.write(path.replace(".test.", ".deno."), src); + await Bun.write(path, dst); +} + +async function visit(src: string, test: any): Promise<string> { + const ast = await parse(src, { + syntax: "typescript", + target: "esnext", + dynamicImport: true, + }); + for (const item of ast.body) { + if (item.type === "ImportDeclaration") { + visitImport(item); + } + if (item.type === "ExpressionStatement") { + visitExpression(item, test); + } + } + const header = `// Copyright 2018+ the Deno authors. All rights reserved. MIT license. +// https://raw.githubusercontent.com/denoland/deno/main/cli/tests/${test.remotePath} +\n`; + const { code } = await print(ast, { + isModule: true, + }); + return header + code; +} + +function visitImport(item: ImportDeclaration): void { + const src = item.source.value; + let match = false; + for (const name of imports) { + if (src.endsWith(name)) { + match = true; + break; + } + } + if (!match) { + console.warn("Unknown import:", src); + return; + } + item.source.raw = '"deno:harness"'; + // FIXME: https://github.com/oven-sh/bun/issues/2350 + /*let missing = []; + for (const specifier of item.specifiers) { + const name = specifier.local.value; + if (!(name in harness)) { + missing.push(name); + } + } + if (missing.length) { + console.warn("Harness does not contain exports:", missing); + }*/ +} + +function visitExpression(item: ExpressionStatement, test: any): void { + if ( + item.expression.type === "CallExpression" && + item.expression.callee.type === "MemberExpression" && + item.expression.callee.object.type === "Identifier" && + item.expression.callee.object.value === "Deno" + ) { + if (item.expression.callee.property.type === "Identifier" && item.expression.callee.property.value === "test") { + visitTest(item.expression); + } + } +} + +function visitTest(item: CallExpression): void { + for (const argument of item.arguments) { + if (argument.expression.type === "FunctionExpression") { + const fn = argument.expression.identifier?.value; + for (const test of tests) { + if (test.skip && test.skip.includes(fn)) { + item.callee.property.value = "test.ignore"; + } + } + } + } } |