|
| 1 | +# spek-testfiles |
| 2 | + |
| 3 | +[Spek](https://www.spekframework.org/) extension to easily create files and directories for testing purposes. |
| 4 | + |
| 5 | + * Organises test files according to the test structure |
| 6 | + * Cleans up test files but can leave them in place if the test fails so you can examine them |
| 7 | + * Generates random file names that are the same each test run |
| 8 | + |
| 9 | + |
| 10 | +## Example |
| 11 | + |
| 12 | +```kotlin |
| 13 | +import de.joshuagleitze.test.spek.testfiles.DeletionMode.* |
| 14 | +import de.joshuagleitze.test.spek.testfiles.testFiles |
| 15 | +import org.spekframework.spek2.Spek |
| 16 | +import org.spekframework.spek2.style.specification.describe |
| 17 | + |
| 18 | +object ExampleSpek : Spek({ |
| 19 | + val testFiles = testFiles() |
| 20 | + |
| 21 | + describe("using test files") { |
| 22 | + it("generates file names") { |
| 23 | + testFiles.createFile() |
| 24 | + testFiles.createDirectory() |
| 25 | + } |
| 26 | + |
| 27 | + it("cleans up files") { |
| 28 | + testFiles.createFile("irrelevant", delete = ALWAYS) |
| 29 | + testFiles.createFile("default mode", delete = IF_SUCCESSFUL) |
| 30 | + testFiles.createFile("output", delete = NEVER) |
| 31 | + } |
| 32 | + } |
| 33 | +}) |
| 34 | +``` |
| 35 | + |
| 36 | +## Test File Directory |
| 37 | + |
| 38 | +`spek-testfiles` will pick the folder it creates the testfiles in automatically according to this logic: |
| 39 | + |
| 40 | +Case | Test File Directory | Use Case |
| 41 | +--- | --- | --- |
| 42 | +`<pwd>/build` exists | `<pwd>/build/test-outputs` | Gradle users |
| 43 | +`<pwd>/target` exists | `<pwd>/target/test-outputs` | Maven users |
| 44 | +`<pwd>/test-outputs` exists | `<pwd>/test-outputs` | Other users wishing to have the files in the project directory |
| 45 | +else | `<tmpdir>/spek-test-outputs` | Fallback |
| 46 | + |
| 47 | +The files in the test directory will be organized into directories according to the test structure. |
| 48 | +For example, the “output” file from the example above would be created at `<test file directory>/[ExampleSpek]/[using test files]/[cleans up files]/output`. |
| 49 | + |
| 50 | +## Deletion Mode |
| 51 | + |
| 52 | +Per default, `spek-testiles` will delete created files and directories if the current test or group was successful and retain them if the test or group failed. |
| 53 | +This allows you to examine test output after tests fails, but does not clutter your test output folder. |
| 54 | + |
| 55 | +The behaviour can be changed by passing the correct [`DeletionMode`](https://jgleitz.github.io/spek-testfiles/spek-testfiles/de.joshuagleitze.test.spek.testfiles/-deletion-mode/index.html) to [`createFile`](https://jgleitz.github.io/spek-testfiles/spek-testfiles/de.joshuagleitze.test.spek.testfiles/-test-files/create-file.html) or [`createDirectory`](https://jgleitz.github.io/spek-testfiles/spek-testfiles/de.joshuagleitze.test.spek.testfiles/-test-files/create-directory.html): |
| 56 | + |
| 57 | +[`DeletionMode`](https://jgleitz.github.io/spek-testfiles/spek-testfiles/de.joshuagleitze.test.spek.testfiles/-deletion-mode/index.html) | behaviour |
| 58 | +--- | --- |
| 59 | +[`ALWAYS`](https://jgleitz.github.io/spek-testfiles/spek-testfiles/de.joshuagleitze.test.spek.testfiles/-deletion-mode/-a-l-w-a-y-s.html) | Always delete the created file after the test or group has run. |
| 60 | +[`IF_SUCCESSFUL`](https://jgleitz.github.io/spek-testfiles/spek-testfiles/de.joshuagleitze.test.spek.testfiles/-deletion-mode/-i-f_-s-u-c-c-e-s-s-f-u-l.html) | Delete the file if the test or group ran through successfully; retain the file if the test or group failed. The default value. |
| 61 | +[`NEVER`](https://jgleitz.github.io/spek-testfiles/spek-testfiles/de.joshuagleitze.test.spek.testfiles/-deletion-mode/-n-e-v-e-r.html) | always retain the file after the test run. |
| 62 | + |
| 63 | +Retained files will be deleted once the group or test they were created for is executed again. |
| 64 | + |
| 65 | +## File Names |
| 66 | + |
| 67 | +If no file or directory name is provided, `spek-testfiles` will create one. |
| 68 | +Names are constructed following the pattern `test-<number>` where `<number>` is a randomly generated non-negative integer. |
| 69 | +The random generator is seeded with the current test directory path to make sure the same test will always get the same file names every execution. |
| 70 | + |
| 71 | +For example, the first generated file from the example above might have the path `<test file directory>/[ExampleSpek]/[using test files]/[generates file names]/test-162363182`. |
| 72 | + |
| 73 | +If a file name is provided, the name must not be wrapped in angle brackets; i.e. either not start with `[` or not end with `]`. |
0 commit comments