diff --git a/playwright/src/index.ts b/playwright/src/index.ts index e6cd83b..2c4ea86 100644 --- a/playwright/src/index.ts +++ b/playwright/src/index.ts @@ -61,6 +61,7 @@ import { test as base, expect } from '@playwright/test'; import type { Page, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, TestInfo, TestType } from '@playwright/test'; import fs from 'node:fs'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { ACTION_FAIL, ACTION_SKIP, AgentClient, KeployError, RANDOM_OWN, RANDOM_SEEDED } from './agent'; import type { Outcome, Random, TestRef } from './agent'; import { ENV_TEST_PID, decide } from './join'; @@ -604,15 +605,43 @@ export function outcomeOf(ti: Pick): Outc return ti.status === ti.expectedStatus ? 'passed' : 'failed'; } +function unwrapLocation(body: string): string | null { + if (!body.endsWith(')')) return body; + let depth = 0; + for (let i = body.length - 1; i >= 0; i--) { + const ch = body[i]; + if (ch === ')') depth++; + else if (ch === '(') { + depth--; + if (depth === 0 && (i === 0 || body[i - 1] === ' ')) { + return body.slice(i + 1, -1); + } + } + } + const sep = body.startsWith('(') ? 0 : body.indexOf(' ('); + if (sep === -1) return null; + return body.slice(sep === 0 ? 1 : sep + 2, -1); +} + // The caller of given, as file:line relative to the project: the first // frame of the stack outside this package. const SOURCE_MAX = 1024; export function sourceOf(stack: string, dir = projectDir): string { const own = path.dirname(__filename); for (const line of stack.split('\n').slice(1)) { - const m = /\(?((?:file:\/\/)?[^\s()]+):(\d+):\d+\)?\s*$/.exec(line.trim()); + const trimmed = line.trim(); + if (!trimmed.startsWith('at ')) continue; + const body = trimmed.slice(3).trimStart().replace(/^async\s+/, ''); + const locAndPos = unwrapLocation(body); + if (locAndPos === null) continue; + const m = /^(.+):(\d+):\d+$/.exec(locAndPos); if (!m) continue; - let file = m[1].startsWith('file://') ? new URL(m[1]).pathname : m[1]; + let file: string; + try { + file = m[1].startsWith('file://') ? fileURLToPath(m[1]) : m[1]; + } catch { + continue; + } if (!path.isAbsolute(file) || path.dirname(file) === own || file.startsWith('node:')) continue; file = toSlash(path.relative(dir || process.cwd(), file)); const s = `${file}:${m[2]}`; diff --git a/playwright/test/fixtures/given caller with spaces.spec.ts b/playwright/test/fixtures/given caller with spaces.spec.ts new file mode 100644 index 0000000..9049d68 --- /dev/null +++ b/playwright/test/fixtures/given caller with spaces.spec.ts @@ -0,0 +1,5 @@ +import { test, given } from '../../dist'; + +test('caller with spaces', async () => { + await given('customer', 'customer-1'); +}); diff --git a/playwright/test/rows.test.mjs b/playwright/test/rows.test.mjs index 0f07920..90b9b5d 100644 --- a/playwright/test/rows.test.mjs +++ b/playwright/test/rows.test.mjs @@ -224,6 +224,19 @@ test('a worker claims the run, each test begins before its beforeEach and ends w } finally { await a.close(); } }); +test('given sends its caller path as it appears on disk when the filename has spaces', async () => { + const j = journal(); + const a = await fakeAgent(j); + try { + const { tests, out } = await playwright(['given caller with spaces.spec.ts'], keployEnv(a.url)); + assert.equal(tests['caller with spaces']?.status, 'passed', out); + const call = calls(j).find((c) => c.path === '/agent/scope/given'); + assert.deepEqual(call?.body, { + name: 'customer', value: 'customer-1', source: 'test/fixtures/given caller with spaces.spec.ts:4', + }, out); + } finally { await a.close(); } +}); + test('keploy skips a set-aside test with its reason, fails a judged-red one, and a refused begin fails the test', async () => { const j = journal(); const a = await fakeAgent(j, (c) => { diff --git a/playwright/test/unit.test.mjs b/playwright/test/unit.test.mjs index 581c5d4..f136be3 100644 --- a/playwright/test/unit.test.mjs +++ b/playwright/test/unit.test.mjs @@ -6,6 +6,7 @@ import fs from 'node:fs'; import http from 'node:http'; import os from 'node:os'; import path from 'node:path'; +import { pathToFileURL } from 'node:url'; import { AgentClient, KeployError } from '../dist/agent.js'; import { decide } from '../dist/join.js'; import { Inflight, workersScript } from '../dist/settle.js'; @@ -997,7 +998,26 @@ test('sourceOf: the first frame outside the package, relative to the project', ( const stack = ['Error', ` at given (${own}:10:5)`, ' at /proj/tests/a.spec.ts:12:7', ' at node:internal/x:1:1'].join('\n'); assert.equal(sourceOf(stack, '/proj'), 'tests/a.spec.ts:12'); assert.equal(sourceOf(['Error', ' at Object. (/proj/t/b.spec.ts:3:1)'].join('\n'), '/proj'), 't/b.spec.ts:3'); - assert.equal(sourceOf(['Error', ' at x (file:///proj/t/c.spec.mjs:4:2)'].join('\n'), '/proj'), 't/c.spec.mjs:4'); + assert.equal(sourceOf(['Error', ` at x (${pathToFileURL('/proj/t/c.spec.mjs').href}:4:2)`].join('\n'), '/proj'), 't/c.spec.mjs:4'); + const encoded = pathToFileURL('/proj/t/a b.spec.mjs').href; + assert.equal(sourceOf(`Error\n at x (${encoded}:5:2)`, '/proj'), 't/a b.spec.mjs:5'); + assert.equal(sourceOf(`Error\n at ${encoded}:5:2`, '/proj'), 't/a b.spec.mjs:5'); + assert.equal(sourceOf(`Error\n at async ${encoded}:5:2`, '/proj'), 't/a b.spec.mjs:5'); + assert.equal(sourceOf('Error\n at /proj/t/a b.spec.ts:5:2', '/proj'), 't/a b.spec.ts:5'); + assert.equal(sourceOf('Error\n at async /proj/t/a b.spec.ts:5:2', '/proj'), 't/a b.spec.ts:5'); + assert.equal(sourceOf('Error\n at async fn (/proj/t/a b.spec.ts:5:2)', '/proj'), 't/a b.spec.ts:5'); + assert.equal(sourceOf('Error\n at fn (/proj/tests (v2)/a b.spec.ts:5:2)', '/proj'), 'tests (v2)/a b.spec.ts:5'); + assert.equal(sourceOf('Error\n at /proj/tests (v2)/a b.spec.ts:5:2', '/proj'), 'tests (v2)/a b.spec.ts:5'); + assert.equal(sourceOf('Error\n at Object.load (v2) (/proj/tests/a.spec.ts:5:2)', '/proj'), 'tests/a.spec.ts:5'); + assert.equal(sourceOf('Error\n at Object.load (v2) (/proj/tests (v3)/a b.spec.ts:5:2)', '/proj'), 'tests (v3)/a b.spec.ts:5'); + assert.equal(sourceOf('Error\n at (anonymous) (/proj/tests/a.spec.ts:5:2)', '/proj'), 'tests/a.spec.ts:5'); + assert.equal(sourceOf('Error\n at (/proj/tests/a.spec.ts:5:2)', '/proj'), 'tests/a.spec.ts:5'); + assert.equal(sourceOf('Error\n at fn (/proj/tests)/a.spec.ts:5:2)', '/proj'), 'tests)/a.spec.ts:5'); + assert.equal(sourceOf('Error\n at fn (/proj/tests(/a.spec.ts:5:2)', '/proj'), 'tests(/a.spec.ts:5'); + assert.equal(sourceOf('Error\n at (/proj/tests)/a.spec.ts:5:2)', '/proj'), 'tests)/a.spec.ts:5'); + assert.equal(sourceOf('Error\n at foo)\n at /proj/a.spec.ts:2:1', '/proj'), 'a.spec.ts:2'); + assert.equal(sourceOf('Error\n at eval (eval at fn (/proj/tests/a.spec.ts:5:2), :1:1)\n at fn (/proj/tests/a.spec.ts:5:2)', '/proj'), 'tests/a.spec.ts:5'); + assert.equal(sourceOf('Error\n at eval (file:///proj/a%2Fb.spec.ts:1:1)\n at /proj/a.spec.ts:2:1', '/proj'), 'a.spec.ts:2'); assert.equal(sourceOf('Error\n at ', '/proj'), ''); });