Skip to content

Commit

Permalink
Merge pull request #243 from DXHeroes/java/adjusting_gradle_kts
Browse files Browse the repository at this point in the history
fix: adjusting practices & inspector for gradle/kotlin variation
  • Loading branch information
prokopsimek authored Feb 25, 2020
2 parents deee080 + 00acff1 commit f702641
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/detectors/Java/JavaLanguageDetector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ describe('JavaLanguageDetector', () => {
expect(langAtPath[0].path).toEqual(nodePath.sep);
});

it('detects kotlin correctly via build.gradle.kts', async () => {
const structure: DirectoryJSON = {
'/build.gradle.kts': '...',
};

virtualFileSystemService.setFileSystem(structure);

const langAtPath = await detector.detectLanguage();
expect(langAtPath.length).toEqual(1);
expect(langAtPath[0].language).toEqual(ProgrammingLanguage.Kotlin);
expect(langAtPath[0].path).toEqual(nodePath.sep);
});

it('detects java correctly via java extension', async () => {
const structure: DirectoryJSON = {
'/*.java': '...',
Expand Down
11 changes: 9 additions & 2 deletions src/inspectors/package/JavaPackageInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ export class JavaPackageInspector extends PackageInspectorBase {
await this.resolveMavenFileString(mavenFileString);
} else {
const isGradle: boolean = await this.fileInspector.exists('build.gradle');
let isGradleKts = false;
if (!isGradle) {
throw ErrorFactory.newInternalError('Unsupported Java project architecture');
if (await this.fileInspector.exists('build.gradle.kts')) {
isGradleKts = true;
} else {
throw ErrorFactory.newInternalError('Unsupported Java project architecture');
}
}
const gradleFileString = await this.fileInspector.readFile('build.gradle');
const gradleFileString = isGradleKts
? await this.fileInspector.readFile('build.gradle.kts')
: await this.fileInspector.readFile('build.gradle');
await this.resolveGradleFileString(gradleFileString);
}
this.addPackages(this.parsedDependencies, DependencyType.Runtime);
Expand Down
9 changes: 9 additions & 0 deletions src/inspectors/package/JavaPackageInspectorGradle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ describe('JavaPackageInspector Gradle', () => {
await inspector.init();
expect(inspector.hasPackageManagement()).toBe(true);
});

it('returns true if build.gradle.kts is valid and present', async () => {
containerCtx.virtualFileSystemService.clearFileSystem();
containerCtx.virtualFileSystemService.setFileSystem({
'build.gradle.kts': buildGRADLEContents,
});
await inspector.init();
expect(inspector.hasPackageManagement()).toBe(true);
});
});

describe('#hasLockFile', () => {
Expand Down
28 changes: 26 additions & 2 deletions src/practices/Java/JavaGitignoreCorrectlySetPractice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ describe('JavaGitignoreCorrectlySetPractice', () => {
expect(evaluated).toEqual(PracticeEvaluationResult.practicing);
});

it('Returns practicing if the .gitignore is set correctly for Kotlin/GRADLE', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'.gitignore': gitignoreContent,
'build.gradle.kts': buildGRADLEContents,
});

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

it('Returns notPracticing if there the .gitignore is NOT set correctly', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'.gitignore': '...',
Expand Down Expand Up @@ -68,7 +78,21 @@ describe('JavaGitignoreCorrectlySetPractice', () => {
expect(evaluated).toEqual(PracticeEvaluationResult.unknown);
});

it('Returns unknown if there is *.class, *.log, *.jar, *.war in .gitignore but not correctly set for build.gradle', async () => {
it('Returns unknown if there is *.class, *.log, *.jar, *.war in .gitignore but not correctly set for build.gradle.kts', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'.gitignore': `
*.class
*.log
*.jar
*.war`,
'build.gradle.kts': buildGRADLEContents,
});

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

it('Returns unknown if there is *.class, *.log, *.jar, *.war in .gitignore but not correctly set for pom.xml', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'.gitignore': `
*.class
Expand All @@ -82,7 +106,7 @@ describe('JavaGitignoreCorrectlySetPractice', () => {
expect(evaluated).toEqual(PracticeEvaluationResult.unknown);
});

it('Returns unknown if there is correctly set .gitignore, but no pom.xml and build.gradle ', async () => {
it('Returns unknown if there is correctly set .gitignore, but no pom.xml and build.gradle or build.gradle.kts', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'.gitignore': gitignoreContent,
});
Expand Down
2 changes: 1 addition & 1 deletion src/practices/Java/JavaGitignoreCorrectlySetPractice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class JavaGitignoreCorrectlySetPractice implements IPractice {
if (await this.resolveGitignorePractice(parsedGitignore, 'Maven')) {
return PracticeEvaluationResult.practicing;
}
} else if (await ctx.fileInspector.exists('build.gradle')) {
} else if ((await ctx.fileInspector.exists('build.gradle')) || (await ctx.fileInspector.exists('build.gradle.kts'))) {
if (await this.resolveGitignorePractice(parsedGitignore, 'Gradle')) {
return PracticeEvaluationResult.practicing;
}
Expand Down
11 changes: 11 additions & 0 deletions src/practices/Java/JavaNamingConventionsPractice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ describe('JavaPackageManagementUsedPractice', () => {
expect(evaluated).toEqual(PracticeEvaluationResult.unknown);
});

it('Does not evaluate & skips build.gradle.kts file to check other class files', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'pom.xml': '...',
'build.gradle.kts': '...',
'src/main/java/org/vision/root/CronOperations/VeryCorrectNamingConvention.kt': '',
});

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

it('Returns unknown if there is no fileInspector', async () => {
const evaluated = await practice.evaluate({ ...containerCtx.practiceContext, fileInspector: undefined });
expect(evaluated).toEqual(PracticeEvaluationResult.unknown);
Expand Down
8 changes: 5 additions & 3 deletions src/practices/Java/JavaNamingConventionsPractice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ export class JavaNamingConventionsPractice implements IPractice {
const incorrectFiles = [];

scannedFiles.forEach((file) => {
const correctPascalCase = camelCase(file.baseName, { pascalCase: true });
if (file.baseName !== correctPascalCase) {
incorrectFiles.push(file.baseName);
if (file.baseName !== 'build.gradle' && file.baseName !== 'settings.gradle') {
const correctPascalCase = camelCase(file.baseName, { pascalCase: true });
if (file.baseName !== correctPascalCase) {
incorrectFiles.push(file.baseName);
}
}
});

Expand Down
8 changes: 8 additions & 0 deletions src/practices/Java/JavaPackageManagementUsedPractice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ describe('JavaPackageManagementUsedPractice', () => {
expect(evaluated).toEqual(PracticeEvaluationResult.practicing);
});

it('Returns practicing if there is a build.gradle.kts', async () => {
containerCtx.virtualFileSystemService.setFileSystem({
'build.gradle.kts': 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': '...',
Expand Down
2 changes: 2 additions & 0 deletions src/practices/Java/JavaPackageManagementUsedPractice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class JavaPackageManagementUsedPractice implements IPractice {
return PracticeEvaluationResult.practicing;
} else if (await ctx.fileInspector.exists('build.gradle')) {
return PracticeEvaluationResult.practicing;
} else if (await ctx.fileInspector.exists('build.gradle.kts')) {
return PracticeEvaluationResult.practicing;
}

return PracticeEvaluationResult.notPracticing;
Expand Down

0 comments on commit f702641

Please sign in to comment.