Skip to content

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

TypeScript

test(swizzle): cover destination symlinks (#10484)

Cindy Zhang99ab16da251f
diff --git a/packages/cli/api/swizzle/copy/copy.test.mjs b/packages/cli/api/swizzle/copy/copy.test.mjs
index bb9a9d85b2..6ba6116e9c 100644
--- a/packages/cli/api/swizzle/copy/copy.test.mjs
+++ b/packages/cli/api/swizzle/copy/copy.test.mjs
@@ -34,6 +34,79 @@ const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../.
const OUT = 'tmp-swizzle-copy-test';
const SLOW = 30_000;
+describe('swizzle.copy — destination symlinks (spec:AST-017 FR1, FR5)', () => {
+ let fixture;
+ afterEach(() => {
+ if (fixture) fs.rmSync(fixture, {recursive: true, force: true});
+ });
+
+ it.each([
+ ['out', false],
+ ['out/Table', false],
+ ['out/Table/plugins', false],
+ ['out/Table/plugins/nested.ts', false],
+ ['out/Table/plugins/nested.ts', true],
+ ])('refuses %s (dangling: %s) linked outside the project before writing, with or without overwrite', async (linkPath, dangling) => {
+ fixture = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-swizzle-symlink-'));
+ const cwd = path.join(fixture, 'project');
+ const source = path.join(cwd, 'node_modules/@astryxdesign/core/src/Table');
+ fs.mkdirSync(path.join(source, 'plugins'), {recursive: true});
+ // Table imports the nested file, so the copy writes it too.
+ fs.writeFileSync(
+ path.join(source, 'Table.tsx'),
+ "import {nested} from './plugins/nested';\nexport const Table = nested;",
+ );
+ fs.writeFileSync(path.join(source, 'plugins/nested.ts'), 'export const nested = 1;');
+ fs.writeFileSync(path.join(cwd, 'package.json'), '{"name":"consumer"}');
+ fs.writeFileSync(
+ path.join(cwd, 'node_modules/@astryxdesign/core/package.json'),
+ '{"name":"@astryxdesign/core","version":"0.0.0"}',
+ );
+ const outside = path.join(fixture, 'outside');
+ fs.mkdirSync(outside);
+ const target = linkPath.endsWith('.ts') ? path.join(outside, 'target.ts') : outside;
+ if (target !== outside && !dangling) fs.writeFileSync(target, '// consumer edit');
+ const link = path.join(cwd, linkPath);
+ fs.mkdirSync(path.dirname(link), {recursive: true});
+ fs.symlinkSync(target, link);
+
+ for (const overwrite of [false, true]) {
+ await expect(swizzle('Table', {cwd, output: './out', overwrite})).rejects.toMatchObject({
+ code: 'ERR_PATH_TRAVERSAL',
+ });
+ expect(fs.existsSync(path.join(cwd, 'out/Table/Table.tsx'))).toBe(false);
+ expect(fs.readdirSync(outside)).toEqual(target !== outside && !dangling ? ['target.ts'] : []);
+ if (target !== outside && !dangling) expect(fs.readFileSync(target, 'utf8')).toBe('// consumer edit');
+ }
+ });
+
+ it('writes through a folder linked elsewhere inside the project, nested folders included', async () => {
+ fixture = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-swizzle-symlink-'));
+ const source = path.join(fixture, 'node_modules/@astryxdesign/core/src/Table');
+ fs.mkdirSync(path.join(source, 'plugins'), {recursive: true});
+ // Table imports the nested file, so the copy writes it too.
+ fs.writeFileSync(
+ path.join(source, 'Table.tsx'),
+ "import {nested} from './plugins/nested';\nexport const Table = nested;",
+ );
+ fs.writeFileSync(path.join(source, 'plugins/nested.ts'), 'export const nested = 1;');
+ fs.writeFileSync(path.join(fixture, 'package.json'), '{"name":"consumer"}');
+ fs.writeFileSync(
+ path.join(fixture, 'node_modules/@astryxdesign/core/package.json'),
+ '{"name":"@astryxdesign/core","version":"0.0.0"}',
+ );
+ // `components` -> `src/components`, a common project layout.
+ fs.mkdirSync(path.join(fixture, 'src/components'), {recursive: true});
+ fs.symlinkSync(path.join(fixture, 'src/components'), path.join(fixture, 'components'));
+
+ const r = await swizzle('Table', {cwd: fixture, output: './components'});
+ expect(r.data.files).toEqual(['Table.tsx', 'plugins/nested.ts']);
+ expect(
+ fs.readFileSync(path.join(fixture, 'src/components/Table/plugins/nested.ts'), 'utf8'),
+ ).toBe('export const nested = 1;');
+ });
+});
+
describe('swizzle.copy — path safety', () => {
afterEach(() => {
fs.rmSync(path.join(REPO, OUT), {recursive: true, force: true});