Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(ci): three CI-script tests write their fixture repositories with one import (#9958)

Cindy Zhangc1a2cae6fbb2
diff --git a/.github/scripts/a11y-pr-contracts.test.mjs b/.github/scripts/a11y-pr-contracts.test.mjs
index 22ffa5e0ee..d77a0aa108 100644
--- a/.github/scripts/a11y-pr-contracts.test.mjs
+++ b/.github/scripts/a11y-pr-contracts.test.mjs
@@ -43,6 +43,10 @@ import {
specOwners,
withinBudget,
} from './a11y-pr-contracts.mjs';
+import {
+ createRepository,
+ importCommits,
+} from '../../scripts/lib/git-fixture.mjs';
import {
A11Y_SPEC_GLOBS,
WEBKIT_A11Y_SPEC_GLOBS,
@@ -890,39 +894,42 @@ describe('component audit axe pass', () => {
describe('changedSinceMergeBase', () => {
const git = (cwd, ...args) =>
execFileSync('git', args, {cwd, encoding: 'utf8', stdio: 'pipe'}).trim();
- const commit = (cwd, file, text) => {
- fs.writeFileSync(path.join(cwd, file), text);
- git(cwd, 'add', file);
- git(
- cwd,
- '-c',
- 'user.email=a@b',
- '-c',
- 'user.name=a',
- 'commit',
- '-qm',
- text,
- );
- };
-
- // An origin whose main has three commits, and a full clone whose feature
- // branch adds one file; main then moves on. Built once: each lookup below
- // gets its own test budget, since every git call is a process of its own.
+ // The origin's main has three commits, and its feature branch adds one
+ // file. A full clone sits on that branch, and main moves on after the clone.
+ // Built once, and fast-import writes the commits: five git launches in all.
const cwd = process.cwd();
let dir = '';
let clone = '';
beforeAll(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-pr-a11y-'));
const origin = path.join(dir, 'origin');
- fs.mkdirSync(origin);
- git(origin, 'init', '-q', '-b', 'main');
- for (const n of ['1', '2', '3']) commit(origin, 'base.txt', n);
+ createRepository(origin, [
+ {ref: 'refs/heads/main', message: '1', files: {'base.txt': '1'}},
+ {ref: 'refs/heads/main', message: '2', files: {'base.txt': '2'}},
+ {
+ ref: 'refs/heads/main',
+ name: '3',
+ message: '3',
+ files: {'base.txt': '3'},
+ },
+ {
+ ref: 'refs/heads/feature',
+ parent: '3',
+ message: 'f',
+ files: {'feature.txt': 'f'},
+ },
+ ]);
clone = path.join(dir, 'clone');
git(dir, 'clone', '-q', origin, clone);
- git(clone, 'checkout', '-qb', 'feature');
- commit(clone, 'feature.txt', 'f');
- git(clone, 'push', '-q', 'origin', 'feature');
- commit(origin, 'later.txt', 'l');
+ git(clone, 'checkout', '-q', 'feature');
+ importCommits(origin, [
+ {
+ ref: 'refs/heads/main',
+ parent: 'main',
+ message: 'l',
+ files: {'later.txt': 'l'},
+ },
+ ]);
});
afterAll(() => {
fs.rmSync(dir, {recursive: true, force: true});
diff --git a/.github/scripts/analyze-pr.test.mjs b/.github/scripts/analyze-pr.test.mjs
index ed09acbfe1..1f5222528f 100644
--- a/.github/scripts/analyze-pr.test.mjs
+++ b/.github/scripts/analyze-pr.test.mjs
@@ -30,6 +30,7 @@ import path from 'node:path';
import {createRequire} from 'node:module';
import {fileURLToPath} from 'node:url';
import {describe, it, expect} from 'vitest';
+import {createRepository} from '../../scripts/lib/git-fixture.mjs';
const SCRIPTS_DIR = path.dirname(fileURLToPath(import.meta.url));
const SCRIPT = path.join(SCRIPTS_DIR, 'analyze-pr.js');
@@ -70,115 +71,135 @@ function buildFixture() {
const upstream = path.join(base, 'upstream');
const clone = path.join(base, 'clone');
- fs.mkdirSync(path.join(upstream, 'packages/core/src/Card'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/core/src/Button'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/core/src/Line'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/core/src/InteractiveRoleContext'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/lab/src/Sankey'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/richtext/src'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/vega/src'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/themes/neutral/src'), {recursive: true});
- fs.mkdirSync(path.join(upstream, 'packages/themes/probe/src'), {recursive: true});
-
- git(upstream, ['init', '-q', '-b', 'main']);
- git(upstream, ['config', 'user.email', '[email protected]']);
- git(upstream, ['config', 'user.name', 'Test']);
- fs.writeFileSync(path.join(upstream, 'package.json'), '{}');
- fs.mkdirSync(path.join(upstream, 'packages/core/src/Badge'), {recursive: true});
- fs.writeFileSync(path.join(upstream, 'packages/core/src/index.ts'), "export {Card} from './Card';\nexport {Badge} from './Badge';\n");
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Badge/Badge.tsx'), 'export function Badge() {}\n');
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Badge/Badge.doc.mjs'), "export default {name: 'Badge'};\n");
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Badge/index.ts'), "export {Badge} from './Badge';\n");
- fs.mkdirSync(path.join(upstream, 'docs/specs/components/Badge'), {recursive: true});
- fs.writeFileSync(path.join(upstream, 'docs/specs/components/Badge/Badge.spec.md'), '---\nschema_version: 3\n---\n');
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Card/Card.tsx'), 'export function Card() {}\n');
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Card/Card.doc.mjs'), "export default {name: 'Card'};\n");
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Card/index.ts'), "export {Card} from './Card';\n");
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Button/index.ts'), 'export {}\n');
- fs.writeFileSync(path.join(upstream, 'packages/core/src/Line/index.ts'), 'export {}\n');
- fs.writeFileSync(
- path.join(upstream, 'packages/richtext/src/RichTextEditor.tsx'),
- 'export function RichTextEditor() {}\n',
- );
- fs.writeFileSync(
- path.join(upstream, 'packages/richtext/src/RichTextEditorAutoLinkPlugin.tsx'),
- 'export function RichTextEditorAutoLinkPlugin() {}\n',
- );
- fs.writeFileSync(
- path.join(upstream, 'packages/richtext/src/RichTextView.tsx'),
- 'export function RichTextView() {}\n',
- );
- fs.writeFileSync(
- path.join(upstream, 'packages/richtext/src/index.ts'),
- [
+ const atBranchPoint = {
+ 'package.json': '{}',
+ 'packages/core/src/index.ts':
+ "export {Card} from './Card';\nexport {Badge} from './Badge';\n",
+ 'packages/core/src/Badge/Badge.tsx': 'export function Badge() {}\n',
+ 'packages/core/src/Badge/Badge.doc.mjs':
+ "export default {name: 'Badge'};\n",
+ 'packages/core/src/Badge/index.ts': "export {Badge} from './Badge';\n",
+ 'docs/specs/components/Badge/Badge.spec.md':
+ '---\nschema_version: 3\n---\n',
+ 'packages/core/src/Card/Card.tsx': 'export function Card() {}\n',
+ 'packages/core/src/Card/Card.doc.mjs': "export default {name: 'Card'};\n",
+ 'packages/core/src/Card/index.ts': "export {Card} from './Card';\n",
+ 'packages/core/src/Button/index.ts': 'export {}\n',
+ 'packages/core/src/Line/index.ts': 'export {}\n',
+ 'packages/richtext/src/RichTextEditor.tsx':
+ 'export function RichTextEditor() {}\n',
+ 'packages/richtext/src/RichTextEditorAutoLinkPlugin.tsx':
+ 'export function RichTextEditorAutoLinkPlugin() {}\n',
+ 'packages/richtext/src/RichTextView.tsx':
+ 'export function RichTextView() {}\n',
+ 'packages/richtext/src/index.ts': [
"export {RichTextEditor} from './RichTextEditor';",
"export {RichTextEditorAutoLinkPlugin} from './RichTextEditorAutoLinkPlugin';",
"export {RichTextView} from './RichTextView';",
'',
].join('\n'),
- );
- fs.writeFileSync(
- path.join(upstream, 'packages/vega/src/VegaChart.tsx'),
- 'export function VegaChart() {}\n',
- );
- fs.writeFileSync(path.join(upstream, 'packages/vega/src/index.ts'), "export {VegaChart} from './VegaChart';\n");
- fs.writeFileSync(path.join(upstream, 'packages/core/src/InteractiveRoleContext/internal.ts'), 'export const shared = true;\n');
- fs.writeFileSync(path.join(upstream, 'packages/lab/src/Sankey/internal.ts'), 'export const family = true;\n');
- fs.writeFileSync(
- path.join(upstream, 'packages/themes/neutral/package.json'),
- JSON.stringify({name: '@astryxdesign/theme-neutral', private: false}),
- );
- fs.writeFileSync(path.join(upstream, 'packages/themes/neutral/src/theme.ts'), 'export {}\n');
- fs.writeFileSync(
- path.join(upstream, 'packages/themes/probe/package.json'),
- JSON.stringify({name: '@astryxdesign/theme-probe', private: true}),
- );
- fs.writeFileSync(path.join(upstream, 'packages/themes/probe/src/theme.ts'), 'export {}\n');
- git(upstream, ['add', '-A']);
- git(upstream, ['commit', '-qm', 'branch point']);
- const branchPoint = git(upstream, ['rev-parse', 'HEAD']);
+ 'packages/vega/src/VegaChart.tsx': 'export function VegaChart() {}\n',
+ 'packages/vega/src/index.ts': "export {VegaChart} from './VegaChart';\n",
+ 'packages/core/src/InteractiveRoleContext/internal.ts':
+ 'export const shared = true;\n',
+ 'packages/lab/src/Sankey/internal.ts': 'export const family = true;\n',
+ 'packages/themes/neutral/package.json': JSON.stringify({
+ name: '@astryxdesign/theme-neutral',
+ private: false,
+ }),
+ 'packages/themes/neutral/src/theme.ts': 'export {}\n',
+ 'packages/themes/probe/package.json': JSON.stringify({
+ name: '@astryxdesign/theme-probe',
+ private: true,
+ }),
+ 'packages/themes/probe/src/theme.ts': 'export {}\n',
+ };
+ const appended = (file, text) => ({[file]: atBranchPoint[file] + text});
// 60 churn commits on main so the branch point is beyond any small depth.
- for (let i = 1; i <= 60; i++) {
- fs.appendFileSync(path.join(upstream, 'packages/core/src/Button/index.ts'), `b${i}\n`);
- fs.appendFileSync(path.join(upstream, 'packages/core/src/Line/index.ts'), `l${i}\n`);
- git(upstream, ['add', '-A']);
- git(upstream, ['commit', '-qm', `churn ${i}`]);
- }
+ const churn = Array.from({length: 60}, (_, index) => {
+ const lines = prefix =>
+ Array.from({length: index + 1}, (__, n) => `${prefix}${n + 1}\n`).join(
+ '',
+ );
+ return {
+ ref: 'refs/heads/main',
+ message: `churn ${index + 1}`,
+ files: {
+ 'packages/core/src/Button/index.ts': `export {}\n${lines('b')}`,
+ 'packages/core/src/Line/index.ts': `export {}\n${lines('l')}`,
+ },
+ };
+ });
// Feature branch off the branch point, touching Core, Rich Text, and Vega.
- git(upstream, ['branch', 'feature', branchPoint]);
- git(upstream, ['checkout', '-q', 'feature']);
- fs.appendFileSync(path.join(upstream, 'packages/core/src/Card/index.ts'), 'export const Card = {}\n');
// Only Badge's knowledge record changes: no browser audit owns Badge.
- fs.appendFileSync(path.join(upstream, 'docs/specs/components/Badge/Badge.spec.md'), 'record text\n');
- fs.appendFileSync(
- path.join(upstream, 'packages/richtext/src/RichTextEditor.tsx'),
- 'export const richTextChanged = true\n',
- );
- fs.appendFileSync(
- path.join(upstream, 'packages/richtext/src/RichTextEditorAutoLinkPlugin.tsx'),
- 'export const autoLinkChanged = true\n',
- );
- fs.appendFileSync(
- path.join(upstream, 'packages/richtext/src/RichTextView.tsx'),
- 'export const viewChanged = true\n',
- );
- fs.appendFileSync(
- path.join(upstream, 'packages/vega/src/VegaChart.tsx'),
- 'export const vegaChanged = true\n',
- );
- fs.appendFileSync(path.join(upstream, 'packages/core/src/InteractiveRoleContext/internal.ts'), 'export const sharedChanged = true\n');
- fs.appendFileSync(path.join(upstream, 'packages/lab/src/Sankey/internal.ts'), 'export const familyChanged = true\n');
- fs.appendFileSync(path.join(upstream, 'packages/themes/neutral/src/theme.ts'), 'export const changed = true\n');
- fs.appendFileSync(path.join(upstream, 'packages/themes/probe/src/theme.ts'), 'export const changed = true\n');
- git(upstream, ['add', '-A']);
- git(upstream, ['commit', '-qm', 'touch three component packages']);
+ const feature = {
+ ref: 'refs/heads/feature',
+ parent: 'point',
+ message: 'touch three component packages',
+ files: {
+ ...appended(
+ 'packages/core/src/Card/index.ts',
+ 'export const Card = {}\n',
+ ),
+ ...appended('docs/specs/components/Badge/Badge.spec.md', 'record text\n'),
+ ...appended(
+ 'packages/richtext/src/RichTextEditor.tsx',
+ 'export const richTextChanged = true\n',
+ ),
+ ...appended(
+ 'packages/richtext/src/RichTextEditorAutoLinkPlugin.tsx',
+ 'export const autoLinkChanged = true\n',
+ ),
+ ...appended(
+ 'packages/richtext/src/RichTextView.tsx',
+ 'export const viewChanged = true\n',
+ ),
+ ...appended(
+ 'packages/vega/src/VegaChart.tsx',
+ 'export const vegaChanged = true\n',
+ ),
+ ...appended(
+ 'packages/core/src/InteractiveRoleContext/internal.ts',
+ 'export const sharedChanged = true\n',
+ ),
+ ...appended(
+ 'packages/lab/src/Sankey/internal.ts',
+ 'export const familyChanged = true\n',
+ ),
+ ...appended(
+ 'packages/themes/neutral/src/theme.ts',
+ 'export const changed = true\n',
+ ),
+ ...appended(
+ 'packages/themes/probe/src/theme.ts',
+ 'export const changed = true\n',
+ ),
+ },
+ };
+
+ // One import writes the whole history: three git launches with the clone.
+ const {point: branchPoint} = createRepository(upstream, [
+ {
+ ref: 'refs/heads/main',
+ name: 'point',
+ message: 'branch point',
+ files: atBranchPoint,
+ },
+ ...churn,
+ feature,
+ ]);
// Shallow single-branch clone of the feature branch.
git(null, [
- 'clone', '-q', '--depth=5', '--single-branch', '--branch=feature',
- `file://${upstream}`, clone,
+ 'clone',
+ '-q',
+ '--depth=5',
+ '--single-branch',
+ '--branch=feature',
+ `file://${upstream}`,
+ clone,
]);
return {base, clone, branchPoint};
diff --git a/.github/scripts/ci-lint-scope.test.mjs b/.github/scripts/ci-lint-scope.test.mjs
index df3fa565b9..56dc6b2daa 100644
--- a/.github/scripts/ci-lint-scope.test.mjs
+++ b/.github/scripts/ci-lint-scope.test.mjs
@@ -18,9 +18,14 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
-import {afterAll, describe, expect, it} from 'vitest';
+import {afterAll, beforeAll, describe, expect, it} from 'vitest';
import yaml from 'yaml';
+import {
+ createRepository,
+ importCommits,
+} from '../../scripts/lib/git-fixture.mjs';
+
const root = path.resolve(import.meta.dirname, '../..');
const lint = yaml.parse(
fs.readFileSync(path.join(root, '.github/workflows/lint.yml'), 'utf8'),
@@ -58,54 +63,83 @@ function write(dir, file, text) {
fs.writeFileSync(path.join(dir, file), text);
}
+// The base repository: the trusted classifier on main, and main moving on
+// after the pull requests below branch. Each pull request adds its own commit
+// and the merge commit GitHub builds for it. fast-import writes them, so a
+// pull request costs one git launch, not one per step.
+let origin = '';
+let base = '';
+let mainTip = '';
+let pullRequests = 0;
+beforeAll(() => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'lint-scope-'));
+ scratch.push(dir);
+ origin = path.join(dir, 'origin.git');
+ const classifier = Object.fromEntries(
+ CLASSIFIER_FILES.map(file => [
+ file,
+ fs.readFileSync(path.join(root, file), 'utf8'),
+ ]),
+ );
+ const hashes = createRepository(
+ origin,
+ [
+ {
+ ref: 'refs/heads/main',
+ name: 'base',
+ message: 'base',
+ files: {
+ ...classifier,
+ 'docs/specs/AST-999-example/spec.md': '# Example\n',
+ 'packages/core/src/Example/Example.tsx': 'export {};\n',
+ },
+ },
+ ...[0, 1, 2].map(i => ({
+ ref: 'refs/heads/main',
+ name: `main ${i}`,
+ message: `main ${i}`,
+ files: {'README.md': `${i}\n`},
+ })),
+ ],
+ {bare: true},
+ );
+ base = hashes.base;
+ mainTip = hashes['main 2'];
+});
+
/**
- * A repository with the trusted classifier on main, a pull request that
- * changes `prFiles`, and main moving on after it branched. Returns the scope
- * outputs the lint step writes when checked out the way lint.yml checks out.
+ * A pull request that changes `prFiles`, branched from main's base before main
+ * moved on. Returns the scope outputs the lint step writes when checked out
+ * the way lint.yml checks out.
*/
function classifyPullRequest(prFiles) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'lint-scope-'));
scratch.push(dir);
- const work = path.join(dir, 'work');
- fs.mkdirSync(work);
- git(work, 'init', '-q', '-b', 'main');
- for (const file of CLASSIFIER_FILES) {
- write(work, file, fs.readFileSync(path.join(root, file), 'utf8'));
- }
- write(work, 'docs/specs/AST-999-example/spec.md', '# Example\n');
- write(work, 'packages/core/src/Example/Example.tsx', 'export {};\n');
- git(work, 'add', '-A');
- git(work, 'commit', '-q', '-m', 'base');
- git(work, 'checkout', '-q', '-b', 'pr');
- for (const [file, text] of Object.entries(prFiles)) write(work, file, text);
- git(work, 'add', '-A');
- git(work, 'commit', '-q', '-m', 'the pull request');
- git(work, 'checkout', '-q', 'main');
- for (let i = 0; i < 3; i++) {
- write(work, 'README.md', `${i}\n`);
- git(work, 'add', '-A');
- git(work, 'commit', '-q', '-m', `main ${i}`);
- }
- git(work, 'merge', '-q', '--no-ff', '--no-edit', 'pr');
- const merge = git(work, 'rev-parse', 'HEAD');
- git(work, 'reset', '-q', '--hard', 'HEAD^1');
- const origin = path.join(dir, 'origin.git');
- git(dir, 'clone', '-q', '--bare', work, origin);
- git(origin, 'update-ref', 'refs/pull/1/merge', merge);
+ pullRequests += 1;
+ const merge = `refs/pull/${pullRequests}/merge`;
+ importCommits(origin, [
+ {
+ ref: `refs/heads/pr-${pullRequests}`,
+ name: 'pr',
+ parent: base,
+ message: 'the pull request',
+ files: prFiles,
+ },
+ {
+ ref: merge,
+ parent: mainTip,
+ merges: ['pr'],
+ message: 'Merge the pull request',
+ files: prFiles,
+ },
+ ]);
const workspace = path.join(dir, 'ws');
fs.mkdirSync(workspace);
git(workspace, 'init', '-q');
git(workspace, 'remote', 'add', 'origin', `file://${origin}`);
const depth = checkout.with?.['fetch-depth'] ?? 1;
- git(
- workspace,
- 'fetch',
- '-q',
- `--depth=${depth}`,
- 'origin',
- 'refs/pull/1/merge',
- );
+ git(workspace, 'fetch', '-q', `--depth=${depth}`, 'origin', merge);
git(workspace, 'checkout', '-q', '--detach', 'FETCH_HEAD');
const output = path.join(dir, 'output');
fs.writeFileSync(output, '');