10
10
<!-- cargo-sync-rdme ]] -->
11
11
<!-- cargo-sync-rdme rustdoc [[ -->
12
12
` datatest-stable ` is a test harness intended to write * file-driven* or * data-driven* tests,
13
- where individual test cases are specified as files and not as code.
13
+ where individual test case fixtures are specified as files and not as code.
14
14
15
15
Given:
16
16
17
17
* a test ` my_test ` that accepts a path, and optionally the contents as input
18
- * a directory to look for files in
18
+ * a directory to look for files (test fixtures) in
19
19
* a pattern to match files on
20
20
21
- ` datatest-stable ` will call the ` my_test ` function once per matching file in the directory.
21
+ ` datatest-stable ` will call the ` my_test ` function once per matching file in
22
+ the directory. Directory traversals are recursive.
22
23
23
24
` datatest-stable ` works with [ cargo nextest] ( https://nexte.st/ ) , and is part of the [ nextest-rs
24
25
organization] ( https://github.com/nextest-rs/ ) on GitHub.
@@ -38,6 +39,7 @@ harness = false
38
39
39
40
* ` testfn ` - The test function to be executed on each matching input. This function can be one
40
41
of:
42
+
41
43
* ` fn(&Path) -> datatest_stable::Result<()> `
42
44
* ` fn(&Utf8Path) -> datatest_stable::Result<()> ` (` Utf8Path ` is part of the
43
45
[ ` camino ` ] ( https://docs.rs/camino ) library, and is re-exported here for convenience.)
@@ -49,14 +51,23 @@ harness = false
49
51
in as a ` Vec<u8> ` (erroring out if that failed).
50
52
* ` root ` - The path to the root directory where the input files (fixtures) live. This path is
51
53
relative to the root of the crate (the directory where the crate’s ` Cargo.toml ` is located).
54
+
55
+ ` root ` is an arbitrary expression that implements ` Display ` , such as ` &str ` , or a
56
+ function call that returns a ` Utf8PathBuf ` .
57
+
52
58
* ` pattern ` - a regex used to match against and select each file to be tested. Extended regexes
53
59
with lookaround and backtracking are supported via the
54
60
[ ` fancy_regex ` ] ( https://docs.rs/fancy-regex ) crate.
61
+
62
+ ` pattern ` is an arbitrary expression that implements ` Display ` , such as
63
+ ` &str ` , or a function call that returns a ` String ` .
64
+
65
+ The passed-in ` Path ` and ` Utf8Path ` are ** absolute** paths to the files to be tested.
55
66
56
67
The three parameters can be repeated if you have multiple sets of data-driven tests to be run:
57
68
` datatest_stable::harness!(testfn1, root1, pattern1, testfn2, root2, pattern2) ` .
58
69
59
- ## Examples
70
+ ### Examples
60
71
61
72
This is an example test. Use it with ` harness = false ` .
62
73
@@ -82,6 +93,92 @@ datatest_stable::harness!(
82
93
);
83
94
````
84
95
96
+ ### Embedding directories at compile time
97
+
98
+ With the ` include-dir ` feature enabled, you can use the
99
+ [ ` include_dir ` ] ( https://docs.rs/include_dir ) crate’s [ ` include_dir! ` ] ( https://docs.rs/include_dir_macros/0.7.4/include_dir_macros/macro.include_dir.html ) macro.
100
+ This allows you to embed directories into the binary at compile time.
101
+
102
+ This is generally not recommended for rapidly-changing test data, since each
103
+ change will force a rebuild. But it can be useful for relatively-unchanging
104
+ data suites distributed separately, e.g. on crates.io.
105
+
106
+ With the ` include-dir ` feature enabled, you can use:
107
+
108
+ ```` rust
109
+ // The `include_dir!` macro is re-exported for convenience.
110
+ use datatest_stable :: include_dir;
111
+ use std :: path :: Path ;
112
+
113
+ fn my_test (path : & Path , contents : Vec <u8 >) -> datatest_stable :: Result <()> {
114
+ // ... write test here
115
+ Ok (())
116
+ }
117
+
118
+ datatest_stable :: harness! (
119
+ my_test , include_dir! (" tests/files" ), r " ^.*/*" ,
120
+ );
121
+ ````
122
+
123
+ You can also use directories published as ` static ` items in upstream crates:
124
+
125
+ ```` rust
126
+ use datatest_stable :: {include_dir, Utf8Path };
127
+
128
+ // In the upstream crate:
129
+ pub static FIXTURES : include_dir :: Dir <'static > = include_dir! (" tests/files" );
130
+
131
+ // In your test:
132
+ fn my_test (path : & Utf8Path , contents : String ) -> datatest_stable :: Result <()> {
133
+ // ... write test here
134
+ Ok (())
135
+ }
136
+
137
+ datatest_stable :: harness! (
138
+ my_test , & FIXTURES , r " ^.*/*" ,
139
+ );
140
+ ````
141
+
142
+ In this case, the passed-in ` Path ` and ` Utf8Path ` are ** relative** to the
143
+ root of the included directory.
144
+
145
+ Because the files don’t exist on disk, the test functions must accept their
146
+ contents as either a ` String ` or a ` Vec<u8> ` . If the argument is not
147
+ provided, the harness will panic at runtime.
148
+
149
+ ### Conditionally embedding directories
150
+
151
+ It is also possible to conditionally include directories at compile time via
152
+ a feature flag. For example, you might have an internal-only ` testing `
153
+ feature that you turn on locally, but users don’t on crates.io. In that
154
+ case, you can use:
155
+
156
+ ```` rust
157
+ use datatest_stable :: Utf8Path ;
158
+
159
+ static FIXTURES : & str = " tests/files" ;
160
+
161
+ static FIXTURES : include_dir :: Dir <'static > = datatest_stable :: include_dir! (" tests/files" );
162
+
163
+ fn my_test (path : & Utf8Path , contents : String ) -> datatest_stable :: Result <()> {
164
+ // ... write test here
165
+ Ok (())
166
+ }
167
+
168
+ datatest_stable :: harness! (
169
+ my_test , & FIXTURES , r " ^.*/*" ,
170
+ );
171
+ ````
172
+
173
+ In this case, note that ` path ` will be absolute if ` FIXTURES ` is a string,
174
+ and relative if ` FIXTURES ` is a ` Dir ` . Your test should be prepared to
175
+ handle either case.
176
+
177
+ ## Features
178
+
179
+ * ` include-dir ` : Enables the ` include_dir! ` macro, which allows embedding
180
+ directories at compile time. This feature is disabled by default.
181
+
85
182
## Minimum supported Rust version (MSRV)
86
183
87
184
The minimum supported Rust version is ** Rust 1.72** . MSRV bumps may be accompanied by a minor
0 commit comments