Skip to content

Commit 592abc8

Browse files
committed
test(css): read the benchmark corpus from the scripts that build it
The fixture list was copied into the helper, and had already drifted: two Bootstrap labels disagreed with the comparison. The scripts export it instead and generate only as the entry point, the way `generate-css-data.js` does. An absent cache now reports, rather than leaving the tier out of the run.
1 parent f1d41cb commit 592abc8

4 files changed

Lines changed: 85 additions & 73 deletions

File tree

‎test/helpers/syntaxEquivalence.js‎

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -61,55 +61,20 @@ const buildCorpus = (dir, extension, minify) => {
6161
});
6262
};
6363

64-
// What the minifier comparisons install, read back as a corpus: real framework
65-
// stylesheets and documents, which `configCases` and wpt between them do not
66-
// have — those are spec fixtures, written to exercise a rule rather than to
67-
// ship. Absent until `yarn benchmark:css-minifiers` / `:html-minifiers` has run
68-
// once, so a caller that finds nothing says so rather than reporting green.
69-
const CSS_BENCHMARK_CACHE = path.join(
70-
__dirname,
71-
"../../node_modules/.cache/css-minifier-comparison"
72-
);
73-
const HTML_BENCHMARK_CACHE = path.join(
74-
__dirname,
75-
"../../node_modules/.cache/html-minifier-comparison"
76-
);
77-
78-
// Named rather than walked: the caches also hold each tool's own `node_modules`,
79-
// and a package's test fixtures are not what any of this is about.
80-
/** @type {[string, string][]} */
81-
const BENCHMARK_STYLESHEETS = [
82-
["Animate.css 4", "node_modules/animate.css/animate.css"],
83-
["Bootstrap 5", "node_modules/bootstrap/dist/css/bootstrap.css"],
84-
["Bootstrap 5 grid", "node_modules/bootstrap/dist/css/bootstrap-grid.css"],
85-
["Bulma 1", "node_modules/bulma/css/bulma.css"],
86-
["Font Awesome 6", "node_modules/@fortawesome/fontawesome-free/css/all.css"],
87-
["Foundation 6", "node_modules/foundation-sites/dist/css/foundation.css"],
88-
["Materialize 1", "node_modules/materialize-css/dist/css/materialize.css"],
89-
["Milligram 1", "node_modules/milligram/dist/milligram.css"],
90-
["normalize.css 8", "node_modules/normalize.css/normalize.css"],
91-
["Pico 2", "node_modules/@picocss/pico/css/pico.css"],
92-
["Primer 21", "node_modules/@primer/css/dist/primer.css"],
93-
["Pure 3", "node_modules/purecss/build/pure.css"],
94-
["sanitize.css 13", "node_modules/sanitize.css/sanitize.css"],
95-
["Semantic UI 2", "node_modules/semantic-ui-css/semantic.css"],
96-
["Tachyons 4", "node_modules/tachyons/css/tachyons.css"],
97-
["UIkit 3", "node_modules/uikit/dist/css/uikit.css"],
98-
["Water.css 2", "node_modules/water.css/out/water.css"],
99-
["Tailwind 4 app", "tailwind-app.css"],
100-
["Tailwind 4 wide", "tailwind-wide.css"],
101-
["Tailwind 4 + daisyUI 5", "tailwind-daisyui.css"]
102-
];
103-
104-
/** @type {[string, string][]} */
105-
const BENCHMARK_DOCUMENTS = [
106-
["HTML5 Boilerplate 9", "node_modules/html5-boilerplate/dist/index.html"],
107-
["Swagger UI 5", "node_modules/swagger-ui-dist/index.html"]
108-
];
64+
// Read from the comparison scripts, so a fixture added to one is added here.
65+
const {
66+
CACHE: CSS_CACHE,
67+
GENERATED_FIXTURES,
68+
INSTALLED_FIXTURES
69+
} = require("../../tooling/compare-css-minifiers");
70+
const {
71+
CACHE: HTML_CACHE,
72+
INSTALLED_DOCUMENTS
73+
} = require("../../tooling/compare-html-minifiers");
10974

