Skip to content

Adding recipe for DoesNotIncludeDependency for Gradle (Maven had one already), to allow for checking whether a dependency is not included.#5725

Merged
steve-aom-elliott merged 11 commits into
mainfrom
does-not-include-dependency-gradle
Jul 14, 2025
Merged

Adding recipe for DoesNotIncludeDependency for Gradle (Maven had one already), to allow for checking whether a dependency is not included.#5725
steve-aom-elliott merged 11 commits into
mainfrom
does-not-include-dependency-gradle

Conversation

@steve-aom-elliott

@steve-aom-elliott steve-aom-elliott commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

What's changed?

  • Added recipe to check that a dependency is not included in Gradle

What's your motivation?

I have a need for being able to detect that a dependency is not included to decide whether to proceed with additional migrations or not. Maven already had a version of this, but Gradle was missing one, and I want to be thorough in this precondition check.

I have not written tests for this quite yet. All of the tests for the Maven version appear to be commented out for some reason, though I do want to add a couple of examples in tests still.

One thing to point out is that it does not include the same directOnly option as the Maven version, as the way the implementations of DependencyInsight is processing dependencies between Maven and Gradle is slightly different, and that class does not take the option in the Gradle version.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@steve-aom-elliott steve-aom-elliott self-assigned this Jul 7, 2025
@steve-aom-elliott steve-aom-elliott added the enhancement New feature or request label Jul 7, 2025
@steve-aom-elliott steve-aom-elliott added recipe Requested Recipe gradle labels Jul 7, 2025
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 7, 2025
@steve-aom-elliott steve-aom-elliott force-pushed the does-not-include-dependency-gradle branch 2 times, most recently from 26793b8 to b4fbdb2 Compare July 8, 2025 21:31
@steve-aom-elliott

Copy link
Copy Markdown
Contributor Author

Still working on the tests to include ones for specific configurations and transitive dependencies, but getting closer.

@steve-aom-elliott steve-aom-elliott force-pushed the does-not-include-dependency-gradle branch from b4fbdb2 to 0c0443a Compare July 10, 2025 19:45
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Jul 10, 2025
@steve-aom-elliott steve-aom-elliott marked this pull request as ready for review July 10, 2025 19:55
@timtebeek timtebeek self-requested a review July 11, 2025 10:42

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like this help readability perhaps? Took me 20 seconds with Claude, so don't go changing things manually:

        @CsvSource(
          useHeadersInDisplayName = true, textBlock = """
            configuration,         annotationProcessor, api,   implementation, compileOnly, runtimeOnly, testImplementation, testCompileOnly, testRuntimeOnly
            annotationProcessor,   true,                false, false,          false,       false,       false,              false,           false
            compileClasspath,      false,               true,  true,           true,        false,       false,              false,           false
            runtimeClasspath,      false,               true,  true,           false,       true,        false,              false,           false
            testCompileClasspath,  false,               true,  true,           false,       false,       true,               true,            false
            testRuntimeClasspath,  false,               true,  true,           false,       true,        true,               false,           true
            """)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I've never used the @CsvSource with textBlock before, but yeah, something like that would also work.

Comment on lines 104 to 106

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit surprised by the repeated annotation here; is there a logic to the grouping that I'm missing? Might a textBlock make sense?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was grouping them by the existing scope of the dependency per row. It was more for my own legibility than anything else.

@timtebeek

Copy link
Copy Markdown
Member

Mostly looks good already; couple questions & comments, but nothing blocking. Once addressed we can merge.

steve-aom-elliott and others added 4 commits July 11, 2025 12:13
…e already), to allow for checking whether a dependency is not included.
…sNotIncludeDependency.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…pendencyInsight` to showcase passing `configuration`. Adding `DoesNotIncludeDependency` tests (still need to add more).
…sts to match naming convention of the Maven version's tests.
… Maven version. I still want to change it a bit so that the test body can be shared for situations that behave the same (like `onlyDirect` has not effect when there is a direct dependency present, so all those tests are the same for whether it's `null`, `true` or `false` for that)
@steve-aom-elliott steve-aom-elliott force-pushed the does-not-include-dependency-gradle branch from e45bea1 to a69aeb7 Compare July 11, 2025 16:13
@steve-aom-elliott

Copy link
Copy Markdown
Contributor Author

I think the only things I didn't end up changing right now were the suggestions for using @CsvSource with a textBlock, but please feel free to use Claude to convert those if they seem more legible afterwards.

@timtebeek timtebeek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved already. Thanks for the help here!

@steve-aom-elliott steve-aom-elliott merged commit 2065d3e into main Jul 14, 2025
2 checks passed
@steve-aom-elliott steve-aom-elliott deleted the does-not-include-dependency-gradle branch July 14, 2025 13:25
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jul 14, 2025
steve-aom-elliott added a commit to openrewrite/rewrite-testing-frameworks that referenced this pull request Jul 14, 2025
…OM or build.gradle have a dependency on `org.testng:testng`. Relies on openrewrite/rewrite#5725
steve-aom-elliott added a commit to openrewrite/rewrite-testing-frameworks that referenced this pull request Jul 16, 2025
…OM or build.gradle have a dependency on `org.testng:testng`. Relies on openrewrite/rewrite#5725
steve-aom-elliott added a commit to openrewrite/rewrite-testing-frameworks that referenced this pull request Jul 24, 2025
…OM or build.gradle have a dependency on `org.testng:testng`. Relies on openrewrite/rewrite#5725
steve-aom-elliott added a commit to openrewrite/rewrite-testing-frameworks that referenced this pull request Jul 28, 2025
…OM or build.gradle have a dependency on `org.testng:testng`. Relies on openrewrite/rewrite#5725
steve-aom-elliott added a commit to openrewrite/rewrite-testing-frameworks that referenced this pull request Jul 31, 2025
…OM or build.gradle have a dependency on `org.testng:testng`. Relies on openrewrite/rewrite#5725
timtebeek added a commit to openrewrite/rewrite-testing-frameworks that referenced this pull request Aug 5, 2025
…OM or build.gradle have a dependency on `org.testng:testng`. (#760)

* Adding dependency guards to the Junit 4 -> 5 migration for when the POM or build.gradle have a dependency on `org.testng:testng`. Relies on openrewrite/rewrite#5725

* Updating `DoesNotIncludeDependency` recipe call to be the shared one in `rewrite-java-dependencies`

* Avoid tests passing for the wrong reason (class present)

* Only pass testng dependency into the single test that needs it

* Match any testng dependency

* Adding TestNgGuard scanning recipe. Needs refinement, but this is a first pass

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Making separate `TestNgGuard` tests and cleaning up other code

* Cleanup of import

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Made the `TestNgGuard` more flexible for situations where there isn't an enclosing project for a source file, which was a majority of our existing tests.
- If it figures out it's a loose file not in a `JavaProject`, it can't ensure project scope anyway, so falls back to solely whether the file itself has a TestNG dependency or uses TestNG types instead for those

* Do not use getters internally

* Adopt `ModuleHasDependency` and drop odd test: only look at dependencies

Assuming classes can not be present without those dependencies

* Revert change to `TestNgToAssertJTest`

* Drop tests that were showing improbable use of types

* Switching to using the new `invertMarking` option on `ModuleHasDependency` for the TestNG guard for JUnit

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request gradle recipe Requested Recipe

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants