Repository navigation
Conversation
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
force-pushed
the
spock/rule-use-temp-dir
branch
from
October 30, 2026 16:24
0aca69f to
cf99fb6
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Spock's own
@TempDiris 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 TemporaryFolderis a JUnit 4 carry-over that only works through Spock's JUnit rule interop, andFile.createTempDir()/Files.createTempDirectory(..)are never cleaned up unless somebody hand-writes acleanup: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:
@Ruleor@ClassRulefield whose type — or, for an untyped field, whose initializer — isTemporaryFolder.File.createTempDir(..),File.createTempFile(prefix, suffix),Files.createTempDirectory(prefix, ..),Files.createTempFile(prefix, suffix, ..).new File(System.getProperty('java.io.tmpdir'), ..), including the concatenatedSystem.getProperty('java.io.tmpdir') + '/x'form.Not reported:
@TempDirfield or parameter of any injectable type —Path,File,FileSystemFixture.Files.createTempFile(tempDir, 'a', 'b')orFile.createTempFile('a', 'b', dir).fixtures.createTempDir()orFileUtils.createTempDirectory('x').The ad-hoc creation shape can be switched off with
checkAdHocCreation, which leaves only theTemporaryFolderfields reported.Telling the overloads apart
The rule runs on the unresolved AST, so it matches the receiver textually against
FileandFilesand 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 inFileAttribute...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
Files.createTempDirectory(prefix, attribute)stays silent.createTempDirectory('x')) or an aliased receiver is not recognized.Implements #844