Skip to content

Commit c8c557f

Browse files
Excavator: Migrate Groovy nebula test TestDependencyVersionsTaskSpec to the new Java Junit framework
1 parent 513c1ec commit c8c557f

File tree

6 files changed

+3247
-119
lines changed

6 files changed

+3247
-119
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
classpath 'com.palantir.jakartapackagealignment:jakarta-package-alignment:0.6.0'
1010
classpath 'com.palantir.gradle.jdks:gradle-jdks:0.69.0'
1111
classpath 'com.palantir.gradle.jdkslatest:gradle-jdks-latest:0.24.0'
12-
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.34.0'
12+
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.38.0'
1313
classpath 'com.palantir.gradle.externalpublish:gradle-external-publish-plugin:1.27.0'
1414
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.82.0'
1515
classpath 'com.palantir.suppressible-error-prone:gradle-suppressible-error-prone:2.26.0'

gradle-plugin-testing/src/test/groovy/com/palantir/gradle/plugintesting/TestDependencyVersionsTaskSpec.groovy

Lines changed: 0 additions & 118 deletions
This file was deleted.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.gradle.plugintesting;
18+
19+
import com.palantir.gradle.testing.execution.GradleInvoker;
20+
import com.palantir.gradle.testing.junit.GradlePluginTests;
21+
import com.palantir.gradle.testing.project.RootProject;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
@GradlePluginTests
26+
class TestDependencyVersionsTaskTest {
27+
28+
@BeforeEach
29+
void beforeEach(RootProject rootProject) {
30+
rootProject
31+
.gradlePropertiesFile()
32+
.appendProperty(PluginTestingPlugin.PLUGIN_VERSION_PROPERTY_NAME, System.getProperty("projectVersion"));
33+
}
34+
35+
@Test
36+
void write_versions_without_GCV(GradleInvoker gradleInvoker, RootProject rootProject) {
37+
rootProject.buildGradle().plugins().add("groovy");
38+
rootProject.buildGradle().plugins().add("com.palantir.gradle-plugin-testing");
39+
40+
// language=gradle
41+
rootProject.buildGradle().append("""
42+
repositories {
43+
mavenCentral()
44+
mavenLocal()
45+
}
46+
47+
dependencies {
48+
//WARNING: Do not include any dependencies here that the plugin-testing-core project uses in it's
49+
// api or implementation configurations. Because the plugin adds the plugin-testing-core artifact as
50+
// a testImplementation dependency, we get the transitive dependencies of it (e.g. guava). Since those
51+
// versions will update over time this test could start erroneously failing. So just list some random
52+
// dependencies here that are unlikely to be added to the plugin-test-core project.
53+
//implementation 'com.google.guava:guava:33.3.1-jre'
54+
implementation 'org.ow2.asm:asm:9.7.1'
55+
56+
testImplementation 'org.apache.httpcomponents.client5:httpclient5:5.3.1'
57+
58+
testRuntimeOnly 'com.palantir.gradle.consistentversions:gradle-consistent-versions:2.31.0'
59+
}
60+
""");
61+
62+
gradleInvoker.withArgs("writeTestDependencyVersions").buildsSuccessfully();
63+
64+
rootProject
65+
.buildDir()
66+
.file("plugin-testing/dependency-versions.properties")
67+
.assertThat()
68+
.exists();
69+
70+
rootProject
71+
.buildDir()
72+
.file("plugin-testing/dependency-versions.properties")
73+
.assertThat()
74+
.content()
75+
.doesNotContain("null")
76+
.contains("org.ow2.asm:asm=9.7.1")
77+
.contains("org.apache.httpcomponents.client5:httpclient5=5.3.1")
78+
.contains("com.palantir.gradle.consistentversions:gradle-consistent-versions=2.31.0");
79+
80+
for (String name : PluginTestingPlugin.CORE_MAVEN_NAMES) {
81+
rootProject
82+
.buildDir()
83+
.file("plugin-testing/dependency-versions.properties")
84+
.assertThat()
85+
.content()
86+
.contains("com.palantir.gradle.plugintesting:" + name);
87+
}
88+
}
89+
90+
@Test
91+
void write_versions_with_GCV(GradleInvoker gradleInvoker, RootProject rootProject) {
92+
// remember - the resolved version for this dependency is using the information passed from the version of the
93+
// plugin
94+
// applied to the gradle-plugin-test project itself, _not_ the current version under test. So the
95+
// addBuildScriptDependencies code and the "resolve" logic it calls is the current version, but the information
96+
// it is working with is from the last published version of the plugin (assuming that's the one applied to the
97+
// root build.gradle file of this project.
98+
rootProject
99+
.buildGradle()
100+
.append(TestContentHelpers.addBuildScriptBlock(
101+
"mavenCentral()", "com.palantir.gradle.consistentversions:gradle-consistent-versions"));
102+
103+
rootProject.buildGradle().plugins().add("com.palantir.consistent-versions");
104+
rootProject.buildGradle().plugins().add("groovy");
105+
rootProject.buildGradle().plugins().add("com.palantir.gradle-plugin-testing");
106+
107+
// language=gradle
108+
rootProject.buildGradle().append("""
109+
repositories {
110+
mavenCentral()
111+
mavenLocal()
112+
}
113+
114+
dependencies {
115+
implementation 'com.google.guava:guava'
116+
117+
testImplementation 'org.junit.jupiter:junit-jupiter'
118+
testImplementation 'com.netflix.nebula:nebula-test'
119+
120+
testRuntimeOnly 'com.palantir.gradle.consistentversions:gradle-consistent-versions'
121+
}
122+
""");
123+
124+
rootProject
125+
.propertiesFile("versions.props")
126+
.appendProperty(
127+
"org.junit.jupiter:junit-jupiter",
128+
TestDependencyVersions.version("org.junit.jupiter:junit-jupiter"))
129+
.appendProperty(
130+
"com.netflix.nebula:nebula-test",
131+
TestDependencyVersions.version("com.netflix.nebula:nebula-test"))
132+
.appendProperty("com.google.guava:guava", TestDependencyVersions.version("com.google.guava:guava"))
133+
.appendProperty(
134+
"com.palantir.gradle.consistentversions:gradle-consistent-versions",
135+
TestDependencyVersions.version(
136+
"com.palantir.gradle.consistentversions:gradle-consistent-versions"));
137+
138+
gradleInvoker.withArgs("writeVersionLocks").buildsSuccessfully();
139+
140+
gradleInvoker.withArgs("writeTestDependencyVersions").buildsSuccessfully();
141+
142+
rootProject
143+
.buildDir()
144+
.file("plugin-testing/dependency-versions.properties")
145+
.assertThat()
146+
.exists();
147+
148+
rootProject
149+
.buildDir()
150+
.file("plugin-testing/dependency-versions.properties")
151+
.assertThat()
152+
.content()
153+
.doesNotContain("null")
154+
.contains("org.junit.jupiter:junit-jupiter");
155+
156+
for (String name : PluginTestingPlugin.CORE_MAVEN_NAMES) {
157+
rootProject
158+
.buildDir()
159+
.file("plugin-testing/dependency-versions.properties")
160+
.assertThat()
161+
.content()
162+
.contains("com.palantir.gradle.plugintesting:" + name);
163+
}
164+
}
165+
}

