Skip to content

Create a unique folder for no-argument TemporaryFolder#newFolder() - #1237

Merged
timtebeek merged 7 commits into
mainfrom
tim/temporary-folder-unique-new-folder
Oct 25, 2026
Merged

timtebeek merged 7 commits into
mainfrom
tim/temporary-folder-unique-new-folder

Conversation

@timtebeek

Copy link
Copy Markdown
Member

TemporaryFolderToTempDir turned a no-argument folder.newFolder() into newFolder(folder, "junit") through the generated helper. That helper calls mkdirs() on a fixed path, so a second call in the same test threw IOException: Couldn't create folders. JUnit 4 creates a fresh directory under the root on every call, and the recipe now emits the same thing JUnit 4 does internally:

File a = folder.newFolder();  // before
File a = Files.createTempDirectory(folder.toPath(), "junit").toFile();  // after

Two smaller fixes are in separate commits:

  • A rule built with TemporaryFolder.builder().assureDeletion().build() kept that initializer on the new @TempDir File field, which doesn't compile. Any initializer on a @Rule or @ClassRule field that becomes @TempDir is now dropped.
  • The name of a rewritten newFolder(..) or newFile(..) call was attributed with the enclosing class type instead of the helper's method type.

No-argument newFile() already mapped to File.createTempFile("junit", null, root), which matches JUnit 4, so it is unchanged. The Files import for the new shape is added from the method invocation, because the visitor's visitCompilationUnit never runs for Groovy sources. A new Groovy test covers this.

Verified locally on the final head: ./gradlew test --offline --tests 'org.openrewrite.java.testing.junit5.TemporaryFolderToTempDirTest' passed 22/22. A broader run over org.openrewrite.java.testing.junit5.*, testcontainers.* and MockitoJUnitToMockitoExtensionTest passed 521/534. The 13 failures were all Tooling API Gradle tests that could not fetch the Gradle model locally, so I'm leaving those to CI.

Fixes item B3 of #1211.

A no-argument `folder.newFolder()` was rewritten to `newFolder(folder, "junit")` through the generated helper, so a second call in the same test failed `mkdirs()` with "Couldn't create folders". JUnit 4 creates a fresh directory on every call via `Files.createTempDirectory(root.toPath(), "junit")`, so emit exactly that instead, and add the `Files` import from the invocation so Groovy sources get it too.
Only a `new TemporaryFolder()` initializer was removed, so `@Rule TemporaryFolder f = TemporaryFolder.builder().assureDeletion().build();` kept its builder chain on the new `@TempDir File f` field, which does not compile. JUnit injects `@TempDir` fields, so drop whatever initializer a `@Rule` or `@ClassRule` field has.
The name identifier of a rewritten `newFolder(..)` or `newFile(..)` call was given the enclosing class type rather than the helper method type, leaving inconsistent type attribution for recipes that run afterwards.
@timtebeek
timtebeek enabled auto-merge (squash) October 25, 2026 19:09
…ockUpVoidTest`

`VariableReferences.findRhsReferences` now matches identifiers semantically rather than by name (openrewrite/rewrite-static-analysis#1122), so the `MockUpClass.Save.text` field access no longer counts as a use of the `text` parameter.
@timtebeek
timtebeek merged commit edc9944 into main Oct 25, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/temporary-folder-unique-new-folder branch October 25, 2026 19:56
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant