-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test for OsscodeActionsProvider
- Loading branch information
Showing
3 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/test/unit/snykOss/providers/ossCodeActionsProvider.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, WorkspaceFolderResult<OssIssueData>>(); | ||
ossResults.set('folderName', [ | ||
{ | ||
filePath: '//folderName//package.json', | ||
additionalData: {}, | ||
} as unknown as Issue<OssIssueData>, | ||
]); | ||
|
||
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<OssIssueData>[]; | ||
|
||
const mostSevereVulnerability = { | ||
id: 'vulnerability3', | ||
severity: IssueSeverity.Critical, | ||
} as Issue<OssIssueData>; | ||
|
||
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, <any>'getVulnerabilities').returns(vulnerabilities); | ||
sinon.stub(ossActionsProvider, <any>'getMostSevereVulnerability').returns(mostSevereVulnerability); | ||
sinon.stub(ossActionsProvider, <any>'getActions').returns(codeActions); | ||
|
||
// act | ||
const result = ossActionsProvider.provideCodeActions(document, clickedRange, context); | ||
|
||
// assert | ||
sinon.assert.calledOnce(logQuickFixIsDisplayed); | ||
assert.deepStrictEqual(result, codeActions); | ||
}); | ||
}); |