11075
/**
111-
* Read a benchmark cache as a corpus. A file the cache does not hold is left out
112-
* rather than failing: each cache is built by the comparison that installs it.
76+
* Read a benchmark cache as a corpus. A file the cache does not hold is left
77+
* out: each cache is built by the comparison that installs it.
11378
* @param {string} cache the cache directory
11479
* @param {[string, string][]} entries `[label, path within the cache]`
11580
* @param {(source: string) => string} minify the printer to run
@@ -128,20 +93,37 @@ const readBenchmarkCache = (cache, entries, minify) => {
12893
};
12994

13095
/**
131-
* The framework stylesheets `yarn benchmark:css-minifiers` installs.
96+
* The stylesheets `yarn benchmark:css-minifiers` installs and builds.
13297
* @param {(source: string) => string} minify the printer to run
13398
* @returns {Fixture[]} the corpus, empty until that has been run once
13499
*/
135100
const benchmarkStylesheets = (minify) =>
136-
readBenchmarkCache(CSS_BENCHMARK_CACHE, BENCHMARK_STYLESHEETS, minify);
101+
readBenchmarkCache(
102+
CSS_CACHE,
103+
[
104+
...INSTALLED_FIXTURES.map(
105+
(/** @type {[string, string]} */ [label, file]) =>
106+
/** @type {[string, string]} */ ([label, `node_modules/${file}`])
107+
),
108+
...GENERATED_FIXTURES
109+
],
110+
minify
111+
);
137112

138113
/**
139114
* The documents `yarn benchmark:html-minifiers` installs.
140115
* @param {(source: string) => string} minify the printer to run
141116
* @returns {Fixture[]} the corpus, empty until that has been run once
142117
*/
143118
const benchmarkDocuments = (minify) =>
144-
readBenchmarkCache(HTML_BENCHMARK_CACHE, BENCHMARK_DOCUMENTS, minify);
119+
readBenchmarkCache(
120+
HTML_CACHE,
121+
INSTALLED_DOCUMENTS.map(
122+
(/** @type {[string, string]} */ [label, file]) =>
123+
/** @type {[string, string]} */ ([label, `node_modules/${file}`])
124+
),
125+
minify
126+
);
145127

146128
/**
147129
* @typedef {{ kind: string, condition: string }} Condition
@@ -293,9 +275,8 @@ const installHelpers = () => {
293275
: word;
294276
word = "";
295277
};
296-
// A pixel ends in a channel, so a token written straight after the color
297-
// would read as part of it — `rgba(…)0` is a color and a zero, not one
298-
// channel more. The printer writes that form, since the `)` parts them.
278+
// A pixel ends in a channel, so `rgba(…)0` — the form the printer writes,
279+
// since the `)` parts them — would read as one channel more.
299280
const painting = run.replace(
300281
COLOR_TOKEN_RE,
301282
(color, at, whole) =>

‎test/syntaxEquivalence.spectest.js‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ const FILED_CONFIG_HTML_DEFECTS = new Map();
7979
const FILED_BENCHMARK_CSS_DEFECTS = new Map([
8080
[
8181
"Semantic UI 2",
82-
"not a printer defect: `.ui.table tr.negative, td.negative` and the same pair for `.error` print the same block, so joining each pair leaves four one-selector entries where the source had eight. The four carry one block between them, which makes their order unobservable — an element carrying both classes computes the same style either way, checked in Chrome. The comparison sorts a run of selectors reaching one block, and these land in two runs rather than one."
82+
"not a printer defect: joining two rules that print one block reorders the selectors, and the comparison sorts a run rather than two — an element matching both computes the same style, checked in Chrome"
8383
],
8484
[
8585
"Tailwind 4 + daisyUI 5",
86-
"not a printer defect: the same shape as Semantic UI, with a `@media` between the two runs. `.collapse-arrow` and `.collapse-plus` `::after` come back reordered and four entries shorter; an element carrying both classes computes the same style at either side of the query, checked in Chrome."
86+
"not a printer defect: the Semantic UI shape with a `@media` between the runs — an element matching both computes the same style at either side of the query, checked in Chrome"
8787
]
8888
]);
8989

@@ -252,9 +252,7 @@ const buildCorpora = () => {
252252
filedCss: FILED_CONFIG_CSS_DEFECTS
253253
}
254254
];
255-
// The framework sheets and documents the minifier comparisons install: real
256-
// projects, where `configCases` and wpt are both spec fixtures. Left out
257-
// entirely when neither comparison has been run, and reported below.
255+
// Real projects, where `configCases` and wpt are both spec fixtures.
258256
const benchHtml = benchmarkDocuments((source) => source);
259257
const benchCss = benchmarkStylesheets(minifyCss);
260258
if (benchHtml.length > 0 || benchCss.length > 0) {
@@ -320,6 +318,9 @@ const inBatches = async (page, items, evaluate) => {
320318
const NO_CORPUS =
321319
"wpt submodule not initialized (run `git submodule update --init --depth 1 test/wpt`)";
322320

321+
const NO_BENCHMARK_CORPUS =
322+
"minifier comparison caches not built (run `yarn benchmark:css-minifiers` / `yarn benchmark:html-minifiers`)";
323+
323324
expectNoDeprecations();
324325

325326
describe("printer output in real Chrome", () => {
@@ -526,7 +527,7 @@ describe("printer output in real Chrome", () => {
526527

527528
const describeCorpus = (at, label) => {
528529
describe(label, () => {
529-
if (at === 1 && !hasCorpus()) {
530+
if (label === "wpt" && !hasCorpus()) {
530531
it(NO_CORPUS, () => {
531532
// No-op: the corpus is an optional git submodule.
532533
});
@@ -687,9 +688,16 @@ describe("printer output in real Chrome", () => {
687688
});
688689
};
689690

690-
// By position: which corpora were built depends on what is checked out and
691-
// which comparisons have been run, so each names itself.
691+
// Which corpora were built depends on what is checked out, so each names
692+
// itself; a tier that cannot run reports that rather than reporting green.
692693
for (const [at, one] of corpora.entries()) describeCorpus(at, one.label);
694+
if (!corpora.some((one) => one.label === "benchmark corpus")) {
695+
describe("benchmark corpus", () => {
696+
it(NO_BENCHMARK_CORPUS, () => {
697+
// No-op: the caches are built by the comparison scripts.
698+
});
699+
});
700+
}
693701

694702
// One test per declaration, not per file: the value is what a defect is filed
695703
// against, so the run names it without anything having to narrow it down.

‎tooling/compare-css-minifiers.js‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,21 @@ const main = async () => {
524524
}
525525
};
526526

527-
(process.argv[2] === "--measure" ? measure(process.argv[3]) : main()).catch(
528-
(error) => {
529-
log(String(error && error.stack ? error.stack : error));
530-
process.exitCode = 1;
531-
}
532-
);
527+
// Only as the entry point, so the corpus below can be required from a test.
528+
if (require.main === module) {
529+
(process.argv[2] === "--measure" ? measure(process.argv[3]) : main()).catch(
530+
(error) => {
531+
log(String(error && error.stack ? error.stack : error));
532+
process.exitCode = 1;
533+
}
534+
);
535+
}
536+
537+
// Where the cache holds each fixture, for a reader that is not this script.
538+
module.exports.CACHE = CACHE;
539+
module.exports.GENERATED_FIXTURES = /** @type {[string, string][]} */ ([
540+
["Tailwind 4 (app-sized)", "tailwind-app.css"],
541+
["Tailwind 4 (wide utilities)", "tailwind-wide.css"],
542+
["Tailwind 4 + daisyUI 5", "tailwind-daisyui.css"]
543+
]);
544+
module.exports.INSTALLED_FIXTURES = INSTALLED_FIXTURES;

‎tooling/compare-html-minifiers.js‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,20 @@ const main = async () => {
839839
}
840840
};
841841

842-
(process.argv[2] === "--measure" ? measure(process.argv[3]) : main()).catch(
843-
(error) => {
844-
log(String(error && error.stack ? error.stack : error));
845-
process.exitCode = 1;
846-
}
847-
);
842+
// Only as the entry point, so the documents below can be required from a test.
843+
if (require.main === module) {
844+
(process.argv[2] === "--measure" ? measure(process.argv[3]) : main()).catch(
845+
(error) => {
846+
log(String(error && error.stack ? error.stack : error));
847+
process.exitCode = 1;
848+
}
849+
);
850+
}
851+
852+
// The documents that are files rather than pages this builds, for a reader that
853+
// is not this script.
854+
module.exports.CACHE = CACHE;
855+
module.exports.INSTALLED_DOCUMENTS = /** @type {[string, string][]} */ ([
856+
["HTML5 Boilerplate 9", "html5-boilerplate/dist/index.html"],
857+
["Swagger UI 5", "swagger-ui-dist/index.html"]
858+
]);

0 commit comments

Comments
 (0)