unitbob 0.1.11 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +7 -1
- package/dist/files/artifactPath.js +19 -0
- package/dist/files/behavioral.js +29 -0
- package/dist/files/guardrails.js +6 -4
- package/dist/files/suiteBuild.js +65 -57
- package/dist/runner/bdd.js +138 -0
- package/dist/runner/boundReport.js +107 -0
- package/dist/runner/precheck.js +60 -1
- package/dist/runner/provision.js +116 -0
- package/dist/runner/pytestBddPlugin.js +62 -0
- package/dist/verbs/contractPrompt.js +25 -0
- package/dist/verbs/putSuiteBuild.js +42 -28
- package/dist/verbs/run.js +71 -144
- package/dist/verbs/suitePrepare.js +46 -20
- package/dist/wire.js +58 -3
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -12,6 +12,7 @@ import { putMapBuild } from "./verbs/putMapBuild.js";
|
|
|
12
12
|
import { suitePrepare } from "./verbs/suitePrepare.js";
|
|
13
13
|
import { putSuiteBuild } from "./verbs/putSuiteBuild.js";
|
|
14
14
|
import { fixPrepare } from "./verbs/fixPrepare.js";
|
|
15
|
+
import { contractPrompt } from "./verbs/contractPrompt.js";
|
|
15
16
|
const USAGE = `unitbob — thin local hands for the Unitbob server.
|
|
16
17
|
|
|
17
18
|
Usage: unitbob <verb> [args]
|
|
@@ -25,7 +26,9 @@ Verbs:
|
|
|
25
26
|
suite-prepare Internal: fetch the recipe and capability assignment, write the host suite-build request.
|
|
26
27
|
put-suite-build Internal: upload the host-built guardrail suite (whole spec file + test_metadata).
|
|
27
28
|
fix-prepare <id> Internal: fetch the per-capability repair packet for one red guard (by interface_id).
|
|
28
|
-
|
|
29
|
+
contract-prompt <digest> <test_id> [fix|accept]
|
|
30
|
+
Internal: fetch the fix/accept brief for one red check on either map.
|
|
31
|
+
check Run every Unitbob contract suite locally and report.
|
|
29
32
|
run Alias for check.
|
|
30
33
|
|
|
31
34
|
Pipeline: map and suite are built on your machine. \`*-prepare\` writes a request
|
|
@@ -69,6 +72,9 @@ async function main(argv) {
|
|
|
69
72
|
case 'fix-prepare':
|
|
70
73
|
await fixPrepare(await ensureLinked(), args);
|
|
71
74
|
return 0;
|
|
75
|
+
case 'contract-prompt':
|
|
76
|
+
await contractPrompt(await ensureLinked(), args);
|
|
77
|
+
return 0;
|
|
72
78
|
case 'run':
|
|
73
79
|
case 'check':
|
|
74
80
|
await run(await ensureLinked(), args);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isAbsolute } from 'node:path';
|
|
2
|
+
// The one rule for whether a host-provided suite file path is safe to write:
|
|
3
|
+
// relative, anchored under the given `.unitbob/<kind>/` root, no traversal.
|
|
4
|
+
// Both contract systems (spec 32) share it — structural under
|
|
5
|
+
// `.unitbob/structural/`, behavioral under `.unitbob/behavioral/`. It mirrors
|
|
6
|
+
// the Rails-side check exactly (any `.`/`..` segment is refused outright, not
|
|
7
|
+
// resolved), so a path the brain would refuse can never materialize here.
|
|
8
|
+
export function assertUnitbobPath(path, root) {
|
|
9
|
+
// The root may arrive with or without a trailing slash (the wire's path_root
|
|
10
|
+
// carries one, the local constants do not) — normalize to exactly one.
|
|
11
|
+
const prefix = `${root.replace(/\/+$/, '')}/`;
|
|
12
|
+
const unsafe = !path ||
|
|
13
|
+
isAbsolute(path) ||
|
|
14
|
+
!path.startsWith(prefix) ||
|
|
15
|
+
path.split('/').some((segment) => segment === '.' || segment === '..');
|
|
16
|
+
if (unsafe) {
|
|
17
|
+
throw new Error(`suite file path must be a relative path under ${prefix} with no traversal (got "${path}").`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { assertUnitbobPath } from "./artifactPath.js";
|
|
4
|
+
// The behavioral suite lives under its own root: the main `.feature` plus its
|
|
5
|
+
// step definitions and any helper files, all under `.unitbob/behavioral/`
|
|
6
|
+
// (spec 32). Nothing here is ever written into the project's own `spec/`,
|
|
7
|
+
// `features/`, or `tests/`. Like the structural flow, the host LLM writes and
|
|
8
|
+
// runs the suite to green before upload; the connector materializes the stored
|
|
9
|
+
// blob only so `check` can execute it locally.
|
|
10
|
+
export const BEHAVIORAL_DIR = '.unitbob/behavioral';
|
|
11
|
+
// Write a behavioral artifact envelope (main file + support files) under the
|
|
12
|
+
// behavioral root, after checking every path is safe. The root is wiped first
|
|
13
|
+
// so a stale file from a previous version never lingers. Returns the absolute
|
|
14
|
+
// path of the materialized main file.
|
|
15
|
+
export function materializeBehavioral(projectRoot, artifact) {
|
|
16
|
+
const files = [artifact, ...(artifact.support_files ?? [])];
|
|
17
|
+
for (const file of files)
|
|
18
|
+
assertUnitbobPath(file.path, BEHAVIORAL_DIR);
|
|
19
|
+
rmSync(join(projectRoot, BEHAVIORAL_DIR), { recursive: true, force: true });
|
|
20
|
+
let mainPath = '';
|
|
21
|
+
for (const file of files) {
|
|
22
|
+
const dest = join(projectRoot, file.path);
|
|
23
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
24
|
+
writeFileSync(dest, file.content);
|
|
25
|
+
if (file === artifact)
|
|
26
|
+
mainPath = dest;
|
|
27
|
+
}
|
|
28
|
+
return { mainPath };
|
|
29
|
+
}
|
package/dist/files/guardrails.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { dirname, isAbsolute, join } from 'node:path';
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
3
|
+
// The structural suite lives under its own root now that behavioral is its peer
|
|
4
|
+
// (spec 32) — `.unitbob/structural/`, matching the Rails StructuralSuiteVersion
|
|
5
|
+
// path root. A forward-slash literal, not path.join: suite paths always arrive
|
|
6
|
+
// over the wire with '/', while path.join would render this '.unitbob\structural'
|
|
7
|
+
// on Windows and make assertGuardrailPath reject every valid path there. node's
|
|
6
8
|
// path.join still accepts a '/'-joined segment as input on every platform, so
|
|
7
9
|
// filesystem builds below are unaffected.
|
|
8
|
-
export const GUARDRAILS_DIR = '.unitbob/
|
|
10
|
+
export const GUARDRAILS_DIR = '.unitbob/structural';
|
|
9
11
|
export const HELPER_FILE = 'unitbob_helper.rb';
|
|
10
12
|
// An always-empty custom options file: pointing rspec's --options here keeps
|
|
11
13
|
// the project's own .rspec (stray --require lines, extra stdout formatters)
|
package/dist/files/suiteBuild.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { dirname, join } from 'node:path';
|
|
3
|
-
import {
|
|
3
|
+
import { assertUnitbobPath } from "./artifactPath.js";
|
|
4
4
|
export function requestPath(projectRoot) {
|
|
5
5
|
return join(projectRoot, '.unitbob', 'suite-build', 'request.json');
|
|
6
6
|
}
|
|
7
7
|
export function outputPath(projectRoot) {
|
|
8
8
|
return join(projectRoot, '.unitbob', 'suite-build', 'suite_output.json');
|
|
9
9
|
}
|
|
10
|
-
export function writeSuiteBuildRequest(projectRoot,
|
|
10
|
+
export function writeSuiteBuildRequest(projectRoot, branches) {
|
|
11
11
|
const request = {
|
|
12
12
|
project_root: projectRoot,
|
|
13
|
-
map_digest: task.map_digest,
|
|
14
13
|
output_path: outputPath(projectRoot),
|
|
15
|
-
|
|
16
|
-
blocks: task.blocks,
|
|
14
|
+
branches,
|
|
17
15
|
};
|
|
18
16
|
const path = requestPath(projectRoot);
|
|
19
17
|
mkdirSync(dirname(path), { recursive: true });
|
|
@@ -26,62 +24,83 @@ export function readSuiteBuildRequest(projectRoot) {
|
|
|
26
24
|
throw new Error(`${path} not found — run \`npx unitbob suite-prepare\` first.`);
|
|
27
25
|
}
|
|
28
26
|
const request = parseJson(readFileSync(path, 'utf8'), path);
|
|
29
|
-
if (!
|
|
30
|
-
|
|
27
|
+
if (!request ||
|
|
28
|
+
typeof request.project_root !== 'string' ||
|
|
29
|
+
typeof request.output_path !== 'string' ||
|
|
30
|
+
!Array.isArray(request.branches)) {
|
|
31
|
+
throw new Error(`${path} is malformed: expected project_root, output_path, and a branches array.`);
|
|
31
32
|
}
|
|
32
33
|
return request;
|
|
33
34
|
}
|
|
34
|
-
// Read
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
export function
|
|
35
|
+
// Read the host's answers, one per branch. The connector verifies each built
|
|
36
|
+
// branch parses, carries a safe-path artifact envelope under its own root, and
|
|
37
|
+
// has a runner_manifest and test_metadata. A branch may instead carry a
|
|
38
|
+
// build_error, which the connector relays. Anything unparseable throws and
|
|
39
|
+
// nothing is uploaded.
|
|
40
|
+
export function readHostSuiteOutputs(path, request) {
|
|
40
41
|
if (!existsSync(path)) {
|
|
41
42
|
throw new Error(`${path} not found — the host suite builder did not write its output.`);
|
|
42
43
|
}
|
|
43
44
|
const parsed = parseJson(readFileSync(path, 'utf8'), path);
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
const branches = parsed && Array.isArray(parsed.branches) ? parsed.branches : null;
|
|
46
|
+
if (!branches) {
|
|
47
|
+
throw new Error(`${path} is malformed: expected a branches array, one entry per contract system.`);
|
|
46
48
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
const rootFor = new Map(request.branches.map((branch) => [branch.suite_kind, branch.path_root]));
|
|
50
|
+
return branches.map((entry) => readBranch(entry, rootFor, path));
|
|
51
|
+
}
|
|
52
|
+
function readBranch(entry, rootFor, path) {
|
|
53
|
+
if (!entry || typeof entry !== 'object') {
|
|
54
|
+
throw new Error(`${path} is malformed: each branch must be an object.`);
|
|
55
|
+
}
|
|
56
|
+
const branch = entry;
|
|
57
|
+
const suiteKind = String(branch.suite_kind ?? '');
|
|
58
|
+
const root = rootFor.get(suiteKind);
|
|
59
|
+
if (!root) {
|
|
60
|
+
throw new Error(`${path}: unknown suite_kind "${suiteKind}" — it was not in the build request.`);
|
|
61
|
+
}
|
|
62
|
+
if ('spec_rb' in branch || 'spec_rb_path' in branch) {
|
|
63
|
+
throw new Error(`${path}: the ${suiteKind} branch uses the legacy spec_rb shape — emit suite_file instead.`);
|
|
64
|
+
}
|
|
65
|
+
if (branch.build_error && typeof branch.build_error === 'object') {
|
|
66
|
+
const message = String(branch.build_error.message ?? 'the host could not build this suite');
|
|
67
|
+
return { suite_kind: suiteKind, build_error: { message } };
|
|
49
68
|
}
|
|
50
|
-
if (!('test_metadata' in
|
|
51
|
-
throw new Error(`${path} is
|
|
69
|
+
if (!('test_metadata' in branch)) {
|
|
70
|
+
throw new Error(`${path}: the ${suiteKind} branch is missing test_metadata.`);
|
|
52
71
|
}
|
|
53
|
-
const manifest =
|
|
72
|
+
const manifest = branch.runner_manifest;
|
|
54
73
|
if (!manifest || typeof manifest !== 'object') {
|
|
55
|
-
throw new Error(`${path} is
|
|
74
|
+
throw new Error(`${path}: the ${suiteKind} branch is missing runner_manifest.`);
|
|
56
75
|
}
|
|
57
76
|
return {
|
|
58
|
-
|
|
77
|
+
suite_kind: suiteKind,
|
|
78
|
+
suite_file: resolveSuiteFile(branch.suite_file, root, path, suiteKind),
|
|
59
79
|
runner_manifest: manifest,
|
|
60
|
-
test_metadata:
|
|
80
|
+
test_metadata: branch.test_metadata,
|
|
61
81
|
};
|
|
62
82
|
}
|
|
63
|
-
// The host
|
|
64
|
-
//
|
|
65
|
-
function resolveSuiteFile(
|
|
66
|
-
const file = parsed.suite_file;
|
|
83
|
+
// The host inlines every file's `content`; each path is checked safe under this
|
|
84
|
+
// branch's root before anything is accepted.
|
|
85
|
+
function resolveSuiteFile(file, root, path, suiteKind) {
|
|
67
86
|
if (!file || typeof file !== 'object') {
|
|
68
|
-
throw new Error(`${path}
|
|
87
|
+
throw new Error(`${path}: the ${suiteKind} branch is missing suite_file.`);
|
|
69
88
|
}
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
throw new Error(`${path}: suite_file content at "${suitePath}" is empty.`);
|
|
89
|
+
const envelope = file;
|
|
90
|
+
const main = readOneFile(envelope, root, path, suiteKind, false);
|
|
91
|
+
const support = Array.isArray(envelope.support_files)
|
|
92
|
+
? envelope.support_files.map((entry) => readOneFile(entry, root, path, suiteKind, true))
|
|
93
|
+
: [];
|
|
94
|
+
return support.length > 0 ? { ...main, support_files: support } : main;
|
|
95
|
+
}
|
|
96
|
+
function readOneFile(file, root, path, suiteKind, support) {
|
|
97
|
+
const filePath = typeof file.path === 'string' ? file.path : '';
|
|
98
|
+
assertUnitbobPath(filePath, root);
|
|
99
|
+
if (typeof file.content === 'string' && file.content.trim()) {
|
|
100
|
+
return { path: filePath, content: file.content };
|
|
83
101
|
}
|
|
84
|
-
|
|
102
|
+
const label = support ? 'support file' : 'suite_file';
|
|
103
|
+
throw new Error(`${path}: the ${suiteKind} ${label} at "${filePath}" has no inline content.`);
|
|
85
104
|
}
|
|
86
105
|
function parseJson(raw, path) {
|
|
87
106
|
try {
|
|
@@ -91,19 +110,8 @@ function parseJson(raw, path) {
|
|
|
91
110
|
throw new Error(`${path} is not valid JSON (${err.message})`);
|
|
92
111
|
}
|
|
93
112
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return (typeof request.project_root === 'string' &&
|
|
99
|
-
typeof request.map_digest === 'string' &&
|
|
100
|
-
typeof request.output_path === 'string' &&
|
|
101
|
-
isRecipe(request.recipe) &&
|
|
102
|
-
Array.isArray(request.blocks));
|
|
103
|
-
}
|
|
104
|
-
function isRecipe(value) {
|
|
105
|
-
if (!value || typeof value !== 'object')
|
|
106
|
-
return false;
|
|
107
|
-
const recipe = value;
|
|
108
|
-
return typeof recipe.name === 'string' && typeof recipe.version === 'string' && typeof recipe.text === 'string';
|
|
113
|
+
// Turn a branch's assignment packet into its recipe name: structural uses the
|
|
114
|
+
// unit-guardrail recipe, behavioral the Gherkin one.
|
|
115
|
+
export function recipeNameFor(packet) {
|
|
116
|
+
return packet.suite_kind === 'behavioral' ? 'generate_behavioral' : 'generate';
|
|
109
117
|
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { runProcess } from "../proc.js";
|
|
4
|
+
import { readReport } from "./types.js";
|
|
5
|
+
import { PYTEST_BDD_PLUGIN } from "./pytestBddPlugin.js";
|
|
6
|
+
export const BDD_TIMEOUT_MS = 10 * 60 * 1000;
|
|
7
|
+
// The behavioral suite lives under one root; the report is written inside it so
|
|
8
|
+
// the app under test cannot pollute it and it travels with the suite.
|
|
9
|
+
const BEHAVIORAL_ROOT = '.unitbob/behavioral';
|
|
10
|
+
const CUCUMBER_REPORT = join(BEHAVIORAL_ROOT, 'cucumber_messages.ndjson');
|
|
11
|
+
const PYTEST_BDD_REPORT = join(BEHAVIORAL_ROOT, 'pytest_bdd_report.json');
|
|
12
|
+
const PYTEST_BDD_PLUGIN_FILE = join(BEHAVIORAL_ROOT, 'unitbob_pytest_bdd_plugin.py');
|
|
13
|
+
const PYTEST_INI_FILE = join(BEHAVIORAL_ROOT, 'pytest.ini');
|
|
14
|
+
const PYTEST_INI = '[pytest]\naddopts =\n';
|
|
15
|
+
// The connector-owned BDD strategy table (spec 32): the `runner` enum names one
|
|
16
|
+
// of these; the connector never executes a host-provided command string. Each
|
|
17
|
+
// strategy runs the whole behavioral bundle and returns the raw machine-readable
|
|
18
|
+
// report verbatim — the connector does no marker join and no aggregation.
|
|
19
|
+
export function runBddSuite(projectRoot, runner, mainPath) {
|
|
20
|
+
switch (runner) {
|
|
21
|
+
case 'cucumber':
|
|
22
|
+
return runCucumberRuby(projectRoot);
|
|
23
|
+
case 'cucumber-js':
|
|
24
|
+
return runCucumberJs(projectRoot);
|
|
25
|
+
case 'pytest-bdd':
|
|
26
|
+
return runPytestBdd(projectRoot, mainPath);
|
|
27
|
+
default:
|
|
28
|
+
return Promise.reject(new Error(`Unsupported BDD runner "${runner}" — rebuild the behavioral suite.`));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// Ruby: `cucumber` with the built-in message formatter. The features and step
|
|
32
|
+
// definitions both live under the behavioral root; --require points at the step
|
|
33
|
+
// definitions so only the Unitbob bundle loads.
|
|
34
|
+
async function runCucumberRuby(projectRoot) {
|
|
35
|
+
const features = join(BEHAVIORAL_ROOT, 'features');
|
|
36
|
+
const steps = join(BEHAVIORAL_ROOT, 'step_definitions');
|
|
37
|
+
const localBin = join(projectRoot, 'bin', 'cucumber');
|
|
38
|
+
const sidecarGemfile = join(projectRoot, BEHAVIORAL_ROOT, 'Gemfile');
|
|
39
|
+
const hasSidecarGemfile = existsSync(sidecarGemfile);
|
|
40
|
+
const command = executable(localBin) ? localBin : 'bundle';
|
|
41
|
+
const base = executable(localBin) ? [] : ['exec', 'cucumber'];
|
|
42
|
+
const args = [...base, features, '--require', steps, '--format', 'message', '--out', CUCUMBER_REPORT];
|
|
43
|
+
const env = {
|
|
44
|
+
...process.env,
|
|
45
|
+
RAILS_ENV: 'test',
|
|
46
|
+
UNITBOB_REPO_ROOT: projectRoot,
|
|
47
|
+
};
|
|
48
|
+
if (hasSidecarGemfile) {
|
|
49
|
+
env.BUNDLE_GEMFILE = join(BEHAVIORAL_ROOT, 'Gemfile');
|
|
50
|
+
}
|
|
51
|
+
const result = await runProcess(command, args, {
|
|
52
|
+
cwd: projectRoot,
|
|
53
|
+
timeoutMs: BDD_TIMEOUT_MS,
|
|
54
|
+
env,
|
|
55
|
+
});
|
|
56
|
+
return finalize(result, command, args, projectRoot, CUCUMBER_REPORT);
|
|
57
|
+
}
|
|
58
|
+
// JS/TS: `@cucumber/cucumber` (cucumber-js) with the message formatter written
|
|
59
|
+
// to a file.
|
|
60
|
+
async function runCucumberJs(projectRoot) {
|
|
61
|
+
const features = join(BEHAVIORAL_ROOT, 'features');
|
|
62
|
+
const steps = join(BEHAVIORAL_ROOT, 'step_definitions', '**', '*');
|
|
63
|
+
const sidecarBin = join(projectRoot, BEHAVIORAL_ROOT, 'node_modules', '.bin', 'cucumber-js');
|
|
64
|
+
const command = executable(sidecarBin) ? sidecarBin : 'npx';
|
|
65
|
+
const baseArgs = executable(sidecarBin) ? [] : ['cucumber-js'];
|
|
66
|
+
const args = [
|
|
67
|
+
...baseArgs,
|
|
68
|
+
features,
|
|
69
|
+
'--require',
|
|
70
|
+
steps,
|
|
71
|
+
'--format',
|
|
72
|
+
`message:${CUCUMBER_REPORT}`,
|
|
73
|
+
];
|
|
74
|
+
const result = await runProcess(command, args, {
|
|
75
|
+
cwd: projectRoot,
|
|
76
|
+
timeoutMs: BDD_TIMEOUT_MS,
|
|
77
|
+
env: { ...process.env, NODE_ENV: 'test', UNITBOB_REPO_ROOT: projectRoot },
|
|
78
|
+
});
|
|
79
|
+
return finalize(result, command, args, projectRoot, CUCUMBER_REPORT);
|
|
80
|
+
}
|
|
81
|
+
// Python: pytest driving pytest-bdd, with the connector's reporter plugin. The
|
|
82
|
+
// plugin writes the JSON report; `-c` isolates the run from the project's own
|
|
83
|
+
// addopts. The runner command is connector-owned.
|
|
84
|
+
async function runPytestBdd(projectRoot, mainPath) {
|
|
85
|
+
mkdirSync(join(projectRoot, BEHAVIORAL_ROOT), { recursive: true });
|
|
86
|
+
writeFileSync(join(projectRoot, PYTEST_INI_FILE), PYTEST_INI);
|
|
87
|
+
writeFileSync(join(projectRoot, PYTEST_BDD_PLUGIN_FILE), PYTEST_BDD_PLUGIN);
|
|
88
|
+
const command = await pickPython(projectRoot);
|
|
89
|
+
const stepsDir = join(BEHAVIORAL_ROOT, 'step_definitions');
|
|
90
|
+
const isVenvPytest = command.endsWith('/pytest');
|
|
91
|
+
const args = isVenvPytest
|
|
92
|
+
? ['-c', PYTEST_INI_FILE, '-p', 'no:cacheprovider', '-p', pluginModule(), stepsDir, '--rootdir', projectRoot]
|
|
93
|
+
: ['-m', 'pytest', '-c', PYTEST_INI_FILE, '-p', 'no:cacheprovider', '-p', pluginModule(), stepsDir, '--rootdir', projectRoot];
|
|
94
|
+
const result = await runProcess(command, args, {
|
|
95
|
+
cwd: projectRoot,
|
|
96
|
+
timeoutMs: BDD_TIMEOUT_MS,
|
|
97
|
+
env: {
|
|
98
|
+
...process.env,
|
|
99
|
+
UNITBOB_REPO_ROOT: projectRoot,
|
|
100
|
+
UNITBOB_PYTEST_BDD_REPORT: join(projectRoot, PYTEST_BDD_REPORT),
|
|
101
|
+
PYTHONPATH: [join(projectRoot, BEHAVIORAL_ROOT), process.env.PYTHONPATH ?? ''].filter(Boolean).join(':'),
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
return finalize(result, command, args, projectRoot, PYTEST_BDD_REPORT);
|
|
105
|
+
// mainPath is accepted for symmetry with the structural runners; pytest-bdd
|
|
106
|
+
// discovers scenarios from the step-definition modules, not the .feature path.
|
|
107
|
+
}
|
|
108
|
+
function pluginModule() {
|
|
109
|
+
return 'unitbob_pytest_bdd_plugin';
|
|
110
|
+
}
|
|
111
|
+
function finalize(result, command, args, projectRoot, reportRel) {
|
|
112
|
+
return {
|
|
113
|
+
...result,
|
|
114
|
+
command,
|
|
115
|
+
args,
|
|
116
|
+
resultPath: reportRel,
|
|
117
|
+
report: readReport(join(projectRoot, reportRel)),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async function pickPython(projectRoot) {
|
|
121
|
+
const sidecarVenvPytest = join(projectRoot, BEHAVIORAL_ROOT, '.venv', 'bin', 'pytest');
|
|
122
|
+
if (executable(sidecarVenvPytest)) {
|
|
123
|
+
return sidecarVenvPytest;
|
|
124
|
+
}
|
|
125
|
+
const probe = await runProcess('python3', ['-m', 'pytest', '--version'], {
|
|
126
|
+
cwd: projectRoot,
|
|
127
|
+
timeoutMs: 10_000,
|
|
128
|
+
}).catch(() => ({ stdout: '', stderr: '', code: 1 }));
|
|
129
|
+
return probe.code === 0 ? 'python3' : 'python';
|
|
130
|
+
}
|
|
131
|
+
function executable(path) {
|
|
132
|
+
try {
|
|
133
|
+
return existsSync(path) && (statSync(path).mode & 0o111) !== 0;
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Size-bound the raw machine-readable report before transport (spec 26/30/32).
|
|
2
|
+
// This is a size limit only, not a privacy filter: application values in failure
|
|
3
|
+
// messages are accepted, not scrubbed. `null` means "no usable report" — the
|
|
4
|
+
// caller takes the structured suite-error path.
|
|
5
|
+
//
|
|
6
|
+
// Structural JSON/XML reports are parsed only to bound per-failure text, then
|
|
7
|
+
// re-serialized. Behavioral reports (Cucumber Messages NDJSON, pytest-bdd JSON)
|
|
8
|
+
// are bounded by whole-document size and shipped verbatim — Rails owns every bit
|
|
9
|
+
// of their interpretation.
|
|
10
|
+
const MAX_FAILURE_CHARS = 8000;
|
|
11
|
+
const MAX_REPORT_CHARS = 4_000_000;
|
|
12
|
+
export function boundReport(runner, result) {
|
|
13
|
+
switch (runner) {
|
|
14
|
+
case 'pytest':
|
|
15
|
+
return result.report.trim() ? boundJunitXml(result.report) : null;
|
|
16
|
+
case 'rspec':
|
|
17
|
+
case 'vitest': {
|
|
18
|
+
const parsed = parseJsonObject(result.report) ?? (runner === 'rspec' ? parseJsonObject(result.stdout) : null);
|
|
19
|
+
if (!parsed)
|
|
20
|
+
return null;
|
|
21
|
+
const bounded = runner === 'rspec' ? boundRspecFailures(parsed) : boundVitestFailures(parsed);
|
|
22
|
+
return JSON.stringify(bounded);
|
|
23
|
+
}
|
|
24
|
+
case 'cucumber':
|
|
25
|
+
case 'cucumber-js':
|
|
26
|
+
case 'pytest-bdd':
|
|
27
|
+
return boundWhole(result.report);
|
|
28
|
+
default:
|
|
29
|
+
return boundWhole(result.report);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// A behavioral report is shipped whole. An empty one is "no usable report" so
|
|
33
|
+
// the caller reports a suite error rather than an empty green.
|
|
34
|
+
function boundWhole(report) {
|
|
35
|
+
if (!report.trim())
|
|
36
|
+
return null;
|
|
37
|
+
return report.length > MAX_REPORT_CHARS ? report.slice(0, MAX_REPORT_CHARS) : report;
|
|
38
|
+
}
|
|
39
|
+
function boundRspecFailures(rspecJson) {
|
|
40
|
+
const examples = rspecJson.examples;
|
|
41
|
+
if (!Array.isArray(examples))
|
|
42
|
+
return rspecJson;
|
|
43
|
+
const bounded = examples.map((example) => {
|
|
44
|
+
if (!example || typeof example !== 'object')
|
|
45
|
+
return example;
|
|
46
|
+
const ex = example;
|
|
47
|
+
const exception = ex.exception;
|
|
48
|
+
if (!exception || typeof exception !== 'object')
|
|
49
|
+
return ex;
|
|
50
|
+
const exc = exception;
|
|
51
|
+
const next = { ...exc };
|
|
52
|
+
if (typeof exc.message === 'string')
|
|
53
|
+
next.message = truncate(exc.message);
|
|
54
|
+
if (Array.isArray(exc.backtrace))
|
|
55
|
+
next.backtrace = exc.backtrace.slice(0, 20);
|
|
56
|
+
return { ...ex, exception: next };
|
|
57
|
+
});
|
|
58
|
+
return { ...rspecJson, examples: bounded };
|
|
59
|
+
}
|
|
60
|
+
function boundVitestFailures(vitestJson) {
|
|
61
|
+
const testResults = vitestJson.testResults;
|
|
62
|
+
if (!Array.isArray(testResults))
|
|
63
|
+
return vitestJson;
|
|
64
|
+
const bounded = testResults.map((fileResult) => {
|
|
65
|
+
if (!fileResult || typeof fileResult !== 'object')
|
|
66
|
+
return fileResult;
|
|
67
|
+
const file = fileResult;
|
|
68
|
+
const assertions = file.assertionResults;
|
|
69
|
+
if (!Array.isArray(assertions))
|
|
70
|
+
return file;
|
|
71
|
+
const next = assertions.map((assertion) => {
|
|
72
|
+
if (!assertion || typeof assertion !== 'object')
|
|
73
|
+
return assertion;
|
|
74
|
+
const a = assertion;
|
|
75
|
+
if (!Array.isArray(a.failureMessages))
|
|
76
|
+
return a;
|
|
77
|
+
return { ...a, failureMessages: a.failureMessages.map((m) => (typeof m === 'string' ? truncate(m) : m)) };
|
|
78
|
+
});
|
|
79
|
+
return { ...file, assertionResults: next };
|
|
80
|
+
});
|
|
81
|
+
return { ...vitestJson, testResults: bounded };
|
|
82
|
+
}
|
|
83
|
+
function truncate(text) {
|
|
84
|
+
return text.length > MAX_FAILURE_CHARS ? `${text.slice(0, MAX_FAILURE_CHARS)}… (truncated)` : text;
|
|
85
|
+
}
|
|
86
|
+
function boundJunitXml(xml) {
|
|
87
|
+
const boundedBodies = xml.replace(/(<(failure|error|skipped|system-out|system-err)\b[^>]*>)([\s\S]*?)(<\/\2>)/g, (_match, open, _tag, body, close) => `${boundXmlMessageAttr(open)}${boundXmlText(body)}${close}`);
|
|
88
|
+
return boundedBodies.replace(/<(failure|error|skipped)\b[^>]*\/>/g, (tag) => boundXmlMessageAttr(tag));
|
|
89
|
+
}
|
|
90
|
+
function boundXmlMessageAttr(tag) {
|
|
91
|
+
return tag.replace(/(\bmessage=")([\s\S]*?)(")/, (_m, pre, value, post) => value.length > MAX_FAILURE_CHARS ? `${pre}${boundXmlText(value)}… (truncated)${post}` : `${pre}${value}${post}`);
|
|
92
|
+
}
|
|
93
|
+
function boundXmlText(text) {
|
|
94
|
+
return text.length > MAX_FAILURE_CHARS ? `${truncateXml(text)}… (truncated)` : text;
|
|
95
|
+
}
|
|
96
|
+
function truncateXml(text) {
|
|
97
|
+
return text.slice(0, MAX_FAILURE_CHARS).replace(/&[^;]*$/, '');
|
|
98
|
+
}
|
|
99
|
+
function parseJsonObject(text) {
|
|
100
|
+
try {
|
|
101
|
+
const parsed = JSON.parse(text);
|
|
102
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
package/dist/runner/precheck.js
CHANGED
|
@@ -17,6 +17,14 @@ export function anyStackPrecheck(projectRoot, deps = defaultDeps) {
|
|
|
17
17
|
}
|
|
18
18
|
// Confirm the host-selected runner against local markers. A mismatch fails
|
|
19
19
|
// closed: the caller writes no files and uploads nothing.
|
|
20
|
+
//
|
|
21
|
+
// Both contract systems route through here (spec 32). The structural runners
|
|
22
|
+
// (rspec/vitest/pytest) confirm the exact test framework is present, because the
|
|
23
|
+
// host writes and runs against it. The behavioral BDD runners
|
|
24
|
+
// (cucumber/cucumber-js/pytest-bdd) confirm only the base language: `check` never
|
|
25
|
+
// installs anything, so a missing BDD runner is left to surface as a suite error
|
|
26
|
+
// from the actual run — the precheck just replaces "bundle: command not found"
|
|
27
|
+
// with an early, clear "this project isn't Ruby".
|
|
20
28
|
export function validateStack(projectRoot, runner, deps = defaultDeps) {
|
|
21
29
|
switch (runner) {
|
|
22
30
|
case 'rspec':
|
|
@@ -25,13 +33,64 @@ export function validateStack(projectRoot, runner, deps = defaultDeps) {
|
|
|
25
33
|
return vitestPrecheck(projectRoot);
|
|
26
34
|
case 'pytest':
|
|
27
35
|
return pytestPrecheck(projectRoot, deps);
|
|
36
|
+
case 'cucumber':
|
|
37
|
+
return rubyBehavioralPrecheck(projectRoot);
|
|
38
|
+
case 'cucumber-js':
|
|
39
|
+
return jsBehavioralPrecheck(projectRoot);
|
|
40
|
+
case 'pytest-bdd':
|
|
41
|
+
return pythonBehavioralPrecheck(projectRoot, deps);
|
|
28
42
|
default:
|
|
29
43
|
return {
|
|
30
44
|
ok: false,
|
|
31
|
-
message: `Unsupported runner "${runner}" — Unitbob supports
|
|
45
|
+
message: `Unsupported runner "${runner}" — Unitbob supports ` +
|
|
46
|
+
'rspec, vitest, pytest, cucumber, cucumber-js, and pytest-bdd only.',
|
|
32
47
|
};
|
|
33
48
|
}
|
|
34
49
|
}
|
|
50
|
+
// The behavioral suite boots the app in its test environment, so a Ruby project
|
|
51
|
+
// is the marker — but not rspec-rails (the behavioral runner is cucumber, not
|
|
52
|
+
// rspec), and not the cucumber gem itself (check never installs; a missing
|
|
53
|
+
// runner surfaces as a suite error from the run).
|
|
54
|
+
function rubyBehavioralPrecheck(projectRoot) {
|
|
55
|
+
if (hasGemfileWith(projectRoot, /\brails\b/))
|
|
56
|
+
return { ok: true };
|
|
57
|
+
return {
|
|
58
|
+
ok: false,
|
|
59
|
+
message: 'The behavioral (Gherkin) suite selected the Ruby stack, but this project does not look ' +
|
|
60
|
+
'like Rails (no `rails` gem found in Gemfile).',
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function jsBehavioralPrecheck(projectRoot) {
|
|
64
|
+
if (existsSync(join(projectRoot, 'package.json')))
|
|
65
|
+
return { ok: true };
|
|
66
|
+
return {
|
|
67
|
+
ok: false,
|
|
68
|
+
message: 'The behavioral (Gherkin) suite selected the JavaScript/TypeScript stack, but this project has no package.json.',
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// pytest-bdd runs under pytest, so the harness must be importable — same probe
|
|
72
|
+
// and message shape as the structural pytest precheck; pytest-bdd itself, if
|
|
73
|
+
// missing, surfaces as a suite error from the run.
|
|
74
|
+
function pythonBehavioralPrecheck(projectRoot, deps) {
|
|
75
|
+
const markers = ['pyproject.toml', 'requirements.txt', 'Pipfile'];
|
|
76
|
+
if (!markers.some((name) => existsSync(join(projectRoot, name)))) {
|
|
77
|
+
return {
|
|
78
|
+
ok: false,
|
|
79
|
+
message: 'The behavioral (Gherkin) suite selected the Python stack, but this project has none of ' +
|
|
80
|
+
`${markers.join(', ')} — it does not look like a Python project.`,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const available = ['python3', 'python'].some((python) => deps.commandSucceeds(python, ['-m', 'pytest', '--version'], projectRoot));
|
|
84
|
+
if (!available) {
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
message: 'The behavioral (Gherkin) suite selected the Python stack, but pytest is not importable in ' +
|
|
88
|
+
'the current Python environment. If your dependencies live in a virtualenv, activate it ' +
|
|
89
|
+
'(e.g. `source .venv/bin/activate`) before running Unitbob, then retry.',
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return { ok: true };
|
|
93
|
+
}
|
|
35
94
|
function rubyPrecheck(projectRoot) {
|
|
36
95
|
if (!hasGemfileWith(projectRoot, /\brails\b/)) {
|
|
37
96
|
return {
|