From af923301c76a2b99c7d3f9fee87f61be8cd14569 Mon Sep 17 00:00:00 2001 From: Tomas Tomecek Date: Fri, 2 Feb 2018 19:00:09 +0100 Subject: [PATCH] let rust test suite run anywhere Fixes #35 Signed-off-by: Tomas Tomecek --- Cargo.toml | 1 + src/conf.rs | 42 +++++++++++++++++++++++++++++++++++++++--- src/main.rs | 2 ++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 33c55f3..f3c7f2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ name = "pretty-git-prompt" [dependencies] yaml-rust = "0.3.4" +tempdir = "0.3" [dependencies.clap] version = "2.19" diff --git a/src/conf.rs b/src/conf.rs index 42a6b2a..d2b5f4c 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -298,20 +298,49 @@ pub fn create_default_config(path: &PathBuf) -> Result { mod tests { // We'll use this git repo for testing + use std::env; use std::fs::{File,OpenOptions,remove_file}; use std::io::{Read}; use std::path::{Path,PathBuf}; + use std::process::{Command,Stdio}; use conf::{get_configuration,create_default_config,DEFAULT_CONF,Conf}; use yaml_rust::{YamlLoader}; use backend::Backend; use models::DisplayMaster; - use git2::Repository; + use git2::{Repository}; + use tempdir::TempDir; + + macro_rules! init_git { + () => { + let dir = TempDir::new("p-g-p").unwrap(); + env::set_current_dir(&dir).unwrap(); + // we could use git2 to create a repo with a commit, but it's soooo complicated + let mut c = Command::new("git").args( + &["-c", "user.name=Git \"Pretty\" Prompter", "-c", "user.email=pretty-git-prompt@example.com", + "init", "."]) + .stdout(Stdio::null()) + .spawn() + .unwrap(); + let rc = c.wait().unwrap(); + + let mut c = Command::new("git").args( + &["-c", "user.name=Git \"Pretty\" Prompter", "-c", "user.email=pretty-git-prompt@example.com", + "commit", "--allow-empty", "-m", "init"]) + .stdout(Stdio::null()) + .spawn() + .unwrap(); + let rc = c.wait().unwrap(); + } + } #[test] #[should_panic(expected = "'version' is missing in config file.")] fn test_empty_config() { let config_text = "{}"; let docs = YamlLoader::load_from_str(config_text).unwrap(); + + init_git!(); + let repo = Repository::discover(".").unwrap(); let backend = Backend::new(repo, true); let dm: DisplayMaster = DisplayMaster::new(backend, true); @@ -323,13 +352,15 @@ mod tests { let config_text = "version: '1' values: []"; let docs = YamlLoader::load_from_str(config_text).unwrap(); + + init_git!(); + let repo = Repository::discover(".").unwrap(); let backend = Backend::new(repo, true); let dm: DisplayMaster = DisplayMaster::new(backend, true); Conf::new(docs[0].clone(), dm); } - #[allow(unused_must_use)] #[test] fn test_create_default_config() { let p = PathBuf::from("/tmp/test_pretty_git_prompt_config1"); @@ -342,7 +373,7 @@ values: []"; let mut file = File::open(p.clone()).unwrap(); let mut contents = String::new(); - file.read_to_string(&mut contents); + file.read_to_string(&mut contents).unwrap(); assert_eq!(contents, DEFAULT_CONF); remove_file(p.clone()).unwrap(); @@ -372,6 +403,8 @@ values: []"; let result = create_default_config(&p); assert!(result.is_ok()); + init_git!(); + let repo = Repository::discover(".").unwrap(); let backend = Backend::new(repo, true); let dm: DisplayMaster = DisplayMaster::new(backend, true); @@ -385,6 +418,9 @@ values: []"; fn test_lower_version() { let config_text = "version: '0'"; let docs = YamlLoader::load_from_str(config_text).unwrap(); + + init_git!(); + let repo = Repository::discover(".").unwrap(); let backend = Backend::new(repo, true); let dm: DisplayMaster = DisplayMaster::new(backend, true); diff --git a/src/main.rs b/src/main.rs index 00ef76d..529e306 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ extern crate clap; extern crate git2; +// for tests +extern crate tempdir; extern crate yaml_rust; use std::io::{self, Write};