Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dedupe environment variables in amplify env type generator #2153

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/silver-rocks-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-function': patch
---

dedupe environment variables in amplify env type generator
Original file line number Diff line number Diff line change
@@ -69,4 +69,36 @@ void describe('FunctionEnvironmentTypeGenerator', () => {

await fsp.rm(targetDirectory, { recursive: true, force: true });
});

void it('does not generate duplicate environment variables', () => {
const fsOpenSyncMock = mock.method(fs, 'openSync');
const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null);
fsOpenSyncMock.mock.mockImplementation(() => 0);
const functionEnvironmentTypeGenerator =
new FunctionEnvironmentTypeGenerator('testFunction');

functionEnvironmentTypeGenerator.generateTypedProcessEnvShim([
'TEST_ENV',
'TEST_ENV',
'ANOTHER_ENV',
]);

const generatedContent =
fsWriteFileSyncMock.mock.calls[0].arguments[1]?.toString() ?? '';

// Check TEST_ENV appears only once
assert.equal(
(generatedContent.match(/TEST_ENV: string;/g) || []).length,
1,
'TEST_ENV should appear only once'
);

// Check ANOTHER_ENV also appears
assert.ok(
generatedContent.includes('ANOTHER_ENV: string;'),
'ANOTHER_ENV should be included'
);

mock.restoreAll();
});
});
Original file line number Diff line number Diff line change
@@ -57,7 +57,11 @@ export class FunctionEnvironmentTypeGenerator {
`/** Amplify backend environment variables available at runtime, this includes environment variables defined in \`defineFunction\` and by cross resource mechanisms */`
);
declarations.push(`type ${amplifyBackendEnvVarTypeName} = {`);
amplifyBackendEnvVars.forEach((envName) => {

// Use a Set to remove duplicates
const uniqueEnvVars = new Set(amplifyBackendEnvVars);

uniqueEnvVars.forEach((envName) => {
const declaration = `${this.indentation}${envName}: string;`;

declarations.push(declaration);