Skip to content

Commit 9214ee8

Browse files
committed
run search first, avoid re-verifying
1 parent eea0507 commit 9214ee8

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

packages/core/src/shared/sam/cli/samCliLocator.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import { PerfLog } from '../../logger/perfLogger'
1313
import { tryRun } from '../../utilities/pathFind'
1414
import { mergeResolvedShellPath } from '../../env/resolveEnv'
1515

16+
interface SamLocation {
17+
path: string
18+
version: string
19+
verified?: boolean
20+
}
1621
export class SamCliLocationProvider {
1722
private static samCliLocator: BaseSamCliLocator | undefined
18-
protected static cachedSamLocation: { path: string; version: string } | undefined
23+
protected static cachedSamLocation: SamLocation | undefined
1924

2025
/** Checks that the given `sam` actually works by invoking `sam --version`. */
2126
private static async isValidSamLocation(samPath: string) {
@@ -31,11 +36,11 @@ export class SamCliLocationProvider {
3136
const cachedLoc = forceSearch ? undefined : SamCliLocationProvider.cachedSamLocation
3237

3338
// Avoid searching the system for `sam` (especially slow on Windows).
34-
if (cachedLoc && (await SamCliLocationProvider.isValidSamLocation(cachedLoc.path))) {
39+
if (cachedLoc && (cachedLoc.verified || (await SamCliLocationProvider.isValidSamLocation(cachedLoc.path)))) {
40+
cachedLoc.verified = true
3541
perflog.done()
3642
return cachedLoc
3743
}
38-
3944
SamCliLocationProvider.cachedSamLocation = await SamCliLocationProvider.getSamCliLocator().getLocation()
4045
perflog.done()
4146
return SamCliLocationProvider.cachedSamLocation

packages/core/src/shared/sam/cli/samCliSettings.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export class SamCliSettings extends fromExtensionManifest('aws.samcli', descript
8484
return { path: fromConfig, autoDetected: false }
8585
}
8686
const fromSearch = await this.locationProvider.getLocation(forceSearch)
87-
8887
SamCliSettings.logIfChanged(`SAM CLI location (version: ${fromSearch?.version}): ${fromSearch?.path}`)
8988
return { path: fromSearch?.path, autoDetected: true }
9089
}

packages/core/src/test/shared/sam/cli/samCliLocalInvoke.test.ts

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import {
1414
import { ChildProcess } from '../../../../shared/utilities/processUtils'
1515
import { assertArgIsPresent, assertArgNotPresent, assertArgsContainArgument } from './samCliTestUtils'
1616
import { fs } from '../../../../shared'
17+
import { SamCliSettings } from '../../../../shared/sam/cli/samCliSettings'
1718
import { isWin } from '../../../../shared/vscode/env'
18-
// eslint-disable-next-line aws-toolkits/no-only-in-tests
19-
describe.only('SamCliLocalInvokeInvocation', async function () {
19+
20+
describe('SamCliLocalInvokeInvocation', async function () {
2021
class TestSamLocalInvokeCommand implements SamLocalInvokeCommand {
2122
public constructor(private readonly onInvoke: ({ ...params }: SamLocalInvokeCommandArgs) => void) {}
2223

@@ -31,6 +32,15 @@ describe.only('SamCliLocalInvokeInvocation', async function () {
3132
let placeholderEventFile: string
3233
const nonRelevantArg = 'arg is not of interest to this test'
3334

35+
before(async function () {
36+
// File system search on windows can take a while.
37+
if (isWin()) {
38+
this.timeout(45000)
39+
}
40+
// This will place the result in the cache allowing all tests to run under same conditions.
41+
await SamCliSettings.instance.getOrDetectSamCli()
42+
})
43+
3444
beforeEach(async function () {
3545
tempFolder = await makeTemporaryToolkitFolder()
3646
placeholderTemplateFile = path.join(tempFolder, 'template.yaml')
@@ -42,40 +52,34 @@ describe.only('SamCliLocalInvokeInvocation', async function () {
4252
afterEach(async function () {
4353
await fs.delete(tempFolder, { recursive: true })
4454
})
45-
for (const _ of Array.from({ length: 1000 }, (i) => i)) {
46-
// eslint-disable-next-line aws-toolkits/no-only-in-tests
47-
it.only('invokes `sam local` with args', async function () {
48-
if (isWin()) {
49-
// TODO: insert 'C:\\Program Files\\Amazon\\AWSSAMCLI\\bin\\sam.cmd' into the cache.
50-
this.timeout(45000)
55+
56+
it('invokes `sam local` with args', async function () {
57+
const taskInvoker: SamLocalInvokeCommand = new TestSamLocalInvokeCommand(
58+
(invokeArgs: SamLocalInvokeCommandArgs) => {
59+
assert.ok(invokeArgs.args.length >= 2, 'Expected args to be present')
60+
assert.strictEqual(invokeArgs.args[0], 'local')
61+
assert.strictEqual(invokeArgs.args[1], 'invoke')
62+
// --debug is present because tests run with "debug" log-level. #1403
63+
assert.strictEqual(invokeArgs.args[2], '--debug')
64+
assert.strictEqual(invokeArgs.args[4], '--template')
65+
assert.strictEqual(invokeArgs.args[6], '--event')
66+
assert.strictEqual(invokeArgs.args[8], '--env-vars')
67+
68+
// `extraArgs` are appended to the end.
69+
assert.strictEqual(invokeArgs.args[10], '--build-dir')
70+
assert.strictEqual(invokeArgs.args[11], 'my/build/dir/')
5171
}
52-
const taskInvoker: SamLocalInvokeCommand = new TestSamLocalInvokeCommand(
53-
(invokeArgs: SamLocalInvokeCommandArgs) => {
54-
assert.ok(invokeArgs.args.length >= 2, 'Expected args to be present')
55-
assert.strictEqual(invokeArgs.args[0], 'local')
56-
assert.strictEqual(invokeArgs.args[1], 'invoke')
57-
// --debug is present because tests run with "debug" log-level. #1403
58-
assert.strictEqual(invokeArgs.args[2], '--debug')
59-
assert.strictEqual(invokeArgs.args[4], '--template')
60-
assert.strictEqual(invokeArgs.args[6], '--event')
61-
assert.strictEqual(invokeArgs.args[8], '--env-vars')
62-
63-
// `extraArgs` are appended to the end.
64-
assert.strictEqual(invokeArgs.args[10], '--build-dir')
65-
assert.strictEqual(invokeArgs.args[11], 'my/build/dir/')
66-
}
67-
)
68-
69-
await new SamCliLocalInvokeInvocation({
70-
templateResourceName: nonRelevantArg,
71-
templatePath: placeholderTemplateFile,
72-
eventPath: placeholderEventFile,
73-
environmentVariablePath: nonRelevantArg,
74-
invoker: taskInvoker,
75-
extraArgs: ['--build-dir', 'my/build/dir/'],
76-
}).execute()
77-
})
78-
}
72+
)
73+
74+
await new SamCliLocalInvokeInvocation({
75+
templateResourceName: nonRelevantArg,
76+
templatePath: placeholderTemplateFile,
77+
eventPath: placeholderEventFile,
78+
environmentVariablePath: nonRelevantArg,
79+
invoker: taskInvoker,
80+
extraArgs: ['--build-dir', 'my/build/dir/'],
81+
}).execute()
82+
})
7983

8084
it('Passes template resource name to sam cli', async function () {
8185
const expectedResourceName = 'HelloWorldResource'

0 commit comments

Comments
 (0)