From 7216fb9e1eba1786e72696b6b9dfaf6c10d9b929 Mon Sep 17 00:00:00 2001 From: Jason Luong Date: Tue, 21 Nov 2023 15:46:04 +0000 Subject: [PATCH] chore: add test for OsscodeActionsProvider --- src/snyk/snykOss/ossService.ts | 2 +- .../providers/ossCodeActionsProvider.ts | 4 +- .../providers/ossCodeActionsProvider.test.ts | 118 ++++++++++++++++++ 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/test/unit/snykOss/providers/ossCodeActionsProvider.test.ts diff --git a/src/snyk/snykOss/ossService.ts b/src/snyk/snykOss/ossService.ts index 78c5c8916..b0394567f 100644 --- a/src/snyk/snykOss/ossService.ts +++ b/src/snyk/snykOss/ossService.ts @@ -42,7 +42,7 @@ export class OssService extends ProductService { ); this.registerCodeActionsProvider( - new OssCodeActionsProvider(this.result, codeActionAdapter, codeActionKindAdapter, languages, analytics), + new OssCodeActionsProvider(languages, codeActionAdapter, codeActionKindAdapter, this.result, analytics), ); } diff --git a/src/snyk/snykOss/providers/ossCodeActionsProvider.ts b/src/snyk/snykOss/providers/ossCodeActionsProvider.ts index 89a3fe4f3..0ed6d9632 100644 --- a/src/snyk/snykOss/providers/ossCodeActionsProvider.ts +++ b/src/snyk/snykOss/providers/ossCodeActionsProvider.ts @@ -13,10 +13,10 @@ import { DIAGNOSTICS_OSS_COLLECTION_NAME_LS } from '../../snykCode/constants/ana export class OssCodeActionsProvider extends CodeActionsProvider { constructor( - issues: Readonly>, + private readonly languages: IVSCodeLanguages, private readonly codeActionAdapter: ICodeActionAdapter, codeActionKindAdapter: ICodeActionKindAdapter, - private readonly languages: IVSCodeLanguages, + issues: Readonly>, analytics: IAnalytics, ) { super(issues, codeActionKindAdapter, analytics); diff --git a/src/test/unit/snykOss/providers/ossCodeActionsProvider.test.ts b/src/test/unit/snykOss/providers/ossCodeActionsProvider.test.ts new file mode 100644 index 000000000..2d351e8f7 --- /dev/null +++ b/src/test/unit/snykOss/providers/ossCodeActionsProvider.test.ts @@ -0,0 +1,118 @@ +import assert from 'assert'; +import sinon from 'sinon'; +import { CodeAction, CodeActionContext, CodeActionKind, Range, TextDocument } from 'vscode'; +import { IAnalytics } from '../../../../snyk/common/analytics/itly'; +import { OpenCommandIssueType, OpenIssueCommandArg } from '../../../../snyk/common/commands/types'; +import { SNYK_OPEN_ISSUE_COMMAND } from '../../../../snyk/common/constants/commands'; +import { Issue, IssueSeverity, OssIssueData } from '../../../../snyk/common/languageServer/types'; +import { WorkspaceFolderResult } from '../../../../snyk/common/services/productService'; +import { ICodeActionAdapter, ICodeActionKindAdapter } from '../../../../snyk/common/vscode/codeAction'; +import { IVSCodeLanguages } from '../../../../snyk/common/vscode/languages'; +import { OssCodeActionsProvider } from '../../../../snyk/snykOss/providers/ossCodeActionsProvider'; + +suite('OSS code actions provider', () => { + let ossActionsProvider: OssCodeActionsProvider; + let logQuickFixIsDisplayed: sinon.SinonSpy; + let rangeMock: Range; + + setup(() => { + const ossResults = new Map>(); + ossResults.set('folderName', [ + { + filePath: '//folderName//package.json', + additionalData: {}, + } as unknown as Issue, + ]); + + logQuickFixIsDisplayed = sinon.fake(); + const analytics = { + logQuickFixIsDisplayed, + } as unknown as IAnalytics; + + const codeActionAdapter = { + create: (_: string, _kind?: CodeActionKind) => ({ + command: {}, + }), + } as ICodeActionAdapter; + + const codeActionKindAdapter = { + getQuickFix: sinon.fake(), + } as ICodeActionKindAdapter; + + rangeMock = { + contains: () => true, + } as unknown as Range; + + ossActionsProvider = new OssCodeActionsProvider( + {} as IVSCodeLanguages, + codeActionAdapter, + codeActionKindAdapter, + ossResults, + analytics, + ); + }); + + teardown(() => { + sinon.restore(); + }); + + test('Provides the most severe vulnerability CodeAction', () => { + // arrange + const document = { + uri: { + fsPath: '//folderName//package.json', + }, + } as unknown as TextDocument; + + const clickedRange = {} as Range; + const context = {} as CodeActionContext; + + const vulnerabilities = [ + { + id: 'vulnerability1', + severity: IssueSeverity.High, + }, + { + id: 'vulnerability2', + severity: IssueSeverity.Medium, + }, + { + id: 'vulnerability3', + severity: IssueSeverity.Critical, + }, + ] as Issue[]; + + const mostSevereVulnerability = { + id: 'vulnerability3', + severity: IssueSeverity.Critical, + } as Issue; + + const codeActions = [ + { + command: { + command: SNYK_OPEN_ISSUE_COMMAND, + title: SNYK_OPEN_ISSUE_COMMAND, + arguments: [ + { + issueType: OpenCommandIssueType.OssVulnerability, + issue: mostSevereVulnerability, + } as OpenIssueCommandArg, + ], + }, + }, + ] as CodeAction[]; + + sinon.stub(ossActionsProvider, 'getIssueRange').returns(rangeMock); + // stubbing private methods workaround is to case to any + sinon.stub(ossActionsProvider, 'getVulnerabilities').returns(vulnerabilities); + sinon.stub(ossActionsProvider, 'getMostSevereVulnerability').returns(mostSevereVulnerability); + sinon.stub(ossActionsProvider, 'getActions').returns(codeActions); + + // act + const result = ossActionsProvider.provideCodeActions(document, clickedRange, context); + + // assert + sinon.assert.calledOnce(logQuickFixIsDisplayed); + assert.deepStrictEqual(result, codeActions); + }); +});