Skip to content

Commit 52a6e96

Browse files
committed
test: check that illegal characters are handled
1 parent ca88e91 commit 52a6e96

1 file changed

Lines changed: 34 additions & 20 deletions

File tree

src/test/kotlin/DefaultTestFilesSpec.kt

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ch.tutteli.atrium.api.fluent.en_GB.isDirectory
66
import ch.tutteli.atrium.api.fluent.en_GB.isReadable
77
import ch.tutteli.atrium.api.fluent.en_GB.isRegularFile
88
import ch.tutteli.atrium.api.fluent.en_GB.isWritable
9+
import ch.tutteli.atrium.api.fluent.en_GB.matches
910
import ch.tutteli.atrium.api.fluent.en_GB.messageContains
1011
import ch.tutteli.atrium.api.fluent.en_GB.notToBe
1112
import ch.tutteli.atrium.api.fluent.en_GB.notToThrow
@@ -161,6 +162,39 @@ object DefaultTestFilesSpec: Spek({
161162
}
162163
}
163164

165+
describe("file name checks") {
166+
it("rejects file names that match the group directory pattern") {
167+
testFiles.beforeExecuteTest(mockScope<TestScopeImpl>("rejects bad file names"))
168+
169+
expect { testFiles.createFile("[test") }.notToThrow()
170+
expect { testFiles.createFile("test]") }.notToThrow()
171+
expect { testFiles.createFile("[test]") }.toThrow<IllegalArgumentException> {
172+
messageContains("[test]")
173+
}
174+
}
175+
176+
it("rejects directory names that match the group directory pattern") {
177+
testFiles.beforeExecuteTest(mockScope<TestScopeImpl>("rejects bad directory names"))
178+
179+
expect { testFiles.createDirectory("[test") }.notToThrow()
180+
expect { testFiles.createDirectory("test]") }.notToThrow()
181+
expect { testFiles.createDirectory("[test]") }.toThrow<IllegalArgumentException> {
182+
messageContains("[test]")
183+
}
184+
}
185+
186+
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."))
193+
}
194+
}
195+
}
196+
}
197+
164198
describe("file creation") {
165199
it("creates an empty file with the provided name") {
166200
val mockGroup = mockScope<GroupScopeImpl>("named file creation group")
@@ -214,26 +248,6 @@ object DefaultTestFilesSpec: Spek({
214248
}
215249
}
216250

217-
it("rejects file names that match the group directory pattern") {
218-
testFiles.beforeExecuteTest(mockScope<TestScopeImpl>("rejects bad file names"))
219-
220-
expect { testFiles.createFile("[test") }.notToThrow()
221-
expect { testFiles.createFile("test]") }.notToThrow()
222-
expect { testFiles.createFile("[test]") }.toThrow<IllegalArgumentException> {
223-
messageContains("[test]")
224-
}
225-
}
226-
227-
it("rejects directory names that match the group directory pattern") {
228-
testFiles.beforeExecuteTest(mockScope<TestScopeImpl>("rejects bad directory names"))
229-
230-
expect { testFiles.createDirectory("[test") }.notToThrow()
231-
expect { testFiles.createDirectory("test]") }.notToThrow()
232-
expect { testFiles.createDirectory("[test]") }.toThrow<IllegalArgumentException> {
233-
messageContains("[test]")
234-
}
235-
}
236-
237251
it("hands out absolute paths") {
238252
testFiles.beforeExecuteTest(mockScope<TestScopeImpl>("hands out absolute paths"))
239253

0 commit comments

Comments
 (0)