Skip to content

Commit

Permalink
fix: add filtering method to ScannerUtils and remove it from Scanner,
Browse files Browse the repository at this point in the history
tests: Add tests for filterNotPracticingPracticesToFail(),
  • Loading branch information
adelkahomolova committed Oct 30, 2019
1 parent 53a15d6 commit 03bf0e8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
34 changes: 0 additions & 34 deletions src/scanner/Scanner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
];
11 changes: 2 additions & 9 deletions src/scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -174,21 +174,14 @@ export class Scanner {
isOn: p.isOn,
};
});
console.log(reportArguments, 'reportArg');

const reportString = this.reporter.report(reportArguments);

typeof reportString === 'string'
? 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);
}
Expand Down
41 changes: 41 additions & 0 deletions src/scanner/ScannerUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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,
},
];
12 changes: 11 additions & 1 deletion src/scanner/ScannerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
);
};
}

0 comments on commit 03bf0e8

Please sign in to comment.