jev-test-filter 0.1.2 → 0.1.5
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/README.md +184 -21
- package/dist/cli.d.ts +23 -1
- package/dist/cli.js +92 -15
- package/dist/context.d.ts +18 -0
- package/dist/context.js +156 -0
- package/dist/diff.d.ts +13 -0
- package/dist/diff.js +27 -2
- package/dist/discover.js +13 -2
- package/dist/extract-rspec.d.ts +20 -0
- package/dist/extract-rspec.js +177 -0
- package/dist/filter.d.ts +3 -0
- package/dist/filter.js +17 -0
- package/dist/framework.d.ts +1 -0
- package/dist/framework.js +6 -1
- package/dist/gate.d.ts +14 -3
- package/dist/gate.js +21 -2
- package/dist/languages.js +3 -2
- package/dist/questions.d.ts +10 -15
- package/dist/questions.js +38 -1
- package/dist/run.d.ts +46 -15
- package/dist/run.js +113 -22
- package/dist/types.d.ts +125 -2
- package/package.json +7 -6
package/dist/context.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What flaker knows, read in: `--context <file>`.
|
|
3
|
+
*
|
|
4
|
+
* flaker keeps the history this tool has no way to see -- which tests are
|
|
5
|
+
* quarantined, which tests failed the last time a given file changed, which
|
|
6
|
+
* gate values its calibration settled on -- and projects it into one JSON
|
|
7
|
+
* file, `jev-context` v1. This module reads that file and answers the two
|
|
8
|
+
* questions a run asks of it, per test: is it skipped, and what has it failed
|
|
9
|
+
* with before.
|
|
10
|
+
*
|
|
11
|
+
* The gate is NOT decided here. The context only supplies default values;
|
|
12
|
+
* `gate.ts` is still the one place a score becomes a selection, so flaker
|
|
13
|
+
* never has to reimplement it and the two can never disagree.
|
|
14
|
+
*
|
|
15
|
+
* A context is validated whole and refused whole. A half-understood context
|
|
16
|
+
* would change questions in ways the digest on the record no longer
|
|
17
|
+
* describes, and a run that cannot say what it was asked is worth less than
|
|
18
|
+
* one that stops and says why.
|
|
19
|
+
*/
|
|
20
|
+
import { readFile } from "node:fs/promises";
|
|
21
|
+
export const CONTEXT_VERSION = 1;
|
|
22
|
+
function fail(what) {
|
|
23
|
+
throw new Error(`invalid context: ${what}`);
|
|
24
|
+
}
|
|
25
|
+
function isRecord(v) {
|
|
26
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
27
|
+
}
|
|
28
|
+
function strings(v, at) {
|
|
29
|
+
if (!Array.isArray(v) || !v.every((s) => typeof s === "string"))
|
|
30
|
+
fail(`${at} must be an array of strings`);
|
|
31
|
+
return [...v];
|
|
32
|
+
}
|
|
33
|
+
function optionalString(v, at) {
|
|
34
|
+
if (v === undefined)
|
|
35
|
+
return undefined;
|
|
36
|
+
if (typeof v !== "string")
|
|
37
|
+
fail(`${at} must be a string`);
|
|
38
|
+
return v;
|
|
39
|
+
}
|
|
40
|
+
function readGate(v) {
|
|
41
|
+
if (v === undefined || v === null)
|
|
42
|
+
return null;
|
|
43
|
+
if (!isRecord(v))
|
|
44
|
+
fail("gate must be an object");
|
|
45
|
+
const out = {};
|
|
46
|
+
for (const key of ["cutoff", "unsure_below", "unsure_margin"]) {
|
|
47
|
+
const n = v[key];
|
|
48
|
+
if (n === undefined || n === null)
|
|
49
|
+
continue;
|
|
50
|
+
if (typeof n !== "number" || !Number.isFinite(n))
|
|
51
|
+
fail(`gate.${key} must be a number`);
|
|
52
|
+
out[key] = n;
|
|
53
|
+
}
|
|
54
|
+
if (v.basis !== undefined)
|
|
55
|
+
out.basis = v.basis;
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
/** The part of an entry that names a test. */
|
|
59
|
+
function readName(v, at) {
|
|
60
|
+
if (!isRecord(v))
|
|
61
|
+
fail(`${at} must be an object`);
|
|
62
|
+
if (typeof v.file !== "string" || v.file === "")
|
|
63
|
+
fail(`${at}.file must be a non-empty string`);
|
|
64
|
+
const title_path = strings(v.title_path, `${at}.title_path`);
|
|
65
|
+
const project = optionalString(v.project, `${at}.project`);
|
|
66
|
+
return { file: v.file, title_path, ...(project === undefined ? {} : { project }) };
|
|
67
|
+
}
|
|
68
|
+
function list(v, at, read) {
|
|
69
|
+
if (v === undefined || v === null)
|
|
70
|
+
return [];
|
|
71
|
+
if (!Array.isArray(v))
|
|
72
|
+
fail(`${at} must be an array`);
|
|
73
|
+
return v.map((item, i) => read(item, `${at}[${i}]`));
|
|
74
|
+
}
|
|
75
|
+
/** Validate a parsed `jev-context`. Throws on anything but a v1 context. */
|
|
76
|
+
export function parseContext(raw) {
|
|
77
|
+
if (!isRecord(raw))
|
|
78
|
+
fail("expected a JSON object");
|
|
79
|
+
if (raw.version !== CONTEXT_VERSION) {
|
|
80
|
+
throw new Error(`unsupported context version ${String(raw.version)}; expected ${CONTEXT_VERSION}`);
|
|
81
|
+
}
|
|
82
|
+
if (typeof raw.digest !== "string" || raw.digest === "")
|
|
83
|
+
fail("digest must be a non-empty string");
|
|
84
|
+
const generated_at = optionalString(raw.generated_at, "generated_at");
|
|
85
|
+
const skip = list(raw.skip, "skip", (item, at) => {
|
|
86
|
+
const reason = optionalString(item?.reason, `${at}.reason`);
|
|
87
|
+
return { ...readName(item, at), ...(reason === undefined ? {} : { reason }) };
|
|
88
|
+
});
|
|
89
|
+
const tests = list(raw.tests, "tests", (item, at) => {
|
|
90
|
+
const name = readName(item, at);
|
|
91
|
+
const o = item;
|
|
92
|
+
const failed_with = strings(o.failed_with ?? [], `${at}.failed_with`);
|
|
93
|
+
if (o.missed !== undefined && (typeof o.missed !== "number" || !Number.isFinite(o.missed))) {
|
|
94
|
+
fail(`${at}.missed must be a number`);
|
|
95
|
+
}
|
|
96
|
+
return { ...name, failed_with, ...(o.missed === undefined ? {} : { missed: o.missed }) };
|
|
97
|
+
});
|
|
98
|
+
return {
|
|
99
|
+
version: 1,
|
|
100
|
+
digest: raw.digest,
|
|
101
|
+
...(generated_at === undefined ? {} : { generated_at }),
|
|
102
|
+
gate: readGate(raw.gate),
|
|
103
|
+
skip,
|
|
104
|
+
tests,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export async function loadContext(path) {
|
|
108
|
+
let raw;
|
|
109
|
+
try {
|
|
110
|
+
raw = JSON.parse(await readFile(path, "utf8"));
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
throw new Error(`could not read context ${path}: ${err instanceof Error ? err.message : String(err)}`);
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
return parseContext(raw);
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
throw new Error(`${path}: ${err instanceof Error ? err.message : String(err)}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The key a context entry and a test meet on. The unit separator for the
|
|
124
|
+
* reason `testId` uses it: no title a source file contains holds one.
|
|
125
|
+
*/
|
|
126
|
+
function key(file, titlePath, project) {
|
|
127
|
+
const base = `${file.replace(/^\.\//, "")}\u001f${titlePath.join("\u001f")}`;
|
|
128
|
+
return project === undefined ? base : `${base}\u001e${project}`;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The context indexed for per-test questions.
|
|
132
|
+
*
|
|
133
|
+
* An entry that names a project is about the test in that project only; one
|
|
134
|
+
* that names none is about the test in every project, which is how a
|
|
135
|
+
* quarantine of a test that fails in every browser is written once.
|
|
136
|
+
*/
|
|
137
|
+
export function lookup(ctx) {
|
|
138
|
+
const skip = new Set(ctx.skip.map((s) => key(s.file, s.title_path, s.project)));
|
|
139
|
+
const hints = new Map();
|
|
140
|
+
for (const t of ctx.tests) {
|
|
141
|
+
if (t.failed_with.length > 0)
|
|
142
|
+
hints.set(key(t.file, t.title_path, t.project), t.failed_with);
|
|
143
|
+
}
|
|
144
|
+
const keys = (t) => t.project === undefined ? [key(t.file, t.titlePath, undefined)] : [key(t.file, t.titlePath, t.project), key(t.file, t.titlePath, undefined)];
|
|
145
|
+
return {
|
|
146
|
+
skipped: (t) => keys(t).some((k) => skip.has(k)),
|
|
147
|
+
failedWith: (t) => {
|
|
148
|
+
for (const k of keys(t)) {
|
|
149
|
+
const hit = hints.get(k);
|
|
150
|
+
if (hit)
|
|
151
|
+
return hit;
|
|
152
|
+
}
|
|
153
|
+
return [];
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
package/dist/diff.d.ts
CHANGED
|
@@ -37,7 +37,12 @@ export interface Diff {
|
|
|
37
37
|
text: string;
|
|
38
38
|
/** `git diff --stat`, which survives truncation of the text. */
|
|
39
39
|
stat: string;
|
|
40
|
+
/** Post-image ranges keyed relative to the requested cwd, like discovered tests. */
|
|
40
41
|
ranges: ChangedRanges;
|
|
42
|
+
/** The requested cwd relative to the repository root: "" at the root, else ending in "/". */
|
|
43
|
+
prefix: string;
|
|
44
|
+
/** The repository root. Paths in `text` and `stat` are relative to this, not to cwd. */
|
|
45
|
+
root: string;
|
|
41
46
|
}
|
|
42
47
|
/**
|
|
43
48
|
* The change under review.
|
|
@@ -50,3 +55,11 @@ export interface Diff {
|
|
|
50
55
|
* otherwise rename them and the parser's paths would match no file.
|
|
51
56
|
*/
|
|
52
57
|
export declare function loadDiff({ cwd, base, staged }?: DiffOptions): Promise<Diff>;
|
|
58
|
+
/**
|
|
59
|
+
* The commit a ref names, or null when it names none -- no repository, no
|
|
60
|
+
* commit yet, or a ref that does not exist.
|
|
61
|
+
*
|
|
62
|
+
* Null rather than a throw: the sha only labels a record, and a run that
|
|
63
|
+
* could select tests must not fail because it could not label them.
|
|
64
|
+
*/
|
|
65
|
+
export declare function resolveSha(ref: string, cwd?: string): Promise<string | null>;
|
package/dist/diff.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* which is the `state` every question is asked against.
|
|
7
7
|
*/
|
|
8
8
|
import { execFile } from "node:child_process";
|
|
9
|
+
import { resolve } from "node:path";
|
|
9
10
|
import { promisify } from "node:util";
|
|
10
11
|
const execFileAsync = promisify(execFile);
|
|
11
12
|
/** git's own prefixes, including the `diff.mnemonicPrefix` spellings. */
|
|
@@ -154,7 +155,7 @@ export async function loadDiff({ cwd = process.cwd(), base = null, staged = fals
|
|
|
154
155
|
// from the ranges, because a deleted file has no post-image line for a test
|
|
155
156
|
// body to overlap; but removing a source file is exactly the kind of change
|
|
156
157
|
// the model has to see, so it stays in the text.
|
|
157
|
-
const common = ["--no-color", "--no-ext-diff", "--src-prefix=a/", "--dst-prefix=b/"];
|
|
158
|
+
const common = ["--no-relative", "--no-color", "--no-ext-diff", "--src-prefix=a/", "--dst-prefix=b/"];
|
|
158
159
|
const range = [];
|
|
159
160
|
if (staged)
|
|
160
161
|
range.push("--cached");
|
|
@@ -164,5 +165,29 @@ export async function loadDiff({ cwd = process.cwd(), base = null, staged = fals
|
|
|
164
165
|
range.push("HEAD");
|
|
165
166
|
const text = await git(["diff", "--unified=0", ...common, ...range], cwd);
|
|
166
167
|
const stat = await git(["diff", "--stat", ...common, ...range], cwd);
|
|
167
|
-
|
|
168
|
+
// Git diff paths are repository-relative, while test discovery uses cwd.
|
|
169
|
+
// Rebase only the lookup keys: sibling-workspace changes must stay in the
|
|
170
|
+
// text and stat passed to the model.
|
|
171
|
+
const prefix = (await git(["rev-parse", "--show-prefix"], cwd)).replace(/\r?\n$/, "");
|
|
172
|
+
const parsed = parseUnifiedDiff(text);
|
|
173
|
+
const ranges = prefix === "" ? parsed : new Map([...parsed].filter(([file]) => file.startsWith(prefix))
|
|
174
|
+
.map(([file, changed]) => [file.slice(prefix.length), changed]));
|
|
175
|
+
const root = resolve(cwd, ...prefix.split("/").filter(Boolean).map(() => ".."));
|
|
176
|
+
return { text, stat, ranges, prefix, root };
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The commit a ref names, or null when it names none -- no repository, no
|
|
180
|
+
* commit yet, or a ref that does not exist.
|
|
181
|
+
*
|
|
182
|
+
* Null rather than a throw: the sha only labels a record, and a run that
|
|
183
|
+
* could select tests must not fail because it could not label them.
|
|
184
|
+
*/
|
|
185
|
+
export async function resolveSha(ref, cwd = process.cwd()) {
|
|
186
|
+
try {
|
|
187
|
+
const out = (await git(["rev-parse", "--verify", "--quiet", `${ref}^{commit}`], cwd)).trim();
|
|
188
|
+
return /^[0-9a-f]{40,64}$/.test(out) ? out : null;
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
168
193
|
}
|
package/dist/discover.js
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Every test in the repository, whatever language it is written in.
|
|
3
3
|
*
|
|
4
|
-
* ECMAScript and
|
|
4
|
+
* ECMAScript, Go and RSpec come out of the source and cost a parse. Rust has to be
|
|
5
5
|
* asked for: its names come from `cargo test -- --list`, which builds the test
|
|
6
6
|
* targets, and a tool that triggers a compile nobody asked for is a tool that
|
|
7
7
|
* gets removed from the workflow. `--format rust` is that asking.
|
|
8
|
+
*
|
|
9
|
+
* RSpec has to be asked for too, for a different reason: `_spec.rb` files were
|
|
10
|
+
* never discovered before, and a Rails application with Vitest or Jest tests
|
|
11
|
+
* for its front end is common. Discovering them unasked would turn that
|
|
12
|
+
* repository's existing `--exec -- vitest run` into a mixed-framework error.
|
|
8
13
|
*/
|
|
9
14
|
import { readFile } from "node:fs/promises";
|
|
10
15
|
import { join } from "node:path";
|
|
11
|
-
import { detectFramework, findTestFiles, isGoTestFile } from "./framework.js";
|
|
16
|
+
import { detectFramework, findTestFiles, isGoTestFile, isRSpecFile } from "./framework.js";
|
|
12
17
|
import { extractTests } from "./extract.js";
|
|
13
18
|
import { extractGoTests } from "./extract-go.js";
|
|
19
|
+
import { extractRSpecTests } from "./extract-rspec.js";
|
|
14
20
|
import { listRustTests } from "./cargo.js";
|
|
15
21
|
export async function discoverTests(cwd, paths, format) {
|
|
16
22
|
const out = [];
|
|
@@ -21,6 +27,11 @@ export async function discoverTests(cwd, paths, format) {
|
|
|
21
27
|
out.push(...extractGoTests(source, file));
|
|
22
28
|
continue;
|
|
23
29
|
}
|
|
30
|
+
if (isRSpecFile(file)) {
|
|
31
|
+
if (format === "rspec")
|
|
32
|
+
out.push(...extractRSpecTests(source, file));
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
24
35
|
const framework = detectFramework(source, file);
|
|
25
36
|
if (format !== null && framework !== format)
|
|
26
37
|
continue;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SgNode } from "@ast-grep/napi";
|
|
2
|
+
import type { TestCase } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* What RSpec prints for one level of the chain.
|
|
5
|
+
*
|
|
6
|
+
* `describe Cart, "#total"` is one description, "Cart#total": a second
|
|
7
|
+
* string argument is appended by the same rule that joins a class to its
|
|
8
|
+
* children. Symbols after the description are metadata and are dropped.
|
|
9
|
+
*/
|
|
10
|
+
export declare function description(call: SgNode): string;
|
|
11
|
+
/**
|
|
12
|
+
* The full description, the way RSpec prints it.
|
|
13
|
+
*
|
|
14
|
+
* Only for reading: the class-versus-string distinction the separator turns
|
|
15
|
+
* on is gone by the time a title is a string, so a group described by the
|
|
16
|
+
* string `"Cart"` reads as if it were the class. Nothing is ever selected by
|
|
17
|
+
* this name.
|
|
18
|
+
*/
|
|
19
|
+
export declare function rspecFullName(titlePath: readonly string[]): string;
|
|
20
|
+
export declare function extractRSpecTests(source: string, file: string): TestCase[];
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RSpec's examples, read out of the source.
|
|
3
|
+
*
|
|
4
|
+
* Asking RSpec instead (`rspec --dry-run`) would load every spec file, and in
|
|
5
|
+
* a Rails application that means booting the application -- work nobody asked
|
|
6
|
+
* this tool to do, for the same reason Rust is never listed unasked. The
|
|
7
|
+
* source has everything a location filter needs, so the source is read.
|
|
8
|
+
*
|
|
9
|
+
* Selection is by location rather than by name, and every choice below
|
|
10
|
+
* follows from how RSpec resolves a `file:line`, which was measured (RSpec
|
|
11
|
+
* 3.13, Ruby 3.3) rather than assumed:
|
|
12
|
+
*
|
|
13
|
+
* - A requested line runs the declaration nearest at or above it. RSpec
|
|
14
|
+
* records an example at a line of its own choosing -- `it "a",\n :slow do`
|
|
15
|
+
* at the first line, but `it(\n "a"\n) do` at the last -- so the first
|
|
16
|
+
* line of a multi-line call can resolve to the example BEFORE it, which
|
|
17
|
+
* is a wrong test run green. The last line of an example cannot: nothing
|
|
18
|
+
* is declared inside an example body. So an example is named by its last
|
|
19
|
+
* line.
|
|
20
|
+
* - A full description is not something to assemble. RSpec joins a group
|
|
21
|
+
* and its child with no space when the group is a class and the child
|
|
22
|
+
* starts with `#`, `.` or `::` (`Cart#total`), and with one otherwise
|
|
23
|
+
* (`"Cart" #total`). A name pattern spelled one way matches nothing
|
|
24
|
+
* spelled the other, and says nothing about it. A location has no
|
|
25
|
+
* spelling to get wrong.
|
|
26
|
+
* - Because of that, `dynamic` is never set. An interpolated title, a
|
|
27
|
+
* one-liner with no description at all and a row of an `.each` loop are
|
|
28
|
+
* all reachable by their line, so none of them has to be selected unasked.
|
|
29
|
+
*/
|
|
30
|
+
import { parse } from "@ast-grep/napi";
|
|
31
|
+
import { registerLanguages } from "./languages.js";
|
|
32
|
+
/** `describe` and its spellings, including shared groups, which declare examples too. */
|
|
33
|
+
const GROUP = /^[xf]?(?:describe|context|feature|example_group)$|^shared_(?:examples(?:_for)?|context)$/;
|
|
34
|
+
/** `it` and its spellings. A call with no block declares nothing that runs. */
|
|
35
|
+
const EXAMPLE = /^[xf]?(?:it|specify|example|scenario|its)$|^(?:focus|pending|skip)$/;
|
|
36
|
+
/**
|
|
37
|
+
* A nested group the shared examples are copied into. RSpec declares that
|
|
38
|
+
* group at the call itself, so its line reaches every example it holds.
|
|
39
|
+
*/
|
|
40
|
+
const IT_BEHAVES_LIKE = /^it_(?:behaves|should_behave)_like$/;
|
|
41
|
+
/**
|
|
42
|
+
* Shared examples copied straight into the enclosing group. They keep the
|
|
43
|
+
* lines of the file that defined them, which is usually a support file, so
|
|
44
|
+
* no line of this file reaches them -- except the enclosing group's own.
|
|
45
|
+
*/
|
|
46
|
+
const INCLUDE_SHARED = /^include_(?:examples|context)$/;
|
|
47
|
+
/** The receiver a group may be called on: none, or `RSpec`. */
|
|
48
|
+
function receiverAllowed(call) {
|
|
49
|
+
const recv = call.field("receiver");
|
|
50
|
+
return recv === null || /^(?:::)?RSpec$/.test(recv.text());
|
|
51
|
+
}
|
|
52
|
+
/** The positional arguments, without parentheses, commas or a trailing hash. */
|
|
53
|
+
function args(call) {
|
|
54
|
+
return (call.field("arguments")?.children() ?? []).filter((n) => n.isNamed() && n.kind() !== "comment");
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A string literal's text, with interpolations left as written. The grammar
|
|
58
|
+
* has no escape nodes inside single quotes, where only `\\` and `\'` are
|
|
59
|
+
* escapes at all.
|
|
60
|
+
*/
|
|
61
|
+
function stringText(node) {
|
|
62
|
+
const text = node
|
|
63
|
+
.children()
|
|
64
|
+
.filter((n) => n.isNamed())
|
|
65
|
+
.map((n) => (n.kind() === "escape_sequence" ? n.text().slice(1) : n.text()))
|
|
66
|
+
.join("");
|
|
67
|
+
return node.child(0)?.text() === "'" ? text.replace(/\\([\\'])/g, "$1") : text;
|
|
68
|
+
}
|
|
69
|
+
/** A class or module, as RSpec prints it: the constant path, exactly as written. */
|
|
70
|
+
function isConstant(node) {
|
|
71
|
+
return node.kind() === "constant" || node.kind() === "scope_resolution";
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* What RSpec prints for one level of the chain.
|
|
75
|
+
*
|
|
76
|
+
* `describe Cart, "#total"` is one description, "Cart#total": a second
|
|
77
|
+
* string argument is appended by the same rule that joins a class to its
|
|
78
|
+
* children. Symbols after the description are metadata and are dropped.
|
|
79
|
+
*/
|
|
80
|
+
export function description(call) {
|
|
81
|
+
const [first, second] = args(call);
|
|
82
|
+
if (!first)
|
|
83
|
+
return "";
|
|
84
|
+
let text;
|
|
85
|
+
if (first.kind() === "string")
|
|
86
|
+
text = stringText(first);
|
|
87
|
+
else if (first.kind() === "simple_symbol")
|
|
88
|
+
text = first.text().slice(1);
|
|
89
|
+
else if (first.kind() === "pair" || first.kind() === "hash")
|
|
90
|
+
return "";
|
|
91
|
+
else
|
|
92
|
+
text = first.text();
|
|
93
|
+
if (second?.kind() === "string")
|
|
94
|
+
text = joinDescriptions(text, stringText(second));
|
|
95
|
+
return text;
|
|
96
|
+
}
|
|
97
|
+
/** RSpec's own separator: none between a class and a `#`, `.` or `::` child. */
|
|
98
|
+
function joinDescriptions(parent, child) {
|
|
99
|
+
if (child === "")
|
|
100
|
+
return parent;
|
|
101
|
+
if (parent === "")
|
|
102
|
+
return child;
|
|
103
|
+
return CONSTANT_PATH.test(parent) && /^(?:#|\.|::)/.test(child) ? `${parent}${child}` : `${parent} ${child}`;
|
|
104
|
+
}
|
|
105
|
+
const CONSTANT_PATH = /^(?:::)?[A-Z]\w*(?:::[A-Z]\w*)*$/;
|
|
106
|
+
/**
|
|
107
|
+
* The full description, the way RSpec prints it.
|
|
108
|
+
*
|
|
109
|
+
* Only for reading: the class-versus-string distinction the separator turns
|
|
110
|
+
* on is gone by the time a title is a string, so a group described by the
|
|
111
|
+
* string `"Cart"` reads as if it were the class. Nothing is ever selected by
|
|
112
|
+
* this name.
|
|
113
|
+
*/
|
|
114
|
+
export function rspecFullName(titlePath) {
|
|
115
|
+
return titlePath.reduce((acc, part) => joinDescriptions(acc, part), "");
|
|
116
|
+
}
|
|
117
|
+
/** The line a block opens on, which is where RSpec records a group. */
|
|
118
|
+
function blockLine(call) {
|
|
119
|
+
const block = call.field("block");
|
|
120
|
+
return (block ?? call).range().start.line + 1;
|
|
121
|
+
}
|
|
122
|
+
export function extractRSpecTests(source, file) {
|
|
123
|
+
registerLanguages();
|
|
124
|
+
const root = parse("ruby", source).root();
|
|
125
|
+
const out = [];
|
|
126
|
+
const push = (call, titlePath, runnerLine) => {
|
|
127
|
+
const range = call.range();
|
|
128
|
+
out.push({
|
|
129
|
+
file,
|
|
130
|
+
titlePath,
|
|
131
|
+
line: range.start.line + 1,
|
|
132
|
+
endLine: range.end.line + 1,
|
|
133
|
+
framework: "rspec",
|
|
134
|
+
runnerLine,
|
|
135
|
+
dynamic: false,
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
// A walk rather than range containment, because where a call sits is what
|
|
139
|
+
// decides whether it means anything: `skip "reason"` inside an example body
|
|
140
|
+
// is a statement, not an example, so the walk never descends into one.
|
|
141
|
+
const walk = (node, chain, groupLine) => {
|
|
142
|
+
if (node.kind() === "call") {
|
|
143
|
+
const method = node.field("method")?.text() ?? "";
|
|
144
|
+
const hasBlock = node.field("block") !== null;
|
|
145
|
+
if (GROUP.test(method) && hasBlock && receiverAllowed(node)) {
|
|
146
|
+
const next = [...chain, description(node)];
|
|
147
|
+
for (const child of node.children())
|
|
148
|
+
walk(child, next, blockLine(node));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (node.field("receiver") === null && chain.length > 0) {
|
|
152
|
+
if (EXAMPLE.test(method) && hasBlock) {
|
|
153
|
+
const title = description(node) || `example at ./${file}:${node.range().start.line + 1}`;
|
|
154
|
+
push(node, [...chain, title], node.range().end.line + 1);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (IT_BEHAVES_LIKE.test(method)) {
|
|
158
|
+
const verb = method === "it_behaves_like" ? "behaves like" : "it should behave like";
|
|
159
|
+
// A customization block may declare its own examples. Its last
|
|
160
|
+
// line would select only the last of those, dropping the shared
|
|
161
|
+
// examples. Select the nested group's declaration instead.
|
|
162
|
+
const runnerLine = hasBlock ? blockLine(node) : node.range().end.line + 1;
|
|
163
|
+
push(node, [...chain, `${verb} ${description(node)}`], runnerLine);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (INCLUDE_SHARED.test(method)) {
|
|
167
|
+
push(node, [...chain, `${method} ${description(node)}`], groupLine ?? node.range().start.line + 1);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
for (const child of node.children())
|
|
173
|
+
walk(child, chain, groupLine);
|
|
174
|
+
};
|
|
175
|
+
walk(root, [], null);
|
|
176
|
+
return out;
|
|
177
|
+
}
|
package/dist/filter.d.ts
CHANGED
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
* alternation of anchored full names, is the only safe shape.
|
|
12
12
|
* - Playwright's own --list supplies project-aware names for --test-list.
|
|
13
13
|
* Source-only discovery retains file:line as its conservative fallback.
|
|
14
|
+
* - RSpec is selected by `file:line` only. Its full description depends on
|
|
15
|
+
* whether a group was described by a class or by a string, which a name
|
|
16
|
+
* read out of the source cannot always tell apart.
|
|
14
17
|
*/
|
|
15
18
|
import type { Framework, Selection, TestCase } from "./types.ts";
|
|
16
19
|
export type FilterMode =
|
package/dist/filter.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
+
import { rspecFullName } from "./extract-rspec.js";
|
|
2
3
|
export function escapeRegExp(s) {
|
|
3
4
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4
5
|
}
|
|
@@ -8,6 +9,8 @@ export function fullName(t) {
|
|
|
8
9
|
return t.titlePath.join("::");
|
|
9
10
|
if (t.framework === "go")
|
|
10
11
|
return t.titlePath.join("/");
|
|
12
|
+
if (t.framework === "rspec")
|
|
13
|
+
return rspecFullName(t.titlePath);
|
|
11
14
|
const sep = t.framework === "node" || t.framework === "bun" ? " " : " > ";
|
|
12
15
|
return t.titlePath.join(sep);
|
|
13
16
|
}
|
|
@@ -64,6 +67,20 @@ export function buildFilter(sel, framework, { fileThreshold = 0.8 } = {}) {
|
|
|
64
67
|
// both, which costs time and cannot lose a test.
|
|
65
68
|
return { mode: "exact", argv: ["--", "--exact", ...selected.map(fullName)] };
|
|
66
69
|
}
|
|
70
|
+
if (framework === "rspec") {
|
|
71
|
+
const files = uniqueFiles(selected);
|
|
72
|
+
if (selected.length / all.length > fileThreshold)
|
|
73
|
+
return { mode: "files", argv: files };
|
|
74
|
+
// One argument per file, `spec/a_spec.rb:12:30`: RSpec takes several
|
|
75
|
+
// lines after one path, and a line filter applies to its own file only.
|
|
76
|
+
const lines = new Map(files.map((f) => [f, new Set()]));
|
|
77
|
+
for (const t of selected)
|
|
78
|
+
lines.get(t.file).add(t.runnerLine ?? t.line);
|
|
79
|
+
return {
|
|
80
|
+
mode: "locations",
|
|
81
|
+
argv: files.map((f) => [f, ...[...lines.get(f)].sort((a, b) => a - b)].join(":")),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
67
84
|
if (framework === "go") {
|
|
68
85
|
// Top-level functions, never subtests. `-run` takes one hierarchical
|
|
69
86
|
// pattern and a second `-run` replaces the first, so "all of TestA, but
|
package/dist/framework.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { Framework } from "./types.ts";
|
|
|
3
3
|
/** The grammar to parse a file under, by extension. */
|
|
4
4
|
export declare function langFor(file: string): Lang;
|
|
5
5
|
export declare function isGoTestFile(file: string): boolean;
|
|
6
|
+
export declare function isRSpecFile(file: string): boolean;
|
|
6
7
|
export declare function isTestFile(file: string): boolean;
|
|
7
8
|
/**
|
|
8
9
|
* Playwright wins a tie because a spec that imports `expect` from elsewhere
|
package/dist/framework.js
CHANGED
|
@@ -27,8 +27,13 @@ const GO_TEST_FILE = /_test\.go$/;
|
|
|
27
27
|
export function isGoTestFile(file) {
|
|
28
28
|
return GO_TEST_FILE.test(file);
|
|
29
29
|
}
|
|
30
|
+
/** RSpec's own convention, and the suffix its default `--pattern` looks for. */
|
|
31
|
+
const RSPEC_TEST_FILE = /_spec\.rb$/;
|
|
32
|
+
export function isRSpecFile(file) {
|
|
33
|
+
return RSPEC_TEST_FILE.test(file);
|
|
34
|
+
}
|
|
30
35
|
export function isTestFile(file) {
|
|
31
|
-
return TEST_FILE.test(file) || GO_TEST_FILE.test(file);
|
|
36
|
+
return TEST_FILE.test(file) || GO_TEST_FILE.test(file) || RSPEC_TEST_FILE.test(file);
|
|
32
37
|
}
|
|
33
38
|
/** `require("x")`, with the specifier captured. */
|
|
34
39
|
const REQUIRE_RULE = {
|
package/dist/gate.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Answer, Selection, TestCase, Verdict } from "./types.ts";
|
|
1
|
+
import type { Answer, RecordGate, Selection, TestCase, Verdict } from "./types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* The boundary between "this change cannot alter the outcome" (level 1) and
|
|
4
4
|
* "it might" (level 2). It is a level boundary, not a tuned number, and it is
|
|
@@ -14,6 +14,14 @@ export interface GateOptions {
|
|
|
14
14
|
unsureBelow?: number;
|
|
15
15
|
unsureMargin?: number;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* The values a gate will actually decide under, with every default filled
|
|
19
|
+
* in, in the spelling a record keeps them in. A record that said only "the
|
|
20
|
+
* flags that were given" would change meaning the day a default did.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveGate({ cutoff, unsureBelow, unsureMargin, }?: GateOptions): RecordGate;
|
|
23
|
+
/** A recorded or supplied gate as options. Nothing, when there is none. */
|
|
24
|
+
export declare function gateOptions(g: RecordGate | null | undefined): GateOptions;
|
|
17
25
|
/** Decide one test. Never returns null: every test gets a side and a reason. */
|
|
18
26
|
export declare function decide(id: string, t: TestCase, answer: Answer | null, touched: boolean, { cutoff, unsureBelow, unsureMargin }?: GateOptions): Verdict;
|
|
19
27
|
/**
|
|
@@ -21,6 +29,9 @@ export declare function decide(id: string, t: TestCase, answer: Answer | null, t
|
|
|
21
29
|
*
|
|
22
30
|
* `answers` is keyed by question id, which is the test's index in `tests`;
|
|
23
31
|
* `touched` is keyed by `testId`, because it is computed from the diff before
|
|
24
|
-
* any question exists
|
|
32
|
+
* any question exists; so is `quarantined`, the tests a context put in
|
|
33
|
+
* `skip`. A quarantined test is decided before anything else, touched
|
|
34
|
+
* included: it was never asked about, and the quarantine is flaker's decision
|
|
35
|
+
* that its result means nothing right now.
|
|
25
36
|
*/
|
|
26
|
-
export declare function gate(tests: TestCase[], answers: Map<string, Answer | null>, touched: Set<string>, opts?: GateOptions): Selection;
|
|
37
|
+
export declare function gate(tests: TestCase[], answers: Map<string, Answer | null>, touched: Set<string>, opts?: GateOptions, quarantined?: ReadonlySet<string>): Selection;
|
package/dist/gate.js
CHANGED
|
@@ -27,6 +27,20 @@ export const DEFAULT_CUTOFF = 2.0;
|
|
|
27
27
|
export const DEFAULT_UNSURE_BELOW = 0.5;
|
|
28
28
|
/** How far under the cutoff an unsure answer is still rescued. */
|
|
29
29
|
export const DEFAULT_UNSURE_MARGIN = 1.0;
|
|
30
|
+
/**
|
|
31
|
+
* The values a gate will actually decide under, with every default filled
|
|
32
|
+
* in, in the spelling a record keeps them in. A record that said only "the
|
|
33
|
+
* flags that were given" would change meaning the day a default did.
|
|
34
|
+
*/
|
|
35
|
+
export function resolveGate({ cutoff = DEFAULT_CUTOFF, unsureBelow = DEFAULT_UNSURE_BELOW, unsureMargin = DEFAULT_UNSURE_MARGIN, } = {}) {
|
|
36
|
+
return { cutoff, unsure_below: unsureBelow, unsure_margin: unsureMargin };
|
|
37
|
+
}
|
|
38
|
+
/** A recorded or supplied gate as options. Nothing, when there is none. */
|
|
39
|
+
export function gateOptions(g) {
|
|
40
|
+
if (!g)
|
|
41
|
+
return {};
|
|
42
|
+
return { cutoff: g.cutoff, unsureBelow: g.unsure_below, unsureMargin: g.unsure_margin };
|
|
43
|
+
}
|
|
30
44
|
/** Decide one test. Never returns null: every test gets a side and a reason. */
|
|
31
45
|
export function decide(id, t, answer, touched, { cutoff = DEFAULT_CUTOFF, unsureBelow = DEFAULT_UNSURE_BELOW, unsureMargin = DEFAULT_UNSURE_MARGIN } = {}) {
|
|
32
46
|
if (touched)
|
|
@@ -47,11 +61,16 @@ export function decide(id, t, answer, touched, { cutoff = DEFAULT_CUTOFF, unsure
|
|
|
47
61
|
*
|
|
48
62
|
* `answers` is keyed by question id, which is the test's index in `tests`;
|
|
49
63
|
* `touched` is keyed by `testId`, because it is computed from the diff before
|
|
50
|
-
* any question exists
|
|
64
|
+
* any question exists; so is `quarantined`, the tests a context put in
|
|
65
|
+
* `skip`. A quarantined test is decided before anything else, touched
|
|
66
|
+
* included: it was never asked about, and the quarantine is flaker's decision
|
|
67
|
+
* that its result means nothing right now.
|
|
51
68
|
*/
|
|
52
|
-
export function gate(tests, answers, touched, opts = {}) {
|
|
69
|
+
export function gate(tests, answers, touched, opts = {}, quarantined = new Set()) {
|
|
53
70
|
const verdicts = tests.map((t, i) => {
|
|
54
71
|
const id = questionId(i);
|
|
72
|
+
if (quarantined.has(testId(t)))
|
|
73
|
+
return { id, test: t, answer: null, reason: "quarantined", selected: false };
|
|
55
74
|
return decide(id, t, answers.get(id) ?? null, touched.has(testId(t)), opts);
|
|
56
75
|
});
|
|
57
76
|
return {
|
package/dist/languages.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The grammars ast-grep does not bundle.
|
|
3
3
|
*
|
|
4
|
-
* `@ast-grep/napi` ships the web languages only; Rust and
|
|
4
|
+
* `@ast-grep/napi` ships the web languages only; Rust, Go and Ruby arrive as
|
|
5
5
|
* separate packages and have to be registered before the first `parse` that
|
|
6
6
|
* names them. Registration is process-wide and rejects a second call for the
|
|
7
7
|
* same language, so it happens once behind a flag rather than per file.
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
import { registerDynamicLanguage } from "@ast-grep/napi";
|
|
10
10
|
import rust from "@ast-grep/lang-rust";
|
|
11
11
|
import go from "@ast-grep/lang-go";
|
|
12
|
+
import ruby from "@ast-grep/lang-ruby";
|
|
12
13
|
let registered = false;
|
|
13
14
|
export function registerLanguages() {
|
|
14
15
|
if (registered)
|
|
15
16
|
return;
|
|
16
|
-
registerDynamicLanguage({ rust, go });
|
|
17
|
+
registerDynamicLanguage({ rust, go, ruby });
|
|
17
18
|
registered = true;
|
|
18
19
|
}
|
package/dist/questions.d.ts
CHANGED
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* One question per test.
|
|
3
|
-
*
|
|
4
|
-
* `score` rather than `choice` because the answer is an ordered conclusion:
|
|
5
|
-
* asking "how much can this change break this test" as an unordered choice
|
|
6
|
-
* discards the ordering, splits the probability mass between adjacent levels,
|
|
7
|
-
* and returns a low confidence that is indistinguishable from real
|
|
8
|
-
* uncertainty.
|
|
9
|
-
*
|
|
10
|
-
* No threshold ever appears in a question. The cutoff is a decision the gate
|
|
11
|
-
* makes from the answer; writing it into the question would mean every
|
|
12
|
-
* recalibration rewrote the question, and no run could be compared with an
|
|
13
|
-
* earlier one.
|
|
14
|
-
*/
|
|
15
1
|
import type { TestCase } from "./types.ts";
|
|
16
2
|
export interface ScoreQuestion {
|
|
17
3
|
type: "score";
|
|
@@ -29,7 +15,16 @@ export declare function questionId(i: number): string;
|
|
|
29
15
|
* its reporter prints it.
|
|
30
16
|
*/
|
|
31
17
|
export declare function displayName(t: TestCase): string;
|
|
32
|
-
|
|
18
|
+
/**
|
|
19
|
+
* The one sentence a hint becomes.
|
|
20
|
+
*
|
|
21
|
+
* A fact about the past and nothing more: nothing a model could read as a
|
|
22
|
+
* bar to clear, which is why flaker's `missed` count stays out of it. Whether
|
|
23
|
+
* THIS change can break the test is still the model's judgment.
|
|
24
|
+
*/
|
|
25
|
+
export declare function historySentence(files: readonly string[]): string;
|
|
26
|
+
/** `failedWith`: the files this test failed with before, from `--context`. */
|
|
27
|
+
export declare function buildQuestion(t: TestCase, id: string, failedWith?: readonly string[]): ScoreQuestion;
|
|
33
28
|
/**
|
|
34
29
|
* One answer, or null when it cannot be used.
|
|
35
30
|
*
|