Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { PatternDetector } from '../../../../../commands/gen2-migration/codegen-custom-resources/parser/pattern-detector';

describe('PatternDetector', () => {
let detector: PatternDetector;

beforeEach(() => {
detector = new PatternDetector();
});

describe('detectPatterns', () => {
it('should detect CfnParameter for env', () => {
const code = `new cdk.CfnParameter(this, 'env', { type: 'String' });`;
const patterns = detector.detectPatterns(code);
expect(patterns.hasCfnParameter).toBe(true);
});

it('should detect cdk.Fn.ref', () => {
const code = `const name = \`resource-\${cdk.Fn.ref('env')}\`;`;
const patterns = detector.detectPatterns(code);
expect(patterns.hasCdkFnRef).toBe(true);
});

it('should detect AmplifyHelpers.getProjectInfo', () => {
const code = `const info = AmplifyHelpers.getProjectInfo();`;
const patterns = detector.detectPatterns(code);
expect(patterns.hasGetProjectInfo).toBe(true);
});

it('should detect AmplifyHelpers.addResourceDependency', () => {
const code = `AmplifyHelpers.addResourceDependency(this, 'auth', 'userPool', []);`;
const patterns = detector.detectPatterns(code);
expect(patterns.hasAddResourceDependency).toBe(true);
});

it('should detect CfnOutput', () => {
const code = `new cdk.CfnOutput(this, 'topicArn', { value: topic.topicArn });`;
const patterns = detector.detectPatterns(code);
expect(patterns.hasCfnOutput).toBe(true);
});
});

describe('extractCfnOutputs', () => {
it('should extract CfnOutput with value and description', () => {
const code = `new cdk.CfnOutput(this, 'snsTopicArn', {
value: topic.topicArn,
description: 'The arn of the SNS topic',
});`;

const outputs = detector.extractCfnOutputs(code);

expect(outputs).toHaveLength(1);
expect(outputs[0].id).toBe('snsTopicArn');
expect(outputs[0].value).toBe('topic.topicArn');
expect(outputs[0].description).toBe('The arn of the SNS topic');
});

it('should extract multiple CfnOutputs', () => {
const code = `
new cdk.CfnOutput(this, 'topicArn', { value: topic.topicArn });
new cdk.CfnOutput(this, 'queueUrl', { value: queue.queueUrl });
`;

const outputs = detector.extractCfnOutputs(code);

expect(outputs).toHaveLength(2);
expect(outputs[0].id).toBe('topicArn');
expect(outputs[1].id).toBe('queueUrl');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { CustomResourceScanner } from '../../../../../commands/gen2-migration/codegen-custom-resources/scanner/custom-resource-scanner';
import * as fs from 'fs-extra';
import { promises as fsPromises } from 'fs';
import * as path from 'path';
import * as os from 'os';

describe('CustomResourceScanner', () => {
let scanner: CustomResourceScanner;

beforeAll(() => {
scanner = new CustomResourceScanner();
});

it('should return empty array when custom directory does not exist', async () => {
const tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'amplify-test-'));
try {
const resources = await scanner.scanCustomResources(tempDir);
expect(resources).toEqual([]);
} finally {
await fs.remove(tempDir);
}
});

it('should find custom resources with cdk-stack.ts', async () => {
const tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'amplify-test-'));
try {
const customDir = path.join(tempDir, 'amplify', 'backend', 'custom');
const notificationsDir = path.join(customDir, 'notifications');

await fsPromises.mkdir(customDir, { recursive: true });
await fsPromises.mkdir(notificationsDir, { recursive: true });
await fsPromises.writeFile(path.join(notificationsDir, 'cdk-stack.ts'), '// CDK stack');

const resources = await scanner.scanCustomResources(tempDir);

expect(resources).toHaveLength(1);
expect(resources[0].name).toBe('notifications');
expect(resources[0].cdkStackPath).toContain('cdk-stack.ts');
} finally {
await fs.remove(tempDir);
}
});

it('should find multiple custom resources', async () => {
const tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'amplify-test-'));
try {
const customDir = path.join(tempDir, 'amplify', 'backend', 'custom');

await fsPromises.mkdir(customDir, { recursive: true });
await fsPromises.mkdir(path.join(customDir, 'notifications'), { recursive: true });
await fsPromises.mkdir(path.join(customDir, 'analytics'), { recursive: true });
await fsPromises.writeFile(path.join(customDir, 'notifications', 'cdk-stack.ts'), '// CDK stack');
await fsPromises.writeFile(path.join(customDir, 'analytics', 'cdk-stack.ts'), '// CDK stack');

const resources = await scanner.scanCustomResources(tempDir);

expect(resources).toHaveLength(2);
expect(resources.map((r) => r.name).sort()).toEqual(['analytics', 'notifications']);
} finally {
await fs.remove(tempDir);
}
});

it('should ignore directories without cdk-stack.ts', async () => {
const tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'amplify-test-'));
try {
const customDir = path.join(tempDir, 'amplify', 'backend', 'custom');

await fsPromises.mkdir(customDir, { recursive: true });
await fsPromises.mkdir(path.join(customDir, 'notifications'), { recursive: true });
await fsPromises.mkdir(path.join(customDir, 'other'), { recursive: true });
await fsPromises.writeFile(path.join(customDir, 'notifications', 'cdk-stack.ts'), '// CDK stack');

const resources = await scanner.scanCustomResources(tempDir);

expect(resources).toHaveLength(1);
expect(resources[0].name).toBe('notifications');
} finally {
await fs.remove(tempDir);
}
});
});
Loading
Loading