Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 82 additions & 22 deletions .github/scripts/a11y-pr-contracts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@
*
* `--changed-only <specs> --changed-files <files> --owned-by <components>
* [--dependents-of <folders>]` (the default check of the components a pull
* request touches, a wide change included): run the browser specs the pull
* request touches, a wide change included; `--shared-modules <files>` adds the
* touched shared hooks and utils, `SHARED_MODULE`): run the browser specs the pull
* request adds or modifies, listed one per line in <specs>, then the `scoped`
* and `release-only` specs that the comma-separated touched components own
* (`ownedSpecs`; <files> lists every changed path, one per line, for the
* folder-change rule), then the `scoped` and `release-only` specs of the
* component folders that render a touched folder (`dependentSpecs`, nearest
* component folders that render a touched folder or import a touched shared
* module (`dependentSpecs`, nearest
* first), in the time left. It reads no title and runs no component audit; a
* changed `audit` spec still waits for its audit pull request. To stay inside the job's 15-minute budget
* it takes at most CHANGED_SPEC_LIMIT changed files, starts each file only
Expand Down Expand Up @@ -406,7 +408,7 @@ export function ownedSpecs({specs, components, changedPaths}) {

const SOURCE = /\.(?:[cm]?[jt]sx?)$/;
const NOT_RENDERED =
/(?:^|\/)__tests__\/|\.(?:test|stories|doc)\.[^/]+$|\.spec\.[^/]+$|\.d\.ts$/;
/(?:^|\/)__tests__\/|\.(?:test|stories|doc)\.[^/]+$|\.spec\.[^/]+$|\.d\.[cm]?ts$/;
const STATEMENT =
/(?:^|[\n;])\s*(import|export)\s+(type\s+)?([^'";]*?)\s*from\s*['"]([^'"]+)['"]|(?:^|[\n;])\s*import\s*['"]([^'"]+)['"]|\bimport\(\s*['"]([^'"]+)['"]\s*\)/g;
/** The suffixes an import specifier resolves through, in order. */
Expand Down Expand Up @@ -604,26 +606,74 @@ export function importerGraph(root) {
}

/**
* The component folders that render a touched folder, nearest first, then in
* path order. A step from one component folder into another counts one;
* steps inside a folder or through shared files count none.
* The component folders that render a touched folder, or that import a
* touched shared module (a hook or util, `SHARED_MODULE`), nearest first, then
* in path order. A step from one component folder into another, or from a
* shared module into a component folder, counts one; steps inside a folder or
* through shared files count none. `via` says which seed reached the folder at
* its depth: `component` before `shared` when both do.
* @param {Map<string, Set<string>>} importers
* @param {string[]} touched `<pkg>/<Folder>`
* @returns {{folder: string, depth: number}[]}
* @param {string[]} [shared] touched shared module files
* @returns {{folder: string, depth: number, via: 'component' | 'shared'}[]}
*/
export function renderingFolders(importers, touched) {
export function renderingFolders(importers, touched, shared = []) {
const byComponent = seededFolders(importers, touched, null);
const byShared =
shared.length > 0
? seededFolders(importers, touched, new Set(shared))
: new Map();
const folders = new Map();
for (const [folder, d] of byComponent)
folders.set(folder, {folder, depth: d, via: 'component'});
for (const [folder, d] of byShared)
if (!folders.has(folder) || folders.get(folder).depth > d)
folders.set(folder, {folder, depth: d, via: 'shared'});
return [...folders.values()].sort(
(a, b) => a.depth - b.depth || a.folder.localeCompare(b.folder),
);
}

/** A changed file that is a shared hook or util Core's components import. */
export const SHARED_MODULE = /^packages\/core\/src\/(?:hooks|utils)\//;

/**
* The changed shared hooks and utils (`SHARED_MODULE`) that can put code on a
* page: never a test, story, doc, spec or type declaration. The touched step
* reads it for its `contract_shared` output, and `--shared-modules` keeps only
* these.
* @param {string[]} files
*/
export function sharedModules(files) {
return files.filter(
file => SHARED_MODULE.test(file) && isRenderedSource(file),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes through isRenderedSource, whose NOT_RENDERED (lines 410-411) excludes only .d.ts. A .d.mts or .d.cts declaration under hooks/ or utils/ is therefore listed and starts the contract job (reproduced locally), although the comment above and the description say declarations are never listed. Exclude \.d\.[cm]?ts$ and add a .d.mts path to the sharedModules test.

);
}

/**
* Each component folder outside `touched` that the seeds reach, at its least
* depth. The seeds are the files of the touched folders, or, with `seeds`,
* those files only.
* @param {Map<string, Set<string>>} importers
* @param {string[]} touched
* @param {Set<string> | null} seeds
* @returns {Map<string, number>}
*/
function seededFolders(importers, touched, seeds) {
const touchedSet = new Set(touched);
const isSeed = file =>
seeds ? seeds.has(file) : touchedSet.has(componentFolderOf(file));
const depth = new Map();
const deque = [];
for (const file of importers.keys()) {
if (touchedSet.has(componentFolderOf(file))) {
if (isSeed(file)) {
depth.set(file, 0);
deque.push(file);
}
}
for (const [, set] of importers) {
for (const file of set) {
if (touchedSet.has(componentFolderOf(file)) && !depth.has(file)) {
if (isSeed(file) && !depth.has(file)) {
depth.set(file, 0);
deque.push(file);
}
Expand All @@ -650,9 +700,7 @@ export function renderingFolders(importers, touched) {
if (!folder || touchedSet.has(folder)) continue;
if (!folders.has(folder) || folders.get(folder) > d) folders.set(folder, d);
}
return [...folders]
.map(([folder, d]) => ({folder, depth: d}))
.sort((a, b) => a.depth - b.depth || a.folder.localeCompare(b.folder));
return folders;
}

/**
Expand Down Expand Up @@ -754,6 +802,7 @@ export function proofImporters({
*/
export function dependentSpecs(specs, rendering, planned) {
const depthOf = new Map(rendering.map(({folder, depth}) => [folder, depth]));
const viaOf = new Map(rendering.map(({folder, via}) => [folder, via]));
const folderOf = file => {
const match = /^packages\/([^/]+)\/src\/([^/]+)\//.exec(file);
return match ? `${match[1]}/${match[2]}` : null;
Expand All @@ -765,11 +814,15 @@ export function dependentSpecs(specs, rendering, planned) {
!planned.has(file) &&
depthOf.has(folderOf(file)),
)
.map(({file}) => ({file, depth: depthOf.get(folderOf(file))}))
.map(({file}) => ({
file,
depth: depthOf.get(folderOf(file)),
via: viaOf.get(folderOf(file)),
}))
.sort((a, b) => a.depth - b.depth || a.file.localeCompare(b.file))
.map(({file, depth}) => ({
.map(({file, depth, via}) => ({
file,
reason: `renders a touched component${depth > 1 ? ` (${depth} folders away)` : ''}`,
reason: `${via === 'shared' ? 'imports a touched shared hook or util' : 'renders a touched component'}${depth > 1 ? ` (${depth} folders away)` : ''}`,
}));
}

Expand Down Expand Up @@ -939,7 +992,7 @@ export function planDefaultCheck({
withFile =>
projectedMs(withFile, msOf, workers) - beforeDependents <=
dependentShareMs,
`${dependentShareMs / 60000}-minute share for folders that render a touched component`,
`${dependentShareMs / 60000}-minute share for folders that render a touched component or import a touched shared hook or util`,
);
}
return {run, notRun};
Expand All @@ -965,7 +1018,7 @@ export function summaryMarkdown({
if (rendering.length) {
lines.push(
'',
`Folders that render a touched component: ${rendering.length}; their specs run, nearest first, within ${DEPENDENT_SHARE_MS === 60 * 1000 ? 'one minute' : `${DEPENDENT_SHARE_MS / 60000} minutes`} of the time left.`,
`Folders that render a touched component or import a touched shared hook or util: ${rendering.length}; their specs run, nearest first, within ${DEPENDENT_SHARE_MS === 60 * 1000 ? 'one minute' : `${DEPENDENT_SHARE_MS / 60000} minutes`} of the time left.`,
);
}
lines.push('');
Expand Down Expand Up @@ -1220,6 +1273,10 @@ function main() {
const ownedBy = (ownedByValue ?? '').split(',').filter(Boolean);
const dependentsOfValue = flagValue(argv, '--dependents-of');
const dependentsOf = (dependentsOfValue ?? '').split(',').filter(Boolean);
const sharedModulesValue = flagValue(argv, '--shared-modules');
const sharedModuleFiles = sharedModules(
(sharedModulesValue ?? '').split(',').filter(Boolean),
);
const countLegsValue = flagValue(argv, '--count-legs');
const legValue = flagValue(argv, '--leg');
const ofValue = flagValue(argv, '--of');
Expand All @@ -1234,11 +1291,12 @@ function main() {
if (
(ownedByValue != null ||
changedFilesFile != null ||
dependentsOfValue != null) &&
dependentsOfValue != null ||
sharedModulesValue != null) &&
changedOnlyFile == null
) {
throw new Error(
'--owned-by, --changed-files and --dependents-of need --changed-only',
'--owned-by, --changed-files, --dependents-of and --shared-modules need --changed-only',
);
}
// Without every changed path the folder rule selects nothing, and the
Expand All @@ -1253,6 +1311,7 @@ function main() {
changedFilesFile,
ownedByValue,
dependentsOfValue,
sharedModulesValue,
countLegsValue,
legValue,
ofValue,
Expand Down Expand Up @@ -1290,8 +1349,9 @@ function main() {
title,
});
const rendering =
changedOnlyFile != null && dependentsOf.length > 0
? renderingFolders(importerGraph(root), dependentsOf)
changedOnlyFile != null &&
(dependentsOf.length > 0 || sharedModuleFiles.length > 0)
? renderingFolders(importerGraph(root), dependentsOf, sharedModuleFiles)
: [];
const budgeted = (() => {
if (changedOnlyFile == null) return {run: routed, notRun: []};
Expand Down
120 changes: 109 additions & 11 deletions .github/scripts/a11y-pr-contracts.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,15 @@ describe('the folders that render a touched component (spec:AST-030 FR12)', () =
"import {useAnnounce} from '../hooks';\n",
'packages/core/src/Meter/Meter.tsx':
"import {useSpin} from '../hooks';\n",
// Shared modules: a util one component imports, a hook nothing imports,
// and a test that imports both.
'packages/core/src/utils/clamp.ts': 'export function clamp() {}\n',
'packages/core/src/Slider/Slider.tsx':
"import {clamp} from '../utils/clamp';\n",
'packages/core/src/hooks/useLonely.ts':
'export function useLonely() {}\n',
'packages/core/src/Chip/__tests__/Chip.test.tsx':
"import {clamp} from '../../utils/clamp';\nimport {useAnnounce} from '../../hooks/useAnnounce';\n",
};
for (const [file, text] of Object.entries(files)) write(root, file, text);
return root;
Expand All @@ -609,12 +618,12 @@ describe('the folders that render a touched component (spec:AST-030 FR12)', () =
const root = repo();
try {
expect(renderingFolders(importerGraph(root), ['core/Spinner'])).toEqual([
{folder: 'core/Banner', depth: 1},
{folder: 'core/Meter', depth: 1},
{folder: 'core/TextInput', depth: 1},
{folder: 'core/Tooltip', depth: 1},
{folder: 'lab/Schedule', depth: 1},
{folder: 'core/CommandPalette', depth: 2},
{folder: 'core/Banner', depth: 1, via: 'component'},
{folder: 'core/Meter', depth: 1, via: 'component'},
{folder: 'core/TextInput', depth: 1, via: 'component'},
{folder: 'core/Tooltip', depth: 1, via: 'component'},
{folder: 'lab/Schedule', depth: 1, via: 'component'},
{folder: 'core/CommandPalette', depth: 2, via: 'component'},
]);
// Nothing renders an untouched leaf.
expect(
Expand All @@ -625,6 +634,93 @@ describe('the folders that render a touched component (spec:AST-030 FR12)', () =
}
});

it('lists the folders that import a touched shared hook, nearest first', () => {
const root = repo();
try {
// A hook-only change: Kbd imports useAnnounce through the hooks barrel.
expect(
renderingFolders(
importerGraph(root),
[],
['packages/core/src/hooks/useAnnounce.ts'],
),
).toEqual([{folder: 'core/Kbd', depth: 1, via: 'shared'}]);
} finally {
fs.rmSync(root, {recursive: true, force: true});
}
});

it('lists the folders that import a touched shared util, and a test that imports it is not a component', () => {
const root = repo();
try {
expect(
renderingFolders(
importerGraph(root),
[],
['packages/core/src/utils/clamp.ts'],
),
).toEqual([{folder: 'core/Slider', depth: 1, via: 'shared'}]);
} finally {
fs.rmSync(root, {recursive: true, force: true});
}
});

it('plans nothing for a shared hook no component imports', () => {
const root = repo();
try {
expect(
renderingFolders(
importerGraph(root),
[],
['packages/core/src/hooks/useLonely.ts'],
),
).toEqual([]);
} finally {
fs.rmSync(root, {recursive: true, force: true});
}
});

it('counts a folder reached both ways once, at its nearest depth, as rendering', () => {
const root = repo();
try {
const both = renderingFolders(
importerGraph(root),
['core/Spinner'],
['packages/core/src/hooks/useSpin.ts'],
);
expect(both.filter(({folder}) => folder === 'core/Meter')).toEqual([
{folder: 'core/Meter', depth: 1, via: 'component'},
]);
} finally {
fs.rmSync(root, {recursive: true, force: true});
}
});

it("names a shared importer's spec by why it runs, within the same one-minute share", () => {
const own =
'packages/core/src/Slider/__tests__/Slider.a11y.chromium.spec.ts';
const specs = [spec(own)];
const dependents = dependentSpecs(
specs,
[{folder: 'core/Slider', depth: 1, via: 'shared'}],
new Set(),
);
expect(dependents).toEqual([
{file: own, reason: 'imports a touched shared hook or util'},
]);
const {notRun} = planDefaultCheck({
changed: [],
owned: [],
dependents,
seconds: {[own]: 50},
workers: 1,
});
expect(notRun[0].reason).toContain(
'imports a touched shared hook or util; its expected 50 s does not fit the 1-minute share for folders that render a touched component or import a touched shared hook or util',
);
expect(DEPENDENT_SHARE_MS).toBe(60 * 1000);
});

it('reads value imports and re-exports, never type-only ones', () => {
expect(
valueImports(
Expand Down Expand Up @@ -652,8 +748,8 @@ describe('the folders that render a touched component (spec:AST-030 FR12)', () =
at('core/src/Spinner', 'SpinnerMotion'),
];
const rendering = [
{folder: 'core/TextInput', depth: 1},
{folder: 'core/CommandPalette', depth: 2},
{folder: 'core/TextInput', depth: 1, via: 'component'},
{folder: 'core/CommandPalette', depth: 2, via: 'component'},
];
expect(dependentSpecs(specs, rendering, new Set())).toEqual([
{
Expand Down Expand Up @@ -722,7 +818,7 @@ describe('the folders that render a touched component (spec:AST-030 FR12)', () =
expect(run.map(({file}) => file)).toEqual([own, deps[0].file]);
expect(notRun.map(({file}) => file)).toEqual([deps[1].file, deps[2].file]);
expect(notRun[0].reason).toContain(
'1-minute share for folders that render a touched component',
'1-minute share for folders that render a touched component or import a touched shared hook or util',
);
expect(DEPENDENT_SHARE_MS).toBe(60 * 1000);
});
Expand All @@ -733,9 +829,11 @@ describe('the folders that render a touched component (spec:AST-030 FR12)', () =
components: ['core/Spinner'],
selected: [],
notRun: [],
rendering: [{folder: 'core/TextInput', depth: 1}],
rendering: [{folder: 'core/TextInput', depth: 1, via: 'component'}],
}),
).toContain('Folders that render a touched component: 1');
).toContain(
'Folders that render a touched component or import a touched shared hook or util: 1',
);
});
});

Expand Down
2 changes: 2 additions & 0 deletions .github/scripts/touched-components.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import path from 'node:path';
import {pathToFileURL} from 'node:url';
import {createRequire} from 'node:module';
import {changedFolderMembers} from './a11y-pr-scope.mjs';
import {sharedModules} from './a11y-pr-contracts.mjs';

const require = createRequire(import.meta.url);
const {diffAgainstBaseline} = require('./lib/a11y-baseline.js');
Expand Down Expand Up @@ -624,6 +625,7 @@ export function main(argv = process.argv.slice(2)) {
// budget (spec:AST-030 FR12).
contract_owners: scope.touched.join(','),
contract_folders: scope.folders.join(','),
contract_shared: sharedModules(files).join(','),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When CI cannot decide the scope (GitHub cannot list the changed files, say), files here is every tracked file (ci.yml lines 7223-7229 and 7254), so contract_shared names every hook and util and the job runs their importers even for a pull request that changes none. That fails closed, which is fine, but the description's "A pull request that changes no Core hook or util runs exactly as before" needs that exception.

rtl_packages: Object.keys(scope.rtlOwnersByPackage).join(','),
rtl_owners: scope.touched.join(','),
});
Expand Down
Loading