From 8df77999c6800e2b179230846054c0e842b5e809 Mon Sep 17 00:00:00 2001 From: Vichym Date: Fri, 8 Nov 2024 15:46:54 -0800 Subject: [PATCH] rename function name and revert getTemplateFile() --- packages/core/src/shared/sam/sync.ts | 4 +- packages/core/src/shared/sam/utils.ts | 18 -------- .../wizards/deployTypeWizard.test.ts | 6 +-- .../core/src/test/shared/sam/deploy.test.ts | 14 +++---- .../core/src/test/shared/sam/sync.test.ts | 42 ++++++++++++------- .../src/test/shared/wizards/prompterTester.ts | 31 +++++++------- 6 files changed, 56 insertions(+), 59 deletions(-) diff --git a/packages/core/src/shared/sam/sync.ts b/packages/core/src/shared/sam/sync.ts index f9ac4fdc1f1..e21d89899f9 100644 --- a/packages/core/src/shared/sam/sync.ts +++ b/packages/core/src/shared/sam/sync.ts @@ -41,7 +41,7 @@ import { IamConnection } from '../../auth/connection' import { CloudFormationTemplateRegistry } from '../fs/templateRegistry' import { TreeNode } from '../treeview/resourceTreeDataProvider' import { getSpawnEnv } from '../env/resolveEnv' -import { getProjectRoot, getProjectRootUri, getSamCliPathAndVersion, getSource, getTemplateFile } from './utils' +import { getProjectRoot, getProjectRootUri, getSamCliPathAndVersion, getSource } from './utils' import { runInTerminal } from './processTerminal' const localize = nls.loadMessageBundle() @@ -598,7 +598,7 @@ export async function prepareSyncParams( const projectRoot = vscode.Uri.joinPath(config.location, '..') const templateUri = params.templatePath ? vscode.Uri.file(path.resolve(projectRoot.fsPath, params.templatePath)) - : await getTemplateFile(projectRoot) + : undefined const template = templateUri ? { uri: templateUri, diff --git a/packages/core/src/shared/sam/utils.ts b/packages/core/src/shared/sam/utils.ts index 5181c9c2e2b..a0c700657d2 100644 --- a/packages/core/src/shared/sam/utils.ts +++ b/packages/core/src/shared/sam/utils.ts @@ -10,7 +10,6 @@ import { TreeNode, isTreeNode } from '../treeview/resourceTreeDataProvider' import * as CloudFormation from '../cloudformation/cloudformation' import { TemplateItem } from './sync' import { RuntimeFamily, getFamily } from '../../lambda/models/samLambdaRuntime' -import fs from '../fs/fs' import { telemetry } from '../telemetry' import { ToolkitError } from '../errors' import { SamCliSettings } from './cli/samCliSettings' @@ -25,23 +24,6 @@ import { parse } from 'semver' export const getProjectRoot = (template: TemplateItem | undefined) => template ? getProjectRootUri(template.uri) : undefined -/** - * @description look for template in project root - * @param projectRoot The URI of the root project folder - * @returns The URI of the template - * */ -export const getTemplateFile = async (projectRoot: vscode.Uri): Promise => { - const templateNames = ['template.yaml', 'template.yml'] - - for (const templateName of templateNames) { - const templatePath = vscode.Uri.file(path.join(projectRoot.fsPath, templateName)) - if (await fs.exists(templatePath.fsPath)) { - return templatePath - } - } - - return undefined -} /** * @description determines the root directory of the project given uri of the template file * @param template The template.yaml uri diff --git a/packages/core/src/test/awsService/appBuilder/wizards/deployTypeWizard.test.ts b/packages/core/src/test/awsService/appBuilder/wizards/deployTypeWizard.test.ts index 80b164436d0..2abe68def9a 100644 --- a/packages/core/src/test/awsService/appBuilder/wizards/deployTypeWizard.test.ts +++ b/packages/core/src/test/awsService/appBuilder/wizards/deployTypeWizard.test.ts @@ -45,7 +45,7 @@ describe('DeployTypeWizard', function () { const choices = await deployTypeWizard.run() // Then assert(!choices) - prompterTester.assertHandlerCall('Select deployment command', 1) + prompterTester.assertCall('Select deployment command', 1) }) it('deploy is selected', async function () { @@ -80,7 +80,7 @@ describe('DeployTypeWizard', function () { const choices = await deployTypeWizard.run() // Then assert.strictEqual(choices?.choice, 'deploy') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('sync is selected', async function () { @@ -109,6 +109,6 @@ describe('DeployTypeWizard', function () { const choices = await deployTypeWizard.run() // Then assert.strictEqual(choices?.choice, 'sync') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) diff --git a/packages/core/src/test/shared/sam/deploy.test.ts b/packages/core/src/test/shared/sam/deploy.test.ts index 68610df71fe..4b7e9576980 100644 --- a/packages/core/src/test/shared/sam/deploy.test.ts +++ b/packages/core/src/test/shared/sam/deploy.test.ts @@ -143,7 +143,7 @@ describe('DeployWizard', async function () { assert.strictEqual(parameters.region, 'us-west-2') assert.strictEqual(parameters.stackName, 'stack1') assert.strictEqual(parameters.bucketSource, 0) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with valid samconfig.toml', async () => { @@ -195,7 +195,7 @@ describe('DeployWizard', async function () { assert(!parameters.region) assert(!parameters.stackName) assert(!parameters.bucketSource) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -333,7 +333,7 @@ describe('DeployWizard', async function () { assert.strictEqual(parameters.region, 'us-west-2') assert(!parameters.stackName) assert(!parameters.bucketSource) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -415,7 +415,7 @@ describe('DeployWizard', async function () { assert.strictEqual(parameters.stackName, 'stack2') assert.strictEqual(parameters.bucketSource, 0) assert(!parameters.bucketName) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with valid samconfig.toml', async () => { @@ -467,7 +467,7 @@ describe('DeployWizard', async function () { assert(!parameters.region) assert(!parameters.stackName) assert(!parameters.bucketSource) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -549,7 +549,7 @@ describe('DeployWizard', async function () { assert.strictEqual(parameters.stackName, 'stack3') assert.strictEqual(parameters.bucketSource, 1) assert.strictEqual(parameters.bucketName, 'stack-3-bucket') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with samconfig.toml', async () => { @@ -602,7 +602,7 @@ describe('DeployWizard', async function () { assert(!parameters.region) assert(!parameters.stackName) assert(!parameters.bucketSource) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) }) diff --git a/packages/core/src/test/shared/sam/sync.test.ts b/packages/core/src/test/shared/sam/sync.test.ts index e854efb3f58..1d3f6317982 100644 --- a/packages/core/src/test/shared/sam/sync.test.ts +++ b/packages/core/src/test/shared/sam/sync.test.ts @@ -999,7 +999,7 @@ describe('SyncWizard', async () => { assert.strictEqual(parameters.bucketName, 'stack-1-bucket') assert.strictEqual(parameters.skipDependencyLayer, true) assert.strictEqual(parameters.syncFlags, '["--dependency-layer","--use-container","--save-params"]') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with valid samconfig.toml', async () => { @@ -1042,7 +1042,7 @@ describe('SyncWizard', async () => { assert(!parameters.bucketName) assert.strictEqual(parameters.skipDependencyLayer, true) assert(!parameters.syncFlags) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -1122,7 +1122,7 @@ describe('SyncWizard', async () => { assert.strictEqual(parameters.deployType, 'infra') assert.strictEqual(parameters.skipDependencyLayer, true) assert.strictEqual(parameters.syncFlags, '["--save-params"]') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with valid samconfig.toml', async () => { @@ -1165,7 +1165,7 @@ describe('SyncWizard', async () => { assert(!parameters.stackName) assert(!parameters.bucketSource) assert.strictEqual(parameters.skipDependencyLayer, true) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -1248,7 +1248,7 @@ describe('SyncWizard', async () => { assert.strictEqual(parameters.deployType, 'infra') assert.strictEqual(parameters.skipDependencyLayer, true) assert.strictEqual(parameters.syncFlags, '["--dependency-layer","--use-container"]') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with valid samconfig.toml', async () => { @@ -1298,7 +1298,7 @@ describe('SyncWizard', async () => { assert(!parameters.stackName) assert(!parameters.bucketSource) assert.strictEqual(parameters.skipDependencyLayer, true) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -1317,6 +1317,13 @@ describe('SyncWizard', async () => { // generate samconfig.toml in temporary test folder const samconfigFile = vscode.Uri.file(await testFolder.write('samconfig.toml', samconfigCompleteData)) const prompterTester = PrompterTester.init() + .handleQuickPick('Select a SAM/CloudFormation Template', async (quickPick) => { + // Need sometime to wait for the template to search for template file + await quickPick.untilReady() + assert.strictEqual(quickPick.items.length, 1) + assert.strictEqual(quickPick.items[0].label, templateFile.fsPath) + quickPick.acceptItem(quickPick.items[0]) + }) .handleQuickPick('Specify parameters for deploy', async (picker) => { // Need time to check samconfig.toml file and generate options await picker.untilReady() @@ -1340,7 +1347,7 @@ describe('SyncWizard', async () => { assert.strictEqual(parameters.bucketName, 'aws-sam-cli-managed-default-samclisourcebucket-lftqponsaxsr') assert.strictEqual(parameters.skipDependencyLayer, true) assert(!parameters.syncFlags) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with empty samconfig.toml', async () => { @@ -1357,6 +1364,13 @@ describe('SyncWizard', async () => { */ const prompterTester = PrompterTester.init() + .handleQuickPick('Select a SAM/CloudFormation Template', async (quickPick) => { + // Need sometime to wait for the template to search for template file + await quickPick.untilReady() + assert.strictEqual(quickPick.items.length, 1) + assert.strictEqual(quickPick.items[0].label, templateFile.fsPath) + quickPick.acceptItem(quickPick.items[0]) + }) .handleQuickPick('Specify parameters for deploy', async (picker) => { // Need time to check samconfig.toml file and generate options await picker.untilReady() @@ -1407,7 +1421,7 @@ describe('SyncWizard', async () => { assert.strictEqual(parameters.deployType, 'infra') assert.strictEqual(parameters.skipDependencyLayer, true) assert.strictEqual(parameters.syncFlags, '["--dependency-layer","--use-container","--watch"]') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -1483,7 +1497,7 @@ describe('SyncWizard', async () => { assert.strictEqual(parameters.deployType, 'infra') assert.strictEqual(parameters.skipDependencyLayer, true) assert.strictEqual(parameters.syncFlags, '["--dependency-layer","--use-container"]') - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('happy path with valid samconfig.toml', async () => { @@ -1534,7 +1548,7 @@ describe('SyncWizard', async () => { assert(!parameters.bucketSource) assert(!parameters.syncFlags) assert.strictEqual(parameters.skipDependencyLayer, true) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) }) @@ -1714,7 +1728,7 @@ describe('SAM Sync', () => { syncedResources: 'CodeOnly', source: undefined, }) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('[entry: template file] specify flag should instantiate correct process in terminal', async () => { @@ -1790,7 +1804,7 @@ describe('SAM Sync', () => { syncedResources: 'AllResources', source: 'template', }) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) it('[entry: appBuilder] use samconfig should instantiate correct process in terminal', async () => { @@ -1846,7 +1860,7 @@ describe('SAM Sync', () => { syncedResources: 'AllResources', source: 'appBuilderDeploy', }) - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) @@ -1934,7 +1948,7 @@ describe('SAM Sync', () => { assert(error instanceof ToolkitError) assert.strictEqual(error.message, 'Failed to sync SAM application') } - prompterTester.assertAllHandlerCall(1) + prompterTester.assertCallAll(prompterTester.getHandlers(), 1) }) }) }) diff --git a/packages/core/src/test/shared/wizards/prompterTester.ts b/packages/core/src/test/shared/wizards/prompterTester.ts index b8d4577c5d5..4e710cd5526 100644 --- a/packages/core/src/test/shared/wizards/prompterTester.ts +++ b/packages/core/src/test/shared/wizards/prompterTester.ts @@ -12,7 +12,7 @@ export class PrompterTester { private inputBoxHanlder: Map void> = new Map() private testWindow: TestWindow private callLog = Array() - private handlerCallHistory = new Map() + private callLogCount = new Map() private constructor(testWindow?: TestWindow) { this.testWindow = testWindow || getTestWindow() @@ -24,13 +24,13 @@ export class PrompterTester { handleQuickPick(titlePattern: string, handler: (input: TestQuickPick) => void): PrompterTester { this.quickPickHandlers.set(titlePattern, handler) - this.handlerCallHistory.set(titlePattern, 0) + this.callLogCount.set(titlePattern, 0) return this } handleInputBox(titlePattern: string, handler: (input: TestInputBox) => void): PrompterTester { this.inputBoxHanlder.set(titlePattern, handler) - this.handlerCallHistory.set(titlePattern, 0) + this.callLogCount.set(titlePattern, 0) return this } @@ -46,22 +46,22 @@ export class PrompterTester { private record(title: string): void { this.callLog.push(title) - this.handlerCallHistory.set(title, (this.handlerCallHistory.get(title) ?? 0) + 1) + this.callLogCount.set(title, (this.callLogCount.get(title) ?? 0) + 1) } /** - * Asserts that a specific handler has been called the expected number of times. + * Asserts that a specific prompter handler has been called the expected number of times. * - * @param title - The title or identifier of the handler to check. - * @param expectedCall - The expected number of times the handler should have been called. + * @param title - The title prompter to check. + * @param expectedCall - The expected number of times the prompted handler should have been called. * @throws AssertionError if the actual number of calls doesn't match the expected number. */ - assertHandlerCall(title: string, expectedCall: number) { - assert.strictEqual(this.handlerCallHistory.get(title), expectedCall, title) + assertCall(title: string, expectedCall: number) { + assert.strictEqual(this.callLogCount.get(title), expectedCall, title) } /** - * Asserts that a specific handler was called in the expected order. + * Asserts that a specific prompter handler was called in the expected order. * * @param title - The title or identifier of the handler to check. * @param expectedOrder - The expected position in the call order (one-based index). @@ -72,14 +72,14 @@ export class PrompterTester { } /** - * Asserts that all handler was called in the expected number of times. + * Asserts that all specified prompter handlers were called in the expected number of times. * * @param expectedCall - The expected number of times the handler should have been called. * @throws AssertionError if the actual number of calls doesn't match the expected number. */ - assertAllHandlerCall(expectedOrder: number) { - this.getAllRegisteredHandlers().every((handler) => { - this.assertHandlerCall(handler, expectedOrder) + assertCallAll(titles: string[], expectedOrder: number) { + titles.every((handler) => { + this.assertCall(handler, expectedOrder) }) } @@ -89,7 +89,7 @@ export class PrompterTester { * @returns An array of strings containing all handler titles, including both * quick pick handlers and input box handlers. */ - getAllRegisteredHandlers(): string[] { + getHandlers(): string[] { return [...this.quickPickHandlers.keys(), ...this.inputBoxHanlder.keys()] } @@ -106,5 +106,6 @@ export class PrompterTester { private handleUnknownPrompter(input: any) { input.dispose() + throw assert.fail(`Unexpected prompter titled: "${input.title}"`) } }