Skip to content

Commit ef3623a

Browse files
committed
refactor: rename generateZip
1 parent 4b48a97 commit ef3623a

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

packages/core/src/amazonq/util/files.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,7 @@ export function registerNewFiles(
299299

300300
return result
301301
}
302+
303+
export function isFileOpenAndDirty(uri: vscode.Uri) {
304+
return vscode.workspace.textDocuments.some((document) => document.uri.fsPath === uri.fsPath && document.isDirty)
305+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export async function startSecurityScan(
157157
})
158158
}
159159
const zipType = scope === CodeAnalysisScope.PROJECT ? 'project' : 'file'
160-
const zipMetadata = await zipUtil.generateZip(editor?.document.uri, zipType)
160+
const zipMetadata = await zipUtil.generateZipCodeScan(editor?.document.uri, zipType)
161161
const projectPaths = getWorkspacePaths()
162162

163163
const contextTruncationStartTime = performance.now()

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ZipStream } from '../../shared/utilities/zipStream'
2828
import { getTextContent } from '../../shared/utilities/textDocumentUtilities'
2929
import { ChildProcess, ChildProcessOptions } from '../../shared/utilities/processUtils'
3030
import { removeAnsi } from '../../shared/utilities/textUtilities'
31+
import { isFileOpenAndDirty } from '../../amazonq/util/files'
3132

3233
export interface ZipMetadata {
3334
rootDir: string
@@ -245,8 +246,8 @@ export class ZipUtil {
245246
if (ZipConstants.knownBinaryFileExts.includes(path.extname(file.fileUri.fsPath)) && includeBinary) {
246247
await this.processBinaryFile(zip, file.fileUri, zipEntryPath)
247248
} else {
248-
const isFileOpenAndDirty = this.isFileOpenAndDirty(file.fileUri)
249-
const fileContent = isFileOpenAndDirty ? await getTextContent(file.fileUri) : file.fileContent
249+
const hasUnsavedChanges = isFileOpenAndDirty(file.fileUri)
250+
const fileContent = hasUnsavedChanges ? await getTextContent(file.fileUri) : file.fileContent
250251
this.processTextFile(zip, file.fileUri, fileContent, languageCount, zipEntryPath)
251252
}
252253
}
@@ -327,18 +328,14 @@ export class ZipUtil {
327328
}
328329
}
329330

330-
protected isFileOpenAndDirty(uri: vscode.Uri) {
331-
return vscode.workspace.textDocuments.some((document) => document.uri.fsPath === uri.fsPath && document.isDirty)
332-
}
333-
334331
public getZipDirPath(): string {
335332
if (this._zipDir === '') {
336333
this._zipDir = path.join(this._tmpDir, `${this._zipDirPrefix}_${this._timestamp}`)
337334
}
338335
return this._zipDir
339336
}
340337

