An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): skip test and fixture files when loading integration codemods (#6230)
Joey1a9bb4a8eda5
diff --git a/.changeset/skip-codemod-test-files.md b/.changeset/skip-codemod-test-files.mdnew file mode 100644index 0000000000..46353eb21c--- /dev/null+++ b/.changeset/skip-codemod-test-files.md@@ -0,0 +1,6 @@+---+'@astryxdesign/cli': patch+---++[fix] Test and fixture files under a codemod version folder are no longer loaded as codemods. Every `.ts`/`.mjs`/`.js` file under a version folder was loaded and validated, so a test colocated with its transform failed validation and — a definition error being a hard error — took every codemod in that version with it, while `upgrade` applied nothing and reported success. Reserved names: `*.test.*`, `*.spec.*`, `*.fixture.*`, and anything under `__tests__/` or `__fixtures__/`.+@josephfarinadiff --git a/packages/cli/assets/codemods/integration-discovery.mjs b/packages/cli/assets/codemods/integration-discovery.mjsindex 4eb38f9143..df2c8fbb62 100644--- a/packages/cli/assets/codemods/integration-discovery.mjs+++ b/packages/cli/assets/codemods/integration-discovery.mjs@@ -32,6 +32,40 @@ import {semverCompare} from '../../foundation/env/semver.mjs';/** File extensions recognized as codemod modules. */const CODEMOD_EXTENSIONS = ['.ts', '.mjs', '.js'];+/**+ * Directories never walked for codemods: a test directory beside a transform is+ * the natural place to put its test, and every file found here is loaded and+ * validated as a codemod.+ */+const SKIP_DIRS = new Set([+ 'node_modules',+ '.git',+ '__tests__',+ '__fixtures__',+]);++/**+ * Whether a file name is a test or fixture rather than a codemod.+ *+ * The trap this closes: EVERY `.ts`/`.mjs`/`.js` under a version folder used to+ * be loaded as a codemod, so a test file colocated with its transform failed+ * validation and — because a definition error is a hard error — took every+ * codemod in that package's version with it. `astryx upgrade` then applied no+ * transforms and reported success, which is the worst shape a failure can take.+ *+ * Core's own codemods never hit this: they are enumerated in a registry, and+ * their colocated tests are simply not in it. Only integrations are discovered+ * by walking a directory, so only integrations carry the landmine — which is why+ * this is a loader fix and not a documentation one. It protects the integrations+ * that already exist, which no authoring tool can reach.+ *+ * @param {string} name a file's base name+ * @returns {boolean}+ */+function isTestFile(name) {+ return /\.(test|spec)\.[^.]+$/.test(name) || /\.fixture\.[^.]+$/.test(name);+}+/*** Recursively collect codemod module files under a version folder. Returns* entries of {id, file} where id is the extension-less relative path@@ -49,14 +83,18 @@ function collectCodemodFiles(versionDir) {for (const entry of entries) {const full = path.join(dir, entry.name);if (entry.isDirectory()) {- if (entry.name === 'node_modules' || entry.name === '.git') continue;+ if (SKIP_DIRS.has(entry.name)) continue;walk(full);continue;}const ext = path.extname(entry.name);if (!CODEMOD_EXTENSIONS.includes(ext)) continue;+ if (isTestFile(entry.name)) continue;const rel = path.relative(versionDir, full);- const id = rel.slice(0, rel.length - ext.length).split(path.sep).join('/');+ const id = rel+ .slice(0, rel.length - ext.length)+ .split(path.sep)+ .join('/');out.push({id, file: full});}}diff --git a/packages/cli/assets/codemods/integration-discovery.test.mjs b/packages/cli/assets/codemods/integration-discovery.test.mjsindex 5a43f4e66d..7c245bdec2 100644--- a/packages/cli/assets/codemods/integration-discovery.test.mjs+++ b/packages/cli/assets/codemods/integration-discovery.test.mjs@@ -221,3 +221,61 @@ describe('integration codemod discovery', () => {).rejects.toThrow(/across versions/i);});});++describe('test files beside a codemod', () => {+ // The incident this closes: every .ts/.mjs/.js under a version folder was+ // loaded AND VALIDATED as a codemod, so a test file colocated with its+ // transform failed validation — and because a definition error is a hard+ // error, it took every codemod in that version with it. `astryx upgrade`+ // then applied nothing and reported success. Core is immune because its own+ // codemods are enumerated in a registry rather than discovered by walking a+ // directory, so its colocated tests are simply never visited. Only+ // integrations carry the landmine.+ const TRANSFORM = `+ export default {+ type: 'code',+ title: 'Drop foo',+ transform: (file) => file.source.replace(/foo/g, 'bar'),+ };+ `;+ // Not a codemod: no default export of the right shape. Loading it throws.+ const A_TEST = `+ import {describe, it, expect} from 'vitest';+ describe('drop-foo', () => {+ it('drops foo', () => expect(1).toBe(1));+ });+ `;++ it.each([+ ['a .test. sibling', '0.2.0/drop-foo.test.mjs'],+ ['a .spec. sibling', '0.2.0/drop-foo.spec.mjs'],+ ['a fixture sibling', '0.2.0/drop-foo.fixture.mjs'],+ ['a __tests__ directory', '0.2.0/__tests__/drop-foo.mjs'],+ ['a __fixtures__ directory', '0.2.0/__fixtures__/input.mjs'],+ ])('ignores %s and still discovers the codemod', async (_label, testPath) => {+ scaffold({'0.2.0/drop-foo.mjs': TRANSFORM, [testPath]: A_TEST});++ const project = await Project.load(tmpDir);+ const byVersion = await discoverIntegrationCodemods(+ project.loadedIntegrations,+ );++ expect([...byVersion.keys()]).toEqual(['0.2.0']);+ expect(byVersion.get('0.2.0').map(entry => entry.id)).toEqual(['drop-foo']);+ });++ it('a nested helper directory is still walked', async () => {+ // Only test and fixture names are skipped. A package that organises its+ // transforms into subdirectories keeps working.+ scaffold({'0.2.0/imports/drop-foo.mjs': TRANSFORM});++ const project = await Project.load(tmpDir);+ const byVersion = await discoverIntegrationCodemods(+ project.loadedIntegrations,+ );++ expect(byVersion.get('0.2.0').map(entry => entry.id)).toEqual([+ 'imports/drop-foo',+ ]);+ });+});diff --git a/packages/cli/assets/docs/cli-integrations.doc.mjs b/packages/cli/assets/docs/cli-integrations.doc.mjsindex 7fcb3716ee..7f82d81771 100644--- a/packages/cli/assets/docs/cli-integrations.doc.mjs+++ b/packages/cli/assets/docs/cli-integrations.doc.mjs@@ -320,6 +320,15 @@ export const docs = {type: 'prose',text: 'Codemod ids (the extension-less relative path under the version folder, e.g. `rename-widget-prop`, `config/rename-integration`) must be unique within a package across all versions. A duplicate id across versions is a hard error.',},+ {+ type: 'prose',+ text: 'The loader automatically skips test and fixture files so you can colocate tests with transforms. Reserved names: files matching `*.test.*`, `*.spec.*`, or `*.fixture.*`, and any file under a `__tests__/` or `__fixtures__/` directory. These are never loaded as codemods regardless of their extension.',+ },+ {+ type: 'code',+ lang: 'text',+ code: 'codemods/\n 0.2.0/\n rename-widget-prop.ts # loaded as a codemod\n rename-widget-prop.test.ts # skipped (reserved name)\n __tests__/\n rename-widget-prop.test.ts # skipped (reserved directory)',+ },{type: 'code',lang: 'typescript',