Skip to content

Commit

Permalink
test: remove panic unwind from test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent 205397b commit 738ed8a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
6 changes: 5 additions & 1 deletion tests/cog_init_test_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::process::Command;

pub mod helper;
use helper::run_test_with_context;
use std::path::PathBuf;

#[test]
#[cfg(not(tarpaulin))]
Expand Down Expand Up @@ -67,7 +68,10 @@ fn fail_if_config_exist() -> Result<()> {
.arg("test_repo_existing")
// Assert
.assert()
.failure();
.stdout("Found git repository in \"test_repo_existing\", skipping initialisation\n")
.success();

assert_file_exists(PathBuf::from("cog.toml"));
Ok(())
})
}
Expand Down
21 changes: 12 additions & 9 deletions tests/cog_verify_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ fn verify_ok() -> Result<()> {
let message = "chore: a commit message";
let expected = indoc!(
"a commit message (not committed) - now
Author: Tom
Type: chore
Scope: none",
\tAuthor: Tom
\tType: chore
\tScope: none
",
);

// Act
Expand All @@ -45,13 +47,14 @@ fn verify_with_scope() -> Result<()> {
std::fs::read_to_string(context.test_dir.join(".git/config"))
);
let message = "feat(feature): a commit message";
let expected = indoc! {
let expected = indoc!(
"a commit message (not committed) - now
Author: Tom
Type: feat
Scope: feature
"
};
\tAuthor: Tom
\tType: feat
\tScope: feature
",
);

// Act
Command::cargo_bin("cog")?
Expand Down
5 changes: 4 additions & 1 deletion tests/git_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use helper::*;

pub mod helper;

use conventional_commit_parser::commit::CommitType;
use helper::run_test_with_context;

#[test]
Expand Down Expand Up @@ -100,15 +101,17 @@ fn check_commit_ok_from_latest_tag() -> Result<()> {
#[test]
fn check_commit_err_from_latest_tag() -> Result<()> {
run_test_with_context(|_| {
// Arrange
git_init_and_set_current_path("commit_err_from_tag")?;

create_empty_config()?;
git_commit("this one should not be picked")?;
git_tag("0.1.0")?;
git_commit("Oh no!")?;

// Act
let cocogitto = CocoGitto::get()?;

// Assert
assert!(cocogitto.check(true).is_err());
Ok(())
})
Expand Down
40 changes: 27 additions & 13 deletions tests/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ where
test_dir: temp_dir.into_path(),
};

let result = panic::catch_unwind(|| test(&context));

assert!(result.is_ok());
test(&context)?;

Ok(std::env::set_current_dir(context.current_dir)?)
}
Expand All @@ -44,7 +42,8 @@ fn setup_git_config() -> Result<()> {
.arg("Tom")
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()?;
.output()
.expect("Unable to set local git user");

Command::new("git")
.arg("config")
Expand All @@ -53,35 +52,50 @@ fn setup_git_config() -> Result<()> {
.arg("toml.bombadil@themail.org")
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()?;
.output()
.expect("Unable to set local git user email");

std::fs::File::open(".git/config")?.sync_all()?;
std::fs::File::open(".git/config")?
.sync_all()
.expect("Error syncing `.git/config` file");

Ok(())
}

pub fn git_init() -> Result<()> {
setup_git_config()?;

Command::new("git")
.arg("init")
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()?;
.output()
.expect("Error initializing empty test repository in current directory");

setup_git_config().expect("Error setting local git config");

Ok(())
}

pub fn git_init_and_set_current_path(path: &str) -> Result<()> {
setup_git_config()?;
std::env::set_current_dir(std::env::current_dir()?.join("open_repo_ok"))?;

Command::new("git")
.arg("init")
.arg(path)
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()?;
.output()
.expect("Error creating empty repo with target path");

std::fs::File::open(path)
.expect("Error opening test repository")
.sync_all()
.expect("Error syncing file system while creating test repository");

let repo_dir = std::env::current_dir()
.expect("Unable to get current directory in test context")
.join(path);

std::env::set_current_dir(repo_dir).expect("Unable to move into to test repository");

setup_git_config().expect("Error setting up local git config for test");

Ok(())
}
Expand Down

0 comments on commit 738ed8a

Please sign in to comment.