Skip to content

Commit

Permalink
Create a tmp directory when such doesn't exist. In some cases the tmp…
Browse files Browse the repository at this point in the history
… director won't exist and an Exception will be thrown when attempting to create a file in that directory. An example of such case is creating a JenkinsRule instance in some tests and executing those tests from an IDE.
  • Loading branch information
Antonio.Busuladzich committed Nov 21, 2024
1 parent b8e5141 commit 05531ef
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/hudson/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@ public static File createTempDir() throws IOException {
// https://github.com/jenkinsci/jenkins/pull/3161 )
final Path tempPath;
final String tempDirNamePrefix = "jenkins";

final Path systemTmpDirectoryPath = Path.of(System.getProperty("java.io.tmpdir"));
if (!systemTmpDirectoryPath.toFile().exists()){

Check warning on line 441 in core/src/main/java/hudson/Util.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 441 is only partially covered, one branch is missing
// In some cases the tmp directory set in the java.io.tmpdir property will not exist and hence will have to
// be created here.
systemTmpDirectoryPath.toFile().mkdirs();

Check warning on line 444 in core/src/main/java/hudson/Util.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 444 is not covered by tests

Check warning on line 444 in core/src/main/java/hudson/Util.java

View check run for this annotation

ci.jenkins.io / SpotBugs

RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

NORMAL: Exceptional return value of java.io.File.mkdirs() ignored in hudson.Util.createTempDir()
Raw output
<p> This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the <code>File.delete()</code> method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value. </p>
}

if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
tempPath = Files.createTempDirectory(tempDirNamePrefix,
PosixFilePermissions.asFileAttribute(EnumSet.allOf(PosixFilePermission.class)));
Expand Down

0 comments on commit 05531ef

Please sign in to comment.