Skip to content

Commit 41e1055

Browse files
committed
refactor: remove scope paramter from size check
1 parent bea2fd4 commit 41e1055

File tree

2 files changed

+17
-50
lines changed

2 files changed

+17
-50
lines changed

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export const ZipConstants = {
5050
codeDiffFilePath: 'codeDiff/code.diff',
5151
}
5252

53+
type ZipType = 'file' | 'project'
54+
5355
export class ZipUtil {
5456
protected _pickedSourceFiles: Set<string> = new Set<string>()
5557
protected _pickedBuildFiles: Set<string> = new Set<string>()
@@ -61,25 +63,18 @@ export class ZipUtil {
6163
protected _fetchedDirs: Set<string> = new Set<string>()
6264
protected _language: CodewhispererLanguage | undefined
6365
protected _timestamp: string = Date.now().toString()
64-
constructor() {}
65-
66-
getFileScanPayloadSizeLimitInBytes(): number {
67-
return CodeWhispererConstants.fileScanPayloadSizeLimitBytes
68-
}
69-
70-
getProjectScanPayloadSizeLimitInBytes(): number {
71-
return CodeWhispererConstants.projectScanPayloadSizeLimitBytes
66+
protected _payloadByteLimits = {
67+
file: CodeWhispererConstants.fileScanPayloadSizeLimitBytes,
68+
project: CodeWhispererConstants.projectScanPayloadSizeLimitBytes,
7269
}
70+
constructor() {}
7371

74-
public aboveByteLimit(size: number, scope: CodeWhispererConstants.CodeAnalysisScope): boolean {
75-
return CodeWhispererConstants.isFileAnalysisScope(scope)
76-
? size > this.getFileScanPayloadSizeLimitInBytes()
77-
: size > this.getProjectScanPayloadSizeLimitInBytes()
72+
public aboveByteLimit(size: number, limitType: ZipType): boolean {
73+
return size > this._payloadByteLimits[limitType]
7874
}
7975

8076
public willReachProjectByteLimit(current: number, adding: number): boolean {
81-
const willReachLimit = current + adding > this.getProjectScanPayloadSizeLimitInBytes()
82-
return willReachLimit
77+
return this.aboveByteLimit(current + adding, 'project')
8378
}
8479

8580
protected async zipFile(uri: vscode.Uri | undefined, scope: CodeWhispererConstants.CodeAnalysisScope) {
@@ -112,7 +107,7 @@ export class ZipUtil {
112107
this._totalSize += (await fs.stat(uri.fsPath)).size
113108
this._totalLines += content.split(ZipConstants.newlineRegex).length
114109

115-
if (this.aboveByteLimit(this._totalSize, scope)) {
110+
if (this.aboveByteLimit(this._totalSize, 'file')) {
116111
throw new FileSizeExceededError()
117112
}
118113
const zipFilePath = this.getZipDirPath(FeatureUseCase.CODE_SCAN) + CodeWhispererConstants.codeScanZipExt
@@ -242,7 +237,7 @@ export class ZipUtil {
242237
)
243238
: vscode.workspace.workspaceFolders) as CurrentWsFolders,
244239
{
245-
maxTotalSizeBytes: this.getProjectScanPayloadSizeLimitInBytes(),
240+
maxTotalSizeBytes: this._payloadByteLimits['project'],
246241
excludePatterns:
247242
useCase === FeatureUseCase.TEST_GENERATION
248243
? [...CodeWhispererConstants.testGenExcludePatterns, ...defaultExcludePatterns]
@@ -305,7 +300,7 @@ export class ZipUtil {
305300
const fileSize = Buffer.from(fileContent).length
306301

307302
if (
308-
this.aboveByteLimit(this._totalSize, CodeWhispererConstants.CodeAnalysisScope.PROJECT) ||
303+
this.aboveByteLimit(this._totalSize, 'project') ||
309304
this.willReachProjectByteLimit(this._totalSize, fileSize)
310305
) {
311306
throw new ProjectSizeExceededError()
@@ -322,7 +317,7 @@ export class ZipUtil {
322317
const fileSize = (await fs.stat(uri.fsPath)).size
323318

324319
if (
325-
this.aboveByteLimit(this._totalSize, CodeWhispererConstants.CodeAnalysisScope.PROJECT) ||
320+
this.aboveByteLimit(this._totalSize, 'project') ||
326321
this.willReachProjectByteLimit(this._totalSize, fileSize)
327322
) {
328323
throw new ProjectSizeExceededError()

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

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,50 +35,22 @@ describe('zipUtil', function () {
3535

3636
it('returns the proper size limit for zip', function () {
3737
assert.strictEqual(
38-
zipUtil.aboveByteLimit(
39-
CodeWhispererConstants.fileScanPayloadSizeLimitBytes + 1,
40-
CodeAnalysisScope.FILE_ON_DEMAND
41-
),
38+
zipUtil.aboveByteLimit(CodeWhispererConstants.fileScanPayloadSizeLimitBytes + 1, 'file'),
4239
true
4340
)
4441

4542
assert.strictEqual(
46-
zipUtil.aboveByteLimit(
47-
CodeWhispererConstants.fileScanPayloadSizeLimitBytes + 1,
48-
CodeAnalysisScope.FILE_AUTO
49-
),
43+
zipUtil.aboveByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes + 1, 'project'),
5044
true
5145
)
5246

5347
assert.strictEqual(
54-
zipUtil.aboveByteLimit(
55-
CodeWhispererConstants.projectScanPayloadSizeLimitBytes + 1,
56-
CodeAnalysisScope.PROJECT
57-
),
58-
true
59-
)
60-
61-
assert.strictEqual(
62-
zipUtil.aboveByteLimit(
63-
CodeWhispererConstants.fileScanPayloadSizeLimitBytes - 1,
64-
CodeAnalysisScope.FILE_ON_DEMAND
65-
),
66-
false
67-
)
68-
69-
assert.strictEqual(
70-
zipUtil.aboveByteLimit(
71-
CodeWhispererConstants.fileScanPayloadSizeLimitBytes - 1,
72-
CodeAnalysisScope.FILE_AUTO
73-
),
48+
zipUtil.aboveByteLimit(CodeWhispererConstants.fileScanPayloadSizeLimitBytes - 1, 'file'),
7449
false
7550
)
7651

7752
assert.strictEqual(
78-
zipUtil.aboveByteLimit(
79-
CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 1,
80-
CodeAnalysisScope.PROJECT
81-
),
53+
zipUtil.aboveByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 1, 'project'),
8254
false
8355
)
8456
})

0 commit comments

Comments
 (0)