Need to create project directory explicitly before each unit test

Answered

I'm using light unit tests with default fixture:

class ProjectTest : BasePlatformTestCase() {

    var file: String? = null

    @BeforeEach
    fun setup() {
        super.setUp()
        file = "${project.basePath}/${UUID.randomUUID()}.tmp"
        Files.createDirectories(Path.of(project.basePath)) <-- without this second test fails because of missing directory
    }

    @AfterEach
    fun teardown() {
        super.tearDown()
    }

    @Test
    fun test1() {
        assertTrue(createFile()?.exists() == true)
    }

    @Test
    fun test2() {
        assertTrue(createFile()?.exists() == true)
    }

    private fun createFile(): VirtualFile? {
        val tempFile = File("${project.basePath}/${UUID.randomUUID()}.tmp")
        tempFile.createNewFile() <-- fails here 2nd time because parent dir does not exist
        tempFile.deleteOnExit()
        return VfsUtil.findFileByIoFile(tempFile, true)
    }

}

Seems like project directory is removed on tearDown but it is not created on setUp.

Please point me to right direction if above is wrong.

0
3 comments

Use the provided CodeInsightTestFixture from field myFixture to create files/directories in the current test project instance.

0

Thank for hint, but above creates file in `temp://` which is not reachable in my project because I'm using findFileByIoFile due to processing file by io paths.

0

Why do you use regular java.io.File instead of VirtualFile if they're located inside the project AFAIU?

Otherwise, you can employ some hack to distinguish test mode filesystem

final VirtualFile relativeFile = ApplicationManager.getApplication().isUnitTestMode() ?
                                 TempFileSystem.getInstance().findFileByPath(path) :
                                 <IO File Lookup>;
1

Please sign in to leave a comment.