Skip to content

Commit 194ccbc

Browse files
refactor: CI test run scripts (#4274)
* refactor: launchTestUtilities.ts - Functionality is untouched - Methods are reordered - 'export' is removed from methods that do not need it - runToolkitTests() is generalized to remove duplication in scripts/test/test.ts Signed-off-by: nkomonen <nkomonen@amazon.com> * do not resolve paths, just give string They paths will eventually be resolved in the function they are being passed to Signed-off-by: nkomonen <nkomonen@amazon.com> --------- Signed-off-by: nkomonen <nkomonen@amazon.com>
1 parent 47b3ccb commit 194ccbc

File tree

6 files changed

+99
-104
lines changed

6 files changed

+99
-104
lines changed

scripts/test/launchTestUtilities.ts

Lines changed: 90 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } from '
99
import { join, resolve } from 'path'
1010
import { runTests } from '@vscode/test-electron'
1111
import { VSCODE_EXTENSION_ID } from '../../src/shared/extensions'
12+
import { TestOptions } from '@vscode/test-electron/out/runTest'
1213

1314
const envvarVscodeTestVersion = 'VSCODE_TEST_VERSION'
1415

@@ -17,8 +18,80 @@ const minimum = 'minimum'
1718

1819
const disableWorkspaceTrust = '--disable-workspace-trust'
1920

20-
export const integrationSuite = 'integration'
21-
export const e2eSuite = 'e2e'
21+
type SuiteName = 'integration' | 'e2e' | 'unit'
22+
23+
/**
24+
* This is the generalized method that is used by different test suites (unit, integration, ...) in CI to
25+
* setup vscode and then run tests. An important thing to note is that in CI we do not have VS Code installed,
26+
* so this script needs to do this itself.
27+
*
28+
* If you want to run the tests manually you should use the `Run & Debug` menu in VS Code instead
29+
* to be able to use to breakpoints.
30+
*/
31+
export async function runToolkitTests(suite: SuiteName, relativeTestEntryPoint: string) {
32+
try {
33+
console.log(`Running ${suite} test suite...`)
34+
35+
const args = await getVSCodeCliArgs({
36+
vsCodeExecutablePath: await setupVSCodeTestInstance(suite),
37+
relativeTestEntryPoint,
38+
suite,
39+
})
40+
console.log(`runTests() args:\n${JSON.stringify(args, undefined, 2)}`)
41+
const result = await runTests(args)
42+
43+
console.log(`Finished running ${suite} test suite with result code: ${result}`)
44+
process.exit(result)
45+
} catch (err) {
46+
console.error(err)
47+
console.error('Failed to run tests')
48+
process.exit(1)
49+
}
50+
}
51+
52+
/**
53+
* Resolves all args for {@link runTests}
54+
*/
55+
async function getVSCodeCliArgs(params: {
56+
vsCodeExecutablePath: string
57+
relativeTestEntryPoint: string
58+
suite: SuiteName
59+
}): Promise<TestOptions> {
60+
const projectRootDir = process.cwd()
61+
62+
let disableExtensionsArgs: string[] = []
63+
let disableWorkspaceTrustArg: string[] = []
64+
65+
if (params.suite !== 'unit') {
66+
disableExtensionsArgs = await getCliArgsToDisableExtensions(params.vsCodeExecutablePath, {
67+
except: [
68+
VSCODE_EXTENSION_ID.python,
69+
VSCODE_EXTENSION_ID.yaml,
70+
VSCODE_EXTENSION_ID.jupyter,
71+
VSCODE_EXTENSION_ID.go,
72+
VSCODE_EXTENSION_ID.java,
73+
VSCODE_EXTENSION_ID.javadebug,
74+
VSCODE_EXTENSION_ID.git,
75+
VSCODE_EXTENSION_ID.remotessh,
76+
],
77+
})
78+
disableWorkspaceTrustArg = [disableWorkspaceTrust]
79+
}
80+
81+
const workspacePath = join(projectRootDir, 'dist', 'src', 'testFixtures', 'workspaceFolder')
82+
83+
return {
84+
vscodeExecutablePath: params.vsCodeExecutablePath,
85+
extensionDevelopmentPath: projectRootDir,
86+
extensionTestsPath: resolve(projectRootDir, params.relativeTestEntryPoint),
87+
// For verbose VSCode logs, add "--verbose --log debug". c2165cf48e62c
88+
launchArgs: [...disableExtensionsArgs, workspacePath, ...disableWorkspaceTrustArg],
89+
extensionTestsEnv: {
90+
['DEVELOPMENT_PATH']: projectRootDir,
91+
['AWS_TOOLKIT_AUTOMATION']: params.suite,
92+
},
93+
}
94+
}
2295

