Skip to content

Commit 9577bb2

Browse files
authored
Merge pull request #77 from tj-actions/feat/improve-test-coverage
2 parents d06585d + a00f519 commit 9577bb2

File tree

2 files changed

+157
-43
lines changed

2 files changed

+157
-43
lines changed

src/main.rs

+137-19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod args;
99
mod writer;
1010

1111
fn main() {
12+
// codecov: disable
1213
let args = args::Args::parse();
1314

1415
let keys: Vec<String> = args
@@ -42,14 +43,17 @@ fn main() {
4243
mod tests {
4344
use std::fs::File;
4445
use std::io::Read;
46+
use std::process;
47+
48+
use crate::writer::create_output_directory;
4549

4650
use super::*;
4751

4852
struct TestContext {
4953
output_directory: PathBuf,
5054
}
5155

52-
fn setup(directory: &str) -> TestContext {
56+
fn setup(directory: &str, create_output_dir: bool) -> TestContext {
5357
println!("Test setup...");
5458

5559
let current_directory: PathBuf = env::current_dir().unwrap_or_else(|err| {
@@ -59,22 +63,8 @@ mod tests {
5963

6064
let output_directory: PathBuf = current_directory.join(directory);
6165

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);
7868
}
7969

8070
TestContext { output_directory }
@@ -133,7 +123,7 @@ mod tests {
133123
"txt",
134124
"--skip-missing-keys",
135125
]);
136-
let context = setup(&args.directory);
126+
let context = setup(&args.directory, true);
137127
let output_directory = &context.output_directory;
138128
let keys: Vec<String> = args
139129
.keys
@@ -181,7 +171,7 @@ mod tests {
181171
"json",
182172
"--skip-missing-keys",
183173
]);
184-
let context = setup(&args.directory);
174+
let context = setup(&args.directory, true);
185175
let output_directory = &context.output_directory;
186176
let keys: Vec<String> = args
187177
.keys
@@ -214,4 +204,132 @@ mod tests {
214204
assert_eq!(contents, "\"value2\"");
215205
teardown(&context);
216206
}
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+
}
217335
}

src/writer.rs

+20-24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ use std::path::PathBuf;
44

55
use unescape::unescape;
66

7+
pub fn create_output_directory(path: &PathBuf) {
8+
if !path.try_exists().unwrap_or(false) {
9+
println!("Creating output directory...");
10+
match std::fs::create_dir_all(path) {
11+
Ok(_) => println!("Output directory created successfully."),
12+
Err(e) => {
13+
eprintln!(
14+
"Error creating output directory '{}': {}",
15+
path.display(),
16+
e
17+
);
18+
std::process::exit(1);
19+
}
20+
}
21+
} else {
22+
println!("Output directory already exists.");
23+
}
24+
}
25+
726
pub fn write_outputs(
827
skip_missing_keys: &bool,
928
keys: &Vec<String>,
@@ -28,30 +47,7 @@ pub fn write_outputs(
2847
}
2948
};
3049

31-
// Create the output directory if it doesn't exist
32-
if !output_directory.exists() {
33-
if *verbose {
34-
println!("Creating output directory...");
35-
}
36-
37-
match std::fs::create_dir_all(output_directory) {
38-
Ok(_) => {
39-
if *verbose {
40-
println!("Output directory created successfully.");
41-
}
42-
}
43-
Err(e) => {
44-
eprintln!(
45-
"Error creating output directory '{}': {}",
46-
output_directory.display(),
47-
e
48-
);
49-
std::process::exit(1);
50-
}
51-
}
52-
} else if *verbose {
53-
println!("Output directory already exists.");
54-
}
50+
create_output_directory(output_directory);
5551

5652
for key in keys {
5753
if *verbose {

0 commit comments

Comments
 (0)