Skip to content

Commit

Permalink
feat(java): package management used practice (#213)
Browse files Browse the repository at this point in the history
feat(java): package management used practice
  • Loading branch information
adelkahomolova authored Jan 23, 2020
2 parents 7e6bc26 + 6147ef5 commit 401d706
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/detectors/Java/JavaComponentDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class JavaComponentDetector implements IProjectComponentDetector {
}

async detectComponent(langAtPath: LanguageAtPath): Promise<ProjectComponent[]> {
// @todo: add framework recognition => need an XML or GRADLE reader to determine the framework
return [
{
framework: ProjectComponentFramework.UNKNOWN,
Expand Down
63 changes: 63 additions & 0 deletions src/practices/Java/JavaPackageManagementUsed.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { JavaPackageManagementUsedPractice } from './JavaPackageManagementUsed';
import { PracticeEvaluationResult, ProgrammingLanguage } from '../../model';
import { TestContainerContext, createTestContainer } from '../../inversify.config';
import { pomXMLContents } from '../../detectors/__MOCKS__/Java/pomXMLContents.mock';
import { buildGRADLEContents } from '../../detectors/__MOCKS__/Java/buildGRADLEContents.mock';

describe('JavaPackageManagementUsedPractice', () => {
let practice: JavaPackageManagementUsedPractice;
let containerCtx: TestContainerContext;

beforeAll(() => {
containerCtx = createTestContainer();
containerCtx.container.bind('JavaPackageManagementUsedPractice').to(JavaPackageManagementUsedPractice);
practice = containerCtx.container.get('JavaPackageManagementUsedPractice');
});

afterEach(async () => {
containerCtx.virtualFileSystemService.clearFileSystem();
containerCtx.practiceContext.fileInspector!.purgeCache();
});

it('Returns practicing if there is a pom.xml', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'pom.xml': pomXMLContents,
});
const evaluated = await practice.evaluate(containerCtx.practiceContext);
expect(evaluated).toEqual(PracticeEvaluationResult.practicing);
});

it('Returns practicing if there is a build.gradle', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'build.gradle': buildGRADLEContents,
});
const evaluated = await practice.evaluate(containerCtx.practiceContext);
expect(evaluated).toEqual(PracticeEvaluationResult.practicing);
});

it('Returns notPracticing if there is NO pom.xml or build.gradle', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'not.exists': '...',
});

const evaluated = await practice.evaluate(containerCtx.practiceContext);
expect(evaluated).toEqual(PracticeEvaluationResult.notPracticing);
});

it('Returns unknown if there is no fileInspector', async () => {
const evaluated = await practice.evaluate({ ...containerCtx.practiceContext, fileInspector: undefined });
expect(evaluated).toEqual(PracticeEvaluationResult.unknown);
});

it('Is applicable to Java', async () => {
containerCtx.practiceContext.projectComponent.language = ProgrammingLanguage.Java;
const result = await practice.isApplicable(containerCtx.practiceContext);
expect(result).toEqual(true);
});

it('Is not applicable to other languages', async () => {
containerCtx.practiceContext.projectComponent.language = ProgrammingLanguage.Python;
const result = await practice.isApplicable(containerCtx.practiceContext);
expect(result).toEqual(false);
});
});
32 changes: 32 additions & 0 deletions src/practices/Java/JavaPackageManagementUsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IPractice } from '../IPractice';
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model';
import { DxPractice } from '../DxPracticeDecorator';
import { PracticeContext } from '../../contexts/practice/PracticeContext';

@DxPractice({
id: 'Java.PackageManagementUsed',
name: 'Use Java Package Management',
impact: PracticeImpact.high,
suggestion: 'Use pom.xml or build.gradle to keep track of packages that are being used in your application.',
reportOnlyOnce: true,
url: 'https://maven.apache.org/',
})
export class JavaPackageManagementUsedPractice implements IPractice {
async isApplicable(ctx: PracticeContext): Promise<boolean> {
return ctx.projectComponent.language === ProgrammingLanguage.Java;
}

async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> {
if (!ctx.fileInspector) {
return PracticeEvaluationResult.unknown;
}

if (await ctx.fileInspector.exists('pom.xml')) {
return PracticeEvaluationResult.practicing;
} else if (await ctx.fileInspector.exists('build.gradle')) {
return PracticeEvaluationResult.practicing;
}

return PracticeEvaluationResult.notPracticing;
}
}
19 changes: 16 additions & 3 deletions src/practices/JavaScript/JsPackageManagementUsedPractice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JsPackageManagementUsedPractice } from './JsPackageManagementUsedPractice';
import { PracticeEvaluationResult } from '../../model';
import { PracticeEvaluationResult, ProgrammingLanguage } from '../../model';
import { TestContainerContext, createTestContainer } from '../../inversify.config';

describe('JsPackageManagementUsedPractice', () => {
Expand Down Expand Up @@ -36,8 +36,21 @@ describe('JsPackageManagementUsedPractice', () => {
expect(evaluated).toEqual(PracticeEvaluationResult.unknown);
});

it('Is always applicable', async () => {
const result = await practice.isApplicable();
it('Is applicable to JS', async () => {
containerCtx.practiceContext.projectComponent.language = ProgrammingLanguage.JavaScript;
const result = await practice.isApplicable(containerCtx.practiceContext);
expect(result).toEqual(true);
});

it('Is applicable to TS', async () => {
containerCtx.practiceContext.projectComponent.language = ProgrammingLanguage.TypeScript;
const result = await practice.isApplicable(containerCtx.practiceContext);
expect(result).toEqual(true);
});

it('Is not applicable to other languages', async () => {
containerCtx.practiceContext.projectComponent.language = ProgrammingLanguage.Python;
const result = await practice.isApplicable(containerCtx.practiceContext);
expect(result).toEqual(false);
});
});
8 changes: 5 additions & 3 deletions src/practices/JavaScript/JsPackageManagementUsedPractice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IPractice } from '../IPractice';
import { PracticeEvaluationResult, PracticeImpact } from '../../model';
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model';
import { DxPractice } from '../DxPracticeDecorator';
import { PracticeContext } from '../../contexts/practice/PracticeContext';

Expand All @@ -12,8 +12,10 @@ import { PracticeContext } from '../../contexts/practice/PracticeContext';
url: 'https://docs.npmjs.com/files/package.json',
})
export class JsPackageManagementUsedPractice implements IPractice {
async isApplicable(): Promise<boolean> {
return true;
async isApplicable(ctx: PracticeContext): Promise<boolean> {
return (
ctx.projectComponent.language === ProgrammingLanguage.JavaScript || ctx.projectComponent.language === ProgrammingLanguage.TypeScript
);
}

async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> {
Expand Down
2 changes: 2 additions & 0 deletions src/practices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { TimeToSolveIssuesPractice } from './LanguageIndependent/TimeToSolveIssu
import { ThinPullRequestsPractice } from './LanguageIndependent/ThinPullRequestsPractice';
import { SecurityVulnerabilitiesPractice } from './JavaScript/SecurityVulnerabilitiesPractice';
import { FirstTestPractice, SecondTestPractice, InvalidTestPractice } from '../scanner';
import { JavaPackageManagementUsedPractice } from './Java/JavaPackageManagementUsed';

// register practices here
export const practices = [
Expand Down Expand Up @@ -64,4 +65,5 @@ export const practices = [
TimeToSolveIssuesPractice,
ThinPullRequestsPractice,
SecurityVulnerabilitiesPractice,
JavaPackageManagementUsedPractice,
];

0 comments on commit 401d706

Please sign in to comment.