From ceabde799b07cb2bd375519e91819b3d762f88eb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 13 Oct 2019 10:04:29 +0200 Subject: [PATCH] Add test: check whether imag-create works as expected Signed-off-by: Matthias Beyer --- tests/ui/Cargo.toml | 1 + tests/ui/src/imag_create.rs | 64 +++++++++++++++++++++++++++++++++++++ tests/ui/src/lib.rs | 1 + 3 files changed, 66 insertions(+) diff --git a/tests/ui/Cargo.toml b/tests/ui/Cargo.toml index 44f2d8d7..1fef31ac 100644 --- a/tests/ui/Cargo.toml +++ b/tests/ui/Cargo.toml @@ -17,3 +17,4 @@ env_logger = "0.7" log = "0.4" predicates = "1" pretty_assertions = "0.6" +semver = "0.9" diff --git a/tests/ui/src/imag_create.rs b/tests/ui/src/imag_create.rs index 987eadc5..4e6719f3 100644 --- a/tests/ui/src/imag_create.rs +++ b/tests/ui/src/imag_create.rs @@ -17,3 +17,67 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::process::Command; + +use assert_cmd::prelude::*; +use assert_fs::fixture::TempDir; + +/// Helper to call imag-create +pub fn call(tempdir: &TempDir, targets: &[&str]) { + let mut bin = binary(tempdir); + + // ensure that stdin is not used by the child process + bin.stdin(std::process::Stdio::inherit()); + + for target in targets.iter() { + bin.arg(target); + } + debug!("Command = {:?}", bin); + + bin.assert().success(); +} + +pub fn binary(tempdir: &TempDir) -> Command { + crate::imag::binary(tempdir, "imag-create") +} + + +#[test] +fn test_creating_works() { + crate::setup_logging(); + let imag_home = crate::imag::make_temphome(); + crate::imag_init::call(&imag_home); + + call(&imag_home, &["test"]); + + let entry_path = { + let mut path = imag_home.path().to_path_buf(); + path.push("store"); + path.push("test"); + path + }; + + debug!("Calculated path = {:?}", entry_path); + + assert!(entry_path.exists(), "Entry was not created: {:?}", entry_path); + assert!(entry_path.is_file() , "Entry is not a file: {:?}", entry_path); + + let contents = std::fs::read_to_string(entry_path).unwrap(); + let mut lines = contents.lines(); + + assert_eq!(lines.next(), Some("---")); + assert_eq!(lines.next(), Some("[imag]")); + { + let version_line = lines.next().unwrap(); + assert!(version_line.starts_with("version = '")); + assert!(version_line.ends_with("'")); + let version = version_line.replace("version = '", "").replace("'", ""); + let semver = semver::Version::parse(&version); + assert!(semver.is_ok()); + assert!(!semver.unwrap().is_prerelease()); // we only want full versions in the header + + } + assert_eq!(lines.next(), Some("---")); + assert_eq!(lines.next(), None); +} + diff --git a/tests/ui/src/lib.rs b/tests/ui/src/lib.rs index 827f8505..56009df6 100644 --- a/tests/ui/src/lib.rs +++ b/tests/ui/src/lib.rs @@ -21,6 +21,7 @@ extern crate assert_cmd; extern crate assert_fs; extern crate env_logger; extern crate predicates; +extern crate semver; #[macro_use] extern crate log; #[macro_use] extern crate pretty_assertions;