diff options
| author | Teddy Wing | 2016-04-24 05:17:19 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-04-24 05:17:19 -0400 |
| commit | 2d1f7031f03194fbceffc15b1d6376abea243e22 (patch) | |
| tree | 29481b3837f6363f6405ac15e82b07b4cb89f773 /src/tests.rs | |
| parent | c67fdff0c3cda883b5bf577988e1c555931969cf (diff) | |
| download | mutt-alias-auto-add-2d1f7031f03194fbceffc15b1d6376abea243e22.tar.bz2 | |
Move `write_alias` function to `Alias#write_to_file`
Expand the responsibility of `Alias#write_to_file` so that it performs
what it used to do plus all of what `write_alias` did.
This allows us to consolidate the functionality into a single method,
and move it into the `Alias` implementation.
The change required some modification to our `write_to_file` test:
* Create our test alias as mutable
* Write a new alias to the test file based on our test alias but with a
different email. This allows the `write_to_file` method to work
without erroring with an `AliasSearchError::NotFound`.
* Needed to `derive(Clone)` on `Alias` in order to be able to easily
clone it into the new near-duplicate alias.
* Change our `unwrap()` calls to `expect()` to make it easier to see
where exactly we panicked. Otherwise I didn't really have any way of
knowing.
* Add some comments for clarity
Diffstat (limited to 'src/tests.rs')
| -rw-r--r-- | src/tests.rs | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/tests.rs b/src/tests.rs index 88f2a3b..21cf147 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,5 +1,5 @@ -use std::fs::{self, File}; -use std::io::Read; +use std::fs::{self, File, OpenOptions}; +use std::io::{Read, Write}; use super::{Alias, AliasSearchError}; @@ -124,17 +124,29 @@ fn update_alias_id_increments_alias() { #[test] fn alias_write_to_file_must_write_given_alias_to_file() { - let alias = update_alias_id_sample_alias(); + let mut alias = update_alias_id_sample_alias(); + // Create a new test file let test_file = "./testdata/write_to_file"; - fs::copy("./testdata/aliases", test_file).unwrap(); - alias.write_to_file(test_file).unwrap(); + fs::copy("./testdata/aliases", test_file).expect("Alias file copy failed"); + + // Write a duplicate alias so that `write_to_file` is able to append a + // new one + let mut f = OpenOptions::new().append(true).open(test_file) + .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"); + + // Write our new alias to the file + alias.write_to_file(test_file).expect("`write_to_file` failed"); - let mut f = File::open(test_file).unwrap(); + // Get the file's contents for testing + 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).unwrap(); + 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).unwrap(); + fs::remove_file(test_file).expect("Failed to delete test file"); assert_eq!(alias.to_string(), file_contents[file_contents.len() - 2]); } |