pr-description-update.txt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
A formatted diff between the old and new test can be viewed [here](https://htmlpreview.github.io/?https://raw.githubusercontent.com/palantir/gradle-baseline/develop/test-migration-notes/TestDependencyVersionsTaskTest.html)
2+
3+
# Test Migration Errors and Fixes
4+
5+
## First Compilation Attempt
6+
7+
### Error 1: Cannot find symbol `projectDir()`
8+
**Error message:**
9+
```
10+
error: cannot find symbol
11+
.projectDir()
12+
^
13+
symbol: method projectDir()
14+
location: variable rootProject of type RootProject
15+
```
16+
17+
**Root cause:** The `RootProject` API doesn't have a `projectDir()` method. The file path needs to be accessed differently.
18+
19+
**Fix:** Removed the `outputFile` field entirely. Instead, used `rootProject.buildDir().file("plugin-testing/dependency-versions.properties")` to access the file through the framework's API.
20+
21+
### Error 2: Cannot find symbol `read()`
22+
**Error message:**
23+
```
24+
error: cannot find symbol
25+
String outputText = rootProject.file("build/plugin-testing/dependency-versions.properties").read();
26+
^
27+
symbol: method read()
28+
location: class ArbitraryFile
29+
```
30+
31+
**Root cause:** The `ArbitraryFile` class doesn't have a `read()` method. The framework uses a different pattern for file assertions.
32+
33+
**Fix:** Changed to use the fluent assertion API:
34+
```java
35+
rootProject
36+
.buildDir()
37+
.file("plugin-testing/dependency-versions.properties")
38+
.assertThat()
39+
.content()
40+
.contains("expected content");
41+
```
42+
43+
### Error 3: Cannot find symbol `asFile()`
44+
**Error message:**
45+
```
46+
error: cannot find symbol
47+
rootProject.file("versions.props").asFile(),
48+
^
49+
symbol: method asFile()
50+
location: class ArbitraryFile
51+
```
52+
53+
**Root cause:** The framework's `ArbitraryFile` doesn't have an `asFile()` method.
54+
55+
**Fix:** Changed from using `TestContentHelpers.addVersionsToPropsFile()` to using the framework's fluent API:
56+
```java
57+
rootProject
58+
.propertiesFile("versions.props")
59+
.appendProperty("key", "value");
60+
```
61+
62+
## Second Compilation Attempt
63+
64+
### Error 4: GradleTestPluginsBlock ErrorProne check
65+
**Error message:**
66+
```
67+
error: [GradleTestPluginsBlock] Plugins must be added using .plugins().add() method.
68+
Use gradleFile.plugins().add("plugin-id") instead.
69+
rootProject.buildGradle().append("""
70+
```
71+
72+
**Root cause:** The ErrorProne check `GradleTestPluginsBlock` detects when plugins are being added via `apply plugin:` syntax in appended strings, which goes against the framework's best practices.
73+
74+
**Fix:** Extracted plugin declarations from the appended strings and used the `.plugins().add()` API instead:
75+
```java
76+
rootProject.buildGradle().plugins().add("groovy");
77+
rootProject.buildGradle().plugins().add("com.palantir.gradle-plugin-testing");
78+
```
79+
80+
## Summary of API Differences
81+
82+
1. **File reading:** Use `.assertThat().content()` instead of `.read()` or `.text`
83+
2. **File assertions:** Chain assertions fluently instead of extracting content to variables
84+
3. **Properties files:** Use `.propertiesFile().appendProperty()` instead of helper methods with File objects
85+
4. **Plugin declarations:** Use `.plugins().add()` instead of `apply plugin:` in appended strings
86+
5. **File paths:** Use `buildDir().file()` instead of constructing paths manually
87+
88+
## Second Pass Refinements
89+
90+
### Refinement 1: Removed redundant Spock-style comments
91+
**Issue:** The migrated test had comments like `// given:`, `// when:`, `// then:` which are Spock syntax remnants and unnecessary in JUnit tests.
92+
93+
**Fix:** Removed these comments, keeping only the `// ***DELINEATOR FOR REVIEW:` comments that will be removed later.
94+
95+
### Refinement 2: Improved line formatting for long method chains
96+
**Issue:** Some property append chains were becoming very long horizontally.
97+
98+
**Fix:** Split the longer `.appendProperty()` calls across multiple lines for better readability, especially for the longer dependency coordinate strings.

0 commit comments

Comments
 (0)