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
@Override
protected void visitClassEx(ClassNode node) {
super.visitClassEx(node)
specification = SpockUtil.isSpockSpecification(node, rule.specificationSuperclassNames, rule.specificationClassNames)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This field seems to copy the spockSpecification field within the AbstractSpockAstVisitor superclass. Instead of redefining a new field, can you expose a protected boolean isInSpecification() for all subclasses?

}
// get() rather than the subscript operator: both keys are identifiers written in the analysed
// source, and for a String key that happens to name a JavaBean property of Map - 'metaClass',
// 'properties' - the subscript operator returns that bean property instead of the entry

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Move this inline comment down one line so it is above the actual line with the get() call.

if (arguments.size() > maxArgumentCount && !(receiver in VARARGS_RECEIVERS && isStringLiteral(arguments[0]))) {
return
}
addViolation(call, "'${receiver}.${methodName}()' is never cleaned up - use a '@TempDir' field")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this error message and the one below on line 185, isn't it possible that it is actually cleaned up elsewhere (but this rule is not checking for it)? Perhaps adjust the violation message to say "is not cleaned up automatically".


//--------------------------------------------------------------------------
// Shape 2 - ad-hoc temporary file / directory creation
//--------------------------------------------------------------------------

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Worth adding tests for calls inside a closure and within a static field initializer?

@chrismair chrismair left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall it looks really good.

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.

2 participants