@ai-ecoverse/slicc-shared-web 2.7.6 → 2.7.8

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 CHANGED
@@ -248,6 +248,7 @@ The diff-coverage hook, `slicc-unit-cover`, runs only the unit tests that cover
248
248
 
249
249
  - **Which directories are source:** `src/` by default, anywhere in the path, as before. A repo whose code lives elsewhere lists its directories in `package.json`: `"slicc": { "unitSources": ["extension", "six", "scripts"] }`, repo-relative, without `.` or `..` segments. `mcr.config.mjs` then collects coverage only from those directories, and the hook treats a staged file outside them as no code. A malformed list fails the hook.
250
250
  - **What counts as changed:** the staged files (`git diff --cached --name-only`), and nothing else. `test/unit/` is gitignored, and repos that keep their unit tests in a store outside git sync the whole store into it before a commit, which brings in tests for code that is on other branches; so a unit test that is new or edited runs only when its imports reach a staged file, like any other. One that reaches nothing staged adds no coverage to the staged lines.
251
+ - **Committing a merge, cherry-pick or rebase:** while `MERGE_HEAD` or `CHERRY_PICK_HEAD` exists, or a rebase is running and has stopped at `REBASE_HEAD`, only lines that are new against `HEAD` and against each of those commits count: conflict resolutions and edits made during the merge, not what the other side brought, which was covered where it was written. Diff coverage checks only those lines; the tests that run and the missing-lcov check follow the files that hold them, and files the merge itself deleted lines from, renamed, added empty or changed the mode of. A merge with no changes of its own is no code staged. A revert counts as usual.
251
252
  - **A unit test for code this tree doesn't have:** a test whose imports reach a staged file but also a code module under the sources that doesn't exist, isn't gitignored and isn't a staged deletion (a store test for a module on another branch) is skipped, and the hook names it in one line before the count. A missing `new URL(…, import.meta.url)` target, a missing non-code file such as `.wasm` or `.json`, and a missing file outside the sources or gitignored (a build output such as `dist/` or a bundled worker) don't skip the test.
252
253
  - **Which tests run:** every `*.test.*` file in `test/unit/` that is changed, or whose static imports reach a changed file. The import graph is parsed with [acorn](https://github.com/acornjs/acorn), TypeScript after Node's type stripping, or amaro's transform where stripping isn't enough. It follows `import`, `export … from`, `import()`, `require()` and `new URL(…, import.meta.url)` with literal paths, relative or through the `paths` of `tsconfig.json` (and the files it extends). A path may leave out its extension or name `index`, and `.js` also finds `.ts`. Type-only imports don't count, and nothing is followed into `node_modules/`.
253
254
  - **Barrels are followed by name:** a test that imports `{ a, b }` from a module follows only what provides `a` and `b`: `export { a } from`, `export { default as a } from`, an imported binding exported again, and `export * from`, by the target's own exports. `const { a, b } = await import('…')` with a literal path is followed by name too. A name the module declares itself follows all of that module's imports. A namespace import (`import * as`), any other `import()` (bound to a namespace, with `.then`, or destructured with a computed key or a rest element), a `require()`, and an `export *` that leads outside the repo or into CommonJS (any `.cjs` or `.cts` file) all count as the whole module.
@@ -5,4 +5,7 @@ if ! command -v uvx > /dev/null 2>&1; then
5
5
  exit 1
6
6
  fi
7
7
  [ "$#" -gt 0 ] || set -- coverage/unit/lcov.info
8
+ n="${GIT_CONFIG_COUNT:-0}"
9
+ export "GIT_CONFIG_COUNT=$((n + 2))" "GIT_CONFIG_KEY_$n=core.quotePath" "GIT_CONFIG_VALUE_$n=false"
10
+ export "GIT_CONFIG_KEY_$((n + 1))=diff.renames" "GIT_CONFIG_VALUE_$((n + 1))=true"
8
11
  exec uvx diff-cover@10.6.0 "$@" --compare-branch HEAD --ignore-unstaged --fail-under 100
@@ -1,11 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawn, spawnSync } from 'node:child_process';
3
- import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
3
+ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { join } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
7
7
  import { code, ParseError, select } from '../unit/graph.mjs';
8
8
  import { inject } from '../unit/inject.mjs';
9
+ import { merged } from '../unit/merge.mjs';
9
10
  import { inSources } from '../unit/sources.mjs';
10
11
 
11
12
  const root = process.cwd();
@@ -75,9 +76,12 @@ function selection(staged) {
75
76
  }
76
77
  }
77
78
 
