use assert_cmd::prelude::*; use assert_fs::prelude::*; use predicates::prelude::*; use std::process::Command; fn get_cmd() -> Command { Command::cargo_bin("top-domain-list").unwrap() } #[test] fn file_doesnt_exist() -> Result<(), Box> { let mut cmd = get_cmd(); cmd.arg("test/file/doesnt/exist"); cmd.assert() .failure() .stderr(predicate::str::contains("Could not read file")); Ok(()) } #[test] fn test_clean_list_without_tld_option() -> Result<(), Box> { let file = assert_fs::NamedTempFile::new("sample.txt")?; file.write_str( "pinterest.com pinterest.map.fastly.net tokeep.pinterest pin.it pinimg.com todelete.pinterest.ch pinterest.global.map.fastly.net pinimg.com.edgekey.net pinterest.com pinterest.ch pinterest.com.au todelete.wow.pinterest.fr pinterest.com.edgekey.net pinterest.fr pinterest todelete.pinterest.com", )?; let mut cmd = get_cmd(); cmd.arg(file.path()); cmd.assert() .success() .stdout(predicate::str::contains("todelete").not()) .stdout(predicate::str::contains("pinterest.com\n").count(1)) .stdout(predicate::str::contains("tokeep").count(1)); Ok(()) } #[test] fn test_clean_list_with_tld_option() -> Result<(), Box> { let file = assert_fs::NamedTempFile::new("sample.txt")?; file.write_str( "pinterest.com pinterest.map.fastly.net todelete.pinterest pin.it pinimg.com todelete.pinterest.ch pinterest.global.map.fastly.net pinimg.com.edgekey.net pinterest.com pinterest.ch pinterest.com.au todelete.wow.pinterest.fr pinterest.com.edgekey.net pinterest.fr pinterest todelete.pinterest.com", )?; let mut cmd = get_cmd(); cmd.arg(file.path()).arg("--tld"); cmd.assert() .success() .stdout(predicate::str::contains("todelete").not()) .stdout(predicate::str::contains("pinterest.com\n").count(1)); Ok(()) } #[test] fn test_empty_lines() -> Result<(), Box> { let file = assert_fs::NamedTempFile::new("sample.txt")?; file.write_str( " pinterest.com todelete.pinterest.com tokeep.google.com ", )?; let mut cmd = get_cmd(); cmd.arg(file.path()); cmd.assert() .success() .stdout(predicate::str::contains("todelete").not()) .stdout(predicate::str::contains("tokeep").count(1)) .stdout(predicate::str::contains("\n\n").not()); Ok(()) }