From c4ae2a48dcd81ccd4a82326d7d3b9e7c9b3b457f Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Wed, 16 Aug 2023 15:32:15 -0400 Subject: [PATCH] [sprout] patch bug + add verbose mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ``` yarn sprout --verbose ``` Verbose mode prints out all outputs of tests. The test output is a status (`ok` or `exception`), a returned or thrown value, and a set of console logs. Currently as of #1960 , this is the output: ```sh $ yarn workspace babel-plugin-react-forget run build && node ../sprout/dist/main.js --verbose $ rimraf dist && tsc PASS alias-nested-member-path ok {"y":{"z":[]}} PASS assignment-variations-complex-lvalue ok {"y":{"z":4}} PASS assignment-variations ok 1 PASS chained-assignment-expressions ok {"z":null} PASS computed-call-evaluation-order ok {"f":"[[ function params=0 ]]"} [ "A", "B", "arg", "original" ] PASS const-propagation-into-function-expression-primitive ok 42 [ "42" ] PASS constant-propagation-for ok 0 PASS constant-propagation-while ok 0 PASS constant-propagation ok -6 [ "foo" ] PASS controlled-input ok PASS do-while-continue ok [1.5,1,0.5] PASS do-while-simple ok [6,4,2] PASS expression-with-assignment ok 5 PASS for-of-break ok [] PASS for-of-conditional-break ok [] PASS for-of-continue ok [0.5,1,1.5] PASS for-of-destructure ok [0,2,4] PASS for-of-simple ok [0,2,4] PASS function-declaration-reassign ok {} PASS function-declaration-redeclare ok "[[ function params=0 ]]" PASS lambda-reassign-primitive ok 41 PASS lambda-reassign-shadowed-primitive ok {} PASS property-call-evaluation-order ok {"f":"[[ function params=0 ]]"} [ "A", "arg", "original" ] PASS reactive-scope-grouping ok {"y":[{}]} PASS sequentially-constant-progagatable-if-test-conditions ok "ok" PASS simple-function-1 ok "[[ function params=1 ]]" PASS ssa-complex-multiple-if ok PASS ssa-complex-single-if ok PASS ssa-for ok 11 PASS ssa-if-else ok PASS ssa-objectexpression-phi ok {"x":1,"y":3} PASS ssa-property-call ok {"x":[[]]} PASS ssa-property ok {"x":[]} PASS ssa-return ok 2 PASS ssa-simple-phi ok PASS ssa-simple ok PASS ssa-single-if ok PASS ssa-switch ok PASS ssa-throw exception undefined PASS ssa-while ok 10 PASS type-field-load ok 1 PASS type-test-field-store ok {} PASS type-test-primitive ok 2 PASS update-expression-constant-propagation ok {"a":0,"b":0,"c":2,"d":2,"e":0} 44 Tests, 44 Passed, 0 Failed ✨ Done in 9.27s. ``` --- .../packages/sprout/src/runner-evaluator.ts | 33 ++++++++++++------- compiler/forget/packages/sprout/src/runner.ts | 19 +++++++++-- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/compiler/forget/packages/sprout/src/runner-evaluator.ts b/compiler/forget/packages/sprout/src/runner-evaluator.ts index 4dd01434d7..d435eb36d5 100644 --- a/compiler/forget/packages/sprout/src/runner-evaluator.ts +++ b/compiler/forget/packages/sprout/src/runner-evaluator.ts @@ -29,10 +29,22 @@ const PLACEHOLDER_VALUE = Symbol(); const seen = new Map(); return JSON.stringify(result, (_key, val) => { - if (typeof val === "object") { + if (typeof val === "function") { + return `[[ function params=${val.length} ]]`; + } else if (typeof val === "object") { let id = seen.get(val); if (id != null) { return `[[ cyclic ref *${id} ]]`; + } else if (val instanceof Map) { + return { + kind: "Map", + value: Array.from(val.entries()), // or with spread: value: [...value] + }; + } else if (val instanceof Set) { + return { + kind: "Set", + value: Array.from(val.values()), // or with spread: value: [...value] + }; } seen.set(val, seen.size); } @@ -56,7 +68,7 @@ export function doEval(source: string): EvaluatorResult { let exports = { FIXTURE_ENTRYPOINT: { fn: globalThis.placeholderFn, - args: [], + params: [], isComponent: false, }, }; @@ -64,14 +76,18 @@ export function doEval(source: string): EvaluatorResult { try { ${source} reachedInvoke = true; - - if (exports.FIXTURE_ENTRYPOINT.isComponent) { + if (exports.FIXTURE_ENTRYPOINT == null || exports.FIXTURE_ENTRYPOINT.fn === globalThis.placeholderFn) { + return { + kind: "UnexpectedError", + value: 'FIXTURE_ENTRYPOINT not exported!', + }; + } else if (exports.FIXTURE_ENTRYPOINT.isComponent) { // try to run fixture as a react component const result = render( React.createElement( exports.FIXTURE_ENTRYPOINT.fn, exports.FIXTURE_ENTRYPOINT.params) - ).asFragment().textContent; + ).container.innerHTML; return { kind: "ok", @@ -83,7 +99,7 @@ export function doEval(source: string): EvaluatorResult { WrapperTestComponent, exports.FIXTURE_ENTRYPOINT ) - ).asFragment().textContent; + ).container.innerHTML; return { kind: "ok", @@ -96,11 +112,6 @@ export function doEval(source: string): EvaluatorResult { kind: "UnexpectedError", value: e.toString(), }; - } else if (e === PLACEHOLDER_VALUE) { - return { - kind: "UnexpectedError", - value: 'FIXTURE_ENTRYPOINT not defined!', - }; } else { return { kind: "exception", diff --git a/compiler/forget/packages/sprout/src/runner.ts b/compiler/forget/packages/sprout/src/runner.ts index e36c3d3530..f456c2a840 100644 --- a/compiler/forget/packages/sprout/src/runner.ts +++ b/compiler/forget/packages/sprout/src/runner.ts @@ -37,6 +37,7 @@ process.on("SIGTERM", function () { type RunnerOptions = { filter: boolean; sync: boolean; + verbose: boolean; }; const opts: RunnerOptions = yargs @@ -52,6 +53,9 @@ const opts: RunnerOptions = yargs `Evaluate fixtures in filter mode ("${FILTER_FILENAME}")\n` ) .default("filter", false) + .boolean("verbose") + .describe("verbose", "Print results of passing fixtures.") + .default("verbose", false) .help("help") .strict() .parseSync(hideBin(process.argv)); @@ -64,7 +68,8 @@ function logsEqual(a: Array, b: Array) { } function reportResults( - results: Array<[string, RunnerWorker.TestResult]> + results: Array<[string, RunnerWorker.TestResult]>, + verbose: boolean ): boolean { const failures: Array<[string, RunnerWorker.TestResult]> = []; @@ -92,6 +97,15 @@ function reportResults( console.log( chalk.green.inverse.bold(" PASS ") + " " + chalk.dim(fixtureName) ); + if (verbose) { + console.log( + ` ${forgetResult.kind} ${forgetResult.value} ${ + forgetResult.logs.length > 0 + ? JSON.stringify(forgetResult.logs, undefined, 2) + : "" + }` + ); + } } } @@ -139,7 +153,6 @@ function reportResults( 2 )}\nFound: ${JSON.stringify(nonForgetResult, undefined, 2)}` ); - failures.push([fixtureName, result]); } } } @@ -196,7 +209,7 @@ export async function main(opts: RunnerOptions): Promise { } } - const isSuccess = reportResults(results); + const isSuccess = reportResults(results, opts.verbose); process.exit(isSuccess ? 0 : 1); }