5
5
[ ![ Build Status] ( https://travis-ci.org/ztombol/bats-file.svg?branch=master )] ( https://travis-ci.org/ztombol/bats-file )
6
6
7
7
` bats-file ` is a helper library providing common filesystem related
8
- assertions for [ Bats] [ bats ] .
8
+ assertions and helpers for [ Bats] [ bats ] .
9
9
10
10
Assertions are functions that perform a test and output relevant
11
11
information on failure to help debugging. They return 1 on failure and 0
12
12
otherwise. Output, [ formatted] [ bats-support-output ] for readability, is
13
13
sent to the standard error to make assertions usable outside of ` @test `
14
14
blocks too.
15
15
16
+ Features:
17
+ - [ assertions] ( #usage )
18
+ - [ temporary directory handling] ( #working-with-temporary-directories )
19
+
16
20
Dependencies:
17
- - [ ` bats-support ` ] [ bats-support ] - output formatting
21
+ - [ ` bats-support ` ] [ bats-support ] - output formatting, function call
22
+ restriction
18
23
19
24
See the [ shared documentation] [ bats-docs ] to learn how to install and
20
25
load this library.
@@ -60,6 +65,123 @@ path : /path/to/existing-file
60
65
```
61
66
62
67
68
+ ## Working with temporary directories
69
+
70
+ When testing code that manipulates the filesystem, it is good practice
71
+ to run tests in clean, throw-away environments to ensure correctness and
72
+ reproducibility. Therefore, this library includes convenient functions
73
+ to create and destroy temporary directories.
74
+
75
+
76
+ ### `temp_make`
77
+
78
+ Create a temporary directory for the current test in `BATS_TMPDIR`. The
79
+ directory is guaranteed to be unique and its name is derived from the
80
+ test' s filename and number for easy identification.
81
+
82
+ ```
83
+ <test-filename >-<test-number >-<random-string >
84
+ ```
85
+
86
+ This information is only available in `setup`, `@test` and `teardown`,
87
+ thus the function must be called from one of these locations.
88
+
89
+ The path of the directory is displayed on the standard output and is
90
+ meant to be captured into a variable.
91
+
92
+ ```bash
93
+ setup() {
94
+ TEST_TEMP_DIR="$(temp_make)"
95
+ }
96
+ ```
97
+
98
+ For example, for the first test in ` sample.bats ` , this snippet creates a
99
+ directory named ` sample.bats-1-XXXXXXXXXX ` , where each trailing ` X ` is a
100
+ random alphanumeric character.
101
+
102
+ If the directory cannot be created, the function fails and displays an
103
+ error message on the standard error.
104
+
105
+ ```
106
+ -- ERROR: temp_make --
107
+ mktemp: failed to create directory via template ‘/etc/samle.bats-1-XXXXXXXXXX’: Permission denied
108
+ --
109
+ ```
110
+
111
+ #### Directory name prefix
112
+
113
+ The directory name can be prefixed with an arbitrary string using the `--prefix
114
+ <prefix >` option ( ` -p <prefix >` for short).
115
+
116
+ ``` bash
117
+ setup () {
118
+ TEST_TEMP_DIR=" $( temp_make --prefix ' myapp-' ) "
119
+ }
120
+ ```
121
+
122
+ Following the previous example, this will create a directory named
123
+ ` myapp-sample.bats-1-XXXXXXXXXX ` . This can be used to group temporary
124
+ directories.
125
+
126
+ Generally speaking, the directory name is of the following form.
127
+
128
+ ```
129
+ <prefix><test-filename>-<test-number>-<random-string>
130
+ ```
131
+
132
+
133
+ ### ` temp_del `
134
+
135
+ Delete a temporary directory, typically created with ` temp_make ` .
136
+
137
+ ``` bash
138
+ teardown () {
139
+ temp_del " $TEST_TEMP_DIR "
140
+ }
141
+ ```
142
+
143
+ If the directory cannot be deleted, the function fails and displays an
144
+ error message on the standard error.
145
+
146
+ ```
147
+ -- ERROR: temp_del --
148
+ rm: cannot remove '/etc/samle.bats-1-04RUVmBP7x': No such file or directory
149
+ --
150
+ ```
151
+
152
+ _ ** Note:** Actually, this function can be used to delete any file or
153
+ directory. However, it is most useful in deleting temporary directories
154
+ created with ` temp_make ` , hence the naming._
155
+
156
+ #### Preserve directory
157
+
158
+ During development, it is useful to peak into temporary directories
159
+ post-mortem to see what the tested code has done.
160
+
161
+ When ` BATSLIB_TEMP_PRESERVE ` is set to 1, the function succeeds but the
162
+ directory is not deleted.
163
+
164
+ ``` bash
165
+ $ BATSLIB_TEMP_PRESERVE=1 bats sample.bats
166
+ ```
167
+
168
+ #### Preserve directory on failure
169
+
170
+ During debugging, it is useful to preserve the temporary directories of
171
+ failing tests.
172
+
173
+ When ` BATSLIB_TEMP_PRESERVE_ON_FAILURE ` is set to 1, the function
174
+ succeeds but the directory is not deleted if the test has failed.
175
+
176
+ ``` bash
177
+ $ BATSLIB_TEMP_PRESERVE_ON_FAILURE=1 bats sample.bats
178
+ ```
179
+
180
+ The outcome of a test is only known in ` teardown ` , therefore this
181
+ feature can be used only when ` temp_del ` is called from that location.
182
+ Otherwise and error is displayed on the standard error.
183
+
184
+
63
185
## Transforming displayed paths
64
186
65
187
Sometimes paths can be long and tiresome to parse to the human eye. To
@@ -75,21 +197,25 @@ ${path/$BATSLIB_FILE_PATH_REM/$BATSLIB_FILE_PATH_ADD}
75
197
76
198
The longest match of the pattern ` BATSLIB_FILE_PATH_REM ` is replaced
77
199
with ` BATSLIB_FILE_PATH_ADD ` . To anchor the pattern to the beginning or
78
- the end, prepend a `#` or `%`, respectively.
200
+ the end, prepend ` # ` or ` % ` , respectively.
79
201
80
202
For example, the following example hides the path of the temporary
81
203
directory where the test takes place.
82
204
83
205
``` bash
84
206
setup {
85
- TEST_TEMP_DIR=' /tmp/bats-app-temp-dir '
207
+ TEST_TEMP_DIR=" $( temp_make ) "
86
208
BATSLIB_FILE_PATH_REM=" #${TEST_TEMP_DIR} "
87
209
BATSLIB_FILE_PATH_ADD=' <temp>'
88
210
}
89
211
90
212
@test ' assert_file_exist()' {
91
213
assert_file_exist " ${TEST_TEMP_DIR} /path/to/non-existent-file"
92
214
}
215
+
216
+ teardown () {
217
+ temp_del " $TEST_TEMP_DIR "
218
+ }
93
219
```
94
220
95
221
On failure, only the relevant part of the path is shown.
0 commit comments