78
- const staged = (git('diff', '--cached', '--name-only', '--relative', '--no-renames', '-z') ?? '')
79
- .split('\0')
80
- .filter(Boolean);
79
+ const merge = merged(root);
80
+ const staged =
81
+ merge?.files ??
82
+ (git('diff', '--cached', '--name-only', '--relative', '--no-renames', '-z') ?? '')
83
+ .split('\0')
84
+ .filter(Boolean);
81
85
  if (
82
86
  process.env.SLICC_UNIT_ALL !== '1' &&
83
87
  !staged.some((file) => code.test(file) && inSources(file, root))
@@ -163,6 +167,16 @@ const lcov = (reports.length > 0 ? reports : [join('coverage', 'unit', 'lcov.inf
163
167
  const seen = new Set(lcov.match(/^SF:.*$/gm)?.map((line) => line.slice(3)) ?? []);
164
168
  const missing = measured.filter((file) => !seen.has(file));
165
169
  if (missing.length > 0) verdict(false, `no coverage was collected for ${missing.join(' ')}`);
166
- const covered = run(cover, reports, process.env);
170
+ let args = reports;
171
+ if (merge !== undefined) {
172
+ const diff = join(scratch, 'diff');
173
+ writeFileSync(diff, merge.diff);
174
+ args = [
175
+ ...(reports.length > 0 ? reports : [join('coverage', 'unit', 'lcov.info')]),
176
+ '--diff-file',
177
+ diff,
178
+ ];
179
+ }
180
+ const covered = run(cover, args, process.env);
167
181
  if (covered !== 0) verdict(false, `diff coverage is not 100% (${files} in ${counted})`);
168
182
  verdict(true, `${files} in ${counted}, diff coverage 100%`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/slicc-shared-web",
3
- "version": "2.7.6",
3
+ "version": "2.7.8",
4
4
  "description": "Shared Renovate, Biome, TypeScript, lefthook and GitHub Actions configuration, plus the CDP integration-test harness, for SLICC's web repos",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -66,8 +66,8 @@
66
66
  "release": "semantic-release"
67
67
  },
68
68
  "devDependencies": {
69
- "@ai-ecoverse/slicc-browsers": "1.16.2",
70
- "@ai-ecoverse/slicc-cdp": "1.30.23",
69
+ "@ai-ecoverse/slicc-browsers": "1.17.0",
70
+ "@ai-ecoverse/slicc-cdp": "1.30.25",
71
71
  "@biomejs/biome": "2.5.15",
72
72
  "lefthook": "2.2.2",
73
73
  "monocart-coverage-reports": "2.13.2",
package/unit/merge.mjs ADDED
@@ -0,0 +1,165 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { existsSync, readFileSync } from 'node:fs';
3
+ import { resolve } from 'node:path';
4
+
5
+ const diff = [
6
+ '-c',
7
+ 'diff.mnemonicprefix=no',
8
+ '-c',
9
+ 'diff.noprefix=no',
10
+ '-c',
11
+ 'diff.srcPrefix=a/',
12
+ '-c',
13
+ 'diff.dstPrefix=b/',
14
+ '-c',
15
+ 'core.quotePath=false',
16
+ 'diff',
17
+ '--cached',
18
+ '--no-color',
19
+ '--no-ext-diff',
20
+ '--no-relative',
21
+ '-M',
22
+ '-U0',
23
+ ];
24
+ const escapes = { a: 7, b: 8, f: 12, n: 10, r: 13, t: 9, v: 11 };
25
+ const hunk = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/;
26
+
27
+ function git(root, args) {
28
+ const run = spawnSync('git', args, { cwd: root, encoding: 'utf8', maxBuffer: 2 ** 30 });
29
+ return run.status === 0 ? run.stdout : undefined;
30
+ }
31
+
32
+ export function unquote(text) {
33
+ if (!text.startsWith('"')) return text;
34
+ const bytes = [];
35
+ for (let at = 1; at < text.length - 1; at++) {
36
+ if (text[at] !== '\\') {
37
+ const char = String.fromCodePoint(text.codePointAt(at));
38
+ bytes.push(...Buffer.from(char));
39
+ at += char.length - 1;
40
+ continue;
41
+ }
42
+ const next = text[++at];
43
+ if (/[0-7]/.test(next)) {
44
+ bytes.push(Number.parseInt(text.slice(at, at + 3), 8));
45
+ at += 2;
46
+ } else bytes.push(escapes[next] ?? next.charCodeAt(0));
47
+ }
48
+ return Buffer.from(bytes).toString('utf8');
49
+ }
50
+
51
+ function named(header) {
52
+ const rest = header.slice('diff --git '.length);
53
+ const half = (rest.length - 1) / 2;
54
+ if (rest[half] !== ' ') return undefined;
55
+ const from = unquote(rest.slice(0, half)).slice(2);
56
+ return from === unquote(rest.slice(half + 1)).slice(2) ? from : undefined;
57
+ }
58
+
59
+ function mark(file, found) {
60
+ file.hunks = true;
61
+ const start = Number(found[1]);
62
+ const count = found[2] === undefined ? 1 : Number(found[2]);
63
+ for (let at = start; at < start + count; at++) file.marks.add(`+${at}`);
64
+ }
65
+
66
+ const meta = /^(new file mode|deleted file mode|new mode|rename from|rename to) /;
67
+
68
+ function describe(files, file, line) {
69
+ file.marks.add(`=${line}`);
70
+ const moved = /^rename (from|to) (.*)$/.exec(line);
71
+ if (moved === null) return;
72
+ file.paths.push(unquote(moved[2]));
73
+ if (moved[1] === 'to') files.set(file.paths.at(-1), file);
74
+ }
75
+
76
+ export function marks(text) {
77
+ const files = new Map();
78
+ let file;
79
+ for (const line of text.split('\n')) {
80
+ if (line.startsWith('diff --git ')) {
81
+ const path = named(line);
82
+ file = {
83
+ header: line,
84
+ paths: path === undefined ? [] : [path],
85
+ marks: new Set(),
86
+ hunks: false,
87
+ };
88
+ if (path !== undefined) files.set(path, file);
89
+ continue;
90
+ }
91
+ if (!file) continue;
92
+ const found = hunk.exec(line);
93
+ if (found) mark(file, found);
94
+ else if (file.hunks && line.startsWith('-')) file.marks.add(line);
95
+ else if (!file.hunks && meta.test(line)) describe(files, file, line);
96
+ }
97
+ return files;
98
+ }
99
+
100
+ function where(root, name) {
101
+ const found = git(root, ['rev-parse', '--git-path', name]);
102
+ return found === undefined ? undefined : resolve(root, found.trim());
103
+ }
104
+
105
+ const present = (file) => file !== undefined && existsSync(file);
106
+
107
+ export function parents(root) {
108
+ const head = where(root, 'MERGE_HEAD');
109
+ const merging = present(head) ? readFileSync(head, 'utf8').split('\n').filter(Boolean) : [];
110
+ const rebasing = ['rebase-merge', 'rebase-apply'].some((dir) => present(where(root, dir)));
111
+ const picked = (rebasing ? ['CHERRY_PICK_HEAD', 'REBASE_HEAD'] : ['CHERRY_PICK_HEAD'])
112
+ .map((ref) => git(root, ['rev-parse', '-q', '--verify', `${ref}^{commit}`])?.trim())
113
+ .filter(Boolean);
114
+ return [...merging, ...picked];
115
+ }
116
+
117
+ export function renames(text) {
118
+ const moved = new Map();
119
+ const parts = text.split('\0');
120
+ for (let at = 0; at < parts.length; at += 2) {
121
+ if (!parts[at].startsWith('R')) continue;
122
+ moved.set(parts[at + 1], parts[at + 2]);
123
+ at += 1;
124
+ }
125
+ return moved;
126
+ }
127
+
128
+ export function merged(root = process.cwd()) {
129
+ const others = parents(root);
130
+ if (others.length === 0) return undefined;
131
+ const views = ['HEAD', ...others].map((parent) => git(root, [...diff, parent]));
132
+ const moves = others.map((parent) =>
133
+ git(root, [
134
+ 'diff',
135
+ '--no-ext-diff',
136
+ '--no-relative',
137
+ '-M',
138
+ '--name-status',
139
+ '-z',
140
+ 'HEAD',
141
+ parent,
142
+ ])
143
+ );
144
+ if ([...views, ...moves].includes(undefined)) return undefined;
145
+ const [base, ...rest] = views.map(marks);
146
+ const moved = moves.map(renames);
147
+ const prefix = git(root, ['rev-parse', '--show-prefix'])?.trim() ?? '';
148
+ const files = new Set();
149
+ let text = '';
150
+ for (const [name, file] of base) {
151
+ const matches = rest.map((other, at) => other.get(name) ?? other.get(moved[at].get(name)));
152
+ const kept = [...file.marks].filter((entry) =>
153
+ matches.every(
154
+ (other) => other !== undefined && (entry.startsWith('=') || other.marks.has(entry))
155
+ )
156
+ );
157
+ if (kept.length === 0) continue;
158
+ for (const each of [file, ...matches].flatMap((one) => one.paths))
159
+ if (each.startsWith(prefix)) files.add(each.slice(prefix.length));
160
+ const lines = kept.filter((entry) => entry.startsWith('+')).map((entry) => entry.slice(1));
161
+ if (lines.length > 0)
162
+ text += `${file.header}\n${lines.map((at) => `@@ -0,0 +${at} @@\n+\n`).join('')}`;
163
+ }
164
+ return { files: [...files], diff: text };
165
+ }