Skip to content

Commit

Permalink
refactor: modify setting and deleting env vars, remove unnecessary co…
Browse files Browse the repository at this point in the history
…mments
  • Loading branch information
Sanay Yogesh Shah committed Oct 29, 2024
1 parent b9326c0 commit b28e101
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import assert from 'node:assert';
import { createNewProjectDir, npmInstall } from '@aws-amplify/amplify-e2e-core';
import { createNewProjectDir, generateRandomShortId, npmInstall } from '@aws-amplify/amplify-e2e-core';
import { createGen2Renderer } from '@aws-amplify/amplify-gen2-codegen';
import { copyFunctionFile } from '../function_utils';
import { copyGen1Schema } from '../api_utils';
Expand Down Expand Up @@ -46,7 +46,7 @@ void describe('Codegen E2E tests', () => {
beforeEach(async () => {
const baseDir = process.env.INIT_CWD ?? process.cwd();
projRoot = await createNewProjectDir('codegen_e2e_flow_test', path.join(baseDir, '..', '..'));
projName = `test${Math.floor(Math.random() * 1000000)}`;
projName = `test${generateRandomShortId()}`;
});

afterEach(async () => {
Expand All @@ -60,10 +60,7 @@ void describe('Codegen E2E tests', () => {
await runCodegenCommand(projRoot);
await copyFunctionFile(projRoot, 'function', gen1FunctionName);
await copyGen1Schema(projRoot, projName);

// TODO: replace below line with correct package version
await updatePackageDependency(projRoot, '@aws-amplify/backend');

await npmInstall(projRoot);
const gen2StackName = await runGen2SandboxCommand(projRoot);
await assertAuthResource(projRoot, gen1UserPoolId, gen1ClientIds, gen1IdentityPoolId, gen1Region);
Expand All @@ -80,10 +77,7 @@ void describe('Codegen E2E tests', () => {
await runCodegenCommand(projRoot);
await copyFunctionFile(projRoot, 'auth', gen1FunctionName);
await removeErrorThrowsFromAuthResourceFile(projRoot);

// TODO: replace below line with correct package version
await updatePackageDependency(projRoot, '@aws-amplify/backend');

await npmInstall(projRoot);
await toggleSandboxSecrets(projRoot, 'set');
const gen2StackName = await runGen2SandboxCommand(projRoot);
Expand All @@ -98,10 +92,7 @@ void describe('Codegen E2E tests', () => {
projRoot,
);
await runCodegenCommand(projRoot);

// TODO: replace below line with correct package version
await updatePackageDependency(projRoot, '@aws-amplify/backend');

await npmInstall(projRoot);
await runGen2SandboxCommand(projRoot);
await assertAuthResource(projRoot, gen1UserPoolId, gen1ClientIds, gen1IdentityPoolId, gen1Region);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import assert from 'node:assert';
import { createNewProjectDir, npmInstall, deleteS3Bucket } from '@aws-amplify/amplify-e2e-core';
import { createNewProjectDir, npmInstall, deleteS3Bucket, generateRandomShortId } from '@aws-amplify/amplify-e2e-core';
import { assertDefaultGen1Setup } from '../assertions';
import { setupAndPushDefaultGen1Project, runCodegenCommand, runGen2SandboxCommand, cleanupProjects } from '..';
import { copyFunctionFile } from '../function_utils';
Expand All @@ -18,8 +18,8 @@ void describe('Templategen E2E tests', () => {
beforeEach(async () => {
const baseDir = process.env.INIT_CWD ?? process.cwd();
projRoot = await createNewProjectDir('templategen_e2e_flow_test', path.join(baseDir, '..', '..'));
projName = `test${Math.floor(Math.random() * 1000000)}`;
bucketName = `testbucket${Math.floor(Math.random() * 1000000)}`;
projName = `test${generateRandomShortId()}`;
bucketName = `testbucket${generateRandomShortId()}`;
});

afterEach(async () => {
Expand All @@ -35,10 +35,7 @@ void describe('Templategen E2E tests', () => {
await runCodegenCommand(projRoot);
await copyFunctionFile(projRoot, 'function', gen1FunctionName);
await copyGen1Schema(projRoot, projName);

// TODO: replace below line with correct package version
await updatePackageDependency(projRoot, '@aws-amplify/backend');

await npmInstall(projRoot);
const gen2StackName = await runGen2SandboxCommand(projRoot);
assert(gen2StackName);
Expand Down
13 changes: 6 additions & 7 deletions packages/amplify-migration-e2e/src/envVariables.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
type EnvVariableAction = 'SET' | 'DELETE';

export function toggleEnvVariable(name: string, option: EnvVariableAction, value?: string) {
if (option === 'SET') {
export const envVariable = {
set: (name: string, value: string): void => {
process.env[name] = value;
} else if (option === 'DELETE') {
},
delete: (name: string): void => {
delete process.env[name];
}
}
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ async function getGen1AuthResourceDetails(projRoot: string) {
const { gen1UserPoolId } = await assertUserPool(gen1Meta, gen1Region);
const { gen1IdentityPoolId } = await assertIdentityPool(gen1Meta, gen1Region);
const { gen1ClientIds } = await assertUserPoolClients(gen1Meta, gen1Region);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [gen1ClientIdWeb, gen1ClientId] = gen1ClientIds;
const gen1ClientIdWeb = gen1ClientIds[0];
const gen1ResourceIds = [gen1UserPoolId, gen1IdentityPoolId, gen1ClientIdWeb];

const gen1ResourceDetails = await Promise.all([
Expand Down
11 changes: 5 additions & 6 deletions packages/amplify-migration-e2e/src/templategen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fs from 'fs-extra';
import { getNpxPath, retry, RetrySettings } from '@aws-amplify/amplify-e2e-core';
import { runGen2SandboxCommand } from './sandbox';
import { getCommandsFromReadme } from './migrationReadmeParser';
import { toggleEnvVariable } from './envVariables';
import { envVariable } from './envVariables';
import { getGen1ResourceDetails } from './gen1ResourceDetailsFetcher';
import { getGen2ResourceDetails } from './gen2ResourceDetailsFetcher';

Expand Down Expand Up @@ -89,11 +89,11 @@ async function executeStep2(cwd: string, commands: string[]) {
}

async function executeStep3(cwd: string, commands: string[], bucketName: string) {
toggleEnvVariable('BUCKET_NAME', 'SET', bucketName);
envVariable.set('BUCKET_NAME', bucketName);
await executeCommand(commands[1], cwd);
await executeCommand(commands[2], cwd);
const stackRefactorId = await executeCreateStackRefactorCallCommand(commands[3], cwd);
toggleEnvVariable('STACK_REFACTOR_ID', 'SET', stackRefactorId);
envVariable.set('STACK_REFACTOR_ID', stackRefactorId);
await retry(
() => assertRefactorStepCompletion(commands[5]),
(status) => status.includes(STATUS_COMPLETE) && !status.includes(STATUS_IN_PROGRESS),
Expand All @@ -107,6 +107,8 @@ async function executeStep3(cwd: string, commands: string[], bucketName: string)
RETRY_CONFIG,
(status) => status.includes(STATUS_FAILED),
);
envVariable.delete('BUCKET_NAME');
envVariable.delete('STACK_REFACTOR_ID');
}

async function assertStepCompletion(command: string) {
Expand Down Expand Up @@ -134,9 +136,6 @@ export async function stackRefactor(projRoot: string, category: RefactorCategory

await runGen2SandboxCommand(projRoot);

toggleEnvVariable('BUCKET_NAME', 'DELETE');
toggleEnvVariable('STACK_REFACTOR_ID', 'DELETE');

const { gen2ResourceIds, gen2ResourceDetails } = await getGen2ResourceDetails(projRoot, category);
assert.deepEqual(gen1ResourceIds, gen2ResourceIds);
assert.deepEqual(gen1ResourceDetails, gen2ResourceDetails);
Expand Down

0 comments on commit b28e101

Please sign in to comment.