2396
/**
2497
* Downloads and unzips a copy of VS Code to run tests against.
@@ -30,7 +103,7 @@ export const e2eSuite = 'e2e'
30103
* The VS Code version under test can be altered by setting the environment variable
31104
* VSCODE_TEST_VERSION prior to running the tests.
32105
*/
33-
export async function setupVSCodeTestInstance(): Promise<string> {
106+
async function setupVSCodeTestInstance(suite: SuiteName): Promise<string> {
34107
let vsCodeVersion = process.env[envvarVscodeTestVersion]
35108
if (!vsCodeVersion) {
36109
vsCodeVersion = stable
@@ -44,10 +117,20 @@ export async function setupVSCodeTestInstance(): Promise<string> {
44117
console.log(`VS Code test instance location: ${vsCodeExecutablePath}`)
45118
console.log(await invokeVSCodeCli(vsCodeExecutablePath, ['--version']))
46119

120+
// Only certain test suites require specific vscode extensions to be installed
121+
if (suite !== 'unit') {
122+
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.python)
123+
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.yaml)
124+
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.go)
125+
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.java)
126+
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.javadebug)
127+
}
128+
129+
console.log('VS Code Test instance has been set up')
47130
return vsCodeExecutablePath
48131
}
49132

50-
export async function invokeVSCodeCli(vsCodeExecutablePath: string, args: string[]): Promise<string> {
133+
async function invokeVSCodeCli(vsCodeExecutablePath: string, args: string[]): Promise<string> {
51134
const [cli, ...cliArgs] = resolveCliArgsFromVSCodeExecutablePath(vsCodeExecutablePath)
52135
const cmdArgs = [...cliArgs, ...args]
53136

@@ -75,7 +158,7 @@ export async function invokeVSCodeCli(vsCodeExecutablePath: string, args: string
75158
return spawnResult.stdout
76159
}
77160

78-
export async function installVSCodeExtension(vsCodeExecutablePath: string, extensionIdentifier: string): Promise<void> {
161+
async function installVSCodeExtension(vsCodeExecutablePath: string, extensionIdentifier: string): Promise<void> {
79162
// HACK: `sam.test.ts` Codelens test was failing for python due to bug in newer version, so lock to last working version.
80163
// Edge Case: This specific python version does not work with the "minimum" vscode version, so we do not override it as it
81164
// will choose its own python extension version that works.
@@ -97,7 +180,7 @@ export async function installVSCodeExtension(vsCodeExecutablePath: string, exten
97180
* @returns List of args which the caller is expected to pass to vscode CLI, of the form:
98181
* ["--disable-extension", "foo.bar.baz", "--disable-extension", ...]
99182
*/
100-
export async function getCliArgsToDisableExtensions(
183+
async function getCliArgsToDisableExtensions(
101184
vsCodeExecutablePath: string,
102185
params: { except: string[] }
103186
): Promise<string[]> {
@@ -114,67 +197,11 @@ export async function getCliArgsToDisableExtensions(
114197
return ids
115198
}
116199

117-
export function getMinVsCodeVersion(): string {
200+
function getMinVsCodeVersion(): string {
118201
const vsCodeVersion = packageJson.engines.vscode
119202

120203
// We assume that we specify a minium, so it matches ^<number>, so remove ^'s
121204
const sanitizedVersion = vsCodeVersion.replace('^', '')
122205
console.log(`Using minimum VSCode version specified in package.json: ${sanitizedVersion}`)
123206
return sanitizedVersion
124207
}
125-
126-
async function setupVSCode(): Promise<string> {
127-
console.log('Setting up VS Code Test instance...')
128-
const vsCodeExecutablePath = await setupVSCodeTestInstance()
129-
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.python)
130-
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.yaml)
131-
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.go)
132-
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.java)
133-
await installVSCodeExtension(vsCodeExecutablePath, VSCODE_EXTENSION_ID.javadebug)
134-
135-
console.log('VS Code Test instance has been set up')
136-
return vsCodeExecutablePath
137-
}
138-
139-
export async function runToolkitTests(suiteName: string, relativeEntryPoint: string) {
140-
try {
141-
console.log(`Running ${suiteName} test suite...`)
142-
const vsCodeExecutablePath = await setupVSCode()
143-
const cwd = process.cwd()
144-
const testEntrypoint = resolve(cwd, relativeEntryPoint)
145-
const workspacePath = join(cwd, 'dist', 'src', 'testFixtures', 'workspaceFolder')
146-
console.log(`Starting tests: ${testEntrypoint}`)
147-
148-
const disableExtensions = await getCliArgsToDisableExtensions(vsCodeExecutablePath, {
149-
except: [
150-
VSCODE_EXTENSION_ID.python,
151-
VSCODE_EXTENSION_ID.yaml,
152-
VSCODE_EXTENSION_ID.jupyter,
153-
VSCODE_EXTENSION_ID.go,
154-
VSCODE_EXTENSION_ID.java,
155-
VSCODE_EXTENSION_ID.javadebug,
156-
VSCODE_EXTENSION_ID.git,
157-
VSCODE_EXTENSION_ID.remotessh,
158-
],
159-
})
160-
const args = {
161-
vscodeExecutablePath: vsCodeExecutablePath,
162-
extensionDevelopmentPath: cwd,
163-
extensionTestsPath: testEntrypoint,
164-
launchArgs: [...disableExtensions, workspacePath, disableWorkspaceTrust],
165-
extensionTestsEnv: {
166-
['DEVELOPMENT_PATH']: cwd,
167-
['AWS_TOOLKIT_AUTOMATION']: suiteName,
168-
},
169-
}
170-
console.log(`runTests() args:\n${JSON.stringify(args, undefined, 2)}`)
171-
const result = await runTests(args)
172-
173-
console.log(`Finished running ${suiteName} test suite with result code: ${result}`)
174-
process.exit(result)
175-
} catch (err) {
176-
console.error(err)
177-
console.error('Failed to run tests')
178-
process.exit(1)
179-
}
180-
}

scripts/test/test.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { resolve } from 'path'
7-
import { runTests } from '@vscode/test-electron'
8-
import { setupVSCodeTestInstance } from './launchTestUtilities'
6+
import { runToolkitTests } from './launchTestUtilities'
97
void (async () => {
10-
try {
11-
console.log('Running Main test suite...')
12-
13-
const vsCodeExecutablePath = await setupVSCodeTestInstance()
14-
const rootDir = process.cwd()
15-
const testEntrypoint = resolve(rootDir, 'dist/src/test/index.js')
16-
const testWorkspace = resolve(rootDir, 'src/testFixtures/workspaceFolder')
17-
console.log(`Starting tests: ${testEntrypoint}`)
18-
19-
const result = await runTests({
20-
vscodeExecutablePath: vsCodeExecutablePath,
21-
extensionDevelopmentPath: rootDir,
22-
extensionTestsPath: testEntrypoint,
23-
// For verbose VSCode logs, add "--verbose --log debug". c2165cf48e62c
24-
launchArgs: [testWorkspace],
25-
extensionTestsEnv: {
26-
['DEVELOPMENT_PATH']: rootDir,
27-
['AWS_TOOLKIT_AUTOMATION']: 'unit',
28-
},
29-
})
30-
31-
console.log(`Finished running Main test suite with result code: ${result}`)
32-
process.exit(result)
33-
} catch (err) {
34-
console.error(err)
35-
console.error('Failed to run tests')
36-
process.exit(1)
37-
}
8+
await runToolkitTests('unit', 'dist/src/test/index.js')
389
})()

scripts/test/testE2E.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { resolve } from 'path'
7-
import { e2eSuite, runToolkitTests } from './launchTestUtilities'
6+
import { runToolkitTests } from './launchTestUtilities'
87
void (async () => {
9-
await runToolkitTests(e2eSuite, resolve('dist', 'src', 'testE2E', 'index.js'))
8+
await runToolkitTests('e2e', 'dist/src/testE2E/index.js')
109
})()

scripts/test/testInteg.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { resolve } from 'path'
7-
import { integrationSuite, runToolkitTests } from './launchTestUtilities'
6+
import { runToolkitTests } from './launchTestUtilities'
87
void (async () => {
9-
await runToolkitTests(integrationSuite, resolve('dist', 'src', 'testInteg', 'index.js'))
8+
await runToolkitTests('integration', 'dist/src/testInteg/index.js')
109
})()

src/test/techdebt.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import assert from 'assert'
77
import * as semver from 'semver'
88
import * as env from '../shared/vscode/env'
9-
import { installVSCodeExtension } from '../../scripts/test/launchTestUtilities'
109

1110
// Checks project config and dependencies, to remind us to remove old things
1211
// when possible.
@@ -46,7 +45,7 @@ describe('tech debt', function () {
4645

4746
it('stop not using latest python extension version in integration CI tests', function () {
4847
/**
49-
* The explicitly set version is done in {@link installVSCodeExtension}
48+
* The explicitly set version is done in launchTestUtilities.ts#installVSCodeExtension
5049
* The parent ticket for SAM test issues: IDE-12295
5150
* Python Extension Bug Issue (if this is fixed, then this should be too): https://github.com/microsoft/vscode-python/issues/22659
5251
*/

src/testInteg/shared/extensions/git.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { realpath } from 'fs-extra'
1313
import { execFileSync } from 'child_process'
1414
import { sleep } from '../../../shared/utilities/timeoutUtils'
1515
import { getLogger } from '../../../shared/logger/logger'
16-
import { getMinVsCodeVersion } from '../../../../scripts/test/launchTestUtilities' // TODO: don't use stuff from 'scripts'
16+
import { getMinVscodeVersion } from '../../../shared/vscode/env'
1717

1818
const testRemoteName = 'test-origin'
1919
const testRemoteUrl = 'https://github.com/aws/aws-toolkit-vscode'
@@ -69,7 +69,7 @@ describe.skip('GitExtension', function () {
6969

7070
before(async function () {
7171
// extension is missing some functionality on the minimum version
72-
if (vscode.version === getMinVsCodeVersion()) {
72+
if (vscode.version === getMinVscodeVersion()) {
7373
this.test?.skip()
7474
}
7575

0 commit comments

Comments
 (0)