diff options
| author | Teddy Wing | 2016-04-22 08:24:15 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-04-22 08:24:15 -0400 |
| commit | ff0b31cfe68b2bc7318a98827d812e32deff0b4c (patch) | |
| tree | db7e367d4c708357f669d1c90e1debef91a5b160 /src/tests.rs | |
| parent | 483b297db9604f7c1d8ae63e6ab4aadec70ce1cb (diff) | |
| download | mutt-alias-auto-add-ff0b31cfe68b2bc7318a98827d812e32deff0b4c.tar.bz2 | |
Alias#write_to_file: Fix append and ending newline
Changed the test to use a file with multiple alias lines for a more
real-world scenario. Achieving this by copying the `testdata/aliases`
file into a new temporary test file instead of creating a new blank
file.
Then needed to change our assertion so that we get the correct line from
the file to compare on. This is always the last line in the file. Note
that `.len() - 2` is used because `.len() - 1` is the final newline.
The test change revealed two errors in my code:
1. I needed to open the file for appending, not just for writing.
2. A newline needs to be appended to the end of the file (otherwise all
our aliases get written to the same line).
Fix the errors.
Diffstat (limited to 'src/tests.rs')
| -rw-r--r-- | src/tests.rs | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/src/tests.rs b/src/tests.rs index 1034bc6..2a6e264 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -130,13 +130,14 @@ fn alias_write_to_file_must_write_given_alias_to_file() { let alias = update_alias_id_sample_alias(); let test_file = "./testdata/write_to_file"; - File::create(test_file).unwrap(); + fs::copy("./testdata/aliases", test_file).unwrap(); alias.write_to_file(test_file).unwrap(); let mut f = File::open(test_file).unwrap(); let mut file_contents = String::new(); f.read_to_string(&mut file_contents).unwrap(); + let file_contents: Vec<&str> = file_contents.split('\n').collect(); fs::remove_file(test_file).unwrap(); - assert_eq!(alias.to_string(), file_contents); + assert_eq!(alias.to_string(), file_contents[file_contents.len() - 2]); } |
