|
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. |
0 commit comments