Files
When testing some file-based tools, it may be necessary to create temporary files or directories, execute a test, and then observe the contents of the files.
Prepared provides helpers to refer to files even before they are created.
Configuration
Add a dependency on dev.opensavvy.prepared:compat-filesystem
to use the features on this page. Not all platforms are supported.
See the reference.
Creating temporary files and directories
New files and new directories can be created as prepared values:
val workingDirectory by createRandomDirectory()
val html by workingDirectory / "index.html" // (1)!
val css by workingDirectory / "index.css"
val output = workingDirectory / "dist"
test("…") {
html().writeText("…")
css().writeText("…")
// Do something…
check(output().readText() == "…")
}
- The
/
operator can be used to refer to child files or directories before the test starts executing. Learn more.
The files are based on prepared values, so they are created lazily as they are needed, and each test gets a different set of files to work with, ensuring no two tests can attempt to modify the same files.
All the created files are deleted automatically if the test is successful. However, if the test fails, the files are kept on disk in the operating system's temporary directory, and their path is printed in the test output, so you can open them and view their exact state.
Reading Java resources
On the JVM, it is possible to organize resources files in the same packages as source classes to avoid conflicts. To access these files, use the resource helper:
package foo.bar.baz
private object TargetClass
val test by resource<TargetClass>("test.txt")
.read()
test("Value should be the same as the file") {
check(someSystem().toString() == test())
}
This will read the file test.txt
that is placed in the same package as the provided class (TargetClass
), as a prepared value.
In this example, the file should be place in src/test/resources/foo/bar/baz/test.txt
to match the package declaration.