Skip to content

Commit 87346e0

Browse files
committed
"Added Stub to tests to make them work"
1 parent 0827fed commit 87346e0

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

packages/core/src/codewhisperer/service/securityScanHandler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function listScanResults(
8585
const document = await vscode.workspace.openTextDocument(filePath)
8686
const aggregatedCodeScanIssue: AggregatedCodeScanIssue = {
8787
filePath: filePath,
88-
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, jobId, scope, document)),
88+
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, document, jobId, scope)),
8989
}
9090
aggregatedCodeScanIssueList.push(aggregatedCodeScanIssue)
9191
}
@@ -95,7 +95,7 @@ export async function listScanResults(
9595
const document = await vscode.workspace.openTextDocument(maybeAbsolutePath)
9696
const aggregatedCodeScanIssue: AggregatedCodeScanIssue = {
9797
filePath: maybeAbsolutePath,
98-
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, jobId, scope, document)),
98+
issues: issues.map((issue) => mapRawToCodeScanIssue(issue, document, jobId, scope)),
9999
}
100100
aggregatedCodeScanIssueList.push(aggregatedCodeScanIssue)
101101
}
@@ -105,9 +105,9 @@ export async function listScanResults(
105105

106106
function mapRawToCodeScanIssue(
107107
issue: RawCodeScanIssue,
108+
document: vscode.TextDocument,
108109
jobId: string,
109-
scope: CodeWhispererConstants.CodeAnalysisScope,
110-
document: vscode.TextDocument
110+
scope: CodeWhispererConstants.CodeAnalysisScope
111111
): CodeScanIssue {
112112
const isIssueTitleIgnored = CodeWhispererSettings.instance.getIgnoredSecurityIssues().includes(issue.title)
113113
const isSingleIssueIgnored = detectCommentAboveLine(
Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { PromiseResult } from 'aws-sdk/lib/request'
7-
import { Stub, stub } from 'aws-core-vscode/test'
6+
import { PromiseResult, Request } from 'aws-sdk/lib/request'
7+
import { createMockDocument } from './testUtil'
8+
import { Stub, stub } from '../utilities/stubber'
89
import { AWSError, HttpResponse } from 'aws-sdk'
910
import {
1011
CodeAnalysisScope,
@@ -15,12 +16,13 @@ import {
1516
ListCodeScanFindingsResponse,
1617
pollScanJobStatus,
1718
SecurityScanTimedOutError,
18-
} from 'aws-core-vscode/codewhisperer'
19-
import { timeoutUtils } from 'aws-core-vscode/shared'
19+
} from '../../codewhisperer'
20+
import { timeoutUtils } from '../../shared'
2021
import assert from 'assert'
21-
import sinon from 'sinon'
22+
import * as sinon from 'sinon'
2223
import * as vscode from 'vscode'
2324
import fs from 'fs' // eslint-disable-line no-restricted-imports
25+
import { GetCodeScanResponse } from '../../codewhisperer/client/codewhispererclient'
2426

2527
const buildRawCodeScanIssue = (params?: Partial<RawCodeScanIssue>): RawCodeScanIssue => ({
2628
filePath: 'workspaceFolder/python3.7-plain-sam-app/hello_world/app.py',
@@ -72,6 +74,8 @@ describe('securityScanHandler', function () {
7274
mockClient = stub(DefaultCodeWhispererClient)
7375
sinon.stub(fs, 'existsSync').returns(true)
7476
sinon.stub(fs, 'statSync').returns({ isFile: () => true } as fs.Stats)
77+
const textDocumentMock = createMockDocument('first line\n second line\n fourth line')
78+
sinon.stub(vscode.workspace, 'openTextDocument').resolves(textDocumentMock)
7579
})
7680

7781
afterEach(function () {
@@ -290,16 +294,64 @@ describe('securityScanHandler', function () {
290294
it('should return status when scan completes successfully', async function () {
291295
mockClient.getCodeScan
292296
.onFirstCall()
293-
.resolves({ status: 'Pending', $response: { requestId: 'req1' } })
297+
.resolves({
298+
status: 'Pending',
299+
$response: {
300+
requestId: 'req1',
301+
hasNextPage: function (): boolean {
302+
throw new Error('Function not implemented.')
303+
},
304+
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
305+
throw new Error('Function not implemented.')
306+
},
307+
data: undefined,
308+
error: undefined,
309+
redirectCount: 0,
310+
retryCount: 0,
311+
httpResponse: new HttpResponse(),
312+
},
313+
})
294314
.onSecondCall()
295-
.resolves({ status: 'Completed', $response: { requestId: 'req2' } })
315+
.resolves({
316+
status: 'Completed',
317+
$response: {
318+
requestId: 'req2',
319+
hasNextPage: function (): boolean {
320+
throw new Error('Function not implemented.')
321+
},
322+
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
323+
throw new Error('Function not implemented.')
324+
},
325+
data: undefined,
326+
error: undefined,
327+
redirectCount: 0,
328+
retryCount: 0,
329+
httpResponse: new HttpResponse(),
330+
},
331+
})
296332

297333
const result = await pollScanJobStatus(mockClient, mockJobId, CodeAnalysisScope.FILE_AUTO, mockStartTime)
298334
assert.strictEqual(result, 'Completed')
299335
})
300336

301337
it('should throw SecurityScanTimedOutError when polling exceeds timeout for express scans', async function () {
302-
mockClient.getCodeScan.resolves({ status: 'Pending', $response: { requestId: 'req1' } })
338+
mockClient.getCodeScan.resolves({
339+
status: 'Pending',
340+
$response: {
341+
requestId: 'req1',
342+
hasNextPage: function (): boolean {
343+
throw new Error('Function not implemented.')
344+
},
345+
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
346+
throw new Error('Function not implemented.')
347+
},
348+
data: undefined,
349+
error: undefined,
350+
redirectCount: 0,
351+
retryCount: 0,
352+
httpResponse: new HttpResponse(),
353+
},
354+
})
303355

304356
const pollPromise = pollScanJobStatus(mockClient, mockJobId, CodeAnalysisScope.FILE_AUTO, mockStartTime)
305357

@@ -310,7 +362,23 @@ describe('securityScanHandler', function () {
310362
})
311363

312364
it('should throw SecurityScanTimedOutError when polling exceeds timeout for standard scans', async function () {
313-
mockClient.getCodeScan.resolves({ status: 'Pending', $response: { requestId: 'req1' } })
365+
mockClient.getCodeScan.resolves({
366+
status: 'Pending',
367+
$response: {
368+
requestId: 'req1',
369+
hasNextPage: function (): boolean {
370+
throw new Error('Function not implemented.')
371+
},
372+
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
373+
throw new Error('Function not implemented.')
374+
},
375+
data: undefined,
376+
error: undefined,
377+
redirectCount: 0,
378+
retryCount: 0,
379+
httpResponse: new HttpResponse(),
380+
},
381+
})
314382

315383
const pollPromise = pollScanJobStatus(mockClient, mockJobId, CodeAnalysisScope.PROJECT, mockStartTime)
316384

0 commit comments

Comments
 (0)