Skip to content

Commit f78cce0

Browse files
committed
Adding sourcefile to the zip before traversing the project
1 parent 6d3670a commit f78cce0

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

packages/amazonq/test/unit/codewhisperer/util/zipUtil.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('zipUtil', function () {
159159
it('should generate zip for test generation successfully', async function () {
160160
const mkdirSpy = sinon.spy(fs, 'mkdir')
161161

162-
const result = await zipUtil.generateZipTestGen(appRoot, false)
162+
const result = await zipUtil.generateZipTestGen(appRoot, appCodePath, false)
163163

164164
assert.ok(mkdirSpy.calledWith(path.join(testTempDirPath, 'utgRequiredArtifactsDir')))
165165
assert.ok(
@@ -184,7 +184,10 @@ describe('zipUtil', function () {
184184
}))
185185
sinon.stub(fs, 'mkdir').rejects(new Error('Directory creation failed'))
186186

187-
await assert.rejects(() => zipUtil.generateZipTestGen(appRoot, false), /Directory creation failed/)
187+
await assert.rejects(
188+
() => zipUtil.generateZipTestGen(appRoot, appCodePath, false),
189+
/Directory creation failed/
190+
)
188191
})
189192

190193
it('Should handle zip project errors', async function () {
@@ -193,7 +196,7 @@ describe('zipUtil', function () {
193196
}))
194197
sinon.stub(zipUtil, 'zipProject' as keyof ZipUtil).rejects(new Error('Zip failed'))
195198

196-
await assert.rejects(() => zipUtil.generateZipTestGen(appRoot, false), /Zip failed/)
199+
await assert.rejects(() => zipUtil.generateZipTestGen(appRoot, appCodePath, false), /Zip failed/)
197200
})
198201
})
199202
})

packages/core/src/codewhisperer/commands/startTestGeneration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function startTestGenerationProcess(
5858
session.projectRootPath = projectPath
5959
session.listOfTestGenerationJobId = []
6060
}
61-
const zipMetadata = await zipUtil.generateZipTestGen(session.projectRootPath, initialExecution)
61+
const zipMetadata = await zipUtil.generateZipTestGen(session.projectRootPath, filePath, initialExecution)
6262
session.srcPayloadSize = zipMetadata.buildPayloadSizeInBytes
6363
session.srcZipFileSize = zipMetadata.zipFileSizeInBytes
6464

packages/core/src/codewhisperer/util/zipUtil.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export class ZipUtil {
204204
await processDirectory(metadataDir)
205205
}
206206

