diff --git a/Cargo.lock b/Cargo.lock index 3bea8b4..abee485 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "ansi_term" version = "0.12.1" @@ -22,6 +31,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bitflags" version = "1.3.2" @@ -55,6 +70,24 @@ dependencies = [ "vec_map", ] +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + +[[package]] +name = "downcast" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" + +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + [[package]] name = "errno" version = "0.3.0" @@ -85,6 +118,21 @@ dependencies = [ "instant", ] +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits", +] + +[[package]] +name = "fragile" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa" + [[package]] name = "heck" version = "0.3.3" @@ -129,6 +177,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.6" @@ -153,10 +210,44 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd550e73688e6d578f0ac2119e32b797a327631a42f9433e59d02e139c8df60d" +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "mockall" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c84490118f2ee2d74570d114f3d0493cbf02790df303d2707606c3e14e07c96" +dependencies = [ + "cfg-if", + "downcast", + "fragile", + "lazy_static", + "mockall_derive", + "predicates", + "predicates-tree", +] + +[[package]] +name = "mockall_derive" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ce75669015c4f47b289fd4d4f56e894e4c96003ffdf3ac51313126f94c6cbb" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "musichoard" version = "0.1.0" dependencies = [ + "mockall", "once_cell", "serde", "serde_json", @@ -165,12 +256,57 @@ dependencies = [ "uuid", ] +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "predicates" +version = "2.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59230a63c37f3e18569bdb90e4a89cbf5bf8b06fea0b84e65ea10cc4df47addd" +dependencies = [ + "difflib", + "float-cmp", + "itertools", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" + +[[package]] +name = "predicates-tree" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -222,6 +358,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "rustix" version = "0.37.4" @@ -338,6 +491,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "termtree" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" + [[package]] name = "textwrap" version = "0.11.0" diff --git a/Cargo.toml b/Cargo.toml index bbacf07..155e270 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ structopt = "0.3" uuid = { version = "1.3", features = ["serde"] } [dev-dependencies] +mockall = "0.11" once_cell = "1.17" tempfile = "3.5" diff --git a/README.md b/README.md index 9969017..067a845 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ grcov target/debug/profraw \ --ignore-not-existing \ --ignore "tests/*" \ --ignore "src/main.rs" \ - --excl-start "#\[cfg\(test\)\]" \ + --excl-start "mod tests \{" \ --output-path ./target/debug/coverage/ xdg-open target/debug/coverage/index.html ``` diff --git a/src/library/beets.rs b/src/library/beets.rs index 77e71fe..d8efb3c 100644 --- a/src/library/beets.rs +++ b/src/library/beets.rs @@ -9,6 +9,9 @@ use std::{ str, }; +#[cfg(test)] +use mockall::automock; + use crate::{Album, AlbumId, Artist, ArtistId, Track, TrackFormat}; use super::{Error, Library, Query, QueryOption}; @@ -84,6 +87,7 @@ impl QueryArgsBeets for Query { } /// Trait for invoking beets commands. +#[cfg_attr(test, automock)] pub trait BeetsLibraryExecutor { /// Invoke beets with the provided arguments. fn exec(&mut self, arguments: &[String]) -> Result, Error>; @@ -285,21 +289,11 @@ impl BeetsLibraryExecutor for BeetsLibraryCommandExecutor { #[cfg(test)] mod tests { - use super::*; + use mockall::predicate; use crate::tests::COLLECTION; - struct BeetsLibraryTestExecutor { - arguments: Option>, - output: Option, Error>>, - } - - impl BeetsLibraryExecutor for BeetsLibraryTestExecutor { - fn exec(&mut self, arguments: &[String]) -> Result, Error> { - assert_eq!(&self.arguments.take().unwrap(), arguments); - self.output.take().unwrap() - } - } + use super::*; fn artist_to_beets_string(artist: &Artist) -> Vec { let mut strings = vec![]; @@ -362,13 +356,16 @@ mod tests { #[test] fn test_list_empty() { - let executor = BeetsLibraryTestExecutor { - arguments: Some(vec![ - "ls".to_string(), - BeetsLibrary::LIST_FORMAT_ARG.to_string(), - ]), - output: Some(Ok(vec![])), - }; + let arguments = vec!["ls".to_string(), BeetsLibrary::LIST_FORMAT_ARG.to_string()]; + let result = Ok(vec![]); + + let mut executor = MockBeetsLibraryExecutor::new(); + executor + .expect_exec() + .with(predicate::eq(arguments)) + .times(1) + .return_once(|_| result); + let mut beets = BeetsLibrary::new(Box::new(executor)); let output = beets.list(&Query::default()).unwrap(); @@ -378,16 +375,17 @@ mod tests { #[test] fn test_list_ordered() { + let arguments = vec!["ls".to_string(), BeetsLibrary::LIST_FORMAT_ARG.to_string()]; let expected = COLLECTION.to_owned(); - let output = artists_to_beets_string(&expected); + let result = Ok(artists_to_beets_string(&expected)); + + let mut executor = MockBeetsLibraryExecutor::new(); + executor + .expect_exec() + .with(predicate::eq(arguments)) + .times(1) + .return_once(|_| result); - let executor = BeetsLibraryTestExecutor { - arguments: Some(vec![ - "ls".to_string(), - BeetsLibrary::LIST_FORMAT_ARG.to_string(), - ]), - output: Some(Ok(output)), - }; let mut beets = BeetsLibrary::new(Box::new(executor)); let output = beets.list(&Query::default()).unwrap(); @@ -396,10 +394,12 @@ mod tests { #[test] fn test_list_unordered() { + let arguments = vec!["ls".to_string(), BeetsLibrary::LIST_FORMAT_ARG.to_string()]; let mut expected = COLLECTION.to_owned(); let mut output = artists_to_beets_string(&expected); let last = output.len() - 1; output.swap(0, last); + let result = Ok(output); // Putting the last track first will make the entire artist come first in the output. expected.rotate_right(1); @@ -413,13 +413,13 @@ mod tests { // And the (now) second album's tracks first track comes last. expected[1].albums[0].tracks.rotate_left(1); - let executor = BeetsLibraryTestExecutor { - arguments: Some(vec![ - "ls".to_string(), - BeetsLibrary::LIST_FORMAT_ARG.to_string(), - ]), - output: Some(Ok(output)), - }; + let mut executor = MockBeetsLibraryExecutor::new(); + executor + .expect_exec() + .with(predicate::eq(arguments)) + .times(1) + .return_once(|_| result); + let mut beets = BeetsLibrary::new(Box::new(executor)); let output = beets.list(&Query::default()).unwrap(); @@ -428,19 +428,20 @@ mod tests { #[test] fn test_list_album_title_year_clash() { + let arguments = vec!["ls".to_string(), BeetsLibrary::LIST_FORMAT_ARG.to_string()]; let mut expected = COLLECTION.to_owned(); expected[0].albums[0].id.year = expected[1].albums[0].id.year; expected[0].albums[0].id.title = expected[1].albums[0].id.title.clone(); - let output = artists_to_beets_string(&expected); + let result = Ok(output); + + let mut executor = MockBeetsLibraryExecutor::new(); + executor + .expect_exec() + .with(predicate::eq(arguments)) + .times(1) + .return_once(|_| result); - let executor = BeetsLibraryTestExecutor { - arguments: Some(vec![ - "ls".to_string(), - BeetsLibrary::LIST_FORMAT_ARG.to_string(), - ]), - output: Some(Ok(output)), - }; let mut beets = BeetsLibrary::new(Box::new(executor)); let output = beets.list(&Query::default()).unwrap(); @@ -454,16 +455,22 @@ mod tests { .track_number(QueryOption::Include(5)) .track_artist(QueryOption::Include(vec![String::from("some.artist")])); - let executor = BeetsLibraryTestExecutor { - arguments: Some(vec![ - "ls".to_string(), - BeetsLibrary::LIST_FORMAT_ARG.to_string(), - String::from("^album:some.album"), - String::from("track:5"), - String::from("artist:some.artist"), - ]), - output: Some(Ok(vec![])), - }; + let arguments = vec![ + "ls".to_string(), + BeetsLibrary::LIST_FORMAT_ARG.to_string(), + String::from("^album:some.album"), + String::from("track:5"), + String::from("artist:some.artist"), + ]; + let result = Ok(vec![]); + + let mut executor = MockBeetsLibraryExecutor::new(); + executor + .expect_exec() + .with(predicate::eq(arguments)) + .times(1) + .return_once(|_| result); + let mut beets = BeetsLibrary::new(Box::new(executor)); let output = beets.list(&query).unwrap();