@invariantly/cli 0.0.0-stage → 0.0.3
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 +201 -0
- package/NOTICE +4 -0
- package/README.md +93 -2
- package/dist/base.d.ts +35 -0
- package/dist/base.js +300 -0
- package/dist/check.d.ts +125 -0
- package/dist/check.js +1247 -0
- package/dist/git.d.ts +36 -0
- package/dist/git.js +765 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +154 -0
- package/dist/validate.d.ts +15 -0
- package/dist/validate.js +68 -0
- package/package.json +59 -4
- package/schema/invariants.v0.schema.json +70 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { BaseRefError, compareWithBase } from "./base.js";
|
|
5
|
+
import { checkFile } from "./check.js";
|
|
6
|
+
import { GitError } from "./git.js";
|
|
7
|
+
import { validateFile } from "./validate.js";
|
|
8
|
+
const pkg = readJson("../package.json");
|
|
9
|
+
const schema = readJson("../schema/invariants.v0.schema.json");
|
|
10
|
+
const HELP = `invariantly ${pkg.version}
|
|
11
|
+
|
|
12
|
+
Usage:
|
|
13
|
+
invariantly validate [file] [--json] Check a registry (default: invariants.yaml)
|
|
14
|
+
invariantly check [file] [--json] [--base <ref>]
|
|
15
|
+
Validate the registry, then check that every rule has an
|
|
16
|
+
active [INV-NN] test and every marker names a rule
|
|
17
|
+
(scans *.test.* / *.spec.* files and pytest's test_*.py /
|
|
18
|
+
*_test.py files under the registry's directory)
|
|
19
|
+
--base <ref> Also report whether the commits since the merge base of
|
|
20
|
+
<ref> and HEAD change a contract path, a rule, or remove
|
|
21
|
+
or skip a guarding test. Advisory: never changes the exit
|
|
22
|
+
code. Compares commits only; reads .git, runs no git.
|
|
23
|
+
invariantly --version
|
|
24
|
+
invariantly --help
|
|
25
|
+
|
|
26
|
+
Exit codes:
|
|
27
|
+
validate 0 valid, 1 invalid registry, 2 usage or file error.
|
|
28
|
+
check 0 valid and every rule linked, 1 invalid registry, a rule without an active test
|
|
29
|
+
or a marker naming an unknown rule, 2 usage or file error (with --base also an
|
|
30
|
+
unknown ref, a missing merge base or a repository the reader cannot handle).
|
|
31
|
+
--json documents carry "schemaVersion": 1; unknown reason kinds and warning codes must be ignored.
|
|
32
|
+
Format: https://github.com/invariantly-dev/invariantly/blob/main/schema/invariants.v0.schema.json`;
|
|
33
|
+
async function main(argv) {
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = parseArgs({
|
|
37
|
+
args: argv,
|
|
38
|
+
allowPositionals: true,
|
|
39
|
+
options: {
|
|
40
|
+
json: { type: "boolean", default: false },
|
|
41
|
+
base: { type: "string" },
|
|
42
|
+
help: { type: "boolean", short: "h", default: false },
|
|
43
|
+
version: { type: "boolean", short: "v", default: false },
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.error(`${error.message}\n\n${HELP}`);
|
|
49
|
+
return 2;
|
|
50
|
+
}
|
|
51
|
+
const { values, positionals } = parsed;
|
|
52
|
+
if (values.version) {
|
|
53
|
+
console.log(pkg.version);
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
const [command, file = "invariants.yaml", ...extra] = positionals;
|
|
57
|
+
if (values.help || command === undefined) {
|
|
58
|
+
console.log(HELP);
|
|
59
|
+
return values.help ? 0 : 2;
|
|
60
|
+
}
|
|
61
|
+
if ((command !== "validate" && command !== "check") || extra.length > 0) {
|
|
62
|
+
console.error(`Unknown command or extra arguments: ${positionals.join(" ")}\n\n${HELP}`);
|
|
63
|
+
return 2;
|
|
64
|
+
}
|
|
65
|
+
const ref = values.base;
|
|
66
|
+
if (ref !== undefined && (command !== "check" || ref === "" || ref.startsWith("-"))) {
|
|
67
|
+
console.error(`${command !== "check" ? "--base works only with check" : `invalid base ref: ${ref}`}\n\n${HELP}`);
|
|
68
|
+
return 2;
|
|
69
|
+
}
|
|
70
|
+
// The comparison runs first, so a missing ref prints no report at all.
|
|
71
|
+
let comparison;
|
|
72
|
+
if (ref !== undefined) {
|
|
73
|
+
try {
|
|
74
|
+
comparison = await compareWithBase(file, schema, ref);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (error instanceof BaseRefError || error instanceof GitError) {
|
|
78
|
+
console.error(error.message);
|
|
79
|
+
return 2;
|
|
80
|
+
}
|
|
81
|
+
const err = error;
|
|
82
|
+
console.error(err.code === "ENOENT" ? `File not found: ${err.path ?? file}` : `Cannot read ${err.path ?? file}: ${err.message}`);
|
|
83
|
+
return 2;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
let result;
|
|
87
|
+
try {
|
|
88
|
+
result = command === "check"
|
|
89
|
+
? await checkFile(file, schema, values.json ? {} : { warn: (message) => console.error(`⚠ ${message}`) })
|
|
90
|
+
: await validateFile(file, schema);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
const err = error;
|
|
94
|
+
const target = err.path ?? file;
|
|
95
|
+
console.error(err.code === "ENOENT" ? `File not found: ${target}` : `Cannot read ${target}: ${err.message}`);
|
|
96
|
+
return 2;
|
|
97
|
+
}
|
|
98
|
+
if (values.json) {
|
|
99
|
+
// schemaVersion comes first; check's warnings are reported here, not on stderr.
|
|
100
|
+
const { warnings, ...rest } = result;
|
|
101
|
+
const doc = command === "check"
|
|
102
|
+
? { schemaVersion: 1, ...rest, ...(comparison ? { contractChange: comparison.contractChange } : {}), warnings }
|
|
103
|
+
: { schemaVersion: 1, ...result };
|
|
104
|
+
console.log(JSON.stringify(doc, null, 2));
|
|
105
|
+
}
|
|
106
|
+
else if (!result.ok && result.problems.length > 0) {
|
|
107
|
+
console.error(`✖ ${file}: ${result.problems.length} problem(s)`);
|
|
108
|
+
for (const p of result.problems)
|
|
109
|
+
console.error(` ${p.path || "(file)"} ${p.message}`);
|
|
110
|
+
}
|
|
111
|
+
else if (command === "check") {
|
|
112
|
+
printCheck(file, result);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
console.log(`✔ ${file}: ${result.ruleCount} rule(s), format v0 valid`);
|
|
116
|
+
}
|
|
117
|
+
if (comparison && !values.json)
|
|
118
|
+
printContractChange(comparison);
|
|
119
|
+
// A contract change is advisory: the exit code follows the coverage result only (OD-01).
|
|
120
|
+
return result.ok ? 0 : 1;
|
|
121
|
+
}
|
|
122
|
+
function printCheck(file, result) {
|
|
123
|
+
const scanned = `${result.testFileCount} test file(s)`;
|
|
124
|
+
if (result.ok) {
|
|
125
|
+
console.log(`✔ ${file}: ${result.ruleCount} rule(s), each with an active test (${scanned})`);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const count = result.untested.length + result.unknownMarkers.length;
|
|
129
|
+
console.error(`✖ ${file}: ${count} problem(s) (${scanned})`);
|
|
130
|
+
for (const rule of result.untested) {
|
|
131
|
+
const skipped = rule.skipped > 0 ? ` (${rule.skipped} skipped or todo)` : "";
|
|
132
|
+
console.error(` ${rule.id} no active test${skipped}`);
|
|
133
|
+
}
|
|
134
|
+
for (const m of result.unknownMarkers) {
|
|
135
|
+
console.error(` ${m.file}:${m.line} marker [${m.id}] names no rule in the registry: "${m.test}"`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function printContractChange({ ref, contractChange, globs }) {
|
|
139
|
+
if (!contractChange.changed) {
|
|
140
|
+
console.log(`✔ no contract change since ${ref}`);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
console.log(`⚑ contract-change since ${ref}: ${contractChange.reasons.length} reason(s)`);
|
|
144
|
+
const width = Math.max(...contractChange.reasons.map((r) => r.kind.length));
|
|
145
|
+
for (const r of contractChange.reasons) {
|
|
146
|
+
const glob = globs.get(r.path);
|
|
147
|
+
const detail = r.ruleId ?? (r.kind === "contract-path" && glob !== undefined ? `matches ${glob}` : "");
|
|
148
|
+
console.log(` ${r.kind.padEnd(width)} ${r.path}${detail ? ` ${detail}` : ""}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function readJson(relative) {
|
|
152
|
+
return JSON.parse(readFileSync(new URL(relative, import.meta.url), "utf8"));
|
|
153
|
+
}
|
|
154
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface Problem {
|
|
2
|
+
/** JSON-pointer-like location inside the registry, e.g. "/rules/2/id". Empty for the whole file. */
|
|
3
|
+
path: string;
|
|
4
|
+
message: string;
|
|
5
|
+
}
|
|
6
|
+
export interface Result {
|
|
7
|
+
file: string;
|
|
8
|
+
ok: boolean;
|
|
9
|
+
ruleCount: number;
|
|
10
|
+
problems: Problem[];
|
|
11
|
+
}
|
|
12
|
+
/** Validates registry text. Never touches the file system: callers pass the text in. */
|
|
13
|
+
export declare function validateText(text: string, schema: object, file?: string): Result;
|
|
14
|
+
/** Reads a registry file and validates it. Opens the file read-only. */
|
|
15
|
+
export declare function validateFile(file: string, schema: object): Promise<Result>;
|
package/dist/validate.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
3
|
+
import { parseDocument } from "yaml";
|
|
4
|
+
/** Validates registry text. Never touches the file system: callers pass the text in. */
|
|
5
|
+
export function validateText(text, schema, file = "invariants.yaml") {
|
|
6
|
+
const doc = parseDocument(text, { uniqueKeys: true });
|
|
7
|
+
if (doc.errors.length > 0) {
|
|
8
|
+
return {
|
|
9
|
+
file,
|
|
10
|
+
ok: false,
|
|
11
|
+
ruleCount: 0,
|
|
12
|
+
problems: doc.errors.map((e) => ({ path: "", message: `YAML: ${firstLine(e.message)}` })),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const data = doc.toJS();
|
|
16
|
+
const ajv = new Ajv2020({ allErrors: true, strict: false });
|
|
17
|
+
const check = ajv.compile(schema);
|
|
18
|
+
const problems = [];
|
|
19
|
+
if (!check(data)) {
|
|
20
|
+
problems.push(...(check.errors ?? []).map(describe));
|
|
21
|
+
}
|
|
22
|
+
const rules = isRecord(data) && Array.isArray(data.rules) ? data.rules : [];
|
|
23
|
+
const firstSeen = new Map();
|
|
24
|
+
rules.forEach((rule, index) => {
|
|
25
|
+
if (!isRecord(rule) || typeof rule.id !== "string")
|
|
26
|
+
return;
|
|
27
|
+
const earlier = firstSeen.get(rule.id);
|
|
28
|
+
if (earlier === undefined) {
|
|
29
|
+
firstSeen.set(rule.id, index);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
problems.push({
|
|
33
|
+
path: `/rules/${index}/id`,
|
|
34
|
+
message: `duplicate id ${rule.id} (first used at /rules/${earlier}/id)`,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return { file, ok: problems.length === 0, ruleCount: rules.length, problems };
|
|
39
|
+
}
|
|
40
|
+
/** Reads a registry file and validates it. Opens the file read-only. */
|
|
41
|
+
export async function validateFile(file, schema) {
|
|
42
|
+
const text = await readFile(file, "utf8");
|
|
43
|
+
return validateText(text, schema, file);
|
|
44
|
+
}
|
|
45
|
+
function describe(error) {
|
|
46
|
+
const path = error.instancePath;
|
|
47
|
+
switch (error.keyword) {
|
|
48
|
+
case "required":
|
|
49
|
+
return { path, message: `missing required field "${String(error.params.missingProperty)}"` };
|
|
50
|
+
case "additionalProperties":
|
|
51
|
+
return { path, message: `unknown field "${String(error.params.additionalProperty)}"` };
|
|
52
|
+
case "enum":
|
|
53
|
+
return { path, message: `must be one of: ${error.params.allowedValues.join(", ")}` };
|
|
54
|
+
case "const":
|
|
55
|
+
return { path, message: `must be ${JSON.stringify(error.params.allowedValue)}` };
|
|
56
|
+
case "pattern":
|
|
57
|
+
return { path, message: "must look like INV-01" };
|
|
58
|
+
default:
|
|
59
|
+
return { path, message: error.message ?? "is invalid" };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** yaml's messages append a multi-line code excerpt; keep the summary line only. */
|
|
63
|
+
function firstLine(message) {
|
|
64
|
+
return (message.split("\n")[0] ?? message).replace(/:\s*$/, "");
|
|
65
|
+
}
|
|
66
|
+
function isRecord(value) {
|
|
67
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
68
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@invariantly/cli",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "Keep your app's promises: a registry of invariants backed by tests, a CI checker and a human gate for contract changes. Early release: validates the registry format.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "cieyhomelab",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"bin": {
|
|
9
|
+
"invariantly": "dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./dist/validate.js",
|
|
13
|
+
"./schema/v0": "./schema/invariants.v0.schema.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"schema",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"NOTICE"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/invariantly-dev/invariantly.git",
|
|
28
|
+
"directory": "packages/cli"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://invariantly.dev",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/invariantly-dev/invariantly/issues"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"invariants",
|
|
36
|
+
"property-based-testing",
|
|
37
|
+
"testing",
|
|
38
|
+
"design-by-contract",
|
|
39
|
+
"ai-agents",
|
|
40
|
+
"governance",
|
|
41
|
+
"cli"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"copy-assets": "node scripts/copy-assets.mjs",
|
|
45
|
+
"build": "node scripts/copy-assets.mjs && tsc -p tsconfig.json",
|
|
46
|
+
"test": "pnpm run build && vitest run",
|
|
47
|
+
"prepublishOnly": "pnpm run test"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"ajv": "^8.20.0",
|
|
51
|
+
"yaml": "^2.9.1"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/node": "^22.0.0",
|
|
55
|
+
"typescript": "^7.0.2",
|
|
56
|
+
"vitest": "^5.1.2"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://github.com/invariantly-dev/invariantly/blob/main/schema/invariants.v0.schema.json",
|
|
4
|
+
"title": "invariantly registry, format version 0",
|
|
5
|
+
"description": "A registry of the promises (invariants) an application makes. Format version 0 is a draft and may change before 1.0.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"required": ["version", "rules"],
|
|
8
|
+
"additionalProperties": false,
|
|
9
|
+
"properties": {
|
|
10
|
+
"version": {
|
|
11
|
+
"description": "Registry format version.",
|
|
12
|
+
"const": 0
|
|
13
|
+
},
|
|
14
|
+
"rules": {
|
|
15
|
+
"type": "array",
|
|
16
|
+
"minItems": 1,
|
|
17
|
+
"items": { "$ref": "#/$defs/rule" }
|
|
18
|
+
},
|
|
19
|
+
"contract_paths": {
|
|
20
|
+
"description": "Files and globs whose changes change the promises. A pull request touching them needs a human (label contract-change).",
|
|
21
|
+
"type": "array",
|
|
22
|
+
"items": { "type": "string", "minLength": 1 },
|
|
23
|
+
"uniqueItems": true
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"$defs": {
|
|
27
|
+
"rule": {
|
|
28
|
+
"type": "object",
|
|
29
|
+
"required": ["id", "statement", "source", "rationale"],
|
|
30
|
+
"additionalProperties": false,
|
|
31
|
+
"properties": {
|
|
32
|
+
"id": {
|
|
33
|
+
"description": "Stable identifier, INV- followed by at least two digits.",
|
|
34
|
+
"type": "string",
|
|
35
|
+
"pattern": "^INV-[0-9]{2,}$"
|
|
36
|
+
},
|
|
37
|
+
"statement": {
|
|
38
|
+
"description": "The promise, in one sentence a person can check.",
|
|
39
|
+
"type": "string",
|
|
40
|
+
"minLength": 1
|
|
41
|
+
},
|
|
42
|
+
"source": {
|
|
43
|
+
"description": "Where the promise comes from: a decision, a spec, a README section, or code.",
|
|
44
|
+
"type": "string",
|
|
45
|
+
"minLength": 1
|
|
46
|
+
},
|
|
47
|
+
"rationale": {
|
|
48
|
+
"description": "What goes wrong if the promise is broken.",
|
|
49
|
+
"type": "string",
|
|
50
|
+
"minLength": 1
|
|
51
|
+
},
|
|
52
|
+
"status": {
|
|
53
|
+
"description": "approved: a person accepted it. proposed: suggested, not yet reviewed. to-confirm: inferred from code, may describe a bug.",
|
|
54
|
+
"enum": ["approved", "proposed", "to-confirm"],
|
|
55
|
+
"default": "proposed"
|
|
56
|
+
},
|
|
57
|
+
"check": {
|
|
58
|
+
"description": "How the promise is checked.",
|
|
59
|
+
"enum": ["example", "integration", "architecture", "graph", "property", "stateful"]
|
|
60
|
+
},
|
|
61
|
+
"components": {
|
|
62
|
+
"description": "Paths or component names the rule watches.",
|
|
63
|
+
"type": "array",
|
|
64
|
+
"items": { "type": "string", "minLength": 1 },
|
|
65
|
+
"uniqueItems": true
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|