From 27ba2d2120f0d8f8b55610f505b3302e29463e9a Mon Sep 17 00:00:00 2001 From: Michel Kaporin Date: Mon, 2 Aug 2021 11:28:10 +0200 Subject: [PATCH] fix: tests, address todo [ROAD-189] (#34) * fix: tests, address todo * fix: remove redundant emitter --- src/snyk/lib/analyzer/SnykAnalyzer.ts | 22 ++++++++++++--------- src/snyk/lib/modules/ReportModule.ts | 2 +- src/snyk/services/viewManagerService.ts | 13 ++---------- src/snyk/view/code/qualityIssueProvider.ts | 2 -- src/snyk/view/code/securityIssueProvider.ts | 2 -- src/snyk/view/loadingBadge.ts | 12 ++--------- src/test/integration/configuration.test.ts | 4 ++-- src/test/integration/viewNavigation.test.ts | 4 ++-- 8 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/snyk/lib/analyzer/SnykAnalyzer.ts b/src/snyk/lib/analyzer/SnykAnalyzer.ts index 346875a95..0e7e08a5d 100644 --- a/src/snyk/lib/analyzer/SnykAnalyzer.ts +++ b/src/snyk/lib/analyzer/SnykAnalyzer.ts @@ -150,9 +150,11 @@ class SnykAnalyzer implements AnalyzerInterface { updatedFile: openedTextEditorType, ): Promise { try { + const isSecurityReviewFile = this.codeSecurityReview && this.codeSecurityReview.has(updatedFile.document.uri); + const isQualityReviewFile = this.codeQualityReview && this.codeQualityReview.has(updatedFile.document.uri); + if ( - !this.codeSecurityReview || - !this.codeSecurityReview.has(updatedFile.document.uri) || + (!isSecurityReviewFile && !isQualityReviewFile) || !updatedFile.contentChanges.length || // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access !updatedFile.contentChanges[0].range @@ -160,13 +162,15 @@ class SnykAnalyzer implements AnalyzerInterface { return; } const fileIssuesList: IFilePath = updateFileReviewResultsPositions(this.analysisResults, updatedFile); - if (this.codeSecurityReview) { - const [securityIssues, qualityIssues] = this.createIssuesList({ - fileIssuesList, - suggestions: this.analysisResults.suggestions, - fileUri: vscode.Uri.file(updatedFile.fullPath), - }); - this.codeSecurityReview.set(vscode.Uri.file(updatedFile.fullPath), [...securityIssues]); // todo + const [securityIssues, qualityIssues] = this.createIssuesList({ + fileIssuesList, + suggestions: this.analysisResults.suggestions, + fileUri: vscode.Uri.file(updatedFile.fullPath), + }); + if (isSecurityReviewFile) { + this.codeSecurityReview?.set(vscode.Uri.file(updatedFile.fullPath), [...securityIssues]); + } else if (isQualityReviewFile) { + this.codeQualityReview?.set(vscode.Uri.file(updatedFile.fullPath), [...qualityIssues]); } } catch (err) { await extension.processError(err, { diff --git a/src/snyk/lib/modules/ReportModule.ts b/src/snyk/lib/modules/ReportModule.ts index fffc819ca..c223ba069 100644 --- a/src/snyk/lib/modules/ReportModule.ts +++ b/src/snyk/lib/modules/ReportModule.ts @@ -19,7 +19,7 @@ abstract class ReportModule extends BaseSnykModule implements ReportModuleInterf constructor() { super(); - this.loadingBadge = new LoadingBadge(this.viewManagerService); + this.loadingBadge = new LoadingBadge(); } private static get shouldReport(): boolean { diff --git a/src/snyk/services/viewManagerService.ts b/src/snyk/services/viewManagerService.ts index 16527fbea..cd204d4ed 100644 --- a/src/snyk/services/viewManagerService.ts +++ b/src/snyk/services/viewManagerService.ts @@ -3,10 +3,10 @@ import { EventEmitter, TreeView } from 'vscode'; import { configuration, FeaturesConfiguration } from '../configuration'; import { REFRESH_VIEW_DEBOUNCE_INTERVAL } from '../constants/general'; import { PendingTask, PendingTaskInterface } from '../utils/pendingTask'; +import { TreeNode } from '../view/treeNode'; import { FeaturesViewProvider } from '../view/welcome/welcomeViewProvider'; -import { Node } from './../view/node'; -export type ViewType = FeaturesViewProvider | TreeView; +export type ViewType = FeaturesViewProvider | TreeView; export class ViewContainer { private container = new Map(); @@ -23,9 +23,6 @@ export class ViewContainer { export interface IViewManagerService { viewContainer: ViewContainer; - initializedView: PendingTaskInterface; - emitViewInitialized(): void; - readonly refreshCodeSecurityViewEmitter: EventEmitter; readonly refreshCodeQualityViewEmitter: EventEmitter; refreshCodeSecurityView(): void; @@ -37,21 +34,15 @@ export interface IViewManagerService { export class ViewManagerService implements IViewManagerService { readonly viewContainer: ViewContainer; - readonly initializedView: PendingTaskInterface; readonly refreshCodeSecurityViewEmitter: EventEmitter; readonly refreshCodeQualityViewEmitter: EventEmitter; constructor() { - this.initializedView = new PendingTask(); this.refreshCodeSecurityViewEmitter = new EventEmitter(); this.refreshCodeQualityViewEmitter = new EventEmitter(); this.viewContainer = new ViewContainer(); } - emitViewInitialized(): void { - if (!this.initializedView.isCompleted) this.initializedView.complete(); - } - refreshAllAnalysisViews(): void { this.refreshCodeSecurityView(); this.refreshCodeQualityView(); diff --git a/src/snyk/view/code/qualityIssueProvider.ts b/src/snyk/view/code/qualityIssueProvider.ts index 7f6c0fa5c..23ad7397a 100644 --- a/src/snyk/view/code/qualityIssueProvider.ts +++ b/src/snyk/view/code/qualityIssueProvider.ts @@ -18,8 +18,6 @@ export class CodeQualityIssueProvider extends IssueProvider { } getRootChildren(): TreeNode[] { - this.viewManagerService.emitViewInitialized(); - if (!configuration.getFeaturesConfiguration()?.codeQualityEnabled) { return [ new TreeNode({ diff --git a/src/snyk/view/code/securityIssueProvider.ts b/src/snyk/view/code/securityIssueProvider.ts index 83e6669c3..5d4b43232 100644 --- a/src/snyk/view/code/securityIssueProvider.ts +++ b/src/snyk/view/code/securityIssueProvider.ts @@ -18,8 +18,6 @@ export class CodeSecurityIssueProvider extends IssueProvider { } getRootChildren(): TreeNode[] { - this.viewManagerService.emitViewInitialized(); - if (!configuration.getFeaturesConfiguration()?.codeSecurityEnabled) { return [ new TreeNode({ diff --git a/src/snyk/view/loadingBadge.ts b/src/snyk/view/loadingBadge.ts index e5c294139..0e4339b06 100644 --- a/src/snyk/view/loadingBadge.ts +++ b/src/snyk/view/loadingBadge.ts @@ -2,7 +2,6 @@ import * as vscode from 'vscode'; import { BaseSnykModuleInterface } from '../../interfaces/SnykInterfaces'; import { SNYK_VIEW_WELCOME } from '../constants/views'; import { errorsLogs } from '../messages/errorsServerLogMessages'; -import { IViewManagerService } from '../services/viewManagerService'; import { PendingTask, PendingTaskInterface } from '../utils/pendingTask'; export interface ILoadingBadge { @@ -13,8 +12,6 @@ export class LoadingBadge implements ILoadingBadge { private progressBadge: PendingTaskInterface | undefined; private shouldShowProgressBadge = false; - constructor(private viewManagerService: IViewManagerService) {} - private getProgressBadgePromise(): Promise { if (!this.shouldShowProgressBadge) return Promise.resolve(); if (!this.progressBadge || this.progressBadge.isCompleted) { @@ -30,13 +27,8 @@ export class LoadingBadge implements ILoadingBadge { // Using closure on this to allow partial binding in arbitrary positions // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; - this.viewManagerService.initializedView.waiter - .then( - () => - vscode.window.withProgress({ location: { viewId: SNYK_VIEW_WELCOME } }, () => - self.getProgressBadgePromise(), - ), // todo check if correct with respect to security/quality split - ) + vscode.window + .withProgress({ location: { viewId: SNYK_VIEW_WELCOME } }, () => self.getProgressBadgePromise()) .then( () => undefined, error => diff --git a/src/test/integration/configuration.test.ts b/src/test/integration/configuration.test.ts index 33335c188..d21655578 100644 --- a/src/test/integration/configuration.test.ts +++ b/src/test/integration/configuration.test.ts @@ -1,4 +1,4 @@ -import { strictEqual } from 'assert'; +import { deepStrictEqual, strictEqual } from 'assert'; import { configuration } from '../../snyk/configuration'; suite('Configuration', () => { @@ -24,7 +24,7 @@ suite('Configuration', () => { await configuration.setShouldReportEvents(false); strictEqual(configuration.token, token); - strictEqual(configuration.getFeaturesConfiguration(), featuresConfig); + deepStrictEqual(configuration.getFeaturesConfiguration(), featuresConfig); strictEqual(configuration.shouldReportEvents, false); await configuration.setToken(''); }); diff --git a/src/test/integration/viewNavigation.test.ts b/src/test/integration/viewNavigation.test.ts index 0e375cff3..e9072d3f4 100644 --- a/src/test/integration/viewNavigation.test.ts +++ b/src/test/integration/viewNavigation.test.ts @@ -3,8 +3,8 @@ import { configuration } from '../../snyk/configuration'; import { getExtension } from '../../extension'; import { SNYK_VIEW_FEATURES, SNYK_VIEW_WELCOME } from '../../snyk/constants/views'; import { TreeView } from 'vscode'; -import { Node } from '../../snyk/view/node'; import { FeaturesViewProvider } from '../../snyk/view/welcome/welcomeViewProvider'; +import { TreeNode } from '../../snyk/view/treeNode'; suite('View Navigation', () => { setup(async () => { @@ -19,7 +19,7 @@ suite('View Navigation', () => { test('"Feature view is seen after user authenticates within welcome view', async () => { const extension = getExtension(); const viewContainer = extension.viewManagerService.viewContainer; - const welcomeTree = viewContainer.get>(SNYK_VIEW_WELCOME); + const welcomeTree = viewContainer.get>(SNYK_VIEW_WELCOME); // 1. Check welcome view is visible strictEqual(welcomeTree?.visible, true);