Skip to content

Commit

Permalink
test(lib): add test for cocogitto check
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 17, 2020
1 parent fac83f2 commit 121235f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 64 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ moins = "0.4.0"
lazy_static = "1.4.0"
clap = { version = "^2", optional = true }

[dev-dependencies]
temp_testdir = "0.2.3"

[features]
default = ["cli"]
cli = [ "clap"]
Expand Down
66 changes: 2 additions & 64 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl CocoGitto {
let msg = "No errored commits".green();
println!("{}", msg)
} else {
errors.iter().for_each(|err| eprintln!("{}", err))
errors.iter().for_each(|err| eprintln!("{}", err));
}

Ok(())
Expand Down Expand Up @@ -219,7 +219,7 @@ impl CocoGitto {
description,
is_breaking_change,
}
.to_string();
.to_string();

let oid = self.repository.commit(message)?;
let commit = self.repository.0.find_commit(oid)?;
Expand Down Expand Up @@ -378,65 +378,3 @@ impl CocoGitto {
}
}
}

#[cfg(test)]
use std::env::temp_dir;

#[cfg(test)]
fn git_init() -> Result<()> {
let temp_dir = temp_dir();
std::env::set_current_dir(temp_dir)?;
Command::new("git")
.arg("init")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;

Ok(())
}

#[cfg(test)]
fn git_commit(message: &str) -> Result<()> {
Command::new("git")
.arg("commit")
.arg("-m")
.arg(message)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;
Ok(())
}

#[cfg(test)]
fn git_tag(tag: &str) -> Result<()> {
Command::new("tag")
.arg(tag)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;
Ok(())
}

#[cfg(test)]
fn create_empty_config() -> Result<()> {
std::fs::File::create("coco.toml")?;
Ok(())
}


#[cfg(test)]
mod test {
use crate::{git_init, create_empty_config, CocoGitto};
use anyhow::Result;

#[test]
fn should_open_repo() -> Result<()> {
git_init()?;
create_empty_config()?;

let gitto = CocoGitto::get();

assert!(gitto.is_ok());
Ok(())
}
}
97 changes: 97 additions & 0 deletions tests/git_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use anyhow::Result;
use chrono::Utc;
use cocogitto::CocoGitto;
use std::process::{Command, Stdio};
use std::time::Duration;

fn git_init() -> Result<()> {
let temp_dir = temp_testdir::TempDir::default().permanent();
println!("{:?}", &temp_dir.as_ref());
std::env::set_current_dir(temp_dir.as_ref())?;

println!("on {:?}", std::env::current_dir()?);
println!("git init");

Command::new("git")
.arg("init")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;

Ok(())
}

fn git_commit(message: &str) -> Result<()> {
std::thread::sleep(Duration::from_millis(100));
let file_name = Utc::now().timestamp().to_string();
std::fs::write(file_name, "dummy")?;

println!("git add .");
Command::new("git")
.arg("add")
.arg(".")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;

println!("git commit -m \"{}\"", message);
Command::new("git")
.arg("commit")
.arg("-m")
.arg(&format!("\"{}\"", message))
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;
Ok(())
}

fn _git_tag(tag: &str) -> Result<()> {
Command::new("tag")
.arg(tag)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;
Ok(())
}

fn create_empty_config() -> Result<()> {
std::fs::File::create("coco.toml")?;
Ok(())
}

#[test]
fn should_open_repo() -> Result<()> {
git_init()?;
create_empty_config()?;

let gitto = CocoGitto::get();

assert!(gitto.is_ok());
Ok(())
}

#[test]
fn should_check_commit_history_ok() -> Result<()> {
git_init()?;
create_empty_config()?;
git_commit("feat: a valid commit")?;
git_commit("chore(test): another valid commit")?;

let gitto = CocoGitto::get()?;
assert!(gitto.check().is_ok());
Ok(())
}

// check stderr and std out instead of exit status
// #[test]
// fn should_check_commit_history_err() -> Result<()> {
// git_init()?;
// create_empty_config()?;
// git_commit("feat: a valid commit")?;
// git_commit("errored commit")?;
//
// let gitto = CocoGitto::get()?;
//
// assert!(gitto.check().is_err());
// Ok(())
// }

0 comments on commit 121235f

Please sign in to comment.