diff options
| author | Teddy Wing | 2016-04-24 09:39:14 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-04-24 09:39:14 -0400 |
| commit | 79e78aaa7c0c77064264ffdad0c3172aa0f5c376 (patch) | |
| tree | 67684ab4f153eb8c01fc25f2c77461d26bab3932 | |
| parent | 2e844b5c528a39e75702d0b412b0396080fa103e (diff) | |
| download | mutt-alias-auto-add-79e78aaa7c0c77064264ffdad0c3172aa0f5c376.tar.bz2 | |
test: Fix `write_to_file` race condition
Have each `write_to_file` test pass in a custom filename to the helper
function. This way each test has its own file to deal with, eliminating
the race condition when running tests in parallel (the default).
| -rw-r--r-- | src/tests.rs | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/src/tests.rs b/src/tests.rs index f00d103..15b5ae8 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -122,23 +122,23 @@ fn update_alias_id_increments_alias() { } -fn alias_write_to_file_helper<F>(alias: &mut Alias, f: F) -> String +fn alias_write_to_file_helper<F>(alias: &mut Alias, filename: &str, f: F) -> String where F: Fn(&Alias, &str) { // Create a new test file - let test_file = "./testdata/write_to_file"; - fs::copy("./testdata/aliases", test_file).expect("Alias file copy failed"); + let test_file = format!("./testdata/{}", filename); + fs::copy("./testdata/aliases", &test_file).expect("Alias file copy failed"); - f(alias, test_file); + f(alias, &test_file); // Write our new alias to the file - alias.write_to_file(test_file).expect("`write_to_file` failed"); + alias.write_to_file(&test_file).expect("`write_to_file` failed"); // Get the file's contents for testing - let mut f = File::open(test_file).expect("Failed to open test file"); + let mut f = File::open(&test_file).expect("Failed to open test file"); let mut file_contents = String::new(); f.read_to_string(&mut file_contents).expect("Failed to read test file contents"); let file_contents: Vec<&str> = file_contents.split('\n').collect(); - fs::remove_file(test_file).expect("Failed to delete test file"); + fs::remove_file(&test_file).expect("Failed to delete test file"); file_contents[file_contents.len() - 2].to_string() } @@ -146,15 +146,19 @@ fn alias_write_to_file_helper<F>(alias: &mut Alias, f: F) -> String #[test] fn alias_write_to_file_must_write_unique_alias_to_file_if_one_already_exists() { let mut alias = update_alias_id_sample_alias(); - let alias_line = alias_write_to_file_helper(&mut alias, |alias: &Alias, filename: &str| { - // Write a duplicate alias so that `write_to_file` is able to append a - // new one - let mut f = OpenOptions::new().append(true).open(filename) - .expect("Failed to open test file for appending"); - writeln!(f, "{}", Alias { email: "derpy@home.pv".to_owned(), .. alias.clone() } - .to_string()) - .expect("Failed to append matching alias"); - }); + let alias_line = alias_write_to_file_helper( + &mut alias, + "alias_write_to_file_must_write_unique_alias_to_file_if_one_already_exists", + |alias: &Alias, filename: &str| { + // Write a duplicate alias so that `write_to_file` is able to append a + // new one + let mut f = OpenOptions::new().append(true).open(filename) + .expect("Failed to open test file for appending"); + writeln!(f, "{}", Alias { email: "derpy@home.pv".to_owned(), .. alias.clone() } + .to_string()) + .expect("Failed to append matching alias"); + } + ); assert_eq!(alias.to_string(), alias_line); } @@ -165,6 +169,7 @@ fn alias_write_to_file_must_write_given_alias_to_file() { let mut alias = update_alias_id_sample_alias(); let alias_line = alias_write_to_file_helper( &mut alias, + "alias_write_to_file_must_write_given_alias_to_file", |alias: &Alias, filename: &str| {}); assert_eq!(alias.to_string(), alias_line); |
