Skip to content

Commit d9b9861

Browse files
authored
test(amazonq): reset state after each testgen e2e test (#6522)
## Problem 1. The /test command can occasionally create test files in directories unrelated to the source file location (this is a known science issue that will be addressed) 2. Unit tests generated after the "Clicks on accept" test may possibly accumulate in the CI since testFixtures isn't reset for each build. This can lead to bloated test environments as these tests should be isolated and start with a clean state. ## Solution 1. Add a dedicated testgen folder within testFixtures to isolate from other files. This prevents cross-contamination (ex: python tests appearing in java directories) 2. Clean up generated unit tests after "Clicks on accept" test --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 0fa1b5b commit d9b9861

File tree

7 files changed

+51
-16
lines changed

7 files changed

+51
-16
lines changed

packages/amazonq/test/e2e/amazonq/testGen.test.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import { qTestingFramework } from './framework/framework'
99
import sinon from 'sinon'
1010
import { Messenger } from './framework/messenger'
1111
import { FollowUpTypes } from 'aws-core-vscode/amazonq'
12-
import { registerAuthHook, using, TestFolder, closeAllEditors } from 'aws-core-vscode/test'
12+
import { registerAuthHook, using, TestFolder, closeAllEditors, getTestWorkspaceFolder } from 'aws-core-vscode/test'
1313
import { loginToIdC } from './utils/setup'
1414
import { waitUntil, workspaceUtils } from 'aws-core-vscode/shared'
15+
import * as path from 'path'
1516

1617
describe('Amazon Q Test Generation', function () {
1718
let framework: qTestingFramework
@@ -20,11 +21,13 @@ describe('Amazon Q Test Generation', function () {
2021
const testFiles = [
2122
{
2223
language: 'python',
23-
filePath: 'python3.7-image-sam-app/hello_world/app.py',
24+
filePath: 'testGenFolder/src/main/math.py',
25+
testFilePath: 'testGenFolder/src/test/test_math.py',
2426
},
2527
{
2628
language: 'java',
27-
filePath: 'java17-gradle/HelloWorldFunction/src/main/java/helloworld/App.java',
29+
filePath: 'testGenFolder/src/main/Math.java',
30+
testFilePath: 'testGenFolder/src/test/MathTest.java',
2831
},
2932
]
3033

@@ -33,14 +36,15 @@ describe('Amazon Q Test Generation', function () {
3336
// must be atleast one unsupported language here for testing
3437
{
3538
language: 'typescript',
36-
filePath: 'ts-plain-sam-app/src/app.ts',
39+
filePath: 'testGenFolder/src/main/math.ts',
3740
},
3841
{
3942
language: 'javascript',
40-
filePath: 'js-plain-sam-app/src/app.js',
43+
filePath: 'testGenFolder/src/main/math.js',
4144
},
4245
]
4346

47+
// handles opening the file since /test must be called on an active file
4448
async function setupTestDocument(filePath: string, language: string) {
4549
const document = await waitUntil(async () => {
4650
const doc = await workspaceUtils.openTextDocument(filePath)
@@ -57,7 +61,7 @@ describe('Amazon Q Test Generation', function () {
5761

5862
const activeEditor = vscode.window.activeTextEditor
5963
if (!activeEditor || activeEditor.document.uri.fsPath !== document.uri.fsPath) {
60-
assert.fail(`Failed to make temp file active`)
64+
assert.fail(`Failed to make ${language} file active`)
6165
}
6266
}
6367

@@ -68,6 +72,15 @@ describe('Amazon Q Test Generation', function () {
6872
})
6973
}
7074

75+
// clears test file to a blank file
76+
// not cleaning up test file may possibly cause bloat in CI since testFixtures does not get reset
77+
async function cleanupTestFile(testFilePath: string) {
78+
const workspaceFolder = getTestWorkspaceFolder()
79+
const absoluteTestFilePath = path.join(workspaceFolder, testFilePath)
80+
const testFileUri = vscode.Uri.file(absoluteTestFilePath)
81+
await vscode.workspace.fs.writeFile(testFileUri, Buffer.from('', 'utf-8'))
82+
}
83+
7184
before(async function () {
7285
await using(registerAuthHook('amazonq-test-account'), async () => {
7386
await loginToIdC()
@@ -112,7 +125,7 @@ describe('Amazon Q Test Generation', function () {
112125
})
113126

114127
describe('/test entry', () => {
115-
describe('Unsupported language', () => {
128+
describe('Unsupported language file', () => {
116129
const { language, filePath } = unsupportedLanguages[0]
117130

118131
beforeEach(async () => {
@@ -134,13 +147,13 @@ describe('Amazon Q Test Generation', function () {
134147
})
135148
})
136149

137-
describe('External file', async () => {
150+
describe('External file out of project', async () => {
138151
let testFolder: TestFolder
139152
let fileName: string
140153

141154
beforeEach(async () => {
142155
testFolder = await TestFolder.create()
143-
fileName = 'test.py'
156+
fileName = 'math.py'
144157
const filePath = await testFolder.write(fileName, 'def add(a, b): return a + b')
145158

146159
const document = await vscode.workspace.openTextDocument(filePath)
@@ -162,10 +175,8 @@ describe('Amazon Q Test Generation', function () {
162175
})
163176
})
164177

165-
for (const { language, filePath } of testFiles) {
166-
// skipping for now since this test is flaky. passes locally, but only half the time in CI
167-
// have tried retries for setupTestDocument, openTextDocument, and showTextDocument
168-
describe.skip(`${language} file`, () => {
178+
for (const { language, filePath, testFilePath } of testFiles) {
179+
describe(`/test on ${language} file`, () => {
169180
beforeEach(async () => {
170181
await waitUntil(async () => await setupTestDocument(filePath, language), {})
171182

@@ -177,7 +188,7 @@ describe('Amazon Q Test Generation', function () {
177188
await tab.waitForChatFinishesLoading()
178189
})
179190

180-
describe('View diff', async () => {
191+
describe('View diff of test file', async () => {
181192
it('Clicks on view diff', async () => {
182193
const chatItems = tab.getChatItems()
183194
const viewDiffMessage = chatItems[5]
@@ -190,7 +201,14 @@ describe('Amazon Q Test Generation', function () {
190201
})
191202
})
192203

193-
describe('Accept code', async () => {
204+
describe('Accept unit tests', async () => {
205+
afterEach(async () => {
206+
// this e2e test generates unit tests, so we want to clean them up after this test is done
207+
await waitUntil(async () => {
208+
await cleanupTestFile(testFilePath)
209+
}, {})
210+
})
211+
194212
it('Clicks on accept', async () => {
195213
await tab.waitForButtons([FollowUpTypes.AcceptCode, FollowUpTypes.RejectCode])
196214
tab.clickButton(FollowUpTypes.AcceptCode)
@@ -204,7 +222,7 @@ describe('Amazon Q Test Generation', function () {
204222
})
205223
})
206224

207-
describe('Reject code', async () => {
225+
describe('Reject unit tests', async () => {
208226
it('Clicks on reject', async () => {
209227
await tab.waitForButtons([FollowUpTypes.AcceptCode, FollowUpTypes.RejectCode])
210228
tab.clickButton(FollowUpTypes.RejectCode)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Math functions
3+
*/
4+
public class Math {
5+
public int add(int a, int b) {
6+
return a + b;
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function addNum(num1, num2) {
2+
return num1 + num2;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# adds two numbers
2+
def add(a, b):
3+
return a + b
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function addTwoNums(a: number, b: number): number {
2+
return a + b;
3+
}

packages/core/src/testFixtures/workspaceFolder/testGenFolder/src/test/MathTest.java

Whitespace-only changes.

packages/core/src/testFixtures/workspaceFolder/testGenFolder/src/test/test_math.py

Whitespace-only changes.

0 commit comments

Comments
 (0)