Skip to content

Commit

Permalink
rename function name and revert getTemplateFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
vicheey committed Nov 8, 2024
1 parent 9533fc0 commit 8df7799
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 59 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/shared/sam/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 0 additions & 18 deletions packages/core/src/shared/sam/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<vscode.Uri | undefined> => {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
})
})
14 changes: 7 additions & 7 deletions packages/core/src/test/shared/sam/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})
})
Expand Down
42 changes: 28 additions & 14 deletions packages/core/src/test/shared/sam/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})

Expand All @@ -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()
Expand All @@ -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 () => {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})
})
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -1846,7 +1860,7 @@ describe('SAM Sync', () => {
syncedResources: 'AllResources',
source: 'appBuilderDeploy',
})
prompterTester.assertAllHandlerCall(1)
prompterTester.assertCallAll(prompterTester.getHandlers(), 1)
})
})

Expand Down Expand Up @@ -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)
})
})
})
Expand Down
31 changes: 16 additions & 15 deletions packages/core/src/test/shared/wizards/prompterTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PrompterTester {
private inputBoxHanlder: Map<string, (input: TestInputBox) => void> = new Map()
private testWindow: TestWindow
private callLog = Array<string>()
private handlerCallHistory = new Map<string, number>()
private callLogCount = new Map<string, number>()

private constructor(testWindow?: TestWindow) {
this.testWindow = testWindow || getTestWindow()
Expand All @@ -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
}

Expand All @@ -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).
Expand All @@ -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)
})
}

Expand All @@ -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()]
}

Expand All @@ -106,5 +106,6 @@ export class PrompterTester {

private handleUnknownPrompter(input: any) {
input.dispose()
throw assert.fail(`Unexpected prompter titled: "${input.title}"`)
}
}

0 comments on commit 8df7799

Please sign in to comment.