Skip to content

Commit adf00a8

Browse files
authored
improve guide and move full prompt to this repo (#300)
improve guide to help with common mistakes from the AI and move the full prompt to this repo
1 parent 45349b3 commit adf00a8

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed
Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
1-
## File Naming
2-
* If a test file's name ends with Spec, replace Spec with Test for the new file and class name.
1+
You are an expert in Gradle plugin development and testing. I need you to migrate a test class from the old Nebula testing framework to the new Java-based testing framework. Below is a complete guide to help with this migration. If you have not already been told the test class to migrate, it will be given at the end of the instructions, along with the location to put the new test.
2+
3+
# Original Framework Overview (Nebula)
4+
5+
The old framework uses:
6+
- Groovy language with Spock testing framework
7+
- Base classes: IntegrationSpec or IntegrationTestKitSpec or custom Specification
8+
- Direct file manipulation with buildFile << "content"
9+
- Multi-version testing with explicit gradleVersion = version and Spock data tables
10+
11+
# New Framework Overview (Java)
12+
13+
The new framework uses:
14+
- Java language with JUnit 5
15+
- @GradlePluginTests annotation instead of inheritance
16+
- Parameter injection for test components (GradleInvoker, RootProject, SubProject)
17+
- Fluent APIs for file manipulation (rootProject.buildGradle().append("content"))
18+
- Automatic multi-version Gradle testing
19+
- Modern assertions with AssertJ
20+
- Structured support for adding files to tests, including gradle build files, properties files, and java source files.
21+
22+
IMPORTANT: The GRADLE_PLUGIN_TEST_REPO_PATH environment variable points to a local copy of the source repository for this new framework. The framework is in the gradle-plugin-testing-junit sub-project. If you cannot find the source repository, you should immediately stop and report the issue.
23+
24+
IMPORTANT: Before starting any migration, you MUST read the testing guide called "testing-guide.md" located in the docs directory at the root level of the cloned repository. Consult this guide first before making assumptions about how to use the framework. Also look at source files in the new framework as needed to learn about additional APIs.
25+
26+
## Preparation Instructions
27+
Before beginning the migration, add comments to the original groovy test files to delineate methods, multiline variable declarations, and sections of test methods to assist human reviewers when comparing the old and new files. These comments should follow the following format:
28+
- Comments should start with the phrase "DELINEATOR FOR REVIEW" to make it easy to automatically remove them later.
29+
- Comments should contain the name of the component they are indicating
30+
- Within test methods, add comments before the keywords for Spock tests that indicate different parts of the test. The keywords are the following when followed by a colon - setup, when, then, expect.
31+
- Do not add a comment before the setup keyword when it is the first line of a test.
32+
33+
An example of delineator comments in a groovy file:
34+
```
35+
// ***DELINEATOR FOR REVIEW: standardBuildFile
36+
def standardBuildFile = '''
37+
plugins {
38+
id 'java-library'
39+
}
40+
41+
apply plugin: 'com.palantir.baseline-testing'
42+
43+
repositories {
44+
mavenCentral()
45+
}
46+
'''
47+
48+
// ***DELINEATOR FOR REVIEW: this_is_a_test_for_something
49+
def "this is a test for something"() {
50+
setup:
51+
buildFile << standardBuildFile
52+
buildFile << '''
53+
dependencies {
54+
testImplementation 'junit:junit'
55+
}
56+
'''
57+
58+
// ***DELINEATOR FOR REVIEW: when
59+
when:
60+
runTasksSuccessfully('test')
61+
// ***DELINEATOR FOR REVIEW: then
62+
then:
63+
fileExists("build/reports/tests/test/classes/test.JUnit4Test.html")
64+
}
65+
```
66+
These comments should be copied over to the new test files as they are created to assist human reviewers.
67+
68+
# General Instructions
69+
- Test names should be changed to a snake_case_english_sentence in all lower case.
70+
- Keep the comments from the original Groovy tests when writing the new tests.
71+
- Method name mappings from old to new framework:
72+
- `.build()``.buildsSuccessfully()`
73+
- `.buildAndFail()``.buildsWithFailure()`
74+
- `with('task').build()``gradle.withArgs('task').buildsSuccessfully()`
75+
76+
77+
# File Manipulation Instructions
78+
- Follow the instructions in the testing-guide.md for how to manipulate files, especially for common types like gradle build files, properties files, and java source files.
79+
- Prefer text blocks instead of String concatenations in texts, especially when writing to files (gradle, java etc. files). Add a new line after a text block for clarity reasons.
80+
- If the groovy test class has a variable which defines a java file as a string, keep the same in the java class and pass it to the `java().writeClass(...)` method when used.
81+
- If the groovy test class has a variable which defines a build file as a string, create a helper method in the java class that does the same.
82+
- Name the helper method the same as the variable it is replacing
83+
- Put the helper method in roughly the same location in the new file as where the variable is defined in the old file, even if that means putting the method before variable definitions at the top of the class.
84+
- The helper method should return the GradleFile object so that further content can be added to it fluidly.
85+
86+
# Assertion Instructions
87+
- Follow the instructions in the test-guide.md under "Assertions" for how to use fluent assertions built into the framework.
88+
- Use chained assertions when checking multiple things on the same value.
89+
90+
# Final Instructions
91+
- Make sure the migrated tests compile by running `./gradlew compileTestJava`.
92+
- As you discover errors in your work, write out what the error was and what you did to find the information to fix it to a file called "test-migration-errors.md".
93+
- Once the migration is complete and the new file compiles, relook at the testing-guide.md file and the above instructions to make sure you've followed all the best practices for use of the framework. Update the converted test as necessary.
94+
- Add notes to the test-migration-errors.md file about the changes made on this second pass through.
95+
- If you made additional changes to the test, then review and update it one more time, also adding to test-migration-errors.md as necessary.

docs/testing-guide.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,12 @@ When using [com.palantir.consistent-versions](https://github.com/palantir/gradle
280280
Create custom Gradle files beyond `build.gradle` and `settings.gradle`:
281281

282282
```java
283+
import com.palantir.gradle.testing.files.gradle.GradleFile;
284+
283285
@Test
284286
void create_custom_gradle_files(RootProject project) {
285287
// Create custom Gradle files
286-
project.gradleFile("dependencies.gradle").overwrite("""
288+
GradleFile gradleFile = project.gradleFile("dependencies.gradle").overwrite("""
287289
dependencies {
288290
implementation 'com.google.guava:guava:32.1.0-jre'
289291
}
@@ -294,6 +296,38 @@ void create_custom_gradle_files(RootProject project) {
294296
}
295297
```
296298

299+
Or use a helper method to setup a standard build file used across multiple tests:
300+
```java
301+
import com.palantir.gradle.testing.files.gradle.GradleFile;
302+
303+
GradleFile standardBuildFile(RootProject project) {
304+
rootProject
305+
.buildGradle()
306+
.plugins()
307+
.add("com.palantir.jdks.latest")
308+
.add("java-library");
309+
310+
// Return the GradleFile instance for further configuration in tests
311+
return rootProject.buildGradle().append("""
312+
repositories {
313+
mavenCentral()
314+
}
315+
""");
316+
}
317+
318+
@Test
319+
void check_jdk_17(RootProject project) {
320+
standardBuildFile(project).append("""
321+
javaVersions {
322+
libraryTarget = 17
323+
}
324+
""");
325+
326+
//...
327+
}
328+
```
329+
330+
297331
### Java Source Files
298332

299333
Write and manipulate Java source. The `writeClass()` method from `JavaSrcDir` (accessed via `project.mainSourceSet().java()` or `project.sourceSet("name").java()`) automatically parses the Java source code to extract the package and class name, then creates the file at the correct path.
@@ -490,6 +524,8 @@ The `MavenRepo` instance is shared across all test methods in a class. Artifacts
490524
Use `GradleInvoker` to run builds:
491525

492526
```java
527+
import com.palantir.gradle.testing.execution.InvocationResult;
528+
493529
@Test
494530
void successful_build(GradleInvoker gradle, RootProject project) {
495531
project.buildGradle().plugins().add("java");

0 commit comments

Comments
 (0)