341-
public async generateZip(
338+
public async generateZipCodeScan(
342339
uri: vscode.Uri | undefined,
343340
zipType: ZipType,
344341
options?: GenerateZipOptions

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('zipUtil', function () {
5858
)
5959
})
6060

61-
describe('generateZip', function () {
61+
describe('generateZipCodeScan', function () {
6262
let zipUtil: ZipUtil
6363
beforeEach(function () {
6464
zipUtil = new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix)
@@ -68,7 +68,7 @@ describe('zipUtil', function () {
6868
})
6969

7070
it('Should generate zip for file scan and return expected metadata', async function () {
71-
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), 'file', {
71+
const zipMetadata = await zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'file', {
7272
includeGitDiff: true,
7373
})
7474
assert.strictEqual(zipMetadata.lines, 49)
@@ -84,13 +84,13 @@ describe('zipUtil', function () {
8484
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
8585

8686
await assert.rejects(
87-
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), 'file', { includeGitDiff: true }),
87+
() => zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'file', { includeGitDiff: true }),
8888
new ToolkitError(`Payload size limit reached`, { code: 'FileSizeExceeded' })
8989
)
9090
})
9191

9292
it('Should generate zip for project scan and return expected metadata', async function () {
93-
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project')
93+
const zipMetadata = await zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project')
9494
assert.ok(zipMetadata.lines > 0)
9595
assert.ok(zipMetadata.rootDir.includes(codeScanTruncDirPrefix))
9696
assert.ok(zipMetadata.srcPayloadSizeInBytes > 0)
@@ -104,7 +104,7 @@ describe('zipUtil', function () {
104104
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
105105

106106
await assert.rejects(
107-
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project'),
107+
() => zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project'),
108108
new ToolkitError('Payload size limit reached', { code: 'ProjectSizeExceeded' })
109109
)
110110
})
@@ -113,29 +113,32 @@ describe('zipUtil', function () {
113113
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
114114

115115
await assert.rejects(
116-
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project'),
116+
() => zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project'),
117117
new ToolkitError('Payload size limit reached', { code: 'ProjectSizeExceeded' })
118118
)
119119
})
120120

121121
it('Should read file content instead of from disk if file is dirty', async function () {
122-
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project')
122+
const zipMetadata = await zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project')
123123

124124
const document = await vscode.workspace.openTextDocument(appCodePath)
125125
await vscode.window.showTextDocument(document)
126126
void vscode.window.activeTextEditor?.edit((editBuilder) => {
127127
editBuilder.insert(new vscode.Position(0, 0), '// a comment\n')
128128
})
129129

130-
const zipMetadata2 = await new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix).generateZip(
130+
const zipMetadata2 = await new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix).generateZipCodeScan(
131131
vscode.Uri.file(appCodePath),
132132
'project'
133133
)
134134
assert.equal(zipMetadata2.lines, zipMetadata.lines + 1)
135135
})
136136

137137
it('should handle path with repeated project name for file scan', async function () {
138-
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePathWithRepeatedProjectName), 'file')
138+
const zipMetadata = await zipUtil.generateZipCodeScan(
139+
vscode.Uri.file(appCodePathWithRepeatedProjectName),
140+
'file'
141+
)
139142

140143
const zipFileData = await fs.readFileBytes(zipMetadata.zipFilePath)
141144
const zip = await JSZip.loadAsync(zipFileData)
@@ -144,7 +147,7 @@ describe('zipUtil', function () {
144147
})
145148

146149
it('should handle path with repeated project name for project scan', async function () {
147-
const zipMetadata = await zipUtil.generateZip(
150+
const zipMetadata = await zipUtil.generateZipCodeScan(
148151
vscode.Uri.file(appCodePathWithRepeatedProjectName),
149152
'project'
150153
)

packages/core/src/testE2E/codewhisperer/securityScan.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('CodeWhisperer security scan', async function () {
9595

9696
const projectPaths = getWorkspacePaths()
9797
const scope = CodeWhispererConstants.CodeAnalysisScope.PROJECT
98-
const zipMetadata = await zipUtil.generateZip(uri, 'project')
98+
const zipMetadata = await zipUtil.generateZipCodeScan(uri, 'project')
9999
const codeScanName = randomUUID()
100100

101101
let artifactMap

packages/core/src/testInteg/perf/startSecurityScan.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('startSecurityScanPerformanceTest', function () {
102102
assert.ok(setup.commandSpy.neverCalledWith('workbench.action.problems.focus'))
103103
assert.ok(setup.securityScanRenderSpy.calledOnce)
104104

105-
assert.ok(setup.zipSpy.generateZip.calledOnce)
105+
assert.ok(setup.zipSpy.generateZipCodeScan.calledOnce)
106106
assert.ok(setup.zipSpy.removeTmpFiles.calledOnce)
107107
assert.ok(
108108
getFsCallsUpperBound(setup.fsSpy) <= 5,

0 commit comments

Comments
 (0)