diff --git a/playwright/src/index.ts b/playwright/src/index.ts index 29c0c14..5dd6a93 100644 --- a/playwright/src/index.ts +++ b/playwright/src/index.ts @@ -36,6 +36,7 @@ import { test as base, expect } from '@playwright/test'; import type { TestInfo } from '@playwright/test'; import fs from 'node:fs'; +import { fileURLToPath } from 'node:url'; import path from 'node:path'; import { ACTION_FAIL, ACTION_SKIP, AgentClient, KeployError } from './agent'; import type { Outcome, TestRef } from './agent'; @@ -172,9 +173,12 @@ 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 m = /\(?((?:file:\/\/)?[^()]+):(\d+):\d+\)?\s*$/.exec(line.trim()); if (!m) continue; - let file = m[1].startsWith('file://') ? new URL(m[1]).pathname : m[1]; + const location = m[1].replace(/^at\s+(?:async\s+)?/, ''); + let file: string; + try { file = location.startsWith('file://') ? fileURLToPath(location) : location; } + 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 8a11dcf..24019b7 100644 --- a/playwright/test/rows.test.mjs +++ b/playwright/test/rows.test.mjs @@ -162,6 +162,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 ca3c6d1..4214d10 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 } from '../dist/settle.js'; @@ -279,6 +280,12 @@ test('sourceOf: the first frame outside the package, relative to the project', ( 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'); + 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 /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 /proj/t/a.spec.ts:5:2', '/proj'), 't/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'), ''); });