207-
protected async zipProject(useCase: FeatureUseCase, projectPath?: string, metadataDir?: string) {
207+
protected async zipProject(useCase: FeatureUseCase, projectPath?: string, filePath?: string, metadataDir?: string) {
208208
const zip = new admZip()
209209
let projectPaths = []
210210
if (useCase === FeatureUseCase.TEST_GENERATION && projectPath) {
@@ -217,7 +217,13 @@ export class ZipUtil {
217217
}
218218
const languageCount = new Map<CodewhispererLanguage, number>()
219219

220-
await this.processSourceFiles(zip, languageCount, projectPaths, useCase)
220+
await this.processSourceFiles(
221+
zip,
222+
languageCount,
223+
projectPaths,
224+
useCase,
225+
useCase === FeatureUseCase.TEST_GENERATION ? filePath : undefined
226+
)
221227
if (metadataDir) {
222228
await this.processMetadataDir(zip, metadataDir)
223229
}
@@ -404,7 +410,8 @@ export class ZipUtil {
404410
zip: admZip,
405411
languageCount: Map<CodewhispererLanguage, number>,
406412
projectPaths: string[] | undefined,
407-
useCase: FeatureUseCase
413+
useCase: FeatureUseCase,
414+
filePath?: string
408415
) {
409416
if (!projectPaths || projectPaths.length === 0) {
410417
return
@@ -414,7 +421,9 @@ export class ZipUtil {
414421
projectPaths,
415422
vscode.workspace.workspaceFolders as CurrentWsFolders,
416423
true,
417-
this.getProjectScanPayloadSizeLimitInBytes()
424+
this.getProjectScanPayloadSizeLimitInBytes(),
425+
true,
426+
useCase === FeatureUseCase.TEST_GENERATION ? filePath : undefined
418427
)
419428
for (const file of sourceFiles) {
420429
const projectName = path.basename(file.workspaceFolder.uri.fsPath)
@@ -560,7 +569,11 @@ export class ZipUtil {
560569
}
561570
}
562571

563-
public async generateZipTestGen(projectPath: string, initialExecution: boolean): Promise<ZipMetadata> {
572+
public async generateZipTestGen(
573+
projectPath: string,
574+
filePath: string,
575+
initialExecution: boolean
576+
): Promise<ZipMetadata> {
564577
try {
565578
// const repoMapFile = await LspClient.instance.getRepoMapJSON()
566579
const zipDirPath = this.getZipDirPath(FeatureUseCase.TEST_GENERATION)
@@ -591,7 +604,12 @@ export class ZipUtil {
591604
}
592605
}
593606

594-
const zipFilePath: string = await this.zipProject(FeatureUseCase.TEST_GENERATION, projectPath, metadataDir)
607+
const zipFilePath: string = await this.zipProject(
608+
FeatureUseCase.TEST_GENERATION,
609+
projectPath,
610+
filePath,
611+
metadataDir
612+
)
595613
const zipFileSize = (await fs.stat(zipFilePath)).size
596614
return {
597615
rootDir: zipDirPath,

packages/core/src/shared/utilities/workspaceUtils.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ export async function collectFiles(
326326
workspaceFolders: CurrentWsFolders,
327327
respectGitIgnore: boolean = true,
328328
maxSize = 200 * 1024 * 1024, // 200 MB
329-
defaultExcludePatterns: boolean = true
329+
defaultExcludePatterns: boolean = true,
330+
filePath?: string
330331
): Promise<
331332
{
332333
workspaceFolder: vscode.WorkspaceFolder
@@ -337,6 +338,7 @@ export async function collectFiles(
337338
}[]
338339
> {
339340
const storage: Awaited<ReturnType<typeof collectFiles>> = []
341+
let totalSizeBytes = 0
340342

341343
const workspaceFoldersMapping = getWorkspaceFoldersByPrefixes(workspaceFolders)
342344
const workspaceToPrefix = new Map<vscode.WorkspaceFolder, string>(
@@ -359,7 +361,28 @@ export async function collectFiles(
359361
return prefix === '' ? path : `${prefix}/${path}`
360362
}
361363

362-
let totalSizeBytes = 0
364+
// Add target file first to the collect files if provided
365+
if (filePath) {
366+
const targetUri = vscode.Uri.file(filePath)
367+
const relativePath = getWorkspaceRelativePath(filePath, { workspaceFolders })
368+
369+
if (relativePath) {
370+
const fileStat = await fs.stat(targetUri)
371+
372+
const fileContent = await readFile(targetUri)
373+
if (fileContent !== undefined) {
374+
totalSizeBytes += fileStat.size
375+
storage.push({
376+
workspaceFolder: relativePath.workspaceFolder,
377+
relativeFilePath: relativePath.relativePath,
378+
fileUri: targetUri,
379+
fileContent: fileContent,
380+
zipFilePath: prefixWithFolderPrefix(relativePath.workspaceFolder, relativePath.relativePath),
381+
})
382+
}
383+
}
384+
}
385+
363386
for (const rootPath of sourcePaths) {
364387
const allFiles = await vscode.workspace.findFiles(
365388
new vscode.RelativePattern(rootPath, '**'),
@@ -371,6 +394,11 @@ export async function collectFiles(
371394
: allFiles
372395

373396
for (const file of files) {
397+
// Skip if this file matches the target file that was already processed
398+
if (filePath && file.fsPath === filePath) {
399+
continue
400+
}
401+
374402
const relativePath = getWorkspaceRelativePath(file.fsPath, { workspaceFolders })
375403
if (!relativePath) {
376404
continue

0 commit comments

Comments
 (0)