Skip to content

Commit 1043489

Browse files
build(html): say why each sweep finding is expected, and catch a stale one (#22167)
* build(html): say why each sweep finding is expected, and catch a stale one The invariant sweep reported eight findings a reader had to re-derive every run, so a real divergence would have hidden among them. Each is now stated with its reason and counted apart; an entry matching nothing is reported as stale, since a divergence that is gone must lose its entry or the list stops saying anything true. * style: format the changelog the release generator re-emits `fmt:check` is red on main: the release rewrites CHANGELOG.md from the changesets and nothing runs prettier over what it writes. Same one-line fix as #22166, which no-ops once either lands. * fix(tooling): scope an expectation to every fixture its group reaches A source-scoped entry matched a group as soon as one of its fixtures matched, so the same repro found somewhere the entry does not name was suppressed with it. Covers findingGroups' classification in the unit suite, which reached none of it.
1 parent 92c8185 commit 1043489

3 files changed

Lines changed: 205 additions & 4 deletions

File tree

‎test/unitCases/CompareToolsHarness.unittest.js‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
compress,
99
exists,
1010
filterFrom,
11+
findingGroups,
1112
formatCost,
1213
installPackages,
1314
kb,
@@ -545,4 +546,100 @@ describe("compare-tools-harness", () => {
545546
expect(wanted("csso")).toBe(false);
546547
});
547548
});
549+
550+
describe("findingGroups", () => {
551+
/**
552+
* @param {string} relation what the finding is about
553+
* @param {string} repro the lines reproducing it
554+
* @returns {import("../../tooling/compare-tools-harness").Report} the finding
555+
*/
556+
const finding = (relation, repro) => ({ relation, what: "bytes", repro });
557+
558+
/**
559+
* @param {ReturnType<typeof findingGroups>} groups the collector
560+
* @returns {{ text: string, count: number }} what it wrote and counted
561+
*/
562+
const written = (groups) => {
563+
let text = "";
564+
const count = groups.write((line) => {
565+
text += line;
566+
});
567+
return { text, count };
568+
};
569+
570+
it("names a finding no entry expects, and counts it", () => {
571+
const groups = findingGroups();
572+
groups.add(finding("idempotence", " a -> b"), "default", "one.html");
573+
const { text, count } = written(groups);
574+
expect(count).toBe(1);
575+
expect(text).toContain("idempotence — bytes");
576+
expect(text).toContain("one.html");
577+
});
578+
579+
it("gathers one repro found in several sources into one finding", () => {
580+
const groups = findingGroups();
581+
groups.add(finding("idempotence", " a -> b"), "default", "one.html");
582+
groups.add(finding("idempotence", " a -> b"), "aggressive", "two.html");
583+
const { text, count } = written(groups);
584+
expect(count).toBe(1);
585+
expect(text).toContain("2 sources, from one.html");
586+
expect(text).toContain("[default, aggressive]");
587+
});
588+
589+
it("counts an expected finding apart, and says why it is owed nothing", () => {
590+
const groups = findingGroups([
591+
{ relation: "idempotence", contains: "a ->", why: "the spec says so" }
592+
]);
593+
groups.add(finding("idempotence", " a -> b"), "default", "one.html");
594+
const { text, count } = written(groups);
595+
expect(count).toBe(0);
596+
expect(text).toContain("expected, and why — 1 of 1");
597+
expect(text).toContain("the spec says so");
598+
});
599+
600+
it("expects nothing on a relation or a repro the entry does not name", () => {
601+
const groups = findingGroups([
602+
{ relation: "idempotence", contains: "a ->", why: "…" }
603+
]);
604+
groups.add(finding("respelling", " a -> b"), "default", "one.html");
605+
groups.add(finding("idempotence", " c -> d"), "default", "two.html");
606+
expect(written(groups).count).toBe(3);
607+
});
608+
609+
it("reports an entry that matched nothing as stale", () => {
610+
const groups = findingGroups([
611+
{ relation: "idempotence", contains: "gone", why: "…" }
612+
]);
613+
const { text, count } = written(groups);
614+
expect(count).toBe(1);
615+
expect(text).toContain("stale — 1 expectation(s) matched nothing");
616+
expect(text).toContain("idempotence: gone");
617+
});
618+
619+
it("lets a source-scoped entry cover the fixture it names", () => {
620+
const groups = findingGroups([
621+
{
622+
relation: "idempotence",
623+
contains: "a ->",
624+
source: "one",
625+
why: "that fixture writes it"
626+
}
627+
]);
628+
groups.add(finding("idempotence", " a -> b"), "default", "one.html");
629+
expect(written(groups).count).toBe(0);
630+
});
631+
632+
it("leaves a group unexpected where it reaches a fixture the entry omits", () => {
633+
const groups = findingGroups([
634+
{ relation: "idempotence", contains: "a ->", source: "one", why: "…" }
635+
]);
636+
groups.add(finding("idempotence", " a -> b"), "default", "one.html");
637+
groups.add(finding("idempotence", " a -> b"), "default", "other.html");
638+
const { text, count } = written(groups);
639+
// The finding itself, and the entry that then covers nothing.
640+
expect(count).toBe(2);
641+
expect(text).toContain("idempotence — bytes");
642+
expect(text).toContain("stale — 1 expectation(s) matched nothing");
643+
});
644+
});
548645
});

‎tooling/compare-html-tools.js‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,56 @@ const invariantFixtures = () => {
13481348
return out;
13491349
};
13501350

1351+
/**
1352+
* What the sweep reports that the printer owes nothing for. Each entry states
1353+
* the reason, since a divergence with no reason beside it is one nobody can
1354+
* tell from a defect later.
1355+
* @type {readonly import("./compare-tools-harness").Expected[]}
1356+
*/
1357+
const EXPECTED = [
1358+
{
1359+
relation: "respelling references",
1360+
contains: "&nbsp;",
1361+
why: "WHATWG 'escape a string' replaces U+00A0 with `&nbsp;`, which is what `escapeAttribute` implements; the literal costs 32 raw bytes less over 939 documents and nothing compressed either way"
1362+
},
1363+
{
1364+
relation: "respelling quote-double",
1365+
contains: "&nbsp;",
1366+
why: "the same escaping, reached through the delimiter the value is written in"
1367+
},
1368+
{
1369+
relation: "respelling quote-single",
1370+
contains: "&nbsp;",
1371+
why: "the same escaping, reached through the delimiter the value is written in"
1372+
},
1373+
{
1374+
relation: "respelling unquote",
1375+
contains: "&nbsp;",
1376+
why: "the same escaping, reached through a value written without quotes"
1377+
},
1378+
{
1379+
relation: "respelling quote-double",
1380+
contains: "&#34;",
1381+
source: "style-attribute",
1382+
why: "`&quot;` for `&#34;` is 3 raw bytes more and 11 gzip bytes less, and gzip decides"
1383+
},
1384+
{
1385+
relation: "respelling quote-double",
1386+
contains: "{%",
1387+
why: "a tag whose duplicate attribute names HTML parsing drops is echoed from source: rebuilding it would print the drop (#22013)"
1388+
},
1389+
{
1390+
relation: "respelling quote-single",
1391+
contains: "{%",
1392+
why: "the same echo, reached through the other delimiter"
1393+
},
1394+
{
1395+
relation: "respelling references",
1396+
contains: "{%",
1397+
why: "the same echo, which also leaves the tag's other values spelled as the source wrote them"
1398+
}
1399+
];
1400+
13511401
/**
13521402
* Sweep mode: hold the printer to its own invariants and report what it breaks.
13531403
* Nothing is installed and nothing is compared to, so this is the cheap half of
@@ -1359,7 +1409,7 @@ const invariants = (write) => {
13591409
const corpus = invariantFixtures().filter(([label]) => wantedFixture(label));
13601410
const presets = PRESETS.filter(([name]) => wantedPreset(name));
13611411
log(`sweeping ${corpus.length} documents under ${presets.length} presets …`);
1362-
const groups = findingGroups();
1412+
const groups = findingGroups(EXPECTED);
13631413
for (const [label, html] of corpus) {
13641414
for (const [preset, options] of presets) {
13651415
for (const report of sweepDocument(printerFor(options), html)) {

‎tooling/compare-tools-harness.js‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,40 @@ const idempotence = ({ minify, source, says, repro }) => {
569569
};
570570
};
571571

572+
/**
573+
* A finding the printer owes nothing for, with the reason it is owed nothing.
574+
* `relation` and `contains` together name it; `source` narrows it to one
575+
* fixture where the same repro is a defect elsewhere.
576+
* @typedef {{ relation: string, contains: string, source?: string, why: string }} Expected
577+
*/
578+
579+
/**
580+
* Whether one expectation covers one group.
581+
* @param {Expected} expected the entry
582+
* @param {Report} report the finding
583+
* @param {Set<string>} sources the fixtures it was found in
584+
* @returns {boolean} true when the entry names this finding
585+
*/
586+
const _covers = (expected, report, sources) => {
587+
if (expected.relation !== report.relation) return false;
588+
if (!report.repro.includes(expected.contains)) return false;
589+
if (expected.source === undefined) return true;
590+
// Every fixture the group reaches, not any one of them: the same repro found
591+
// somewhere the entry does not name is a divergence it says nothing about.
592+
for (const source of sources) {
593+
if (!source.includes(expected.source)) return false;
594+
}
595+
return true;
596+
};
597+
572598
/**
573599
* Findings gathered by the repro rather than by the source: one printer defect
574-
* reaches hundreds of files, and is one thing to fix.
600+
* reaches hundreds of files, and is one thing to fix. An expected finding is
601+
* counted apart, so the number a run reports is what is left to answer for.
602+
* @param {readonly Expected[]=} expected findings already answered for
575603
* @returns {{ add: (report: Report, preset: string, label: string) => void, write: (out: (text: string) => void) => number }} the collector
576604
*/
577-
const findingGroups = () => {
605+
const findingGroups = (expected = []) => {
578606
/** @type {Map<string, { report: Report, presets: Set<string>, sources: Set<string> }>} */
579607
const groups = new Map();
580608
return {
@@ -605,7 +633,18 @@ const findingGroups = () => {
605633
const ordered = [...groups.values()].sort(
606634
(a, b) => b.sources.size - a.sources.size
607635
);
636+
/** @type {Set<Expected>} */
637+
const matched = new Set();
638+
/** @type {typeof ordered} */
639+
const unexpected = [];
608640
for (const group of ordered) {
641+
const covering = expected.find((entry) =>
642+
_covers(entry, group.report, group.sources)
643+
);
644+
if (covering === undefined) unexpected.push(group);
645+
else matched.add(covering);
646+
}
647+
for (const group of unexpected) {
609648
const { relation, what, repro } = group.report;
610649
const sources = [...group.sources];
611650
const reach =
@@ -618,7 +657,22 @@ const findingGroups = () => {
618657
)}] ${reach}\n`
619658
);
620659
}
621-
return ordered.length;
660+
if (matched.size !== 0) {
661+
out(`\nexpected, and why — ${matched.size} of ${expected.length}\n`);
662+
for (const entry of expected) {
663+
if (matched.has(entry)) out(` ${entry.relation}: ${entry.why}\n`);
664+
}
665+
}
666+
// An entry matching nothing is the divergence it named being gone: the
667+
// list has to lose it, or it stops saying anything true.
668+
const stale = expected.filter((entry) => !matched.has(entry));
669+
if (stale.length !== 0) {
670+
out(`\nstale — ${stale.length} expectation(s) matched nothing\n`);
671+
for (const entry of stale) {
672+
out(` ${entry.relation}: ${entry.contains}\n`);
673+
}
674+
}
675+
return unexpected.length + stale.length;
622676
}
623677
};
624678
};

0 commit comments

Comments
 (0)