From 2e844b5c528a39e75702d0b412b0396080fa103e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 24 Apr 2016 09:31:25 -0400 Subject: Write alias even if we haven't found a matching one If we get an `AliasSearchError::NotFound` error we still want to write the alias to the file. After all, it's an email we haven't encountered yet so we should store an alias for it. If the email exists, we can do what we were doing before and change the current alias to be unique. Rename our "append" `write_to_file` test method to be a little more clear about its purpose. Create a new `write_to_file` test function that tests the case we're adding here. Note that having these two tests together seems to cause a race condition because they're both using the same temporary test file. If we run tests with `RUST_TEST_THREADS=1 cargo test` then they pass. Will see about fixing that next. --- src/alias.rs | 7 +++++-- src/tests.rs | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/alias.rs b/src/alias.rs index 8a66db8..0fd05d2 100644 --- a/src/alias.rs +++ b/src/alias.rs @@ -75,8 +75,11 @@ impl Alias { } pub fn write_to_file>(&mut self, file: P) -> Result<(), AliasSearchError> { - let similar_aliases = try!(self.find_in_file(&file)); - self.update_alias_id(similar_aliases); + match self.find_in_file(&file) { + Ok(similar_aliases) => self.update_alias_id(similar_aliases), + Err(AliasSearchError::NotFound) => {}, + Err(e) => return Err(e), + }; let mut f = try!(OpenOptions::new().append(true).open(file)); try!(f.write_all(format!("{}\n", self.to_string()).as_bytes())); diff --git a/src/tests.rs b/src/tests.rs index c897a32..f00d103 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -144,7 +144,7 @@ fn alias_write_to_file_helper(alias: &mut Alias, f: F) -> String } #[test] -fn alias_write_to_file_must_write_given_alias_to_file() { +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 @@ -158,3 +158,14 @@ fn alias_write_to_file_must_write_given_alias_to_file() { assert_eq!(alias.to_string(), alias_line); } + +#[test] +#[allow(unused_variables)] +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: &Alias, filename: &str| {}); + + assert_eq!(alias.to_string(), alias_line); +} -- cgit v1.2.3