@@ -9,6 +9,7 @@ mod args;
9
9
mod writer;
10
10
11
11
fn main ( ) {
12
+ // codecov: disable
12
13
let args = args:: Args :: parse ( ) ;
13
14
14
15
let keys: Vec < String > = args
@@ -42,14 +43,17 @@ fn main() {
42
43
mod tests {
43
44
use std:: fs:: File ;
44
45
use std:: io:: Read ;
46
+ use std:: process;
47
+
48
+ use crate :: writer:: create_output_directory;
45
49
46
50
use super :: * ;
47
51
48
52
struct TestContext {
49
53
output_directory : PathBuf ,
50
54
}
51
55
52
- fn setup ( directory : & str ) -> TestContext {
56
+ fn setup ( directory : & str , create_output_dir : bool ) -> TestContext {
53
57
println ! ( "Test setup..." ) ;
54
58
55
59
let current_directory: PathBuf = env:: current_dir ( ) . unwrap_or_else ( |err| {
@@ -59,22 +63,8 @@ mod tests {
59
63
60
64
let output_directory: PathBuf = current_directory. join ( directory) ;
61
65
62
- // Create the output directory if it doesn't exist
63
- if !output_directory. exists ( ) {
64
- println ! ( "Creating output directory..." ) ;
65
- match std:: fs:: create_dir_all ( & output_directory) {
66
- Ok ( _) => println ! ( "Output directory created successfully." ) ,
67
- Err ( e) => {
68
- eprintln ! (
69
- "Error creating output directory '{}': {}" ,
70
- output_directory. display( ) ,
71
- e
72
- ) ;
73
- std:: process:: exit ( 1 ) ;
74
- }
75
- }
76
- } else {
77
- println ! ( "Output directory already exists." ) ;
66
+ if create_output_dir {
67
+ create_output_directory ( & output_directory) ;
78
68
}
79
69
80
70
TestContext { output_directory }
@@ -133,7 +123,7 @@ mod tests {
133
123
"txt" ,
134
124
"--skip-missing-keys" ,
135
125
] ) ;
136
- let context = setup ( & args. directory ) ;
126
+ let context = setup ( & args. directory , true ) ;
137
127
let output_directory = & context. output_directory ;
138
128
let keys: Vec < String > = args
139
129
. keys
@@ -181,7 +171,7 @@ mod tests {
181
171
"json" ,
182
172
"--skip-missing-keys" ,
183
173
] ) ;
184
- let context = setup ( & args. directory ) ;
174
+ let context = setup ( & args. directory , true ) ;
185
175
let output_directory = & context. output_directory ;
186
176
let keys: Vec < String > = args
187
177
. keys
@@ -214,4 +204,132 @@ mod tests {
214
204
assert_eq ! ( contents, "\" value2\" " ) ;
215
205
teardown ( & context) ;
216
206
}
207
+
208
+ #[ test]
209
+ fn test_if_output_directory_doesnt_exist_it_gets_created ( ) {
210
+ let args = args:: Args :: parse_from ( & [
211
+ "" ,
212
+ "--keys" ,
213
+ "key1 key2" ,
214
+ "--outputs" ,
215
+ "{ \" key1\" : \" value1\" , \" key2\" : \" value2\" , \" key3\" : \" value3\" }" ,
216
+ "--directory" ,
217
+ "test_output_directory_doesnt_exist" ,
218
+ "--extension" ,
219
+ "txt" ,
220
+ "--skip-missing-keys" ,
221
+ ] ) ;
222
+
223
+ let context = setup ( & args. directory , false ) ;
224
+ let output_directory = & context. output_directory ;
225
+ let keys: Vec < String > = args
226
+ . keys
227
+ . split_whitespace ( )
228
+ . map ( |s| s. to_string ( ) )
229
+ . collect ( ) ;
230
+
231
+ write_outputs (
232
+ & args. skip_missing_keys ,
233
+ & keys,
234
+ & args. outputs ,
235
+ & output_directory,
236
+ & args. extension ,
237
+ & args. verbose ,
238
+ ) ;
239
+
240
+ // Check that the files were created.
241
+ assert ! ( output_directory. join( "key1.txt" ) . exists( ) ) ;
242
+ assert ! ( output_directory. join( "key2.txt" ) . exists( ) ) ;
243
+
244
+ // Check that the files contain the correct values.
245
+ let mut file = File :: open ( output_directory. join ( "key1.txt" ) ) . unwrap ( ) ;
246
+ let mut contents = String :: new ( ) ;
247
+ file. read_to_string ( & mut contents) . unwrap ( ) ;
248
+ assert_eq ! ( contents, "value1" ) ;
249
+
250
+ let mut file = File :: open ( output_directory. join ( "key2.txt" ) ) . unwrap ( ) ;
251
+ let mut contents = String :: new ( ) ;
252
+ file. read_to_string ( & mut contents) . unwrap ( ) ;
253
+ assert_eq ! ( contents, "value2" ) ;
254
+ teardown ( & context) ;
255
+ }
256
+
257
+ #[ test]
258
+ fn test_main_txt_verbose ( ) {
259
+ let args = args:: Args :: parse_from ( & [
260
+ "" ,
261
+ "--keys" ,
262
+ "key1 key2" ,
263
+ "--outputs" ,
264
+ "{ \" key1\" : \" value1\" , \" key2\" : \" value2\" , \" key3\" : \" value3\" }" ,
265
+ "--directory" ,
266
+ "test_txt_verbose" ,
267
+ "--extension" ,
268
+ "txt" ,
269
+ "--skip-missing-keys" ,
270
+ "--verbose" ,
271
+ ] ) ;
272
+
273
+ let context = setup ( & args. directory , true ) ;
274
+ let output_directory = & context. output_directory ;
275
+ let keys: Vec < String > = args
276
+ . keys
277
+ . split_whitespace ( )
278
+ . map ( |s| s. to_string ( ) )
279
+ . collect ( ) ;
280
+
281
+ write_outputs (
282
+ & args. skip_missing_keys ,
283
+ & keys,
284
+ & args. outputs ,
285
+ & output_directory,
286
+ & args. extension ,
287
+ & args. verbose ,
288
+ ) ;
289
+
290
+ // Check that the files were created.
291
+ assert ! ( output_directory. join( "key1.txt" ) . exists( ) ) ;
292
+ assert ! ( output_directory. join( "key2.txt" ) . exists( ) ) ;
293
+
294
+ // Check that the files contain the correct values.
295
+ let mut file = File :: open ( output_directory. join ( "key1.txt" ) ) . unwrap ( ) ;
296
+ let mut contents = String :: new ( ) ;
297
+
298
+ file. read_to_string ( & mut contents) . unwrap ( ) ;
299
+ assert_eq ! ( contents, "value1" ) ;
300
+
301
+ let mut file = File :: open ( output_directory. join ( "key2.txt" ) ) . unwrap ( ) ;
302
+ let mut contents = String :: new ( ) ;
303
+
304
+ file. read_to_string ( & mut contents) . unwrap ( ) ;
305
+ assert_eq ! ( contents, "value2" ) ;
306
+ teardown ( & context) ;
307
+ }
308
+
309
+ #[ test]
310
+ fn test_invalid_key_without_skip_missing_keys ( ) {
311
+ let context = setup ( "test_invalid_key_without_skip_missing_keys" , true ) ;
312
+ let output_directory = & context. output_directory ;
313
+
314
+ let status = process:: Command :: new ( "cargo" )
315
+ . args ( & [
316
+ "run" ,
317
+ "--" ,
318
+ "--keys" ,
319
+ "invalid_keys" ,
320
+ "--outputs" ,
321
+ "{ \" key1\" : \" value1\" , \" key2\" : \" value2\" , \" key3\" : \" value3\" }" ,
322
+ "--directory" ,
323
+ & output_directory. to_str ( ) . unwrap ( ) ,
324
+ "--extension" ,
325
+ "txt" ,
326
+ ] )
327
+ . status ( )
328
+ . expect ( "failed to execute process" ) ;
329
+
330
+ assert ! ( !status. success( ) ) ;
331
+ assert_eq ! ( status. code( ) . unwrap( ) , 1 ) ;
332
+ assert ! ( !output_directory. join( "invalid_keys.txt" ) . exists( ) ) ;
333
+ teardown ( & context) ;
334
+ }
217
335
}
0 commit comments