Skip to content

Commit

Permalink
let rust test suite run anywhere
Browse files Browse the repository at this point in the history
Fixes #35

Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
  • Loading branch information
TomasTomecek committed Feb 2, 2018
1 parent fc7e19e commit af92330
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ name = "pretty-git-prompt"

[dependencies]
yaml-rust = "0.3.4"
tempdir = "0.3"

[dependencies.clap]
version = "2.19"
Expand Down
42 changes: 39 additions & 3 deletions src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,49 @@ pub fn create_default_config(path: &PathBuf) -> Result<String, io::Error> {

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);
Expand All @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

extern crate clap;
extern crate git2;
// for tests
extern crate tempdir;
extern crate yaml_rust;

use std::io::{self, Write};
Expand Down

0 comments on commit af92330

Please sign in to comment.