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.
0aca69f to
cf99fb6
Compare
| @Override | ||
| protected void visitClassEx(ClassNode node) { | ||
| super.visitClassEx(node) | ||
| specification = SpockUtil.isSpockSpecification(node, rule.specificationSuperclassNames, rule.specificationClassNames) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 | ||
| //-------------------------------------------------------------------------- |
There was a problem hiding this comment.
Worth adding tests for calls inside a closure and within a static field initializer?
chrismair
left a comment
There was a problem hiding this comment.
Overall it looks really good.
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