Skip to content

Commit b2a62d9

Browse files
authored
ci: the interaction lanes leave out unit tests, docs, records and a11y specs in a component folder (#10979)
* ci: the interaction lanes leave out unit tests, docs, records and a11y specs under a package's src Those files put nothing on a page an interaction proof loads, so a pull request that changes only them no longer starts the full Chromium suite. Coverage is unchanged. * test(ci): the scope test names real files the proofs load and never load * ci: theme sources stay in the interaction scopes, tests included * ci: a file a proof imports is never left out of the interaction scopes * ci: the proof import map lives with the other import graph * ci: only the component packages' non-runtime files leave the scopes; the guard walks stories and preview too * ci: only component folders lose their non-runtime files; the guard follows bundled fixtures * Merge main (the nightly full interaction run) into ci/interaction-scope-skips-non-runtime
1 parent 0a320be commit b2a62d9

3 files changed

Lines changed: 317 additions & 4 deletions

File tree

‎.github/scripts/a11y-pr-contracts.mjs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,81 @@ export function renderingFolders(importers, touched) {
638638
.sort((a, b) => a.depth - b.depth || a.folder.localeCompare(b.folder));
639639
}
640640

641+
/**
642+
* The files each proof page loads through value imports, followed file by
643+
* file: relative specifiers and `@astryxdesign/<package>` roots and paths.
644+
* The roots are the interaction specs and what their pages render: the
645+
* Storybook stories, Storybook's preview, and each fixture a spec bundles
646+
* (a quoted path in the spec that names a listed file). A unit test, doc,
647+
* record or
648+
* accessibility spec in a component folder puts nothing on a proof's page
649+
* and selects no proof, unless a root loads it; this map finds that case
650+
* (spec:AST-030 FR31). It throws when a root or a file it loads cannot be
651+
* read, so a caller runs every proof instead.
652+
* @param {{root: string, listed: string[], isProof: (file: string) => boolean}} options
653+
* @returns {Map<string, Set<string>>} each loaded file, to the roots that load it
654+
*/
655+
export function proofImporters({root, listed, isProof}) {
656+
const known = new Set(listed);
657+
const resolve = target => {
658+
for (const suffix of RESOLVE_SUFFIXES)
659+
if (known.has(`${target}${suffix}`)) return `${target}${suffix}`;
660+
return null;
661+
};
662+
const targetOf = (file, specifier) => {
663+
if (specifier.startsWith('.'))
664+
return resolve(
665+
path.posix.normalize(`${path.posix.dirname(file)}/${specifier}`),
666+
);
667+
const match = /^@astryxdesign\/([^/]+)(?:\/(.+))?$/.exec(specifier);
668+
return match
669+
? resolve(`packages/${match[1]}/src/${match[2] ?? 'index'}`)
670+
: null;
671+
};
672+
// A spec bundles a fixture by path (`new URL('./fixture.ts', …)`,
673+
// `bundleOf('fixture.ts')`): every quoted path beside it that names a
674+
// listed source file counts as an import.
675+
const QUOTED_PATH = /['"`]((?:\.{1,2}\/)?[\w./-]+\.[cm]?[jt]sx?)['"`]/g;
676+
const bundledBy = (file, text) =>
677+
[...text.matchAll(QUOTED_PATH)]
678+
.map(match =>
679+
resolve(
680+
path.posix.normalize(
681+
`${path.posix.dirname(file)}/${match[1].replace(/^(?!\.)/, './')}`,
682+
),
683+
),
684+
)
685+
.filter(Boolean);
686+
const imports = new Map();
687+
const importsOf = file => {
688+
if (!imports.has(file)) {
689+
const text = fs.readFileSync(path.join(root, file), 'utf8');
690+
imports.set(file, [
691+
...valueImports(text)
692+
.map(({specifier}) => targetOf(file, specifier))
693+
.filter(Boolean),
694+
...(isProof(file) ? bundledBy(file, text) : []),
695+
]);
696+
}
697+
return imports.get(file);
698+
};
699+
const importers = new Map();
700+
for (const proof of listed.filter(isProof)) {
701+
const seen = new Set([proof]);
702+
const queue = [proof];
703+
while (queue.length > 0) {
704+
for (const target of importsOf(queue.pop())) {
705+
if (seen.has(target)) continue;
706+
seen.add(target);
707+
queue.push(target);
708+
if (!importers.has(target)) importers.set(target, new Set());
709+
importers.get(target).add(proof);
710+
}
711+
}
712+
}
713+
return importers;
714+
}
715+
641716
/**
642717
* The `scoped` and `release-only` specs of the component folders that render a
643718
* touched folder, nearest first, then in path order, leaving out `planned`.
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file Contract: the interaction lanes' scope leaves out the files under a
5+
* package's src that put nothing on a page the proofs load (unit tests,
6+
* docs, Markdown records and accessibility specs), in both engines, in the
7+
* leg-count job and in each lane's own scope step.
8+
*/
9+
10+
import {execFileSync} from 'node:child_process';
11+
import fs from 'node:fs';
12+
import os from 'node:os';
13+
import path from 'node:path';
14+
15+
import {describe, expect, it} from 'vitest';
16+
import yaml from 'yaml';
17+
18+
import {isInteractionSpec} from './playwright-legs.mjs';
19+
import {proofImporters} from './a11y-pr-contracts.mjs';
20+
21+
// What a proof's page loads: the interaction specs, the stories they open,
22+
// and Storybook's preview.
23+
const isProofPage = file =>
24+
isInteractionSpec(file) ||
25+
/\.stories\.[cm]?[jt]sx?$/.test(file) ||
26+
file === 'apps/storybook/.storybook/preview.tsx';
27+
28+
const root = path.resolve(import.meta.dirname, '../..');
29+
const ci = yaml.parse(
30+
fs.readFileSync(path.join(root, '.github/workflows/ci.yml'), 'utf8'),
31+
);
32+
const scopeOf = (job, id) =>
33+
new RegExp(ci.jobs[job].steps.find(step => step.id === id).with.paths);
34+
35+
const SCOPES = {
36+
'Chromium legs': scopeOf('interaction-legs', 'scope-chromium'),
37+
'Chromium proofs': scopeOf('pr-interaction', 'scope'),
38+
'WebKit legs': scopeOf('interaction-legs', 'scope-webkit'),
39+
'WebKit proofs': scopeOf('pr-interaction-webkit', 'scope'),
40+
};
41+
42+
// Real files the proofs never load.
43+
const NON_RUNTIME = [
44+
'packages/core/src/BottomSheet/BottomSheet.test.tsx',
45+
'packages/core/src/BottomSheet/BottomSheetCloseFocus.test.tsx',
46+
'packages/core/src/BottomSheet/BottomSheet.doc.mjs',
47+
// Accessibility specs, in and outside a `__tests__/` folder.
48+
'packages/core/src/Table/__tests__/TableFocusRing.a11y.chromium.spec.ts',
49+
'packages/core/src/Lightbox/LightboxKeyboard.a11y.browser.spec.ts',
50+
];
51+
52+
// Real files a proof's page or fixture loads.
53+
const RUNTIME = [
54+
'packages/core/src/BottomSheet/BottomSheet.tsx',
55+
'packages/core/src/BottomSheet/BottomSheetPanel.tsx',
56+
'packages/core/src/BottomSheet/useSheetGestures.ts',
57+
'apps/storybook/stories/BottomSheet.stories.tsx',
58+
'packages/core/src/gestures/__tests__/browser/fixture.ts',
59+
'packages/themes/neutral/src/neutralTheme.ts',
60+
// Theme sources are shared infrastructure: a test there selects too.
61+
'packages/themes/neutral/src/neutralTheme.test.ts',
62+
'packages/core/src/theme/Theme.test.tsx',
63+
// So are a component package's shared folders.
64+
'packages/lab/src/__tests__/ReorderGripPinchZoom.a11y.chromium.spec.ts',
65+
'packages/core/src/gestures/README.md',
66+
'packages/core/src/gestures/__tests__/GestureHost.test.ts',
67+
// So is every package outside the component packages.
68+
'packages/native/src/contract/contracts.test.ts',
69+
];
70+
71+
describe('the interaction scope', () => {
72+
for (const [lane, scope] of Object.entries(SCOPES)) {
73+
it(`${lane}: a test-, doc- or record-only change selects nothing`, () => {
74+
for (const file of NON_RUNTIME) {
75+
expect(scope.test(file), file).toBe(false);
76+
}
77+
});
78+
79+
it(`${lane}: a runtime file still selects the proofs`, () => {
80+
for (const file of RUNTIME) {
81+
expect(scope.test(file), file).toBe(true);
82+
}
83+
});
84+
85+
if (lane.startsWith('Chromium'))
86+
it(`${lane}: a test at the root of a package outside the component packages still selects them`, () => {
87+
// WebKit's scope never covered a package's src root.
88+
expect(scope.test('packages/build/src/babel.test.mjs')).toBe(true);
89+
// A component package's src root is shared too.
90+
expect(scope.test('packages/charts/src/Chart.test.tsx')).toBe(true);
91+
});
92+
93+
it(`${lane}: a changed interaction spec still selects them`, () => {
94+
expect(
95+
scope.test(
96+
'packages/core/src/BottomSheet/__tests__/BottomSheetDetent.interaction.browser.spec.ts',
97+
),
98+
).toBe(true);
99+
});
100+
}
101+
102+
it('each lane decides the same way in the leg count and in its own scope step', () => {
103+
expect(SCOPES['Chromium legs'].source).toBe(
104+
SCOPES['Chromium proofs'].source,
105+
);
106+
expect(SCOPES['WebKit legs'].source).toBe(SCOPES['WebKit proofs'].source);
107+
});
108+
});
109+
110+
describe('a left-out file that a proof imports', () => {
111+
it("is found through the proofs' value imports", () => {
112+
// A spec that imports a `.test` helper beside it.
113+
const tree = fs.mkdtempSync(path.join(os.tmpdir(), 'proof-imports-'));
114+
const files = {
115+
'packages/core/src/Sheet/__tests__/Sheet.interaction.browser.spec.ts':
116+
"import {drag} from './drag.test';\nimport type {Kind} from './kinds.test';\n",
117+
'packages/core/src/Sheet/__tests__/drag.test.ts':
118+
"export {ease} from '../Sheet.doc.mjs';\n",
119+
'packages/core/src/Sheet/__tests__/kinds.test.ts': '',
120+
'packages/core/src/Sheet/Sheet.doc.mjs': '',
121+
};
122+
for (const [file, text] of Object.entries(files)) {
123+
fs.mkdirSync(path.dirname(path.join(tree, file)), {recursive: true});
124+
fs.writeFileSync(path.join(tree, file), text);
125+
}
126+
const importers = proofImporters({
127+
root: tree,
128+
listed: Object.keys(files),
129+
isProof: isInteractionSpec,
130+
});
131+
const spec =
132+
'packages/core/src/Sheet/__tests__/Sheet.interaction.browser.spec.ts';
133+
expect([...importers.keys()].sort()).toEqual([
134+
'packages/core/src/Sheet/Sheet.doc.mjs',
135+
'packages/core/src/Sheet/__tests__/drag.test.ts',
136+
]);
137+
expect([...importers.get('packages/core/src/Sheet/Sheet.doc.mjs')]).toEqual(
138+
[spec],
139+
);
140+
// So the scopes would leave out files this proof loads.
141+
for (const file of importers.keys())
142+
expect(SCOPES['Chromium proofs'].test(file), file).toBe(false);
143+
});
144+
145+
it('is found through a fixture a proof bundles by path', () => {
146+
const tree = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-imports-'));
147+
const files = {
148+
'packages/core/src/Sheet/__tests__/Sheet.interaction.browser.spec.ts':
149+
"const entry = new URL('./sheetBundle.ts', import.meta.url);\nconst other = bundleOf('bare.ts');\n",
150+
'packages/core/src/Sheet/__tests__/sheetBundle.ts':
151+
"import {ease} from '../ease.test';\n",
152+
'packages/core/src/Sheet/__tests__/bare.ts': '',
153+
'packages/core/src/Sheet/ease.test.ts': '',
154+
};
155+
for (const [file, text] of Object.entries(files)) {
156+
fs.mkdirSync(path.dirname(path.join(tree, file)), {recursive: true});
157+
fs.writeFileSync(path.join(tree, file), text);
158+
}
159+
const importers = proofImporters({
160+
root: tree,
161+
listed: Object.keys(files),
162+
isProof: isProofPage,
163+
});
164+
expect([...importers.keys()].sort()).toEqual([
165+
'packages/core/src/Sheet/__tests__/bare.ts',
166+
'packages/core/src/Sheet/__tests__/sheetBundle.ts',
167+
'packages/core/src/Sheet/ease.test.ts',
168+
]);
169+
});
170+
171+
it('is found through a story a proof opens', () => {
172+
const tree = fs.mkdtempSync(path.join(os.tmpdir(), 'story-imports-'));
173+
const files = {
174+
'apps/storybook/stories/Sheet.stories.tsx':
175+
"import {Sheet} from '@astryxdesign/core/Sheet/Sheet';\n",
176+
'packages/core/src/Sheet/Sheet.tsx':
177+
"import {notes} from './Sheet.doc.mjs';\n",
178+
'packages/core/src/Sheet/Sheet.doc.mjs': '',
179+
};
180+
for (const [file, text] of Object.entries(files)) {
181+
fs.mkdirSync(path.dirname(path.join(tree, file)), {recursive: true});
182+
fs.writeFileSync(path.join(tree, file), text);
183+
}
184+
const importers = proofImporters({
185+
root: tree,
186+
listed: Object.keys(files),
187+
isProof: isProofPage,
188+
});
189+
expect([...importers.get('packages/core/src/Sheet/Sheet.doc.mjs')]).toEqual(
190+
['apps/storybook/stories/Sheet.stories.tsx'],
191+
);
192+
});
193+
194+
it('does not exist: no proof, story or Storybook preview in the repository loads a file the scopes leave out', () => {
195+
const listed = execFileSync('git', ['ls-files'], {
196+
cwd: root,
197+
encoding: 'utf8',
198+
})
199+
.split('\n')
200+
.filter(Boolean);
201+
const importers = proofImporters({
202+
root,
203+
listed,
204+
isProof: isProofPage,
205+
});
206+
expect(importers.size).toBeGreaterThan(0);
207+
const leftOut = [...importers.keys()].filter(
208+
file =>
209+
/^packages\/[^/]+\/src\//.test(file) &&
210+
(!SCOPES['Chromium proofs'].test(file) ||
211+
(/^packages\/[^/]+\/src\/[^/]+\//.test(file) &&
212+
!SCOPES['WebKit proofs'].test(file))),
213+
);
214+
// A proof that needs such a file: rename the file so its name says it is
215+
// runtime code, or narrow the scopes' lookahead.
216+
expect(leftOut).toEqual([]);
217+
});
218+
});

‎.github/workflows/ci.yml‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,7 +2608,12 @@ jobs:
26082608
id: scope-chromium
26092609
uses: ./.test-scope/.github/actions/test-scope
26102610
with:
2611-
paths: '^(internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/[^/]+/src/|packages/themes/[^/]+/src/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|\.github/scripts/(stale-head-hint|merge-base)\.sh$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$)'
2611+
# Unit tests, docs, records and accessibility specs in a component
2612+
# folder (packages/<component package>/src/<Component>/) put nothing
2613+
# on a page these proofs load, so they stay out of scope. Shared
2614+
# folders, src roots, theme sources and every other package are
2615+
# shared code and stay in.
2616+
paths: '^(internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/(?!(?:core|lab|charts|richtext|vega)/)[^/]+/src/|packages/(?:core|lab|charts|richtext|vega)/src/(?![A-Z][^/]*/)|packages/(?:core|lab|charts|richtext|vega)/src/[A-Z][^/]*/(?!.*(?:\.test\.[^/]+|\.doc\.mjs|\.md|\.a11y\.[^/]+\.spec\.ts)$)|packages/themes/[^/]+/src/|packages/[^/]+/src/theme/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|\.github/scripts/(stale-head-hint|merge-base)\.sh$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$)'
26122617
# Main's nightly full run plans every spec; a merge-group entry plans
26132618
# none.
26142619
outside-pull-request: ${{ inputs.operation == 'interaction-full' && 'run' || 'skip' }}
@@ -2620,7 +2625,12 @@ jobs:
26202625
id: scope-webkit
26212626
uses: ./.test-scope/.github/actions/test-scope
26222627
with:
2623-
paths: '^(packages/[^/]+/src/[^/]+/|internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/themes/[^/]+/src/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$|\.github/scripts/(stale-head-hint|merge-base)\.sh$)'
2628+
# Unit tests, docs, records and accessibility specs in a component
2629+
# folder (packages/<component package>/src/<Component>/) put nothing
2630+
# on a page these proofs load, so they stay out of scope. Shared
2631+
# folders, src roots, theme sources and every other package are
2632+
# shared code and stay in.
2633+
paths: '^(packages/(?!(?:core|lab|charts|richtext|vega)/)[^/]+/src/[^/]+/|packages/(?:core|lab|charts|richtext|vega)/src/(?![A-Z])[^/]+/|packages/(?:core|lab|charts|richtext|vega)/src/[A-Z][^/]*/(?!.*(?:\.test\.[^/]+|\.doc\.mjs|\.md|\.a11y\.[^/]+\.spec\.ts)$)|internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/themes/[^/]+/src/|packages/[^/]+/src/theme/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$|\.github/scripts/(stale-head-hint|merge-base)\.sh$)'
26242634
outside-pull-request: skip
26252635
requires-head-file: playwright.interaction.config.ts
26262636
# A Lab-only change still runs each interaction spec it edits.
@@ -2779,7 +2789,12 @@ jobs:
27792789
id: scope
27802790
uses: ./.test-scope/.github/actions/test-scope
27812791
with:
2782-
paths: '^(internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/[^/]+/src/|packages/themes/[^/]+/src/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|\.github/scripts/(stale-head-hint|merge-base)\.sh$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$)'
2792+
# Unit tests, docs, records and accessibility specs in a component
2793+
# folder (packages/<component package>/src/<Component>/) put nothing
2794+
# on a page these proofs load, so they stay out of scope. Shared
2795+
# folders, src roots, theme sources and every other package are
2796+
# shared code and stay in.
2797+
paths: '^(internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/(?!(?:core|lab|charts|richtext|vega)/)[^/]+/src/|packages/(?:core|lab|charts|richtext|vega)/src/(?![A-Z][^/]*/)|packages/(?:core|lab|charts|richtext|vega)/src/[A-Z][^/]*/(?!.*(?:\.test\.[^/]+|\.doc\.mjs|\.md|\.a11y\.[^/]+\.spec\.ts)$)|packages/themes/[^/]+/src/|packages/[^/]+/src/theme/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|\.github/scripts/(stale-head-hint|merge-base)\.sh$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$)'
27832798
outside-pull-request: ${{ inputs.operation == 'interaction-full' && 'run' || 'skip' }}
27842799
requires-head-file: playwright.interaction.config.ts
27852800
# A Lab-only change still runs each interaction spec it edits.
@@ -3044,7 +3059,12 @@ jobs:
30443059
id: scope
30453060
uses: ./.test-scope/.github/actions/test-scope
30463061
with:
3047-
paths: '^(packages/[^/]+/src/[^/]+/|internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/themes/[^/]+/src/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$|\.github/scripts/(stale-head-hint|merge-base)\.sh$)'
3062+
# Unit tests, docs, records and accessibility specs in a component
3063+
# folder (packages/<component package>/src/<Component>/) put nothing
3064+
# on a page these proofs load, so they stay out of scope. Shared
3065+
# folders, src roots, theme sources and every other package are
3066+
# shared code and stay in.
3067+
paths: '^(packages/(?!(?:core|lab|charts|richtext|vega)/)[^/]+/src/[^/]+/|packages/(?:core|lab|charts|richtext|vega)/src/(?![A-Z])[^/]+/|packages/(?:core|lab|charts|richtext|vega)/src/[A-Z][^/]*/(?!.*(?:\.test\.[^/]+|\.doc\.mjs|\.md|\.a11y\.[^/]+\.spec\.ts)$)|internal/interaction-harness/|internal/a11y-spec/|playwright\.interaction\.config\.ts$|packages/themes/[^/]+/src/|packages/[^/]+/src/theme/|apps/storybook/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$|\.github/scripts/(playwright-legs|a11y-pr-contracts|lane-environment)\.mjs$|scripts/lib/duration-split\.mjs$|\.github/(playwright-leg-durations|lane-environments)\.json$|\.github/actions/test-scope/|.*\.interaction\.browser\.spec\.ts$|\.github/scripts/(stale-head-hint|merge-base)\.sh$)'
30483068
outside-pull-request: skip
30493069
requires-head-file: playwright.interaction.config.ts
30503070
# A Lab-only change still runs each interaction spec it edits.

0 commit comments

Comments
 (0)