Skip to content

Commit 043e99e

Browse files
committed
fix: escape illegal path characters in scope names (#30)
1 parent 52a6e96 commit 043e99e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DefaultTestFiles internal constructor(): LifecycleListener, TestFiles {
5151

5252
private fun enter(target: Scope) {
5353
val currentScopeFiles = scopeFiles.peek()
54-
val nextScopeDirectory = currentScopeFiles.targetDirectory.resolve("[${target.name}]")
54+
val nextScopeDirectory = currentScopeFiles.targetDirectory.resolve("[${escapeScopeName(target.name)}]")
5555
clear(nextScopeDirectory)
5656
scopeFiles.push(ScopeFiles(nextScopeDirectory))
5757
}
@@ -63,6 +63,8 @@ class DefaultTestFiles internal constructor(): LifecycleListener, TestFiles {
6363
scopeFiles.pop().cleanup()
6464
}
6565

66+
private fun escapeScopeName(name: String) = name.replace(invalidFileNameCharacters, "-")
67+
6668
private val Scope.name: String
6769
get() = when (this) {
6870
is ScopeImpl -> this.id.name
@@ -185,3 +187,11 @@ private inline fun tolerateDoesNotExist(path: Path, block: (Path) -> Unit) {
185187
// swallow
186188
}
187189
}
190+
191+
private val unixInvalidCharacters get() = Regex("[/\u0000]")
192+
private val windowsInvalidCharacters get() = Regex("[/\\\\<>:\"|?*\u0000]")
193+
private val invalidFileNameCharacters by lazy {
194+
val osName = System.getProperty("os.name").toLowerCase()
195+
if (setOf("nix", "nux", "aix", "mac").any { osName.contains(it) }) unixInvalidCharacters
196+
else windowsInvalidCharacters // default to windows because it is the most restrictive
197+
}

src/test/kotlin/DefaultTestFilesSpec.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,17 @@ object DefaultTestFilesSpec: Spek({
184184
}
185185

186186
listOf('/', '\\', '<', '>', ':', '\"', '|', '?', '*', '\u0000').forEach { badCharacter ->
187-
it("escapes '$badCharacter' if necessary") {
188-
testFiles.beforeExecuteTest(mockScope<TestScopeImpl>("test with -$badCharacter- in it"))
189-
190-
expect { testFiles.createFile("test") }.notToThrow {
191-
// check that / \ is not messing up the directory structure
192-
parent.fileName.matches(Regex(".test with -[^-]- in it."))
187+
listOf(
188+
"group" to { name: String -> testFiles.beforeExecuteGroup(mockScope<GroupScopeImpl>(name)) },
189+
"test" to { name: String -> testFiles.beforeExecuteTest(mockScope<TestScopeImpl>(name)) }
190+
).forEach { (scopeType, enterScope) ->
191+
it("escapes '$badCharacter' in a $scopeType name if necessary") {
192+
enterScope("test with -$badCharacter- in it")
193+
194+
expect { testFiles.createFile("test") }.notToThrow {
195+
// check that / \ is not messing up the directory structure
196+
parent.fileName.matches(Regex(".test with -.- in it."))
197+
}
193198
}
194199
}
195200
}

0 commit comments

Comments
 (0)