Assertj-core: Files#temporaryFolder() will fail if System temporary folder does not exist

Created on 6 Aug 2018  路  10Comments  路  Source: assertj/assertj-core

Summary

We noticed this issue when working on a docker based build. We found that the Java defined property for java.io.tmpdir was defined but the folder did _not exist_. This caused calls to Files#temporaryFolder to fail with the exception:

org.assertj.core.api.exception.RuntimeIOException: Unable to find temporary directory

Example

    public static void main(String ... args) {
        System.setProperty("java.io.tmpdir", "badger");
        assertThat(new File(System.getProperty("java.io.tmpdir"))).doesNotExist();
        Files.temporaryFolder(); // Will fail
    }

The following code sample would resolve this issue. I am happy to raise this as a pull request if this issue is considered valid.

    public static File temporaryFolder() {
        File temp = new File(Files.temporaryFolderPath());
        if (!temp.isDirectory()) {
            throw new RuntimeIOException("Unable to find temporary directory");
        }
        if (!temp.exists() && !temp.mkdirs()) {
            throw new RuntimeIOException("Failed to create temporary directory");
        }
        return temp;
    }
improvement

All 10 comments

If no one is working on this issue, I'd like to work on it. @joel-costigliola

Sure, go for it @Sunt-ing

So when we only set the property for java.io.tmpdir like System.setProperty("java.io.tmpdir", "badger");, although there is no temporary folder, we don't want Files.temporaryFolder(); to fail, right?

What needs to be done here is trying to create the directory if it does not exist

This seems to be an old feature which probably doesn't make sense anymore, considering that for example JUnit 4 and 5 have already good support for temporary directories.

I'd propose not to spend effort on it and to deprecate this api.

It seems that we should close this issue now.

let's just deprecate it and then we can close it

Should I add an annotation @depracated before the method File temporaryFolder() and then create a PR?

Sure, also add in javadoc a deprected annotation and explain the alternatives (JUnit 5 + JUnit 4)

OK. Please check this simple PR first: #1851

Was this page helpful?
0 / 5 - 0 ratings