From b8d85b036e7db96c7f79a2eae3926059ede69be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Sun, 11 May 2025 16:23:02 -0400 Subject: [PATCH] Move runifmain in test --- index.js | 1 - lib/metaltest.js | 22 ++++++++++++++++ lib/metaltest.test.js | 3 +-- lib/runifmain.js | 25 ------------------ lib/stackparser.test.js | 3 +-- lib/stacktrace.js | 57 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 30 deletions(-) delete mode 100644 lib/runifmain.js create mode 100644 lib/stacktrace.js diff --git a/index.js b/index.js index 419fd40..52c78a9 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ export * from './reporter/index.js' -export { runifmain } from './lib/runifmain.js' export { suite } from './lib/suite.js' import { metaltest } from './lib/metaltest.js' diff --git a/lib/metaltest.js b/lib/metaltest.js index 1c69bcd..a6996ef 100644 --- a/lib/metaltest.js +++ b/lib/metaltest.js @@ -1,3 +1,7 @@ +import { fileURLToPath } from 'node:url' +import { stacktrace } from './stacktrace.js' +import { errorreporter, summaryreporter, endreporter } from '../index.js' + const metaltest = (title) => { const suite = [] const only = [] @@ -27,6 +31,14 @@ const metaltest = (title) => { return { title, success, fail, skip, total: success + fail, testSuccess, testFail } } + const getAt = (shiftHowMany = 2) => { + const err = new Error() + const stack = stacktrace(err, { shiftHowMany }) + const at = stack.stacktraces[0] + + return at + } + const runner = (name, fn) => { suite.push({ name, fn }) } @@ -89,6 +101,16 @@ const metaltest = (title) => { return r } + runner.runifmain = async () => { + const at = getAt() + const path = fileURLToPath(at.callsite.getFileName) + const [_, script] = process.argv + + if (path !== script) return + + await runner.run(summaryreporter(), errorreporter(), endreporter()) + } + return runner } diff --git a/lib/metaltest.test.js b/lib/metaltest.test.js index 4eb4696..02a69dd 100644 --- a/lib/metaltest.test.js +++ b/lib/metaltest.test.js @@ -24,5 +24,4 @@ test.end('verify', async (stats) => { assert.equal(stats.testFail.length, 0) }) -import { runifmain } from '../index.js' -await runifmain(import.meta, test) +await test.runifmain() diff --git a/lib/runifmain.js b/lib/runifmain.js deleted file mode 100644 index 6e1d6b3..0000000 --- a/lib/runifmain.js +++ /dev/null @@ -1,25 +0,0 @@ -import { fileURLToPath } from 'node:url' - -const isMain = (meta) => { - const path = fileURLToPath(meta.url) - - return path.includes(process.argv[1]) -} - -import { errorreporter, summaryreporter, endreporter } from '../index.js' -const runifmain = async (meta, fn, ...args) => { - if (!isMain(meta)) return - - if (fn.name === 'runner' && fn.run) { - if (args.length == 0) args.push( - summaryreporter(), - errorreporter(), - endreporter(), - ) - await fn.run(...args) - } else { - await fn(...args) - } -} - -export { runifmain } diff --git a/lib/stackparser.test.js b/lib/stackparser.test.js index 8b4a5bc..8fd96fb 100644 --- a/lib/stackparser.test.js +++ b/lib/stackparser.test.js @@ -22,5 +22,4 @@ test('Diff', () => { } }) -import { runifmain } from '../index.js' -await runifmain(import.meta, test) +await test.runifmain() diff --git a/lib/stacktrace.js b/lib/stacktrace.js new file mode 100644 index 0000000..36f5c15 --- /dev/null +++ b/lib/stacktrace.js @@ -0,0 +1,57 @@ +const LINE_RE = /(?<=^\s*)at.*/gm + +const format = (callsite) => { + return { + getThis: callsite.getThis(),//: Returns the value of this. + getTypeName: callsite.getTypeName(),//: Returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property. + getFunction: callsite.getFunction(),//: Returns the current function. + getFunctionName: callsite.getFunctionName(),//: Returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context. + getMethodName: callsite.getMethodName(),//: Returns the name of the property of this or one of its prototypes that holds the current function. + getFileName: callsite.getFileName(),//: If this function was defined in a script returns the name of the script. + getLineNumber: callsite.getLineNumber(),//: If this function was defined in a script returns the current line number. + getColumnNumber: callsite.getColumnNumber(),//: If this function was defined in a script returns the current column number + getEvalOrigin: callsite.getEvalOrigin(),//: If this function was created using a call to eval returns a string representing the location where eval was called. + isToplevel: callsite.isToplevel(),//: Returns true if this is a top-level invocation, that is, if it's a global object. + isEval: callsite.isEval(),//: Returns true if this call takes place in code defined by a call to eval. + isNative: callsite.isNative(),//: Returns true if this call is in native V8 code. + isConstructor: callsite.isConstructor(),//: Returns true if this is a constructor call. + isAsync: callsite.isAsync(),//: Returns true if this call is asynchronous (i.e. await, Promise.all(), or Promise.any()). + isPromiseAll: callsite.isPromiseAll(),//: Returns true if this is an asynchronous call to Promise.all(). + getPromiseIndex: callsite.getPromiseIndex(),//: Returns the index of the promise element that was followed in Promise.all() or Promise.any() for async stack traces, or null if the CallSite is not an asynchronous Promise.all() or Promise.any() call. + } +} + +const getStack = (err, { shiftHowMany = 0 } = {}) => { + const old = Error.prepareStackTrace + + Error.prepareStackTrace = (err, callsites) => { + return { + summary: err.stack.split("\n").toSpliced(1, shiftHowMany).join("\n"), + callsites: callsites.toSpliced(0, shiftHowMany).map(format), + } + } + + const stack = err.stack + + Error.prepareStackTrace = old + + return stack +} + +const stacktrace = (err, { shiftHowMany = 0 } = {}) => { + // if (position >= Error.stackTraceLimit) { + // throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' + position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`') + // } + + const { summary, callsites } = getStack(err, { shiftHowMany }) + const lines = summary.match(LINE_RE) + + const info = { + summary, + stacktraces: callsites.map((callsite, index) => ({ summary: lines[index], callsite })) + } + + return info +} + +export { stacktrace }