Skip to content

Commit

Permalink
fix(gradle): Update same dependencies in single file (#6777)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 18, 2020
1 parent af72c43 commit 51619a6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/manager/gradle/__fixtures__/build.gradle.example1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ buildscript {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
classpath 'com.fkorotkov:gradle-libraries-plugin:0.1'
classpath "gradle.plugin.se.patrikerdes:gradle-use-latest-versions-plugin:0.2.3"
classpath 'org.apache.openjpa:openjpa:3.1.1'
}
}

Expand Down Expand Up @@ -73,6 +74,8 @@ dependencies {
// optional dependencies for using Spock
testCompile "org.hamcrest:hamcrest-core:1.3" // only necessary if Hamcrest matchers are used
testRuntime "cglib:cglib-nodep:3.1" // allows mocking of classes (in addition to interfaces)

dependency 'org.apache.openjpa:openjpa:3.1.1'
}

task integration(type: Test) {
Expand Down
2 changes: 2 additions & 0 deletions lib/manager/gradle/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,5 @@ exports[`manager/gradle/index updateDependency should update an existing module
exports[`manager/gradle/index updateDependency should update an existing plugin dependency 1`] = `Array []`;

exports[`manager/gradle/index updateDependency should update an existing plugin dependency with Kotlin DSL 1`] = `Array []`;

exports[`manager/gradle/index updateDependency should update dependencies in same file 1`] = `Array []`;
19 changes: 13 additions & 6 deletions lib/manager/gradle/build-gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ function kotlinPluginStringVersionFormatMatch(
return regEx(`(id\\("${dependency.group}"\\)\\s+version\\s+")[^$].*?(")`);
}

function dependencyStringVersionFormatMatch(
dependency: GradleDependency
): RegExp {
return regEx(
`(dependency\\s+['"]${dependency.group}:${dependency.name}:)[^'"]+(['"])`
);
}

function allMapFormatOrders(
group: string,
name: string,
Expand Down Expand Up @@ -202,19 +210,18 @@ function updateVersionLiterals(
moduleStringVersionFormatMatch(dependency),
groovyPluginStringVersionFormatMatch(dependency),
kotlinPluginStringVersionFormatMatch(dependency),
dependencyStringVersionFormatMatch(dependency),
...moduleMapVersionFormatMatch(dependency),
...moduleKotlinNamedArgumentVersionFormatMatch(dependency),
];
let result = buildGradleContent;
for (const regex of regexes) {
const match = regex.exec(buildGradleContent);
const match = regex.exec(result);
if (match) {
return buildGradleContent.replace(
match[0],
`${match[1]}${newVersion}${match[2]}`
);
result = result.replace(match[0], `${match[1]}${newVersion}${match[2]}`);
}
}
return null;
return result === buildGradleContent ? null : result;
}

function updateLocalVariables(
Expand Down
41 changes: 41 additions & 0 deletions lib/manager/gradle/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,47 @@ describe(getName(__filename), () => {

expect(execSnapshots).toMatchSnapshot();
});

it('should update dependencies in same file', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);

const buildGradleContent = await fsExtra.readFile(
`${fixtures}/build.gradle.example1`,
'utf8'
);

const upgrade = {
depGroup: 'org.apache.openjpa',
name: 'openjpa',
version: '3.1.1',
newValue: '3.1.2',
};

const buildGradleContentUpdated = manager.updateDependency({
fileContent: buildGradleContent,
upgrade,
});

expect(buildGradleContent).not.toContain(
'org.apache.openjpa:openjpa:3.1.2'
);

expect(buildGradleContentUpdated).not.toContain(
"dependency 'org.apache.openjpa:openjpa:3.1.1'"
);
expect(buildGradleContentUpdated).not.toContain(
"dependency 'org.apache.openjpa:openjpa:3.1.1'"
);

expect(buildGradleContentUpdated).toContain(
"classpath 'org.apache.openjpa:openjpa:3.1.2'"
);
expect(buildGradleContentUpdated).toContain(
"classpath 'org.apache.openjpa:openjpa:3.1.2'"
);

expect(execSnapshots).toMatchSnapshot();
});
});

ifSystemSupportsGradle(6).describe('executeGradle integration', () => {
Expand Down

0 comments on commit 51619a6

Please sign in to comment.