fix(web-test): резолвить корень сьюта подъёмом вверх, а не от переданного пути

Конфиг и хуки резолвились строго от каталога первого позиционного пути, поэтому
запуск подкаталога сьюта был невозможен: `test tests/app/00-smoke/` падал с
«No URL provided and no webtest.config.mjs found» — хотя спека прямо обещает
запуск подкаталога («Фильтр по пути с CLI»).

Опаснее отказа по URL были два молчаливых следствия: при `--url=` прогон
подкаталога терял `_hooks.mjs` и ехал по неподготовленному стенду без единого
предупреждения, а `_allure/` не находился. Плюс `file:` в отчёте считался от
переданного пути, из-за чего один и тот же тест получал разный ID в зависимости
от способа запуска и рвал историю Allure/JUnit.

Введён корень сьюта: подъём от каталога пути до первого `webtest.config.mjs`
ИЛИ `_hooks.mjs` (конфиг необязателен — сьют только с хуками иначе снова терял
бы подготовку), с ограничением подъёма каталогом `.git`/`.v8-project.json`, а
при их отсутствии — cwd. Граница ничего не выбирает, только останавливает, так
что ложная граница даёт «корень не найден», а не чужой корень. От найденного
корня берутся все пять ролей: конфиг, хуки, каталог отчёта, пути в отчёте,
`_allure/`.

Попутно: пути из разных сьютов в одном прогоне теперь отвергаются (раньше
молча выигрывал первый путь, и сьют B ехал по подготовке сьюта A); найденный
корень печатается в шапке; отсутствие корня — предупреждение в stderr;
диагностика говорит про корень сьюта, а не только про URL.

Проверено: 12/12 офлайн-кейсов резолвера; полный регресс 29/29 до и после —
`file`/`name`/`status` идентичны; `_suite-root/nested/` (сценарий, который
падал) проходит с подхваченными конфигом и хуками; `_hang/` 6/6.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Nick Shirokov
2026-07-20 14:41:16 +03:00
co-authored by Claude Opus 4.8
parent 9c7010a49e
commit 90d8263a05
10 changed files with 309 additions and 29 deletions
@@ -1,4 +1,4 @@
// web-test cli/commands/test v1.8 — regression test runner
// web-test cli/commands/test v1.9 — regression test runner
// Source: https://github.com/Nikolay-Shirokov/cc-1c-skills
import { existsSync, writeFileSync, mkdirSync, renameSync, copyFileSync, unlinkSync } from 'fs';
import { resolve, dirname, basename, relative } from 'path';
@@ -9,6 +9,7 @@ import { createAssertions } from '../test-runner/assertions.mjs';
import { buildSeverityIndex } from '../test-runner/severity.mjs';
import { writeAllure, buildJUnit, syncAllureExtras } from '../test-runner/reporters.mjs';
import { discoverTests, resetState } from '../test-runner/discover.mjs';
import { findSuiteRoot, startDirOf } from '../test-runner/suite-root.mjs';
import { planEviction, touchLru, dropLru } from '../test-runner/context-pool.mjs';
export async function cmdTest(rawArgs) {
@@ -78,12 +79,24 @@ export async function cmdTest(rawArgs) {
die(`Test path not found: "${p}". To run a subset use --grep= / --tags=, or pass an existing dir/file.`);
}
// Load config if exists. config (webtest.config.mjs) and hooks (_hooks.mjs) resolve from
// the FIRST path's directory — list paths from the same suite folder.
const firstPath = resolve(testPaths[0]);
const isFile = firstPath.endsWith('.test.mjs');
const testDir = isFile ? dirname(firstPath) : firstPath;
const configPath = resolve(testDir, 'webtest.config.mjs');
// Suite root — the directory `webtest.config.mjs`, `_hooks.mjs`, `_allure/` and report paths
// all hang off. It is NOT the passed path: walking up to the nearest marker is what makes
// `test tests/myapp/sales/` work, as docs/web-test-regression-spec.md has always promised.
// Resolving from the passed path instead lost the hooks of any subfolder run — silently, so
// the run went ahead against an unprepared stand.
const startDirs = testPaths.map(p => startDirOf(p));
const roots = startDirs.map(d => findSuiteRoot(d));
// Paths from different suites must not share hooks — that used to resolve to "first path
// wins", silently running suite B's tests under suite A's preparation.
const distinct = [...new Set(roots.map(r => r?.root ?? null))];
if (distinct.length > 1) {
const lines = testPaths.map((p, i) => ` ${p}${roots[i]?.root ?? '(корень не найден)'}`);
die(`Paths belong to different suites — config and hooks would be ambiguous:\n${lines.join('\n')}\n` +
`Run them separately, or pass one suite root and narrow with --grep= / --tags=.`);
}
const suiteRoot = roots[0]?.root ?? startDirs[0];
const suiteRootFound = !!roots[0];
const configPath = resolve(suiteRoot, 'webtest.config.mjs');
let config = {};
if (existsSync(configPath)) {
const mod = await import('file:///' + configPath.replace(/\\/g, '/'));
@@ -103,7 +116,16 @@ export async function cmdTest(rawArgs) {
if (url) contextSpecs[defaultContextName] = { ...contextSpecs[defaultContextName], url };
} else {
const fallbackUrl = url || config.url;
if (!fallbackUrl) die('No URL provided and no webtest.config.mjs found');
// Name the real problem: with no suite root there is no config to take a URL from — and,
// more dangerously, no `_hooks.mjs` either. The old wording talked only about the URL and
// sent readers looking in the wrong place.
if (!fallbackUrl) {
die(suiteRootFound
? `No URL: ${configPath} defines neither "contexts" nor "url", and --url= was not given.`
: `Suite root not found above "${testPaths[0]}" — no webtest.config.mjs / _hooks.mjs up to ` +
`the repository (or working) directory, so there is no URL and no stand preparation.\n` +
`Pass the suite root (e.g. tests/myapp/) and narrow with --grep= / --tags=, or give --url=.`);
}
contextSpecs.default = { url: fallbackUrl };
}
if (!contextSpecs[defaultContextName]) {
@@ -175,7 +197,7 @@ export async function cmdTest(rawArgs) {
}
const reportDir = opts.reportDir
? resolve(opts.reportDir)
: (opts.report && !reportToStdout ? dirname(resolve(opts.report)) : testDir);
: (opts.report && !reportToStdout ? dirname(resolve(opts.report)) : suiteRoot);
if (opts.screenshot !== 'off') {
try { mkdirSync(reportDir, { recursive: true }); } catch {}
// 1C-error screenshots (taken inside the action wrapper) default to a single
@@ -194,7 +216,10 @@ export async function cmdTest(rawArgs) {
for (const file of testFiles) {
const mod = await import('file:///' + file.replace(/\\/g, '/'));
const base = {
file: relative(testDir, file).replace(/\\/g, '/'),
// Relative to the SUITE ROOT, not to the passed path — otherwise the same test gets a
// different id depending on how it was launched (`sales/01-x.test.mjs` vs `01-x.test.mjs`),
// and Allure history / JUnit trends treat the two as unrelated tests.
file: relative(suiteRoot, file).replace(/\\/g, '/'),
name: mod.name || basename(file, '.test.mjs'),
tags: mod.tags || [],
timeout: mod.timeout || opts.timeout,
@@ -229,7 +254,7 @@ export async function cmdTest(rawArgs) {
});
// Load hooks
const hooksPath = resolve(testDir, '_hooks.mjs');
const hooksPath = resolve(suiteRoot, '_hooks.mjs');
let hooks = {};
if (existsSync(hooksPath)) {
hooks = await import('file:///' + hooksPath.replace(/\\/g, '/'));
@@ -239,7 +264,17 @@ export async function cmdTest(rawArgs) {
// In `--report -` mode the machine JSON/XML takes over stdout, so progress moves to stderr.
const W = reportToStdout ? process.stderr : process.stdout;
W.write(`\nweb-test -- ${url}\n`);
W.write(`Running ${filtered.length} tests from ${relative(process.cwd(), testDir).replace(/\\/g, '/') || '.'}/\n\n`);
// Always name the resolved suite root: a climb that landed on the wrong directory is then
// visible in the first line of output instead of being diagnosed from symptoms later.
const rel = (p) => relative(process.cwd(), p).replace(/\\/g, '/') || '.';
const shownPaths = testPaths.map(p => rel(resolve(p))).filter(p => p !== rel(suiteRoot));
W.write(`Running ${filtered.length} tests from ${rel(suiteRoot)}/`);
W.write(shownPaths.length ? ` (paths: ${shownPaths.join(', ')})\n\n` : `\n\n`);
if (!suiteRootFound) {
// Not fatal — a one-off test outside any suite is legitimate. But a missing suite root also
// means no `_hooks.mjs` was even looked for above, so the stand is whatever it was.
process.stderr.write(`! no suite root (webtest.config.mjs / _hooks.mjs) found above ${rel(startDirs[0])} — running without hooks\n`);
}
const startedAt = new Date().toISOString();
const results = [];
@@ -819,10 +854,10 @@ export async function cmdTest(rawArgs) {
if (opts.format === 'allure') {
// Guard against a result-producing path that skipped recordResult; normally a no-op.
if (!allureWritten) writeAllure(results, reportDir, severityIndex);
syncAllureExtras(testDir, reportDir);
syncAllureExtras(suiteRoot, reportDir);
} else if (opts.format === 'junit') {
if (reportToStdout) process.stdout.write(buildJUnit(report, testDir) + '\n');
else writeFileSync(resolve(opts.report), buildJUnit(report, testDir));
if (reportToStdout) process.stdout.write(buildJUnit(report, suiteRoot) + '\n');
else writeFileSync(resolve(opts.report), buildJUnit(report, suiteRoot));
} else if (reportToStdout) {
out(report);
} else if (opts.report) {
@@ -0,0 +1,55 @@
// web-test cli/test-runner/suite-root v1.0 — locate the suite root above a given test path
// Source: https://github.com/Nikolay-Shirokov/cc-1c-skills
import { existsSync, statSync } from 'fs';
import { resolve, dirname } from 'path';
// Files that MARK a suite root. Both count, not just the config: `webtest.config.mjs` is
// optional (a single-URL suite may pass --url= instead), and a suite that ships only
// `_hooks.mjs` must still be found — otherwise its stand preparation is silently skipped,
// which is worse than any URL error.
const MARKERS = ['webtest.config.mjs', '_hooks.mjs'];
// Files that BOUND the climb. A boundary never selects a root — it only stops the search,
// so a wrong boundary degrades to "root not found" (= the pre-v1.9 behaviour plus a clear
// message) and can never produce a wrong root. `package.json` is deliberately absent: it
// occurs nested and would stop the climb below a legitimate suite root.
const BOUNDARIES = ['.git', '.v8-project.json'];
const isDir = (p) => { try { return statSync(p).isDirectory(); } catch { return false; } };
/**
* Walk up from `startPath` looking for a suite root.
*
* @param {string} startPath A test file or directory (absolute or cwd-relative).
* @param {{cwd?: string}} [opts]
* @returns {{root: string, marker: string} | null} null when no marker was found within bounds.
*
* Stops after examining the first directory that contains `.git` / `.v8-project.json`
* (that directory IS examined for markers), or — when neither is met — after examining `cwd`.
* A path outside `cwd` degenerates to the filesystem root; the marker requirement still
* makes a wrong hit unlikely, and the resolved root is printed in the run banner.
*/
export function findSuiteRoot(startPath, { cwd = process.cwd() } = {}) {
const full = resolve(startPath);
let dir = isDir(full) ? full : dirname(full);
const cwdAbs = resolve(cwd);
while (true) {
for (const m of MARKERS) {
if (existsSync(resolve(dir, m))) return { root: dir, marker: m };
}
const atBoundary = BOUNDARIES.some(b => existsSync(resolve(dir, b))) || dir === cwdAbs;
const parent = dirname(dir);
if (atBoundary || parent === dir) return null;
dir = parent;
}
}
/**
* The directory a path contributes to root resolution — its own dir for a file, itself for
* a directory. Also the fallback root when no marker is found (pre-v1.9 behaviour).
*/
export function startDirOf(testPath) {
const full = resolve(testPath);
return isDir(full) ? full : dirname(full);
}