-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): package management used practice (#213)
feat(java): package management used practice
- Loading branch information
Showing
6 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters