Skip to content

Commit

Permalink
Merge pull request #184 from snyk/fix/prevent-project-name-to-be-empt…
Browse files Browse the repository at this point in the history
…y-string

fix: prevent project name to be empty string
  • Loading branch information
anthogez authored May 11, 2021
2 parents cc2b337 + 314c4d0 commit bdbd479
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
14 changes: 7 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ function getVersionBuildInfo(
gradleVersionOutput: string,
): VersionBuildInfo | undefined {
try {
const cleanedVersionOutput: string = cleanupVersionOutput(
gradleVersionOutput,
);
const cleanedVersionOutput: string =
cleanupVersionOutput(gradleVersionOutput);

if (cleanedVersionOutput !== '') {
const gradleOutputArray = cleanedVersionOutput.split(/\r\n|\r|\n/);
// from first 3 new lines, we get the gradle version
Expand All @@ -463,9 +463,8 @@ function getVersionBuildInfo(
.map((value) => value.split(/(.*): (.*)/))
.forEach(
(splitValue) =>
(metaBuildVersion[
toCamelCase(splitValue[1].trim())
] = splitValue[2].trim()),
(metaBuildVersion[toCamelCase(splitValue[1].trim())] =
splitValue[2].trim()),
);
return {
gradleVersion,
Expand Down Expand Up @@ -618,7 +617,8 @@ export async function processProjectsInExtractedJSON(
continue;
}

const isValidRootDir = root !== null && root !== undefined;
const invalidValues = [null, undefined, ''];
const isValidRootDir = invalidValues.indexOf(root) === -1;
const isSubProject = projectId !== defaultProject;

let projectName = isValidRootDir ? path.basename(root) : defaultProject;
Expand Down
5 changes: 2 additions & 3 deletions test/functional/gradle-version-build-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ describe('Gradle Version Build Info', () => {
'Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018\n' +
'JVM: 9.0.4 (Oracle Corporation 9.0.4+11)\n' +
'OS: Mac OS X 10.15 x86_64';
const versionBuildInfo = testableMethods.getVersionBuildInfo(
expectedGradleOutput,
)!;
const versionBuildInfo =
testableMethods.getVersionBuildInfo(expectedGradleOutput)!;

expect(versionBuildInfo).toEqual({
gradleVersion: '5.4.1',
Expand Down
14 changes: 6 additions & 8 deletions test/manual/gradle-stdout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('findProjectsInExtractedJSON', () => {

it.each`
rootDir | targetFile
${''} | ${'build.gradle'}
${null} | ${'build.gradle'}
${undefined} | ${'build.gradle'}
${fakeRootDir} | ${path.join(fakeRootDir, 'build.gradle')}
Expand Down Expand Up @@ -35,14 +36,11 @@ describe('findProjectsInExtractedJSON', () => {
allSubProjectNames: [],
};

const {
defaultProject,
projects,
allSubProjectNames,
} = await processProjectsInExtractedJSON(
rootDir,
jsonExtractedFromGradleStdout,
);
const { defaultProject, projects, allSubProjectNames } =
await processProjectsInExtractedJSON(
rootDir,
jsonExtractedFromGradleStdout,
);

expect(defaultProject).toEqual('tardis-master');
expect(projects['tardis-master']?.targetFile).toEqual(`${targetFile}`);
Expand Down

0 comments on commit bdbd479

Please sign in to comment.