Skip to content

New SpockUseTempDir rule - #877

Open
leonard84 wants to merge 1 commit into
CodeNarc:masterfrom
leonard84:spock/rule-use-temp-dir
Open

leonard84 wants to merge 1 commit into
CodeNarc:masterfrom
leonard84:spock/rule-use-temp-dir

Conversation

@leonard84

@leonard84 leonard84 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Spock's own @TempDir is the lifecycle-correct temporary directory: one per iteration by default, one per specification when combined with @Shared, and always deleted afterwards.
The alternatives leak.
@Rule TemporaryFolder is a JUnit 4 carry-over that only works through Spock's JUnit rule interop, and File.createTempDir() / Files.createTempDirectory(..) are never cleaned up unless somebody hand-writes a cleanup: block for them.

This rule reports both shapes in a Spock specification, with distinct messages, because the fixes differ in effort.

What is and is not reported

Reported:

  • A @Rule or @ClassRule field whose type — or, for an untyped field, whose initializer — is TemporaryFolder.
  • A call that creates a file or directory in the JVM-wide default temporary directory: File.createTempDir(..), File.createTempFile(prefix, suffix), Files.createTempDirectory(prefix, ..), Files.createTempFile(prefix, suffix, ..).
  • new File(System.getProperty('java.io.tmpdir'), ..), including the concatenated System.getProperty('java.io.tmpdir') + '/x' form.

Not reported:

  • A @TempDir field or parameter of any injectable type — Path, File, FileSystemFixture.
  • The overloads that take an explicit target directory, e.g. Files.createTempFile(tempDir, 'a', 'b') or File.createTempFile('a', 'b', dir).
  • Similarly named helpers on other receivers, such as fixtures.createTempDir() or FileUtils.createTempDirectory('x').
  • Anything outside a Spock specification, including a helper class in the same file.

The ad-hoc creation shape can be switched off with checkAdHocCreation, which leaves only the TemporaryFolder fields reported.

Telling the overloads apart

The rule runs on the unresolved AST, so it matches the receiver textually against File and Files and tells the overloads apart by argument count: each explicit-directory overload takes one more argument than its default-directory sibling.
The exception is Files, whose default-directory overloads end in FileAttribute... varargs.
There a call with extra arguments is only reported when its first argument is a String literal or GString, as in Files.createTempDirectory('x', attribute).

Known limits

  • A variable in that first position could hold either the prefix or the directory, so Files.createTempDirectory(prefix, attribute) stays silent.
  • A static import (createTempDirectory('x')) or an aliased receiver is not recognized.

Implements #844

Reports the two ways a Spock specification can get a temporary directory
that Spock does not manage: a JUnit 4 @Rule/@ClassRule TemporaryFolder
field, and a call that creates a file or directory in the JVM-wide
default temporary directory. Spock's own @tempdir replaces both.

Only calls that positively select the default temporary directory are
reported; a call handed an explicit target directory, such as a @tempdir
field or parameter, is left alone. The overloads are told apart by
argument count, except for the Files methods with trailing FileAttribute
varargs, which are only reported when the first argument is a String
literal. The ad-hoc creation shape can be switched off with the
checkAdHocCreation property.
@leonard84
leonard84 force-pushed the spock/rule-use-temp-dir branch from 0aca69f to cf99fb6 Compare October 30, 2026 16:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant