checkchange 0.4.0

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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +150 -0
  3. package/dist/attribution.d.ts +19 -0
  4. package/dist/attribution.js +175 -0
  5. package/dist/auto-coverage.d.ts +8 -0
  6. package/dist/auto-coverage.js +93 -0
  7. package/dist/cache.d.ts +70 -0
  8. package/dist/cache.js +458 -0
  9. package/dist/cli.d.ts +51 -0
  10. package/dist/cli.js +810 -0
  11. package/dist/complexity-providers/pythonASTComplexityProvider.d.ts +9 -0
  12. package/dist/complexity-providers/pythonASTComplexityProvider.js +148 -0
  13. package/dist/complexity-providers/pythonDescriptorProvider.d.ts +17 -0
  14. package/dist/complexity-providers/pythonDescriptorProvider.js +119 -0
  15. package/dist/complexity-providers.d.ts +6 -0
  16. package/dist/complexity-providers.js +1 -0
  17. package/dist/complexity.d.ts +14 -0
  18. package/dist/complexity.js +75 -0
  19. package/dist/coverage-providers/lcovProvider.d.ts +12 -0
  20. package/dist/coverage-providers/lcovProvider.js +126 -0
  21. package/dist/coverage.d.ts +28 -0
  22. package/dist/coverage.js +451 -0
  23. package/dist/crap.d.ts +31 -0
  24. package/dist/crap.js +84 -0
  25. package/dist/crapCalc.d.ts +5 -0
  26. package/dist/crapCalc.js +11 -0
  27. package/dist/delta.d.ts +100 -0
  28. package/dist/delta.js +214 -0
  29. package/dist/evidence.d.ts +201 -0
  30. package/dist/evidence.js +906 -0
  31. package/dist/execute.d.ts +59 -0
  32. package/dist/execute.js +92 -0
  33. package/dist/formatters/github.d.ts +5 -0
  34. package/dist/formatters/github.js +27 -0
  35. package/dist/formatters/index.d.ts +47 -0
  36. package/dist/formatters/index.js +14 -0
  37. package/dist/formatters/junit.d.ts +5 -0
  38. package/dist/formatters/junit.js +32 -0
  39. package/dist/formatters/sarif.d.ts +5 -0
  40. package/dist/formatters/sarif.js +35 -0
  41. package/dist/fs-safety.d.ts +10 -0
  42. package/dist/fs-safety.js +47 -0
  43. package/dist/git.d.ts +50 -0
  44. package/dist/git.js +137 -0
  45. package/dist/help.d.ts +40 -0
  46. package/dist/help.js +274 -0
  47. package/dist/index.d.ts +6 -0
  48. package/dist/index.js +4 -0
  49. package/dist/providers/config.d.ts +41 -0
  50. package/dist/providers/config.js +172 -0
  51. package/dist/providers/genericCommand.d.ts +3 -0
  52. package/dist/providers/genericCommand.js +89 -0
  53. package/dist/providers/index.d.ts +7 -0
  54. package/dist/providers/index.js +4 -0
  55. package/dist/providers/prepare.d.ts +124 -0
  56. package/dist/providers/prepare.js +459 -0
  57. package/dist/providers/runner-detection.d.ts +30 -0
  58. package/dist/providers/runner-detection.js +207 -0
  59. package/dist/rules.d.ts +27 -0
  60. package/dist/rules.js +54 -0
  61. package/package.json +52 -0
@@ -0,0 +1,207 @@
1
+ /**
2
+ * Generic test-runner resolver.
3
+ * Given a provider registry (Map<extension, ResolvedProvider>) and a cwd,
4
+ * detect which test runners are available per language.
5
+ *
6
+ * Precedence: configFiles glob match OR binaryProbes match, first runner per
7
+ * language wins. Preserves current auto-coverage behavior:
8
+ * - vitest/jest: root-level config file (e.g. vitest.config.*) OR package.json dep
9
+ * - pytest: root-level config (pytest.ini/pyproject.toml/...) + venv-aware binary probe
10
+ * (source-file presence is a precondition so runners only report for languages
11
+ * actually present in the repo.)
12
+ */
13
+ import * as fs from 'node:fs';
14
+ import * as path from 'node:path';
15
+ import { spawnSync } from 'node:child_process';
16
+ const PROBE_VERSION_TIMEOUT_MS = 10_000;
17
+ const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', 'build', 'coverage']);
18
+ // ---- Helpers ----
19
+ /** True if any file under cwd (recursive) carries one of the provider's extensions. */
20
+ function hasSourceFiles(cwd, extensions) {
21
+ const exts = new Set(extensions.map((e) => e.toLowerCase()));
22
+ function walk(dir) {
23
+ let entries;
24
+ try {
25
+ entries = fs.readdirSync(dir, { withFileTypes: true });
26
+ }
27
+ catch {
28
+ return false;
29
+ }
30
+ for (const e of entries) {
31
+ if (e.isDirectory()) {
32
+ if (SKIP_DIRS.has(e.name))
33
+ continue;
34
+ if (walk(path.join(dir, e.name)))
35
+ return true;
36
+ }
37
+ else if (e.isFile() && exts.has(path.extname(e.name).toLowerCase())) {
38
+ return true;
39
+ }
40
+ }
41
+ return false;
42
+ }
43
+ return walk(cwd);
44
+ }
45
+ /** Match a configured configFile glob against root-level files in cwd. Returns matched filename. */
46
+ function matchConfigFile(cwd, pattern) {
47
+ let files;
48
+ try {
49
+ files = fs.readdirSync(cwd);
50
+ }
51
+ catch {
52
+ return null;
53
+ }
54
+ const re = pattern.includes('*')
55
+ ? new RegExp('^' + pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*') + '$')
56
+ : null;
57
+ for (const f of files) {
58
+ const full = path.join(cwd, f);
59
+ const isMatch = re ? re.test(f) : f === pattern;
60
+ if (isMatch) {
61
+ try {
62
+ if (fs.statSync(full).isFile())
63
+ return f;
64
+ }
65
+ catch {
66
+ /* not a file / unreadable — skip */
67
+ }
68
+ }
69
+ }
70
+ return null;
71
+ }
72
+ function hasPackageDep(cwd, dep) {
73
+ try {
74
+ const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
75
+ return !!(pkg.devDependencies?.[dep] || pkg.dependencies?.[dep]);
76
+ }
77
+ catch {
78
+ return false;
79
+ }
80
+ }
81
+ /**
82
+ * Probe a command-style binaryProbe (e.g. 'python3 -m pytest') via `--version`.
83
+ *
84
+ * PROBE PHASE: Runs `<command> --version` with a 10s timeout.
85
+ * Returns TRUE only if exit code === 0 (binary exists and responds to --version).
86
+ * This is a BOOLEAN AVAILABILITY CHECK — no test execution, no coverage, no artifacts.
87
+ * Used by probeRunner() to validate binaryProbes that contain spaces (command + args).
88
+ */
89
+ function commandProbeAvailable(parts, cwd) {
90
+ if (parts.length === 0)
91
+ return false;
92
+ const cmd = parts[0];
93
+ const args = [...parts.slice(1), '--version'];
94
+ try {
95
+ const res = spawnSync(cmd, args, {
96
+ cwd,
97
+ encoding: 'utf8',
98
+ timeout: PROBE_VERSION_TIMEOUT_MS,
99
+ });
100
+ return res.status === 0;
101
+ }
102
+ catch {
103
+ return false;
104
+ }
105
+ }
106
+ /**
107
+ * Probe a single TestRunner; returns provenance string if available, else null.
108
+ *
109
+ * PROBE PHASE (detection only):
110
+ * - Config file glob match at repo root (runner.configFiles)
111
+ * - Binary probes in order:
112
+ * * 'VIRTUAL_ENV' env var presence
113
+ * * Command probes (with spaces) -> commandProbeAvailable() -> `--version` boolean check
114
+ * * Literal paths -> fs.accessSync(X_OK) executable check
115
+ * - npx <runner> -> package.json dep OR config match (no --version probe)
116
+ *
117
+ * Returns provenance string (e.g. 'vitest.config.ts + npx vitest') if ANY probe succeeds.
118
+ * DOES NOT execute the runner. DOES NOT run tests. DOES NOT generate coverage.
119
+ * Execution happens later in auto-coverage.ts via the resolved runner.command.
120
+ */
121
+ function probeRunner(cwd, runner) {
122
+ const configMatches = [];
123
+ for (const cf of runner.configFiles) {
124
+ const m = matchConfigFile(cwd, cf);
125
+ if (m)
126
+ configMatches.push(m);
127
+ }
128
+ const binaryMatches = [];
129
+ for (const bp of runner.binaryProbes) {
130
+ if (bp === 'VIRTUAL_ENV') {
131
+ if (process.env.VIRTUAL_ENV)
132
+ binaryMatches.push('VIRTUAL_ENV');
133
+ }
134
+ else if (bp.includes(' ')) {
135
+ const parts = bp.split(/\s+/);
136
+ if (parts[0] === 'npx') {
137
+ // npx <runner>: available via package.json dep OR config already matched.
138
+ const dep = parts[1] ?? '';
139
+ if (configMatches.length > 0 || hasPackageDep(cwd, dep))
140
+ binaryMatches.push(bp);
141
+ }
142
+ else if (commandProbeAvailable(parts, cwd)) {
143
+ binaryMatches.push(bp);
144
+ }
145
+ }
146
+ else {
147
+ // literal path: must exist and be executable.
148
+ const full = path.isAbsolute(bp) ? bp : path.join(cwd, bp);
149
+ try {
150
+ fs.accessSync(full, fs.constants.X_OK);
151
+ binaryMatches.push(bp);
152
+ }
153
+ catch {
154
+ /* missing or not executable */
155
+ }
156
+ }
157
+ }
158
+ if (configMatches.length === 0 && binaryMatches.length === 0)
159
+ return null;
160
+ return { provenance: [...configMatches, ...binaryMatches].join(' + ') };
161
+ }
162
+ // ---- Public API ----
163
+ /**
164
+ * Resolve available test runners per language.
165
+ * Returns one entry per language whose source files are present AND whose
166
+ * first-available runner (by configFiles/binaryProbes) probes successfully.
167
+ * Empty map if none detected.
168
+ *
169
+ * RESOLUTION PHASE (selection only):
170
+ * - Iterates providers with source files present
171
+ * - For each provider's testRunners, calls probeRunner() (PROBE PHASE)
172
+ * - First successful probe per language wins (short-circuits)
173
+ * - Returns ResolvedRunner with:
174
+ * * command: full spawn command for EXECUTION (e.g. ['npx','vitest','run','--coverage'])
175
+ * * artifact: expected coverage artifact path
176
+ * * provenance: human-readable evidence string from probe
177
+ *
178
+ * EXECUTION BOUNDARY:
179
+ * - This function ONLY selects and returns the command to run.
180
+ * - Actual test execution with coverage happens in auto-coverage.ts:82-86
181
+ * via spawnSync(runner.command, runner.args, ...) — separate process, full test run.
182
+ */
183
+ export function resolveRunner(cwd, registry) {
184
+ const result = new Map();
185
+ const seen = new Set();
186
+ for (const provider of registry.values()) {
187
+ if (seen.has(provider))
188
+ continue; // dedupe extensions → one pass per provider
189
+ seen.add(provider);
190
+ if (!hasSourceFiles(cwd, provider.extensions))
191
+ continue;
192
+ if (!provider.testRunners)
193
+ continue;
194
+ for (const runner of provider.testRunners) {
195
+ const probed = probeRunner(cwd, runner);
196
+ if (probed) {
197
+ result.set(provider.language, {
198
+ command: runner.command,
199
+ artifact: runner.artifact,
200
+ provenance: probed.provenance,
201
+ });
202
+ break; // first hit per language wins
203
+ }
204
+ }
205
+ }
206
+ return result;
207
+ }
@@ -0,0 +1,27 @@
1
+ import { ChangedFunction } from './evidence.js';
2
+ export declare const rulesProvenance: {
3
+ readonly tool: "checkchange";
4
+ readonly version: "0.4.0";
5
+ };
6
+ export interface RuleResult {
7
+ ruleId: string;
8
+ result: "PASS" | "WARN" | "NOT_EVALUATED";
9
+ file: string;
10
+ method: string;
11
+ crap: number | null;
12
+ threshold: number;
13
+ cc: number;
14
+ coverage: number | null;
15
+ }
16
+ export interface Rule {
17
+ ruleId: string;
18
+ evaluate(changed: ChangedFunction[], threshold: number): RuleResult[];
19
+ }
20
+ /**
21
+ * Evaluate changed functions against the high-CRAP rule.
22
+ * @param changed Array of changed functions
23
+ * @param threshold CRAP threshold (default 30)
24
+ * @returns Array of rule results
25
+ */
26
+ export declare function evaluateHighCrap(changed: ChangedFunction[], threshold: number): RuleResult[];
27
+ export declare function loadRuleRegistry(externalRules?: Rule[]): Rule[];
package/dist/rules.js ADDED
@@ -0,0 +1,54 @@
1
+ // Provenance for the rules lineage stage. Self tool version tracks package.json.
2
+ export const rulesProvenance = { tool: 'checkchange', version: '0.4.0' };
3
+ /**
4
+ * Evaluate changed functions against the high-CRAP rule.
5
+ * @param changed Array of changed functions
6
+ * @param threshold CRAP threshold (default 30)
7
+ * @returns Array of rule results
8
+ */
9
+ export function evaluateHighCrap(changed, threshold) {
10
+ return changed.map((cf) => {
11
+ if (cf.crap === null) {
12
+ return {
13
+ ruleId: "changed-function-high-crap",
14
+ result: "NOT_EVALUATED",
15
+ file: cf.file,
16
+ method: cf.method,
17
+ crap: cf.crap,
18
+ threshold,
19
+ cc: cf.cc,
20
+ coverage: cf.coverage,
21
+ };
22
+ }
23
+ else if (cf.crap <= threshold) {
24
+ return {
25
+ ruleId: "changed-function-high-crap",
26
+ result: "PASS",
27
+ file: cf.file,
28
+ method: cf.method,
29
+ crap: cf.crap,
30
+ threshold,
31
+ cc: cf.cc,
32
+ coverage: cf.coverage,
33
+ };
34
+ }
35
+ else {
36
+ return {
37
+ ruleId: "changed-function-high-crap",
38
+ result: "WARN",
39
+ file: cf.file,
40
+ method: cf.method,
41
+ crap: cf.crap,
42
+ threshold,
43
+ cc: cf.cc,
44
+ coverage: cf.coverage,
45
+ };
46
+ }
47
+ });
48
+ }
49
+ export function loadRuleRegistry(externalRules = []) {
50
+ return [
51
+ { ruleId: "changed-function-high-crap", evaluate: evaluateHighCrap },
52
+ ...externalRules,
53
+ ];
54
+ }
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "checkchange",
3
+ "version": "0.4.0",
4
+ "license": "MIT",
5
+ "packageManager": "pnpm@11.17.0",
6
+ "type": "module",
7
+ "bin": {
8
+ "checkchange": "dist/cli.js",
9
+ "code-risk": "dist/cli.js"
10
+ },
11
+ "files": [
12
+ "dist/",
13
+ "!dist/**/*.test.*"
14
+ ],
15
+ "scripts": {
16
+ "build": "tsc && chmod +x dist/cli.js",
17
+ "test": "vitest run",
18
+ "typecheck:test": "tsc -p tsconfig.test.json --noEmit",
19
+ "typecheck:test:baseline": "tsc -p tsconfig.test.json --noEmit 2>&1 | grep '^test/' | LC_ALL=C sort | diff tsconfig.test.baseline.txt -",
20
+ "typecheck:test:baseline:update": "tsc -p tsconfig.test.json --noEmit 2>&1 | grep '^test/' | LC_ALL=C sort > tsconfig.test.baseline.txt",
21
+ "check": "node dist/cli.js check",
22
+ "prepare": "pnpm run build"
23
+ },
24
+ "dependencies": {
25
+ "@barney-media/crap-typescript": "0.5.0",
26
+ "@barney-media/crap-typescript-core": "0.5.0"
27
+ },
28
+ "devDependencies": {
29
+ "@eslint/js": "^10.0.1",
30
+ "@types/node": "latest",
31
+ "@typescript-eslint/eslint-plugin": "latest",
32
+ "@typescript-eslint/parser": "latest",
33
+ "@vitest/coverage-v8": "^4.1.11",
34
+ "eslint": "latest",
35
+ "tsx": "^4.23.12",
36
+ "typescript": "6.0.3",
37
+ "vitest": "4.1.11"
38
+ },
39
+ "allowScripts": {
40
+ "esbuild@0.28.2": true,
41
+ "fsevents@2.3.3": true
42
+ },
43
+ "description": "Independent, deterministic evidence and verification layer for AI-assisted software development.",
44
+ "repository": {
45
+ "type": "git",
46
+ "url": "https://github.com/victor-petropoulos/CheckChange.git"
47
+ },
48
+ "homepage": "https://github.com/victor-petropoulos/CheckChange",
49
+ "bugs": {
50
+ "url": "https://github.com/victor-petropoulos/CheckChange/issues"
51
+ }
52
+ }