@@ -2,11 +2,22 @@ import assert from 'node:assert/strict';
22import fs from 'node:fs' ;
33import { test } from 'node:test' ;
44import {
5+ coverageMapPaths ,
6+ decideCrapVerdict ,
57 parseFallowJsonReport ,
8+ parseIstanbulCoverageMap ,
9+ readCrapExemptions ,
610 readCrapFindings ,
11+ readConfiguredExemptions ,
712 renderCrapAnnotations ,
813 renderCrapReport ,
914 sanitizeIstanbulCoverage ,
15+ usableEnumeration ,
16+ type CrapFinding ,
17+ type CrapProblem ,
18+ type CrapScope ,
19+ type CrapVerdict ,
20+ type EstimatedFinding ,
1021} from './model.ts' ;
1122
1223const valid = { s : { 0 : 3 , 1 : 0 } , f : { 0 : 1 } , b : { 0 : [ 2 , 0 ] } } ;
@@ -71,13 +82,33 @@ test('a report without a findings array is an error, not an empty verdict', () =
7182 assert . throws ( ( ) => readCrapFindings ( { error : true } , 30 ) , / n o f i n d i n g s a r r a y / ) ;
7283} ) ;
7384
85+ /** A verdict that owes nothing: what `decideCrapVerdict` returns when the reports answer cleanly. */
86+ const clean = ( scope : CrapScope , findings : readonly CrapFinding [ ] = [ ] ) : CrapVerdict => ( {
87+ blocking : false ,
88+ problems : [ ] ,
89+ unscoped : [ ] ,
90+ offenders : scope === 'touched' ? findings : [ ] ,
91+ } ) ;
92+
93+ /** A verdict that blocks or lists problems, for the renderer cases that build one by hand. */
94+ const verdictWith = (
95+ problems : readonly CrapProblem [ ] ,
96+ unscoped : readonly EstimatedFinding [ ] = [ ] ,
97+ ) => ( {
98+ blocking : true ,
99+ problems,
100+ unscoped,
101+ offenders : [ ] ,
102+ } ) ;
103+
74104test ( 'the report names touched functions and unscored files' , ( ) => {
75105 const findings = readCrapFindings ( { findings : [ finding ( { } ) ] } , 30 ) ;
76106 const report = renderCrapReport ( {
77107 findings,
78108 dropped : [ 'src/odd.ts' ] ,
79109 threshold : 30 ,
80110 scope : 'touched' ,
111+ verdict : { blocking : true , problems : [ ] , unscoped : [ ] , offenders : findings } ,
81112 } ) ;
82113 assert . match ( report , / 1 t o u c h e d f u n c t i o n \( s \) a t o r a b o v e 3 0 / ) ;
83114 assert . match ( report , / \| ` s r c \/ a \. t s : 1 0 ` r u n \| 4 2 \. 0 \| 6 \| 2 0 % \| / ) ;
@@ -88,17 +119,299 @@ test('the report names touched functions and unscored files', () => {
88119} ) ;
89120
90121test ( 'an empty report says none and lists no table' , ( ) => {
91- const report = renderCrapReport ( { findings : [ ] , dropped : [ ] , threshold : 30 , scope : 'touched' } ) ;
122+ const report = renderCrapReport ( {
123+ findings : [ ] ,
124+ dropped : [ ] ,
125+ threshold : 30 ,
126+ scope : 'touched' ,
127+ verdict : clean ( 'touched' ) ,
128+ } ) ;
92129 assert . match ( report , / : n o t o u c h e d f u n c t i o n / ) ;
93130 assert . doesNotMatch ( report , / \| F u n c t i o n / ) ;
131+ // Each section owns its own emptiness guard, so a clean run must not announce empty ones: an
132+ // "Unscored: 0 file(s)" or "exemptions (0)" line reads as a finding to someone scanning the log.
133+ assert . doesNotMatch ( report , / U n s c o r e d : / ) ;
134+ assert . doesNotMatch ( report , / e x e m p t i o n s / i) ;
94135} ) ;
95136
96137test ( 'a whole-repository report does not claim the functions were touched' , ( ) => {
97138 const findings = readCrapFindings ( { findings : [ finding ( { } ) ] } , 30 ) ;
98- const report = renderCrapReport ( { findings, dropped : [ ] , threshold : 30 , scope : 'all' } ) ;
139+ const report = renderCrapReport ( {
140+ findings,
141+ dropped : [ ] ,
142+ threshold : 30 ,
143+ scope : 'all' ,
144+ verdict : clean ( 'all' ) ,
145+ } ) ;
99146 assert . match ( report , / : 1 f u n c t i o n \( s \) a t o r a b o v e 3 0 / ) ;
100147} ) ;
101148
149+ test ( 'a touched report whose reports cannot answer names the reason and stays ahead of the table' , ( ) => {
150+ const report = renderCrapReport ( {
151+ findings : [ ] ,
152+ dropped : [ ] ,
153+ threshold : 30 ,
154+ scope : 'touched' ,
155+ verdict : verdictWith ( [
156+ { kind : 'unscored_touched' , files : [ { path : 'src/new.ts' , addedLines : 4 } ] } ,
157+ { kind : 'corrupt_touched' , files : [ 'src/odd.ts' ] } ,
158+ ] ) ,
159+ } ) ;
160+ assert . match ( report , / n o t r u s t w o r t h y v e r d i c t / ) ;
161+ assert . match ( report , / n o r e c o r d i n t h e c o v e r a g e m a p \* \* : ` s r c \/ n e w \. t s ` \( 4 c h a n g e d l i n e \( s \) \) / ) ;
162+ assert . match ( report , / i m p o s s i b l e c o u n t s \* \* : ` s r c \/ o d d \. t s ` / ) ;
163+ // Every problem carries the command that clears it, since the reader has no other way to act.
164+ assert . match ( report , / R u n ` p n p m t e s t : c o v e r a g e ` a g a i n / ) ;
165+ } ) ;
166+
167+ test ( 'an unanswerable touched report never reads as the empty verdict it was computed from' , ( ) => {
168+ const report = renderCrapReport ( {
169+ findings : [ ] ,
170+ dropped : [ ] ,
171+ threshold : 30 ,
172+ scope : 'touched' ,
173+ verdict : verdictWith ( [ { kind : 'enumeration_missing' , detail : 'no coverage/lcov.info' } ] ) ,
174+ } ) ;
175+ assert . doesNotMatch ( report , / n o t o u c h e d f u n c t i o n \( s \) / ) ;
176+ assert . match (
177+ report ,
178+ / w h i c h f i l e s t h e c o v e r a g e r u n e n u m e r a t e d i s m i s s i n g o r u n r e a d a b l e \* \* \( n o c o v e r a g e \/ l c o v \. i n f o \) / ,
179+ ) ;
180+ } ) ;
181+
182+ test ( 'the same gap in the whole-repo report is a note, not a verdict' , ( ) => {
183+ const report = renderCrapReport ( {
184+ findings : [ ] ,
185+ dropped : [ ] ,
186+ threshold : 30 ,
187+ scope : 'all' ,
188+ verdict : {
189+ ...verdictWith ( [ { kind : 'map_partial' , mapFiles : 9 , enumeratedFiles : 12 } ] ) ,
190+ blocking : false ,
191+ } ,
192+ } ) ;
193+ assert . match ( report , / r e p o r t - o n l y \) : n o f u n c t i o n \( s \) a t o r a b o v e 3 0 / ) ;
194+ assert . match ( report , / N o t g a t i n g , b u t t h e r e p o r t i s i n c o m p l e t e / ) ;
195+ assert . match ( report , / \( 9 r e c o r d e d , 1 2 e n u m e r a t e d \) / ) ;
196+ } ) ;
197+
198+ test ( 'the exemptions are read from the resolved config and stated with their file-wide scope' , ( ) => {
199+ const exemptions = readCrapExemptions ( {
200+ health : {
201+ thresholdOverrides : [
202+ { files : [ 'src/worker.ts' ] , maxCrap : 1e12 , reason : 'Worker thread.' } ,
203+ { files : [ 'src/cheaper.ts' ] , maxCrap : 12 , reason : 'A cap, not an exemption.' } ,
204+ { files : [ 'src/no-crap.ts' ] , reason : 'Raises nothing.' } ,
205+ ] ,
206+ } ,
207+ } ) ;
208+ assert . deepEqual ( exemptions , [
209+ { files : [ 'src/worker.ts' ] , maxCrap : 1e12 , reason : 'Worker thread.' } ,
210+ ] ) ;
211+ const report = renderCrapReport ( {
212+ findings : [ ] ,
213+ dropped : [ ] ,
214+ threshold : 30 ,
215+ scope : 'all' ,
216+ verdict : clean ( 'all' ) ,
217+ exemptions : { available : true , exemptions } ,
218+ } ) ;
219+ assert . match ( report , / e x e m p t i o n s i n ` f a l l o w - c r a p \. j s o n ` \( 1 \) .* e v e r y f u n c t i o n i n i t s f i l e s / s) ;
220+ assert . match ( report , / - ` s r c \/ w o r k e r \. t s ` — ` m a x C r a p : 1 e \+ 1 2 ` : W o r k e r t h r e a d \. / ) ;
221+ } ) ;
222+
223+ test ( 'the exemption listing distinguishes none, unknown, and unreadable' , ( ) => {
224+ // None and unknown are different claims, and the third must not be a crash: fallow can exit 0 and
225+ // print a warning with no report, and which files are exempt is reporting, not gating. Throwing
226+ // there would turn a clean advisory `--all` run into exit 1.
227+ assert . deepEqual ( readCrapExemptions ( { health : { } } ) , [ ] ) ;
228+ assert . deepEqual ( readConfiguredExemptions ( '{"health":{"thresholdOverrides":[]}}' ) , {
229+ available : true ,
230+ exemptions : [ ] ,
231+ } ) ;
232+ assert . deepEqual ( readConfiguredExemptions ( 'Error: no config' ) , {
233+ available : false ,
234+ detail : 'fallow printed no JSON report:' ,
235+ } ) ;
236+ } ) ;
237+
238+ test ( 'exemptions that could not be read are reported as unknown rather than as none' , ( ) => {
239+ const report = renderCrapReport ( {
240+ findings : [ ] ,
241+ dropped : [ ] ,
242+ threshold : 30 ,
243+ scope : 'all' ,
244+ verdict : clean ( 'all' ) ,
245+ exemptions : { available : false , detail : 'fallow config exited with code 1' } ,
246+ } ) ;
247+ assert . match ( report , / c o u l d n o t b e r e a d f r o m t h e c o n f i g \( f a l l o w c o n f i g e x i t e d w i t h c o d e 1 \) / ) ;
248+ assert . doesNotMatch ( report , / e x e m p t i o n s i n ` f a l l o w - c r a p \. j s o n ` \( 0 \) / ) ;
249+ } ) ;
250+
251+ /**
252+ * A verdict over one touched file in one measured file, with whatever a case cares about overridden.
253+ * The defaults are the shape of a clean answer, so each case states only its difference.
254+ */
255+ const decide = ( input : Partial < Parameters < typeof decideCrapVerdict > [ 0 ] > = { } ) =>
256+ decideCrapVerdict ( {
257+ scope : 'touched' ,
258+ findings : [ ] ,
259+ estimated : [ ] ,
260+ touched : [ { path : 'src/a.ts' , addedLines : 1 } ] ,
261+ dropped : new Set ( ) ,
262+ mapPaths : new Set ( [ 'src/a.ts' ] ) ,
263+ enumeratedPaths : new Set ( [ 'src/a.ts' ] ) ,
264+ ioProblems : [ ] ,
265+ ...input ,
266+ } ) ;
267+
268+ const kinds = ( verdict : CrapVerdict ) => verdict . problems . map ( ( problem ) => problem . kind ) ;
269+
270+ test ( 'the verdict blocks a touched function at or above the threshold' , ( ) => {
271+ const findings = readCrapFindings ( { findings : [ finding ( { } ) ] } , 30 ) ;
272+ const verdict = decide ( { findings } ) ;
273+ assert . equal ( verdict . blocking , true ) ;
274+ assert . deepEqual ( verdict . problems , [ ] ) ;
275+ assert . deepEqual ( verdict . offenders , findings ) ;
276+ } ) ;
277+
278+ test ( 'the whole-repo verdict reports the backlog without blocking' , ( ) => {
279+ const verdict = decide ( {
280+ scope : 'all' ,
281+ findings : readCrapFindings ( { findings : [ finding ( { } ) ] } , 30 ) ,
282+ } ) ;
283+ assert . equal ( verdict . blocking , false ) ;
284+ assert . deepEqual ( verdict . offenders , [ ] ) ;
285+ } ) ;
286+
287+ test ( 'a touched file the sanitizer dropped is named once, as corrupt' , ( ) => {
288+ assert . deepEqual (
289+ kinds (
290+ decide ( {
291+ touched : [ { path : 'src/odd.ts' , addedLines : 1 } ] ,
292+ dropped : new Set ( [ 'src/odd.ts' ] ) ,
293+ } ) ,
294+ ) ,
295+ [ 'corrupt_touched' ] ,
296+ ) ;
297+ } ) ;
298+
299+ test ( 'an unmeasured touched function is a problem only in a file the map records' , ( ) => {
300+ const estimated = [ { path : 'src/a.ts' , name : 'added' , line : 40 , crap : 42 } ] ;
301+ // The map has the file but not the function: it predates the code and the score is a guess.
302+ assert . deepEqual ( kinds ( decide ( { estimated } ) ) , [ 'unmeasured_touched' ] ) ;
303+ // A file entirely outside the map is already reported as unscored, not also as unmeasured — and
304+ // an empty map is partial on its own, so both names appear and neither is a duplicate.
305+ assert . deepEqual ( kinds ( decide ( { estimated, mapPaths : new Set ( ) } ) ) , [
306+ 'map_partial' ,
307+ 'unscored_touched' ,
308+ ] ) ;
309+ } ) ;
310+
311+ test ( 'an estimate in a file coverage never enumerated is listed, not blocked on' , ( ) => {
312+ // `scripts/**` and other out-of-scope files are the shape of a normal tooling PR: coverage does not
313+ // instrument them at all, so re-running cannot measure them and the estimate is the only score
314+ // available. Blocking would refuse every such PR; staying silent would hide a number ≥ 30.
315+ const verdict = decide ( {
316+ estimated : [ { path : 'scripts/tool.ts' , name : 'parse' , line : 12 , crap : 31.6 } ] ,
317+ touched : [ { path : 'scripts/tool.ts' , addedLines : 3 } ] ,
318+ } ) ;
319+ assert . deepEqual ( verdict . problems , [ ] ) ;
320+ assert . equal ( verdict . blocking , false ) ;
321+ assert . deepEqual ( verdict . unscoped , [
322+ { path : 'scripts/tool.ts' , name : 'parse' , line : 12 , crap : 31.6 } ,
323+ ] ) ;
324+ const report = renderCrapReport ( {
325+ findings : [ ] ,
326+ dropped : [ ] ,
327+ threshold : 30 ,
328+ scope : 'touched' ,
329+ verdict,
330+ } ) ;
331+ assert . match ( report , / n o t o u c h e d f u n c t i o n \( s \) a t o r a b o v e 3 0 / ) ;
332+ assert . match (
333+ report ,
334+ / N o t m e a s u r e d , a n d n o t b l o c k i n g .* ` s c r i p t s \/ t o o l \. t s : 1 2 ` p a r s e \( C R A P 3 1 \. 6 \) / s,
335+ ) ;
336+ } ) ;
337+
338+ test ( 'an estimate in a file the map predates stays a blocking problem, not a listing' , ( ) => {
339+ const verdict = decide ( { estimated : [ { path : 'src/a.ts' , name : 'added' , line : 40 , crap : 42 } ] } ) ;
340+ assert . deepEqual ( kinds ( verdict ) , [ 'unmeasured_touched' ] ) ;
341+ assert . deepEqual ( verdict . unscoped , [ ] ) ;
342+ assert . equal ( verdict . blocking , true ) ;
343+ } ) ;
344+
345+ test ( 'an estimate is never called out-of-scope without an enumeration to check it against' , ( ) => {
346+ // Without lcov the run cannot tell "coverage does not instrument this" from "the run should have
347+ // measured this and did not" — the latter is the gap it already reports. Listing the file as
348+ // out-of-scope beside `no trustworthy verdict` would answer a question this run cannot ask.
349+ const verdict = decide ( {
350+ estimated : [ { path : 'scripts/tool.ts' , name : 'parse' , line : 12 , crap : 31.6 } ] ,
351+ touched : [ { path : 'scripts/tool.ts' , addedLines : 3 } ] ,
352+ mapPaths : new Set ( ) ,
353+ enumeratedPaths : new Set ( ) ,
354+ ioProblems : [ { kind : 'enumeration_missing' , detail : 'no coverage/lcov.info' } ] ,
355+ } ) ;
356+ assert . deepEqual ( verdict . unscoped , [ ] ) ;
357+ assert . equal ( verdict . blocking , true ) ;
358+ const report = renderCrapReport ( {
359+ findings : [ ] ,
360+ dropped : [ ] ,
361+ threshold : 30 ,
362+ scope : 'touched' ,
363+ verdict,
364+ } ) ;
365+ assert . doesNotMatch ( report , / d o e s n o t i n s t r u m e n t / ) ;
366+ } ) ;
367+
368+ test ( 'a map that records every enumerated file is trusted, and none is a partial map' , ( ) => {
369+ const both = [ 'src/a.ts' , 'src/b.ts' ] ;
370+ assert . deepEqual ( kinds ( decide ( { mapPaths : new Set ( both ) , enumeratedPaths : new Set ( both ) } ) ) , [ ] ) ;
371+ // One enumerated file the map does not record is enough to call the map incomplete.
372+ assert . deepEqual (
373+ kinds ( decide ( { mapPaths : new Set ( [ 'src/a.ts' ] ) , enumeratedPaths : new Set ( both ) } ) ) ,
374+ [ 'map_partial' ] ,
375+ ) ;
376+ } ) ;
377+
378+ test ( 'a same-sized map holding the wrong paths is still a partial map' , ( ) => {
379+ // Two coverage runs mixed into one directory can produce equal counts over different files, and a
380+ // count comparison would call that complete. The check is per path, so one unrecorded file is
381+ // enough — and it names the touched file rather than only the discrepancy.
382+ const verdict = decide ( {
383+ mapPaths : new Set ( [ 'src/other.ts' , 'src/b.ts' ] ) ,
384+ enumeratedPaths : new Set ( [ 'src/a.ts' , 'src/b.ts' ] ) ,
385+ } ) ;
386+ assert . deepEqual ( kinds ( verdict ) , [ 'map_partial' , 'unscored_touched' ] ) ;
387+ assert . equal ( verdict . blocking , true ) ;
388+ } ) ;
389+
390+ test ( 'an enumeration of zero files cannot serve as the record of what ran' , ( ) => {
391+ // A truncated or malformed lcov parses to nothing rather than failing, and every completeness
392+ // check reads this list: an empty one would silence them all and leave a clean-looking verdict.
393+ assert . equal ( usableEnumeration ( [ ] ) , null ) ;
394+ assert . deepEqual ( [ ...usableEnumeration ( [ 'src/a.ts' ] ) ! ] , [ 'src/a.ts' ] ) ;
395+ } ) ;
396+
397+ test ( 'a coverage map is only readable as files to counter objects' , ( ) => {
398+ assert . deepEqual ( parseIstanbulCoverageMap ( '{"src/a.ts":{"s":{"0":0}}}' ) , {
399+ 'src/a.ts' : { s : { 0 : 0 } } ,
400+ } ) ;
401+ assert . throws ( ( ) => parseIstanbulCoverageMap ( 'not json' ) , / n o t v a l i d J S O N / ) ;
402+ assert . throws ( ( ) => parseIstanbulCoverageMap ( '[]' ) , / a n a r r a y , n o t a f i l e - > c o u n t e r s o b j e c t / ) ;
403+ assert . throws ( ( ) => parseIstanbulCoverageMap ( '{"src/a.ts":[]}' ) , / f o r s r c \/ a \. t s i s o b j e c t / ) ;
404+ assert . throws ( ( ) => parseIstanbulCoverageMap ( '{"src/a.ts":{"s":[]}}' ) , / h a s s a s o b j e c t / ) ;
405+ } ) ;
406+
407+ test ( "coverage keys from another checkout do not match this root's files" , ( ) => {
408+ const paths = coverageMapPaths (
409+ { '/work/other/src/a.ts' : { } , 'src/relative.ts' : { } } ,
410+ '/work/this' ,
411+ ) ;
412+ assert . deepEqual ( [ ...paths ] , [ '../other/src/a.ts' , 'src/relative.ts' ] ) ;
413+ } ) ;
414+
102415test ( 'annotations put each finding on its file and line' , ( ) => {
103416 const findings = readCrapFindings ( { findings : [ finding ( { } ) ] } , 30 ) ;
104417 assert . equal (
0 commit comments