diff --git a/src/scanner/Scanner.spec.ts b/src/scanner/Scanner.spec.ts index 62889ec7d..721b11eb5 100644 --- a/src/scanner/Scanner.spec.ts +++ b/src/scanner/Scanner.spec.ts @@ -21,43 +21,9 @@ describe('Scanner', () => { expect(scanner).toBeDefined(); }); - it('Report correctly', async () => { - const notPracticingHighImpactPracticeWithCtx = practiceWithContextFactory({ evaluation: PracticeEvaluationResult.notPracticing }); - const notPracticingPracticesToFail = reportArguments.filter( - (practice) => practice.evaluation === PracticeEvaluationResult.notPracticing && practice.impact === 'high', - ); - }); - /* Other tests are missing. They have to be either heavily mocked or we have to wait for other components to be finished to write the integration tests. (Scanner class is a integration point) */ }); - -const reportArguments = [ - { - component: { - framework: 'UNKNOWN', - language: 'JavaScript', - path: '/var/folders/w2/1gzmv1n51bs2z13yd5558ny80000gn/T/dx-scannerIrp1iV', - platform: 'UNKNOWN', - repositoryPath: 'https://github.com/DXHeroes/empty-js', - type: 'Library', - }, - practice: { - id: 'LanguageIndependent.GitignoreIsPresent', - name: 'Having a .gitignore', - impact: 'high', - suggestion: - 'Add gitignore which allow you to ignore files, such as editor backup files, build products or local configuration overrides that you never want to commit into a repository.', - reportOnlyOnce: true, - url: 'https://git-scm.com/docs/gitignore', - defaultImpact: 'high', - matcher: ['JsGitignoreIsPresentPractice'], - }, - evaluation: 'practicing', - impact: 'high', - isOn: true, - }, -]; diff --git a/src/scanner/Scanner.ts b/src/scanner/Scanner.ts index f6a585584..8e8f8e3fe 100644 --- a/src/scanner/Scanner.ts +++ b/src/scanner/Scanner.ts @@ -22,7 +22,7 @@ import { PracticeImpact, } from '../model'; import { IPracticeWithMetadata } from '../practices/DxPracticeDecorator'; -import { IReporter } from '../reporters/IReporter'; +import { IReporter, PracticeWithContextForReporter } from '../reporters/IReporter'; import { ScannerContextFactory, Types } from '../types'; import { ScannerUtils } from './ScannerUtils'; import _ from 'lodash'; @@ -174,7 +174,6 @@ export class Scanner { isOn: p.isOn, }; }); - console.log(reportArguments, 'reportArg'); const reportString = this.reporter.report(reportArguments); @@ -182,13 +181,7 @@ export class Scanner { ? console.log(reportString) : console.log(util.inspect(reportString, { showHidden: false, depth: null })); - const notPracticingPracticesToFail = reportArguments.filter( - (practice) => - practice.evaluation === PracticeEvaluationResult.notPracticing && - (_.includes(ScannerUtils.getImpactFailureLevels(this.argumentsProvider.fail), practice.impact) || - this.argumentsProvider.fail === 'all'), - ); - + const notPracticingPracticesToFail = ScannerUtils.filterNotPracticingPracticesToFail(reportArguments, this.argumentsProvider); if (notPracticingPracticesToFail.length > 0) { process.exit(1); } diff --git a/src/scanner/ScannerUtils.spec.ts b/src/scanner/ScannerUtils.spec.ts index 5507f5f0b..1e76beeaf 100644 --- a/src/scanner/ScannerUtils.spec.ts +++ b/src/scanner/ScannerUtils.spec.ts @@ -15,6 +15,8 @@ import { JsGitignoreCorrectlySetPractice } from '../practices/JavaScript/JsGitig import { TypeScriptUsedPractice } from '../practices/JavaScript/TypeScriptUsedPractice'; import { ScannerUtils } from './ScannerUtils'; import { FirstTestPractice, InvalidTestPractice, SecondTestPractice } from './__MOCKS__'; +import { PracticeWithContextForReporter } from '../reporters/IReporter'; +import { LanguageContext } from '../contexts/language/LanguageContext'; describe('ScannerUtils', () => { describe('#sortPractices', () => { @@ -110,5 +112,44 @@ describe('ScannerUtils', () => { expect(filteredPractices.practicesOff.length).toBeGreaterThanOrEqual(1); expect(filteredPractices.customApplicablePractices.length).toBeGreaterThanOrEqual(2); }); + + it('Filter correctly if practice impact is high and fail=high', () => { + const argumentsProvider = { uri: '.', fail: PracticeImpact.high }; + const result = ScannerUtils.filterNotPracticingPracticesToFail(reportArguments, argumentsProvider); + expect(result).toHaveLength(1); + }); + + it('Filter correctly if practice impact is high and fail=off', () => { + const argumentsProvider = { uri: '.', fail: PracticeImpact.off }; + const result = ScannerUtils.filterNotPracticingPracticesToFail(reportArguments, argumentsProvider); + expect(result).toHaveLength(0); + }); }); }); + +const reportArguments: PracticeWithContextForReporter[] = [ + { + component: { + framework: ProjectComponentFramework.UNKNOWN, + language: ProgrammingLanguage.JavaScript, + path: '/var/folders/w2/1gzmv1n51bs2z13yd5558ny80000gn/T/dx-scannerIrp1iV', + platform: ProjectComponentPlatform.UNKNOWN, + repositoryPath: 'https://github.com/DXHeroes/empty-js', + type: ProjectComponentType.Library, + }, + practice: { + id: 'LanguageIndependent.GitignoreIsPresent', + name: 'Having a .gitignore', + impact: PracticeImpact.high, + suggestion: + 'Add gitignore which allow you to ignore files, such as editor backup files, build products or local configuration overrides that you never want to commit into a repository.', + reportOnlyOnce: true, + url: 'https://git-scm.com/docs/gitignore', + defaultImpact: PracticeImpact.high, + //matcher: ['JsGitignoreIsPresentPractice'], + }, + evaluation: PracticeEvaluationResult.notPracticing, + impact: PracticeImpact.high, + isOn: true, + }, +]; diff --git a/src/scanner/ScannerUtils.ts b/src/scanner/ScannerUtils.ts index 2e0e466e6..d5b675983 100644 --- a/src/scanner/ScannerUtils.ts +++ b/src/scanner/ScannerUtils.ts @@ -3,11 +3,13 @@ import filterAsync from 'node-filter-async'; import toposort from 'toposort'; import { ProjectComponentContext } from '../contexts/projectComponent/ProjectComponentContext'; import { ErrorFactory } from '../lib/errors'; -import { PracticeImpact } from '../model'; +import { PracticeImpact, PracticeEvaluationResult } from '../model'; import { IPracticeWithMetadata } from '../practices/DxPracticeDecorator'; import { IPractice } from '../practices/IPractice'; import { PracticeWithContext } from './Scanner'; import { assertNever } from '../lib/assertNever'; +import { PracticeWithContextForReporter } from '../reporters/IReporter'; +import { ArgumentsProvider } from '../inversify.config'; /** * Scanner helpers & utilities @@ -118,4 +120,12 @@ export class ScannerUtils { return []; } }; + + static filterNotPracticingPracticesToFail = (reportArguments: PracticeWithContextForReporter[], argumentsProvider: ArgumentsProvider) => { + return reportArguments.filter( + (practice) => + practice.evaluation === PracticeEvaluationResult.notPracticing && + (_.includes(ScannerUtils.getImpactFailureLevels(argumentsProvider.fail), practice.impact) || argumentsProvider.fail === 'all'), + ); + }; }