Skip to content

Commit

Permalink
chore: add test for OsscodeActionsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
j-luong committed Nov 21, 2023
1 parent 6dedebd commit 7216fb9
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/snyk/snykOss/ossService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class OssService extends ProductService<OssIssueData> {
);

this.registerCodeActionsProvider(
new OssCodeActionsProvider(this.result, codeActionAdapter, codeActionKindAdapter, languages, analytics),
new OssCodeActionsProvider(languages, codeActionAdapter, codeActionKindAdapter, this.result, analytics),
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/snyk/snykOss/providers/ossCodeActionsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { DIAGNOSTICS_OSS_COLLECTION_NAME_LS } from '../../snykCode/constants/ana

export class OssCodeActionsProvider extends CodeActionsProvider<OssIssueData> {
constructor(
issues: Readonly<ProductResult<OssIssueData>>,
private readonly languages: IVSCodeLanguages,
private readonly codeActionAdapter: ICodeActionAdapter,
codeActionKindAdapter: ICodeActionKindAdapter,
private readonly languages: IVSCodeLanguages,
issues: Readonly<ProductResult<OssIssueData>>,
analytics: IAnalytics,
) {
super(issues, codeActionKindAdapter, analytics);
Expand Down
118 changes: 118 additions & 0 deletions src/test/unit/snykOss/providers/ossCodeActionsProvider.test.ts
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);
});
});

0 comments on commit 7216fb9

Please sign in to comment.