Skip to content

Commit 425c587

Browse files
committed
test: test in special pwd and test file root determination
1 parent bdb4f19 commit 425c587

File tree

5 files changed

+78
-30
lines changed

5 files changed

+78
-30
lines changed

build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ dependencies {
4141
tasks.withType<Test> {
4242
useJUnitPlatform()
4343
reports.junitXml.isEnabled = true
44+
45+
val testPwd = buildDir.resolve("test-pwd")
46+
doFirst {
47+
testPwd.mkdirs()
48+
}
49+
workingDir = testPwd
4450
}
4551

4652
java {

src/main/kotlin/de/joshuagleitze/test/spek/testfiles/DefaultTestFiles.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class DefaultTestFiles internal constructor(): LifecycleListener, TestFiles {
118118

119119
companion object {
120120
private val ROOT_SCOPE_FILES by lazy {
121-
ScopeFiles(testFilesRootDirectory)
121+
ScopeFiles(determineTestFilesRootDirectory())
122122
}
123123

124124
/**
@@ -127,16 +127,13 @@ class DefaultTestFiles internal constructor(): LifecycleListener, TestFiles {
127127
val SCOPE_DIRECTORY_PATTERN = Regex("^\\[.*]$")
128128

129129
/**
130-
* The root directory within which all test files will be created. Accessing this property may create the
131-
* directory if it did not exist before.
130+
* Determines the root directory within which all test files will be created.
132131
*/
133-
val testFilesRootDirectory: Path by lazy {
134-
when {
135-
isDirectory(Paths.get("build")) -> createDirectories(Paths.get("build/test-outputs"))
136-
isDirectory(Paths.get("target")) -> createDirectories(Paths.get("target/test-outputs"))
137-
isDirectory(Paths.get("test-outputs")) -> createDirectories(Paths.get("test-outputs"))
138-
else -> createDirectories(Paths.get(System.getProperty("java.io.tmpdir")).resolve("spek-test-outputs"))
139-
}
132+
fun determineTestFilesRootDirectory(): Path = when {
133+
isDirectory(Paths.get("build")) -> Paths.get("build/test-outputs")
134+
isDirectory(Paths.get("target")) -> Paths.get("target/test-outputs")
135+
isDirectory(Paths.get("test-outputs")) -> Paths.get("test-outputs")
136+
else -> Paths.get(System.getProperty("java.io.tmpdir")).resolve("spek-test-outputs")
140137
}
141138

142139
private fun checkFileName(name: String) {

src/test/kotlin/DefaultTestFilesSpec.kt

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import ch.tutteli.atrium.api.fluent.en_GB.startsWith
1313
import ch.tutteli.atrium.api.fluent.en_GB.toBe
1414
import ch.tutteli.atrium.api.fluent.en_GB.toThrow
1515
import ch.tutteli.atrium.api.verbs.expect
16-
import ch.tutteli.niok.deleteRecursively
1716
import de.joshuagleitze.test.spek.testfiles.DefaultTestFiles
17+
import de.joshuagleitze.test.spek.testfiles.DefaultTestFiles.Companion.determineTestFilesRootDirectory
1818
import de.joshuagleitze.test.spek.testfiles.DeletionMode.ALWAYS
1919
import de.joshuagleitze.test.spek.testfiles.DeletionMode.IF_SUCCESSFUL
2020
import de.joshuagleitze.test.spek.testfiles.DeletionMode.NEVER
@@ -27,26 +27,47 @@ import org.spekframework.spek2.style.specification.describe
2727
import java.nio.file.Files.createDirectories
2828
import java.nio.file.Files.createDirectory
2929
import java.nio.file.Files.createFile
30-
import java.nio.file.Paths
3130

3231
object DefaultTestFilesSpec: Spek({
33-
val expectedRootFolder = Paths.get("build/test-outputs")
32+
val fileRoot = freezeFileRoot()
3433
lateinit var testFiles: DefaultTestFiles
3534

36-
beforeGroup {
37-
expectedRootFolder.deleteRecursively()
38-
}
39-
4035
beforeEachTest {
4136
testFiles = DefaultTestFiles()
4237
}
4338

4439
describe("DefaultTestFiles") {
45-
describe("Housekeeping") {
40+
describe("root folder") {
41+
beforeEachTest { deletePotentialTargetDirectories() }
42+
43+
it("uses the build directory if present") {
44+
createDirectories(buildDir)
45+
createDirectories(targetDir)
46+
createDirectories(testOutputsDir)
47+
expect(determineTestFilesRootDirectory()).toBe(buildDir.resolve("test-outputs"))
48+
}
49+
50+
it("uses the target directory if present") {
51+
createDirectories(targetDir)
52+
createDirectories(testOutputsDir)
53+
expect(determineTestFilesRootDirectory()).toBe(targetDir.resolve("test-outputs"))
54+
}
55+
56+
it("uses the test-outputs directory if present") {
57+
createDirectories(testOutputsDir)
58+
expect(determineTestFilesRootDirectory()).toBe(testOutputsDir)
59+
}
60+
61+
it("falls back to the tmpdir") {
62+
expect(determineTestFilesRootDirectory()).toBe(tmpDir.resolve("spek-test-outputs"))
63+
}
64+
}
65+
66+
describe("housekeeping") {
4667
it("clears pre-existing files when entering a group") {
4768
val mockGroup = mockScope<GroupScopeImpl>("delete pre-existing group")
4869

49-
val scopeDir = createDirectories(expectedRootFolder.resolve("[delete pre-existing group]"))
70+
val scopeDir = createDirectories(fileRoot.resolve("[delete pre-existing group]"))
5071
val testDir = createDirectory(scopeDir.resolve("pre-existing dir"))
5172
val subTestFile = createFile(testDir.resolve("sub"))
5273
val testFile = createFile(scopeDir.resolve("pre-existing file"))
@@ -64,7 +85,7 @@ object DefaultTestFilesSpec: Spek({
6485
it("clears pre-existing files when entering a test") {
6586
val mockTest = mockScope<TestScopeImpl>("delete pre-existing test")
6687

67-
val scopeDir = createDirectories(expectedRootFolder.resolve("[delete pre-existing test]"))
88+
val scopeDir = createDirectories(fileRoot.resolve("[delete pre-existing test]"))
6889
val testDir = createDirectory(scopeDir.resolve("pre-existing dir"))
6990
val subTestFile = createFile(testDir.resolve("deeper pre-existing file"))
7091
val testFile = createFile(scopeDir.resolve("pre-existing file"))
@@ -82,7 +103,7 @@ object DefaultTestFilesSpec: Spek({
82103
it("retains existing group directories when entering a group") {
83104
val mockGroup = mockScope<GroupScopeImpl>("retain pre-existing group")
84105

85-
val scopeDir = createDirectories(expectedRootFolder.resolve("[retain pre-existing group]"))
106+
val scopeDir = createDirectories(fileRoot.resolve("[retain pre-existing group]"))
86107
val groupTestDir = createDirectory(scopeDir.resolve("[test]"))
87108
val subTestFile = createFile(groupTestDir.resolve("deeper pre-existing file"))
88109
val testFile = createFile(scopeDir.resolve("pre-existing file"))
@@ -100,7 +121,7 @@ object DefaultTestFilesSpec: Spek({
100121
it("retains existing group directories when entering a test") {
101122
val mockTest = mockScope<TestScopeImpl>("retain pre-existing test")
102123

103-
val scopeDir = createDirectories(expectedRootFolder.resolve("[retain pre-existing test]"))
124+
val scopeDir = createDirectories(fileRoot.resolve("[retain pre-existing test]"))
104125
val groupTestDir = createDirectory(scopeDir.resolve("[test]"))
105126
val subTestFile = createFile(groupTestDir.resolve("deeper pre-existing file"))
106127
val testFile = createFile(scopeDir.resolve("pre-existing file"))
@@ -117,7 +138,7 @@ object DefaultTestFilesSpec: Spek({
117138

118139
it("does not create a group or test folder if not necessary") {
119140
val mockGroup = mockScope<GroupScopeImpl>("no premature creation group")
120-
val mockGroupTarget = expectedRootFolder.resolve("[no premature creation group]")
141+
val mockGroupTarget = fileRoot.resolve("[no premature creation group]")
121142
val mockSubGroup = mockScope<GroupScopeImpl>("sub")
122143
val mockSubGroupTarget = mockGroupTarget.resolve("[sub]")
123144
val mockTest = mockScope<TestScopeImpl>("test")
@@ -143,7 +164,7 @@ object DefaultTestFilesSpec: Spek({
143164
describe("file creation") {
144165
it("creates an empty file with the provided name") {
145166
val mockGroup = mockScope<GroupScopeImpl>("named file creation group")
146-
val mockGroupTarget = expectedRootFolder.resolve("[named file creation group]")
167+
val mockGroupTarget = fileRoot.resolve("[named file creation group]")
147168
val mockTest = mockScope<TestScopeImpl>("test")
148169
val mockTestTarget = mockGroupTarget.resolve("[test]")
149170

@@ -170,7 +191,7 @@ object DefaultTestFilesSpec: Spek({
170191

171192
it("creates an empty directory with the provided name") {
172193
val mockGroup = mockScope<GroupScopeImpl>("named directory creation group")
173-
val mockGroupTarget = expectedRootFolder.resolve("[named directory creation group]")
194+
val mockGroupTarget = fileRoot.resolve("[named directory creation group]")
174195
val mockTest = mockScope<TestScopeImpl>("test")
175196
val mockTestTarget = mockGroupTarget.resolve("[test]")
176197

@@ -215,7 +236,7 @@ object DefaultTestFilesSpec: Spek({
215236

216237
it("creates an empty file with a generated name") {
217238
val mockGroup = mockScope<GroupScopeImpl>("unnamed file creation group")
218-
val mockGroupTarget = expectedRootFolder.resolve("[unnamed file creation group]")
239+
val mockGroupTarget = fileRoot.resolve("[unnamed file creation group]")
219240
val mockTest = mockScope<TestScopeImpl>("test")
220241
val mockTestTarget = mockGroupTarget.resolve("[test]")
221242

@@ -242,7 +263,7 @@ object DefaultTestFilesSpec: Spek({
242263

243264
it("creates an empty directory with a generated name") {
244265
val mockGroup = mockScope<GroupScopeImpl>("unnamed directory creation group")
245-
val mockGroupTarget = expectedRootFolder.resolve("[unnamed directory creation group]")
266+
val mockGroupTarget = fileRoot.resolve("[unnamed directory creation group]")
246267
val mockTest = mockScope<TestScopeImpl>("test")
247268
val mockTestTarget = mockGroupTarget.resolve("[test]")
248269

src/test/kotlin/TestFileIntegrationSpec.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import ch.tutteli.atrium.api.verbs.expect
88
import de.joshuagleitze.test.spek.testfiles.testFiles
99
import org.spekframework.spek2.Spek
1010
import org.spekframework.spek2.style.specification.describe
11-
import java.nio.file.Paths
1211

1312
object TestFileIntegrationSpec: Spek({
14-
val expectedRootFolder = Paths.get("build/test-outputs")
13+
val fileRoot = freezeFileRoot()
1514
val testFiles = testFiles()
1615

1716
describe("testFiles") {
18-
val expectedGroupFolder = expectedRootFolder
17+
val expectedGroupFolder = fileRoot
1918
.resolve("[TestFileIntegrationSpec]")
2019
.resolve("[testFiles]")
2120

src/test/kotlin/util.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
import ch.tutteli.atrium.api.fluent.en_GB.feature
22
import ch.tutteli.atrium.creating.Expect
3+
import ch.tutteli.niok.deleteRecursively
4+
import de.joshuagleitze.test.spek.testfiles.DefaultTestFiles
5+
import de.joshuagleitze.test.spek.testfiles.DefaultTestFiles.Companion.determineTestFilesRootDirectory
36
import io.mockk.every
47
import io.mockk.mockk
58
import org.spekframework.spek2.runtime.scope.ScopeId
69
import org.spekframework.spek2.runtime.scope.ScopeImpl
710
import org.spekframework.spek2.runtime.scope.ScopeType
11+
import java.nio.file.Files.createDirectories
812
import java.nio.file.Files.readAllBytes
913
import java.nio.file.Path
14+
import java.nio.file.Paths
1015

1116
internal inline fun <reified S: ScopeImpl> mockScope(name: String) = mockk<S>().also {
1217
every { it.id } returns ScopeId(ScopeType.Scope, name)
1318
}
1419

1520
internal val Expect<Path>.content get() = feature("string content") { String(readAllBytes(this)) }
21+
22+
val buildDir = Paths.get("build")
23+
val targetDir = Paths.get("target")
24+
val testOutputsDir = Paths.get("test-outputs")
25+
val tmpDir = Paths.get(System.getProperty("java.io.tmpdir"))
26+
27+
fun deletePotentialTargetDirectories() {
28+
listOf(buildDir, targetDir, testOutputsDir).forEach { it.deleteRecursively() }
29+
determineTestFilesRootDirectory().deleteRecursively()
30+
}
31+
32+
private val fileRoot by lazy {
33+
deletePotentialTargetDirectories()
34+
createDirectories(testOutputsDir)
35+
val fileRoot = determineTestFilesRootDirectory()
36+
DefaultTestFiles() // call constructor to freeze output directory
37+
fileRoot
38+
}
39+
40+
fun freezeFileRoot() = fileRoot

0 commit comments

Comments
 (0)