Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

robstoll/tutteli-spek-extensions

Repository files navigation

Download Apache license Build Status Ubuntu Build Status Windows SonarCloud Status SonarCloud Coverage

Tutteli spek extension

A set of Spek extensions such as MemoizedTempFolder.

Installation

tutteli-spek-extensions is published to maven central.

repositories { mavenCentral() }
dependencies {
    testImplementation("ch.tutteli.spek:tutteli-spek-extensions:1.2.1")
}

Features

MemoizedTempFolder

memoizedTempFolder provides -- similar to TemporaryFolder in junit4 -- utility methods to create temp files and folders and takes care of deleting them.

Specify a memoizedTempFolder within a group like scope near to the test you are going to use the tempFolder (default CachingMode is per TEST, so each test gets its own temporary directory)

import memoizedTempFolder
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

object MySpec: Spek({
    
    describe("...") {
        val tempFolder by memoizedTempFolder()

        it ("...") {
            val file = tempFolder.newFile("test.txt")
        }
    }
})

Pass a CachingMode if required (see Caching modes @ spekframework.org) For instance:

import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.verbs.expect
import memoizedTempFolder
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.spekframework.spek2.lifecycle.CachingMode

object MySpec: Spek({
    
    describe("...") {
        val tempFolder by memoizedTempFolder(CachingMode.SCOPE)
        
        it ("test1") {
            val file = tempFolder.newFile("test.txt")
            file.delete()
        }
        it("test2"){
            expect(file).exists() // would fail
        }       
    }
})

And you can use the second argument of memoizedTempFolder for additional setup:

import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.verbs.expect
import memoizedTempFolder
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.spekframework.spek2.lifecycle.CachingMode

object MySpec: Spek({
    
    describe("...") {
        val tempFolder by memoizedTempFolder(CachingMode.TEST) {
            val f = newDirectory("folderWithinTempFolder")
            newSymbolicLink("link", f)
        }
        
        it ("test1") {
            expect(tempFolder.resolve("folderWithinTempFolder")).exists()
            expect(tempFolder.resolve("link")).exists()   
        }    
    }
})

There are a few other utility methods defined on MemoizedTempFolder: newDirectory, newSymbolicLink, resolves and withinTmpDir.

Tutteli spek extension works best in combination with Niok which enhances Path with methods like createDirectories, setAttribute, writeLines and many more (not only useful in tests but also in production code). With Niok in place, more complicated setup can be defined easily:

import memoizedTempFolder
import ch.tutteli.niok.*
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.spekframework.spek2.lifecycle.CachingMode

object MySpec: Spek({
    
    describe("...") {
        val tempFolder by memoizedTempFolder(CachingMode.TEST) {
            withinTmpDir {
                val subDir = resolve("dir1/dir2/dir3").createDirectories()
                subDir.resolve("a.txt").writeLines(listOf("a", "b", "c"))
            }
        }
    }
})

And if you like to assert certain properties of a Path, then we recommend using Atrium.

License

tutteli-spek-extensions is licensed under Apache 2.0.