Skip to content

Commit a28c669

Browse files
committed
test: refactor tests to avoid mocks
1 parent bc9bd8b commit a28c669

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { getTextContent } from '../../shared/utilities/textDocumentUtilities'
2323
import { ChildProcess, ChildProcessOptions } from '../../shared/utilities/processUtils'
2424
import { removeAnsi } from '../../shared/utilities/textUtilities'
2525
import { isFileOpenAndDirty } from '../../shared/utilities/vsCodeUtils'
26+
import { ToolkitError } from '../../shared/errors'
2627

2728
export interface ZipMetadata {
2829
rootDir: string
@@ -51,6 +52,7 @@ export const ZipConstants = {
5152
}
5253

5354
type ZipType = 'file' | 'project'
55+
type PayloadLimits = Record<ZipType, number>
5456

5557
export class ZipUtil {
5658
protected _pickedSourceFiles: Set<string> = new Set<string>()
@@ -63,18 +65,23 @@ export class ZipUtil {
6365
protected _fetchedDirs: Set<string> = new Set<string>()
6466
protected _language: CodewhispererLanguage | undefined
6567
protected _timestamp: string = Date.now().toString()
66-
protected static _payloadByteLimits = {
67-
file: CodeWhispererConstants.fileScanPayloadSizeLimitBytes,
68-
project: CodeWhispererConstants.projectScanPayloadSizeLimitBytes,
68+
protected _payloadByteLimits: PayloadLimits
69+
constructor(
70+
protected _zipDirPrefix: string,
71+
payloadLimits?: PayloadLimits
72+
) {
73+
this._payloadByteLimits = payloadLimits ?? {
74+
file: CodeWhispererConstants.fileScanPayloadSizeLimitBytes,
75+
project: CodeWhispererConstants.projectScanPayloadSizeLimitBytes,
76+
}
6977
}
70-
constructor(protected _zipDirPrefix: string) {}
7178

72-
public static aboveByteLimit(size: number, limitType: ZipType): boolean {
79+
public aboveByteLimit(size: number, limitType: ZipType): boolean {
7380
return size > this._payloadByteLimits[limitType]
7481
}
7582

76-
public static willReachProjectByteLimit(current: number, adding: number): boolean {
77-
return ZipUtil.aboveByteLimit(current + adding, 'project')
83+
public willReachProjectByteLimit(current: number, adding: number): boolean {
84+
return this.aboveByteLimit(current + adding, 'project')
7885
}
7986

8087
public async zipFile(uri: vscode.Uri | undefined, includeGitDiffHeader?: boolean) {
@@ -107,7 +114,7 @@ export class ZipUtil {
107114
this._totalSize += (await fs.stat(uri.fsPath)).size
108115
this._totalLines += content.split(ZipConstants.newlineRegex).length
109116

110-
if (ZipUtil.aboveByteLimit(this._totalSize, 'file')) {
117+
if (this.aboveByteLimit(this._totalSize, 'file')) {
111118
throw new FileSizeExceededError()
112119
}
113120
const zipDirPath = this.getZipDirPath()
@@ -226,10 +233,15 @@ export class ZipUtil {
226233
if (!projectPaths || projectPaths.length === 0) {
227234
return
228235
}
229-
230236
const sourceFiles = await collectFiles(projectPaths, workspaceFolders, {
231-
maxTotalSizeBytes: ZipUtil._payloadByteLimits['project'],
237+
maxTotalSizeBytes: this._payloadByteLimits['project'],
232238
excludePatterns,
239+
}).catch((e) => {
240+
// Check if project is too large to collect.
241+
if (e instanceof ToolkitError && e.code === 'ContentLengthError') {
242+
throw new ProjectSizeExceededError()
243+
}
244+
throw e
233245
})
234246
for (const file of sourceFiles) {
235247
const projectName = path.basename(file.workspaceFolder.uri.fsPath)
@@ -263,8 +275,8 @@ export class ZipUtil {
263275
const fileSize = Buffer.from(fileContent).length
264276

265277
if (
266-
ZipUtil.aboveByteLimit(this._totalSize, 'project') ||
267-
ZipUtil.willReachProjectByteLimit(this._totalSize, fileSize)
278+
this.aboveByteLimit(this._totalSize, 'project') ||
279+
this.willReachProjectByteLimit(this._totalSize, fileSize)
268280
) {
269281
throw new ProjectSizeExceededError()
270282
}
@@ -280,8 +292,8 @@ export class ZipUtil {
280292
const fileSize = (await fs.stat(uri.fsPath)).size
281293

282294
if (
283-
ZipUtil.aboveByteLimit(this._totalSize, 'project') ||
284-
ZipUtil.willReachProjectByteLimit(this._totalSize, fileSize)
295+
this.aboveByteLimit(this._totalSize, 'project') ||
296+
this.willReachProjectByteLimit(this._totalSize, fileSize)
285297
) {
286298
throw new ProjectSizeExceededError()
287299
}

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import assert from 'assert'
77
import vscode from 'vscode'
88
import path from 'path'
9-
import sinon from 'sinon'
109
import { join } from 'path'
1110
import JSZip from 'jszip'
1211
import { getTestWorkspaceFolder } from '../../testInteg/integrationTestsUtilities'
@@ -30,40 +29,36 @@ describe('zipUtil', function () {
3029
zipUtil = new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix)
3130
})
3231

33-
afterEach(function () {
34-
sinon.restore()
35-
})
36-
37-
it('returns the proper size limit for zip', function () {
32+
it('defaults to proper size limits for zip', function () {
3833
assert.strictEqual(
39-
ZipUtil.aboveByteLimit(CodeWhispererConstants.fileScanPayloadSizeLimitBytes + 1, 'file'),
34+
zipUtil.aboveByteLimit(CodeWhispererConstants.fileScanPayloadSizeLimitBytes + 1, 'file'),
4035
true
4136
)
4237

4338
assert.strictEqual(
44-
ZipUtil.aboveByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes + 1, 'project'),
39+
zipUtil.aboveByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes + 1, 'project'),
4540
true
4641
)
4742

4843
assert.strictEqual(
49-
ZipUtil.aboveByteLimit(CodeWhispererConstants.fileScanPayloadSizeLimitBytes - 1, 'file'),
44+
zipUtil.aboveByteLimit(CodeWhispererConstants.fileScanPayloadSizeLimitBytes - 1, 'file'),
5045
false
5146
)
5247

5348
assert.strictEqual(
54-
ZipUtil.aboveByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 1, 'project'),
49+
zipUtil.aboveByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 1, 'project'),
5550
false
5651
)
5752
})
5853

5954
it('determines if adding file will exceed project byte limit', function () {
6055
assert.strictEqual(
61-
ZipUtil.willReachProjectByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes, 1),
56+
zipUtil.willReachProjectByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes, 1),
6257
true
6358
)
6459

6560
assert.strictEqual(
66-
ZipUtil.willReachProjectByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 10, 9),
61+
zipUtil.willReachProjectByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 10, 9),
6762
false
6863
)
6964
})
@@ -94,33 +89,44 @@ describe('zipUtil', function () {
9489
})
9590

9691
it('throws error if payload size limit is reached for file', async function () {
97-
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
98-
92+
const limitedZipUtil = new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix, {
93+
file: 1,
94+
project: 1,
95+
})
9996
await assert.rejects(
100-
() => zipUtil.zipFile(vscode.Uri.file(appCodePath), true),
97+
() => limitedZipUtil.zipFile(vscode.Uri.file(appCodePath), true),
10198
new ToolkitError(`Payload size limit reached`, { code: 'FileSizeExceeded' })
10299
)
103100
})
104101

105102
it('throws error if payload size limit is reached for project', async function () {
106-
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
107-
103+
const testWorkspace = await TestFolder.create()
104+
await testWorkspace.write('afile.py', '12345')
105+
const limitedZipUtil = new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix, {
106+
file: 1,
107+
project: 4,
108+
})
108109
await assert.rejects(
109110
() =>
110-
zipUtil.zipProject(
111-
[...(vscode.workspace.workspaceFolders ?? [])] as CurrentWsFolders,
112-
defaultExcludePatterns
111+
limitedZipUtil.zipProject(
112+
[{ uri: vscode.Uri.parse(testWorkspace.path), name: 'testWorkspace', index: 0 }],
113+
defaultExcludePatterns,
114+
{
115+
projectPath: testWorkspace.path,
116+
}
113117
),
114118
new ToolkitError('Payload size limit reached', { code: 'ProjectSizeExceeded' })
115119
)
116120
})
117121

118122
it('throws error if payload size limit will be reached for project', async function () {
119-
sinon.stub(ZipUtil, 'willReachProjectByteLimit').returns(true)
120-
123+
const limitedZipUtil = new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix, {
124+
file: 1,
125+
project: 1,
126+
})
121127
await assert.rejects(
122128
() =>
123-
zipUtil.zipProject(
129+
limitedZipUtil.zipProject(
124130
[...(vscode.workspace.workspaceFolders ?? [])] as CurrentWsFolders,
125131
defaultExcludePatterns
126132
),
@@ -255,10 +261,7 @@ describe('zipUtil', function () {
255261
async () =>
256262
await zipUtil.zipProject(
257263
[{ uri: vscode.Uri.file(project.path), name: 'project1', index: 0 }] as CurrentWsFolders,
258-
defaultExcludePatterns,
259-
{
260-
includeNonWorkspaceFiles: true,
261-
}
264+
defaultExcludePatterns
262265
),
263266
new NoSourceFilesError()
264267
)

0 commit comments

Comments
 (0)