specgate-cli 0.0.0-stage → 0.1.4
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/LICENSE +21 -0
- package/README.md +347 -2
- package/action.yml +83 -0
- package/assets/demo.gif +0 -0
- package/bin/specgate.js +221 -0
- package/examples/gherkin-export/features/export.feature +33 -0
- package/examples/gherkin-export/src/export.js +26 -0
- package/examples/gherkin-export/test/export.test.js +21 -0
- package/examples/invoice-export/SPEC.md +12 -0
- package/examples/invoice-export/src/export.js +20 -0
- package/examples/invoice-export/src/mailer.js +18 -0
- package/examples/invoice-export/test/export.test.js +27 -0
- package/examples/rust-counter/Cargo.toml +4 -0
- package/examples/rust-counter/SPEC.md +7 -0
- package/examples/rust-counter/src/lib.rs +19 -0
- package/examples/rust-counter/tests/counter.rs +9 -0
- package/package.json +48 -4
- package/src/aliases.js +151 -0
- package/src/index.js +175 -0
- package/src/matcher.js +183 -0
- package/src/report.js +190 -0
- package/src/scaffold/init.js +65 -0
- package/src/scaffold/spec-template.js +67 -0
- package/src/scaffold/stack.js +77 -0
- package/src/scaffold/workflow.js +45 -0
- package/src/spec/gherkin.js +164 -0
- package/src/spec/parse.js +148 -0
- package/src/status.js +24 -0
- package/src/walk.js +202 -0
package/src/matcher.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic evidence matching.
|
|
3
|
+
*
|
|
4
|
+
* The whole point of specgate is that the result is reproducible: same repo,
|
|
5
|
+
* same spec, same coverage. No model calls, no fuzzy guesses.
|
|
6
|
+
*
|
|
7
|
+
* Evidence tiers, strongest first:
|
|
8
|
+
* tagged an explicit `# AC-1` style marker in the file
|
|
9
|
+
* named the criterion's distinctive words appear in a test name
|
|
10
|
+
* text the words appear in a source file body
|
|
11
|
+
*
|
|
12
|
+
* A criterion with no evidence is `uncovered`. A criterion with evidence in a
|
|
13
|
+
* test file but no tagged marker is `untested` — implemented, but nothing
|
|
14
|
+
* guarantees it keeps working.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { tokenize, normaliseId } from './spec/parse.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Explicit criterion markers we accept in source code:
|
|
21
|
+
* # AC-1 (shell, python comment, yaml)
|
|
22
|
+
* // AC-1 (js, ts, go, rust)
|
|
23
|
+
* slash-star AC-1 slash-star (block comment)
|
|
24
|
+
* * AC-1 (doc comment continuation)
|
|
25
|
+
* @AC-1 (annotation, decorator)
|
|
26
|
+
* [AC-1] (plain bracket)
|
|
27
|
+
*
|
|
28
|
+
* Tags are only ever compared against criterion ids that actually exist in
|
|
29
|
+
* the spec, so an incidental "S3" in a comment is harmless.
|
|
30
|
+
*/
|
|
31
|
+
const TAGGED_RE = new RegExp(
|
|
32
|
+
'(?:^|\\s)(?:#|//|/\\*|\\*|@)\\s*([A-Za-z]{1,6}[-_ ]?\\d+(?:[._]\\d+)*)\\b',
|
|
33
|
+
'g'
|
|
34
|
+
);
|
|
35
|
+
const BRACKETED_RE = new RegExp('\\[([A-Za-z]{1,6}[-_ ]?\\d+(?:[._]\\d+)*)\\]', 'g');
|
|
36
|
+
|
|
37
|
+
/** Pull explicit criterion markers out of a line of code or prose. */
|
|
38
|
+
export function extractTags(text) {
|
|
39
|
+
const out = new Set();
|
|
40
|
+
const source = String(text);
|
|
41
|
+
let m;
|
|
42
|
+
TAGGED_RE.lastIndex = 0;
|
|
43
|
+
while ((m = TAGGED_RE.exec(source)) !== null) {
|
|
44
|
+
out.add(normaliseId(m[1]));
|
|
45
|
+
}
|
|
46
|
+
BRACKETED_RE.lastIndex = 0;
|
|
47
|
+
while ((m = BRACKETED_RE.exec(source)) !== null) {
|
|
48
|
+
out.add(normaliseId(m[1]));
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Words that carry no discriminating power when matching a criterion to a
|
|
55
|
+
* test name. Everything else in the criterion must appear in the test.
|
|
56
|
+
*/
|
|
57
|
+
const WEAK_TOKENS = new Set([
|
|
58
|
+
'system', 'user', 'users', 'page', 'screen', 'button', 'form', 'field',
|
|
59
|
+
'data', 'value', 'result', 'results', 'api', 'endpoint', 'request',
|
|
60
|
+
'response', 'feature', 'features', 'allow', 'allows', 'support', 'supports',
|
|
61
|
+
'display', 'show', 'shows', 'shown', 'return', 'returns', 'able', 'correctly',
|
|
62
|
+
'properly', 'proper', 'appropriate', 'expected', 'way', 'ways', 'option',
|
|
63
|
+
'settings', 'setting', 'settings page', 'dashboard', 'home', 'main',
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
/** Distinctive tokens of a criterion: content words, longest first. */
|
|
67
|
+
export function keyTerms(text, limit = 6) {
|
|
68
|
+
const tokens = [...new Set(tokenize(text))].filter((t) => !WEAK_TOKENS.has(t));
|
|
69
|
+
const sorted = tokens.sort((a, b) => b.length - a.length);
|
|
70
|
+
return sorted.slice(0, limit);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Fraction of a criterion's key terms present in a haystack. */
|
|
74
|
+
export function termCoverage(terms, haystackTokens) {
|
|
75
|
+
if (!terms.length) return 0;
|
|
76
|
+
let hits = 0;
|
|
77
|
+
for (const t of terms) {
|
|
78
|
+
if (haystackTokens.has(t)) hits += 1;
|
|
79
|
+
}
|
|
80
|
+
return hits / terms.length;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @typedef {object} EvidenceFile
|
|
85
|
+
* @property {string} path
|
|
86
|
+
* @property {boolean} isTest
|
|
87
|
+
* @property {string} content
|
|
88
|
+
* @property {string[]} lines
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Match one criterion against the indexed files.
|
|
93
|
+
* @returns {{status: string, tier: string|null, evidence: object|null}}
|
|
94
|
+
*/
|
|
95
|
+
export function matchCriterion(criterion, files) {
|
|
96
|
+
const id = criterion.normalisedId;
|
|
97
|
+
// A Gherkin scenario is matched on its title *or* on any single step: the
|
|
98
|
+
// step definition in the code is phrased like the step, not like the title.
|
|
99
|
+
// Matching the whole scenario text at once would demand every step in one
|
|
100
|
+
// file, which is almost never true.
|
|
101
|
+
const searchTexts = criterion.searchTexts && criterion.searchTexts.length
|
|
102
|
+
? criterion.searchTexts
|
|
103
|
+
: [criterion.text];
|
|
104
|
+
const termSets = searchTexts.map((t) => keyTerms(t));
|
|
105
|
+
|
|
106
|
+
let best = null;
|
|
107
|
+
|
|
108
|
+
for (const file of files) {
|
|
109
|
+
// The searchable text of a file is its content plus, when the project uses
|
|
110
|
+
// path aliases, its own path. A criterion that talks about "the audit
|
|
111
|
+
// writer" should be able to find `src/lib/audit-writer.ts` even when the
|
|
112
|
+
// file body never says so.
|
|
113
|
+
const pathText = file.searchPath ? `${file.searchPath} ${(file.aliasTargets || []).join(' ')}` : '';
|
|
114
|
+
const fileTokens = new Set(tokenize(`${file.content} ${pathText}`));
|
|
115
|
+
|
|
116
|
+
// Tier 1: explicit tag anywhere in the file.
|
|
117
|
+
if (file.tags && file.tags.has(id)) {
|
|
118
|
+
const score = 1;
|
|
119
|
+
if (!best || score > best.score) {
|
|
120
|
+
best = { tier: 'tagged', score, file: file.path, isTest: file.isTest, line: file.tagLines.get(id) };
|
|
121
|
+
}
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Tier 2/3: lexical coverage. The best-matching phrase wins, so one
|
|
126
|
+
// matching step is enough evidence for the whole scenario.
|
|
127
|
+
let cov = 0;
|
|
128
|
+
for (const terms of termSets) {
|
|
129
|
+
const c = termCoverage(terms, fileTokens);
|
|
130
|
+
if (c > cov) cov = c;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The module-name signal. When a file is reached through a tsconfig path
|
|
134
|
+
// alias, its name is what the criterion is most likely talking about:
|
|
135
|
+
// "the audit writer" means `audit-writer.ts`. This is a containment test,
|
|
136
|
+
// not a ratio — the criterion names the whole module, even when the rest
|
|
137
|
+
// of the sentence is about something else. Using a ratio here would need
|
|
138
|
+
// a lower threshold, and a lower threshold would weaken every ordinary
|
|
139
|
+
// match in every project.
|
|
140
|
+
let moduleMatch = false;
|
|
141
|
+
if (file.moduleName) {
|
|
142
|
+
const moduleTokens = tokenize(file.moduleName);
|
|
143
|
+
if (moduleTokens.length) {
|
|
144
|
+
for (const text of searchTexts) {
|
|
145
|
+
const criterionTokens = new Set(tokenize(text));
|
|
146
|
+
if (moduleTokens.every((t) => criterionTokens.has(t))) {
|
|
147
|
+
moduleMatch = true;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!moduleMatch && cov < 0.6) continue;
|
|
155
|
+
|
|
156
|
+
const tier = file.isTest ? 'named' : 'text';
|
|
157
|
+
const score = (moduleMatch ? 0.6 : cov) + (file.isTest ? 0.5 : 0);
|
|
158
|
+
if (!best || score > best.score) {
|
|
159
|
+
best = { tier, score, file: file.path, isTest: file.isTest, coverage: cov };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!best) return { status: 'uncovered', tier: null, evidence: null };
|
|
164
|
+
if (best.tier === 'text') return { status: 'untested', tier: 'text', evidence: best };
|
|
165
|
+
if (best.tier === 'named') return { status: 'inferred', tier: 'named', evidence: best };
|
|
166
|
+
return { status: 'covered', tier: 'tagged', evidence: best };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Index files once, then reuse across criteria. */
|
|
170
|
+
export function indexFiles(fileEntries) {
|
|
171
|
+
return fileEntries.map((f) => {
|
|
172
|
+
const tags = new Set();
|
|
173
|
+
const tagLines = new Map();
|
|
174
|
+
const lines = f.content.split(/\r?\n/);
|
|
175
|
+
lines.forEach((line, idx) => {
|
|
176
|
+
for (const tag of extractTags(line)) {
|
|
177
|
+
tags.add(tag);
|
|
178
|
+
if (!tagLines.has(tag)) tagLines.set(tag, idx + 1);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
return { ...f, lines, tags, tagLines };
|
|
182
|
+
});
|
|
183
|
+
}
|
package/src/report.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coverage report generation: score, table, JSON, SARIF.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { STATUS_LABEL, STATUS_WEIGHT } from './status.js';
|
|
6
|
+
|
|
7
|
+
export function computeScore(criteria) {
|
|
8
|
+
const total = criteria.length;
|
|
9
|
+
if (!total) return { score: 0, total: 0, covered: 0, inferred: 0, untested: 0, uncovered: 0 };
|
|
10
|
+
|
|
11
|
+
let covered = 0;
|
|
12
|
+
let inferred = 0;
|
|
13
|
+
let untested = 0;
|
|
14
|
+
let uncovered = 0;
|
|
15
|
+
for (const c of criteria) {
|
|
16
|
+
if (c.status === 'covered') covered += 1;
|
|
17
|
+
else if (c.status === 'inferred') inferred += 1;
|
|
18
|
+
else if (c.status === 'untested') untested += 1;
|
|
19
|
+
else uncovered += 1;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Only explicit, traceable evidence counts fully. Inferred evidence is
|
|
23
|
+
// discounted because a name match is a hint, not a guarantee.
|
|
24
|
+
const weighted =
|
|
25
|
+
covered * 1.0 + inferred * 0.75 + untested * 0.4 + uncovered * 0.0;
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
score: Math.round((weighted / total) * 100),
|
|
29
|
+
total,
|
|
30
|
+
covered,
|
|
31
|
+
inferred,
|
|
32
|
+
untested,
|
|
33
|
+
uncovered,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const SYMBOL = {
|
|
38
|
+
covered: '✔',
|
|
39
|
+
inferred: '~',
|
|
40
|
+
untested: '!',
|
|
41
|
+
uncovered: '✗',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Human-readable console report. */
|
|
45
|
+
export function renderText(report, opts = {}) {
|
|
46
|
+
const { score, total } = report.summary;
|
|
47
|
+
const lines = [];
|
|
48
|
+
|
|
49
|
+
lines.push('');
|
|
50
|
+
lines.push(` Requirement coverage: ${score}% (${total} criteria)`);
|
|
51
|
+
lines.push('');
|
|
52
|
+
lines.push(` ${SYMBOL.covered} covered ${report.summary.covered}`);
|
|
53
|
+
lines.push(` ${SYMBOL.inferred} inferred ${report.summary.inferred}`);
|
|
54
|
+
lines.push(` ${SYMBOL.untested} untested ${report.summary.untested}`);
|
|
55
|
+
lines.push(` ${SYMBOL.uncovered} uncovered ${report.summary.uncovered}`);
|
|
56
|
+
lines.push('');
|
|
57
|
+
|
|
58
|
+
if (!report.criteria.length) {
|
|
59
|
+
lines.push(' No acceptance criteria found.');
|
|
60
|
+
lines.push('');
|
|
61
|
+
return lines.join('\n');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const showAll = opts.verbose || score < 100;
|
|
65
|
+
const shown = showAll ? report.criteria : report.criteria.filter((c) => c.status !== 'covered');
|
|
66
|
+
|
|
67
|
+
if (shown.length) {
|
|
68
|
+
lines.push(' Criterion Status Evidence');
|
|
69
|
+
lines.push(' ' + '-'.repeat(78));
|
|
70
|
+
for (const c of shown) {
|
|
71
|
+
const id = c.id.padEnd(12).slice(0, 12);
|
|
72
|
+
const status = STATUS_LABEL[c.status].padEnd(10).slice(0, 10);
|
|
73
|
+
const ev = c.evidence
|
|
74
|
+
? `${c.evidence.tier === 'tagged' ? '#' : ''}${c.evidence.file}${c.evidence.line ? ':' + c.evidence.line : ''}`
|
|
75
|
+
: '—';
|
|
76
|
+
lines.push(` ${id} ${status} ${ev.slice(0, 54)}`);
|
|
77
|
+
lines.push(` ${' '.repeat(12)} ${c.text.slice(0, 70)}`);
|
|
78
|
+
}
|
|
79
|
+
lines.push('');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (report.summary.uncovered || report.summary.untested) {
|
|
83
|
+
lines.push(' Gaps:');
|
|
84
|
+
for (const c of report.criteria) {
|
|
85
|
+
if (c.status === 'uncovered' || c.status === 'untested') {
|
|
86
|
+
lines.push(` ${c.id} ${c.text.slice(0, 66)}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
lines.push('');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return lines.join('\n');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Machine-readable JSON report. */
|
|
96
|
+
export function renderJson(report) {
|
|
97
|
+
return JSON.stringify(
|
|
98
|
+
{
|
|
99
|
+
version: report.version,
|
|
100
|
+
score: report.summary.score,
|
|
101
|
+
summary: report.summary,
|
|
102
|
+
specs: report.specs,
|
|
103
|
+
criteria: report.criteria.map((c) => ({
|
|
104
|
+
id: c.id,
|
|
105
|
+
text: c.text,
|
|
106
|
+
section: c.section,
|
|
107
|
+
file: c.file,
|
|
108
|
+
line: c.line,
|
|
109
|
+
status: c.status,
|
|
110
|
+
statusLabel: STATUS_LABEL[c.status],
|
|
111
|
+
weight: STATUS_WEIGHT[c.status],
|
|
112
|
+
evidence: c.evidence
|
|
113
|
+
? {
|
|
114
|
+
tier: c.evidence.tier,
|
|
115
|
+
file: c.evidence.file,
|
|
116
|
+
line: c.evidence.line || null,
|
|
117
|
+
coverage: c.evidence.coverage ?? null,
|
|
118
|
+
}
|
|
119
|
+
: null,
|
|
120
|
+
})),
|
|
121
|
+
},
|
|
122
|
+
null,
|
|
123
|
+
2
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* SARIF 2.1.0 output so findings show up in GitHub code scanning.
|
|
129
|
+
*/
|
|
130
|
+
export function renderSarif(report) {
|
|
131
|
+
const rules = [
|
|
132
|
+
{ id: 'spec-uncovered', name: 'RequirementUncovered', level: 'warning' },
|
|
133
|
+
{ id: 'spec-untested', name: 'RequirementUntested', level: 'warning' },
|
|
134
|
+
{ id: 'spec-inferred', name: 'RequirementInferred', level: 'note' },
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
const ruleIndex = new Map(rules.map((r, i) => [r.id, i]));
|
|
138
|
+
const results = [];
|
|
139
|
+
|
|
140
|
+
for (const c of report.criteria) {
|
|
141
|
+
if (c.status === 'covered') continue;
|
|
142
|
+
const ruleId =
|
|
143
|
+
c.status === 'uncovered' ? 'spec-uncovered'
|
|
144
|
+
: c.status === 'untested' ? 'spec-untested'
|
|
145
|
+
: 'spec-inferred';
|
|
146
|
+
const rule = rules[ruleIndex.get(ruleId)];
|
|
147
|
+
const message =
|
|
148
|
+
c.status === 'uncovered'
|
|
149
|
+
? `Acceptance criterion "${c.id}" has no evidence in the codebase.`
|
|
150
|
+
: c.status === 'untested'
|
|
151
|
+
? `Acceptance criterion "${c.id}" is implemented but not covered by a tagged test.`
|
|
152
|
+
: `Acceptance criterion "${c.id}" is only matched by test naming; add an explicit tag to make it verifiable.`;
|
|
153
|
+
|
|
154
|
+
results.push({
|
|
155
|
+
ruleId,
|
|
156
|
+
level: rule.level,
|
|
157
|
+
message: { text: message },
|
|
158
|
+
locations: [
|
|
159
|
+
{
|
|
160
|
+
physicalLocation: {
|
|
161
|
+
artifactLocation: { uri: c.file, uriBaseId: '%SRCROOT%' },
|
|
162
|
+
region: { startLine: c.line || 1 },
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return JSON.stringify(
|
|
170
|
+
{
|
|
171
|
+
$schema: 'https://json.schemastore.org/sarif-2.1.0.json',
|
|
172
|
+
version: '2.1.0',
|
|
173
|
+
runs: [
|
|
174
|
+
{
|
|
175
|
+
tool: {
|
|
176
|
+
driver: {
|
|
177
|
+
name: 'specgate',
|
|
178
|
+
informationUri: 'https://github.com/aazh2026/specgate',
|
|
179
|
+
version: report.version,
|
|
180
|
+
rules,
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
results,
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
},
|
|
187
|
+
null,
|
|
188
|
+
2
|
|
189
|
+
);
|
|
190
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `specgate init` — scaffold a spec and a CI gate in an existing project.
|
|
3
|
+
*
|
|
4
|
+
* The barrier to using a requirement-coverage tool is that most repos have no
|
|
5
|
+
* written spec to point it at. `init` removes that barrier: it inspects the
|
|
6
|
+
* project, writes a SPEC.md with the acceptance criteria already structured,
|
|
7
|
+
* and drops in a CI workflow that gates on coverage.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
|
|
13
|
+
import { detectStack } from './stack.js';
|
|
14
|
+
import { renderSpecTemplate } from './spec-template.js';
|
|
15
|
+
import { renderWorkflow } from './workflow.js';
|
|
16
|
+
|
|
17
|
+
const SPEC_HEADER = '## Acceptance Criteria';
|
|
18
|
+
|
|
19
|
+
export function init(root, opts = {}) {
|
|
20
|
+
const created = [];
|
|
21
|
+
const skipped = [];
|
|
22
|
+
|
|
23
|
+
const specPath = path.resolve(root, opts.spec || 'SPEC.md');
|
|
24
|
+
if (fs.existsSync(specPath) && !opts.force) {
|
|
25
|
+
skipped.push({ file: path.relative(root, specPath), reason: 'already exists' });
|
|
26
|
+
} else {
|
|
27
|
+
const stack = detectStack(root);
|
|
28
|
+
const content = renderSpecTemplate(stack, opts);
|
|
29
|
+
fs.mkdirSync(path.dirname(specPath), { recursive: true });
|
|
30
|
+
fs.writeFileSync(specPath, content);
|
|
31
|
+
created.push(path.relative(root, specPath));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const workflowDir = path.join(root, '.github', 'workflows');
|
|
35
|
+
const workflowPath = path.join(workflowDir, 'requirements.yml');
|
|
36
|
+
if (fs.existsSync(workflowPath) && !opts.force) {
|
|
37
|
+
skipped.push({ file: '.github/workflows/requirements.yml', reason: 'already exists' });
|
|
38
|
+
} else {
|
|
39
|
+
fs.mkdirSync(workflowDir, { recursive: true });
|
|
40
|
+
fs.writeFileSync(workflowPath, renderWorkflow(opts));
|
|
41
|
+
created.push('.github/workflows/requirements.yml');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { created, skipped, specPath };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Human-readable summary printed by the CLI. */
|
|
48
|
+
export function renderInitResult(result) {
|
|
49
|
+
const lines = [''];
|
|
50
|
+
for (const file of result.created) {
|
|
51
|
+
lines.push(` created ${file}`);
|
|
52
|
+
}
|
|
53
|
+
for (const file of result.skipped) {
|
|
54
|
+
lines.push(` skipped ${file} (${file.reason})`);
|
|
55
|
+
}
|
|
56
|
+
lines.push('');
|
|
57
|
+
lines.push(' Next:');
|
|
58
|
+
lines.push(' 1. Edit the acceptance criteria in your spec.');
|
|
59
|
+
lines.push(' 2. Tag your tests with the criterion id, e.g. // AC-1');
|
|
60
|
+
lines.push(' 3. Run `specgate` to see coverage.');
|
|
61
|
+
lines.push('');
|
|
62
|
+
return lines.join('\n');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { SPEC_HEADER };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SPEC.md template.
|
|
3
|
+
*
|
|
4
|
+
* The template deliberately ships with *example* criteria that reference
|
|
5
|
+
* common features, plus empty ones to fill in. A blank file teaches nothing;
|
|
6
|
+
* a filled-in example shows the shape immediately.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { extractTags } from '../matcher.js';
|
|
10
|
+
|
|
11
|
+
const EXAMPLES = [
|
|
12
|
+
'User can sign in with email and password',
|
|
13
|
+
'User can reset their password via an emailed link',
|
|
14
|
+
'Invalid input is rejected with a helpful message',
|
|
15
|
+
'The user sees a loading state while the request is in flight',
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
export function renderSpecTemplate(stack, opts = {}) {
|
|
19
|
+
const name = opts.name || 'your-project';
|
|
20
|
+
const lines = [];
|
|
21
|
+
|
|
22
|
+
lines.push(`# ${name} — Specification`);
|
|
23
|
+
lines.push('');
|
|
24
|
+
lines.push('> Acceptance criteria for this project. `specgate` checks each one');
|
|
25
|
+
lines.push('> against the code and reports which requirements have no evidence.');
|
|
26
|
+
lines.push('>');
|
|
27
|
+
lines.push('> Delete the examples you do not need and write your own.');
|
|
28
|
+
lines.push('');
|
|
29
|
+
lines.push('## Acceptance Criteria');
|
|
30
|
+
lines.push('');
|
|
31
|
+
|
|
32
|
+
EXAMPLES.forEach((text, i) => {
|
|
33
|
+
lines.push(`- [ ] AC-${i + 1}: ${text}`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
lines.push('');
|
|
37
|
+
lines.push('## Notes');
|
|
38
|
+
lines.push('');
|
|
39
|
+
lines.push(`- Language: ${stack.language}`);
|
|
40
|
+
lines.push(`- Test command: ${stack.testRunner}`);
|
|
41
|
+
lines.push(`- Package manager: ${stack.packageManager}`);
|
|
42
|
+
lines.push('');
|
|
43
|
+
lines.push('<!--');
|
|
44
|
+
lines.push(' Add a criterion id to a test to make it verifiable:');
|
|
45
|
+
lines.push('');
|
|
46
|
+
lines.push(' // AC-1: User can sign in with email and password');
|
|
47
|
+
lines.push(' test("signs in with email and password", () => { ... });');
|
|
48
|
+
lines.push('-->');
|
|
49
|
+
lines.push('');
|
|
50
|
+
|
|
51
|
+
return lines.join('\n');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Suggest criterion ids for existing test files, so the user can paste them
|
|
56
|
+
* straight into their test files.
|
|
57
|
+
*/
|
|
58
|
+
export function suggestTags(root, codeFiles) {
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const file of codeFiles) {
|
|
61
|
+
if (!file.isTest) continue;
|
|
62
|
+
const tags = extractTags(file.content);
|
|
63
|
+
if (tags.size === 0) continue;
|
|
64
|
+
out.push({ file: file.path, tags: [...tags] });
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect what a project is built with, so the scaffolded spec and CI
|
|
3
|
+
* workflow match reality rather than guessing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
function exists(root, rel) {
|
|
10
|
+
try {
|
|
11
|
+
return fs.existsSync(path.join(root, rel));
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function readJSON(root, rel) {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(fs.readFileSync(path.join(root, rel), 'utf8'));
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @returns {{language: string, testRunner: string, ci: string[], packageManager: string}}
|
|
27
|
+
*/
|
|
28
|
+
export function detectStack(root) {
|
|
29
|
+
const pkg = readJSON(root, 'package.json');
|
|
30
|
+
const hasPython = exists(root, 'pyproject.toml') || exists(root, 'setup.py') || exists(root, 'requirements.txt');
|
|
31
|
+
const hasGo = exists(root, 'go.mod');
|
|
32
|
+
const hasRust = exists(root, 'Cargo.toml');
|
|
33
|
+
const hasRuby = exists(root, 'Gemfile');
|
|
34
|
+
const hasJava = exists(root, 'pom.xml') || exists(root, 'build.gradle');
|
|
35
|
+
|
|
36
|
+
let language = 'javascript';
|
|
37
|
+
if (pkg && pkg.dependencies && Object.keys(pkg.dependencies || {}).length >= 0 && exists(root, 'package.json')) {
|
|
38
|
+
language = 'javascript';
|
|
39
|
+
}
|
|
40
|
+
if (hasPython && !pkg) language = 'python';
|
|
41
|
+
else if (hasPython && pkg) language = 'javascript'; // mixed; JS tooling still applies
|
|
42
|
+
if (hasGo && !pkg && !hasPython) language = 'go';
|
|
43
|
+
if (hasRust && !pkg && !hasPython && !hasGo) language = 'rust';
|
|
44
|
+
if (hasRuby && !pkg && !hasPython && !hasGo && !hasRust) language = 'ruby';
|
|
45
|
+
if (hasJava && !pkg && !hasPython && !hasGo && !hasRust && !hasRuby) language = 'java';
|
|
46
|
+
|
|
47
|
+
// Test runner
|
|
48
|
+
let testRunner = 'node --test';
|
|
49
|
+
if (language === 'python') testRunner = 'pytest';
|
|
50
|
+
if (pkg) {
|
|
51
|
+
const dev = { ...(pkg.devDependencies || {}), ...(pkg.dependencies || {}) };
|
|
52
|
+
if (dev.vitest) testRunner = 'vitest run';
|
|
53
|
+
else if (dev.jest) testRunner = 'jest';
|
|
54
|
+
else if (dev.mocha) testRunner = 'npm test';
|
|
55
|
+
} else if (language === 'python') {
|
|
56
|
+
testRunner = 'pytest';
|
|
57
|
+
} else if (language === 'go') {
|
|
58
|
+
testRunner = 'go test ./...';
|
|
59
|
+
} else if (language === 'rust') {
|
|
60
|
+
testRunner = 'cargo test';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Package manager
|
|
64
|
+
let packageManager = 'npm';
|
|
65
|
+
if (exists(root, 'pnpm-lock.yaml')) packageManager = 'pnpm';
|
|
66
|
+
else if (exists(root, 'yarn.lock')) packageManager = 'yarn';
|
|
67
|
+
else if (exists(root, 'bun.lockb') || exists(root, 'bun.lock')) packageManager = 'bun';
|
|
68
|
+
else if (language === 'python') packageManager = 'pip';
|
|
69
|
+
else if (language === 'go') packageManager = 'go';
|
|
70
|
+
else if (language === 'rust') packageManager = 'cargo';
|
|
71
|
+
|
|
72
|
+
const ci = [];
|
|
73
|
+
if (exists(root, '.github/workflows')) ci.push('github-actions');
|
|
74
|
+
if (exists(root, '.gitlab-ci.yml')) ci.push('gitlab-ci');
|
|
75
|
+
|
|
76
|
+
return { language, testRunner, ci, packageManager, hasPackageJson: Boolean(pkg) };
|
|
77
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub Actions workflow that gates on requirement coverage.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function renderWorkflow(opts = {}) {
|
|
6
|
+
const min = opts.min ?? 80;
|
|
7
|
+
const failUncovered = opts.failUncovered !== false;
|
|
8
|
+
|
|
9
|
+
const args = [`--min ${min}`];
|
|
10
|
+
if (failUncovered) args.push('--fail-uncovered');
|
|
11
|
+
|
|
12
|
+
return `# Requirement coverage gate.
|
|
13
|
+
#
|
|
14
|
+
# Generated by \`specgate init\`. Adjust the threshold below to match your
|
|
15
|
+
# project's current state, then raise it as coverage improves.
|
|
16
|
+
|
|
17
|
+
name: Requirement coverage
|
|
18
|
+
|
|
19
|
+
on:
|
|
20
|
+
pull_request:
|
|
21
|
+
push:
|
|
22
|
+
branches: [main]
|
|
23
|
+
|
|
24
|
+
jobs:
|
|
25
|
+
specgate:
|
|
26
|
+
runs-on: ubuntu-latest
|
|
27
|
+
steps:
|
|
28
|
+
- uses: actions/checkout@v4
|
|
29
|
+
- uses: actions/setup-node@v4
|
|
30
|
+
with:
|
|
31
|
+
node-version: 20
|
|
32
|
+
|
|
33
|
+
- name: Check requirement coverage
|
|
34
|
+
uses: aazh2026/specgate@v0.1.1
|
|
35
|
+
with:
|
|
36
|
+
min: ${min}
|
|
37
|
+
fail-uncovered: ${failUncovered}
|
|
38
|
+
|
|
39
|
+
# Or run the CLI directly:
|
|
40
|
+
# - uses: actions/setup-node@v4
|
|
41
|
+
# with:
|
|
42
|
+
# node-version: 20
|
|
43
|
+
# - run: npx --yes github:aazh2026/specgate ${args.join(' ')}
|
|
44
|
+
`;
|
|
45
|
+
}
|