Skip to content

Commit

Permalink
test: refactor IT test using a run in context helper
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent 5eff372 commit a32a517
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 159 deletions.
10 changes: 2 additions & 8 deletions tests/cmd_help_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ use std::process::Command;
#[test]
#[cfg(not(tarpaulin))]
fn cog_display_help() -> Result<()> {
Command::cargo_bin("cog")?
.arg("--help")
.assert()
.success();
Command::cargo_bin("cog")?.arg("--help").assert().success();

Ok(())
}

#[test]
#[cfg(not(tarpaulin))]
fn coco_display_help() -> Result<()> {
Command::cargo_bin("coco")?
.arg("--help")
.assert()
.success();
Command::cargo_bin("coco")?.arg("--help").assert().success();

Ok(())
}
11 changes: 6 additions & 5 deletions tests/coco_commit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use helper::run_test_with_context;
fn commit_ok() -> Result<()> {
run_test_with_context(|context| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
std::fs::write(context.test_dir.join("test_file"), "content")?;
helper::git_add()?;

Expand All @@ -22,7 +22,8 @@ fn commit_ok() -> Result<()> {
.arg("this is the body")
.arg("this is the footer")
// Assert
.assert().success();
.assert()
.success();
Ok(())
})
}
Expand All @@ -32,7 +33,7 @@ fn commit_ok() -> Result<()> {
fn unstaged_changes_commit_err() -> Result<()> {
run_test_with_context(|context| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
std::fs::write(context.test_dir.join("test_file"), "content")?;

// Act
Expand All @@ -54,7 +55,7 @@ fn unstaged_changes_commit_err() -> Result<()> {
fn untracked_changes_commit_ok() -> Result<()> {
run_test_with_context(|context| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
std::fs::write(context.test_dir.join("staged"), "content")?;
helper::git_add()?;
std::fs::write(context.test_dir.join("untracked"), "content")?;
Expand All @@ -78,7 +79,7 @@ fn untracked_changes_commit_ok() -> Result<()> {
fn empty_commit_err() -> Result<()> {
run_test_with_context(|_context| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;

// Act
Command::cargo_bin("coco")?
Expand Down
31 changes: 15 additions & 16 deletions tests/cog_bump_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn auto_bump_from_start_ok() -> Result<()> {
run_test_with_context(|context| {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--auto");
helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_commit("fix: bug fix")?;
Expand All @@ -33,7 +33,7 @@ fn auto_bump_minor_from_latest_tag() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--auto");

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_commit("fix: bug fix")?;
Expand All @@ -56,7 +56,7 @@ fn auto_bump_major_from_latest_tag() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;

command.arg("bump").arg("--auto");
helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_commit("fix: bug fix")?;
Expand All @@ -79,7 +79,7 @@ fn auto_bump_patch_from_latest_tag() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--auto");

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_commit("fix: bug fix")?;
Expand All @@ -102,7 +102,7 @@ fn auto_bump_respect_semver_sorting() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--auto");

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_commit("fix: bug fix")?;
Expand All @@ -126,7 +126,7 @@ fn minor_bump() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--minor");

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_tag("1.0.0")?;
helper::git_commit("feat: feature")?;
Expand All @@ -145,7 +145,7 @@ fn major_bump() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--major");

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_tag("1.0.0")?;
helper::git_commit("feat: feature")?;
Expand All @@ -164,7 +164,7 @@ fn patch_bump() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--patch");

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_tag("1.0.0")?;
helper::git_commit("feat: feature")?;
Expand All @@ -183,7 +183,7 @@ fn pre_release_bump() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--major").arg("--pre").arg("alpha");

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_tag("1.0.0")?;
helper::git_commit("feat: feature")?;
Expand All @@ -203,7 +203,7 @@ fn bump_with_hook() -> Result<()> {
// Arrange
std::fs::write("cog.toml", r#"pre_bump_hooks = ["touch {{version}}"]"#)?;

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_tag("1.0.0")?;
helper::git_commit("feat: feature")?;
Expand All @@ -229,25 +229,25 @@ fn bump_with_profile_hook() -> Result<()> {
run_test_with_context(|_context| {
// Arrange
let config = indoc! {
"[bump_profiles.custom]
"[bump_profiles.custom]
pre_bump_hooks = [ \"echo current {{latest}}\" ]
post_bump_hooks = [ \"echo next {{version}}\" ]
"
};
};

std::fs::write("cog.toml", config)?;

helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_tag("1.0.0")?;
helper::git_commit("feat: feature")?;

let expected = indoc! {
"current 1.0.0
"current 1.0.0
next 1.0.1
Bumped version : 1.0.0 -> 1.0.1
"
};
};

// Act
Command::cargo_bin("cog")?
Expand All @@ -265,4 +265,3 @@ fn bump_with_profile_hook() -> Result<()> {
Ok(())
})
}

6 changes: 3 additions & 3 deletions tests/cog_changelog_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use helper::run_test_with_context;
fn get_changelog_from_untagged_repo() -> Result<()> {
run_test_with_context(|_| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_commit("fix: bug fix")?;
Expand All @@ -32,7 +32,7 @@ fn get_changelog_from_untagged_repo() -> Result<()> {
fn get_changelog_from_tagged_repo() -> Result<()> {
run_test_with_context(|_| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_tag("1.0.0")?;
Expand All @@ -55,7 +55,7 @@ fn get_changelog_from_tagged_repo() -> Result<()> {
fn get_changelog_from_at_tag() -> Result<()> {
run_test_with_context(|_| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat(taef): feature")?;
helper::git_commit("feat: feature 2")?;
Expand Down
65 changes: 32 additions & 33 deletions tests/cog_check_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,54 @@ use predicates::prelude::predicate;
mod helper;
use helper::run_test_with_context;


#[test]
#[cfg(not(tarpaulin))]
fn cog_check_ok() -> Result<()> {
run_test_with_context(|_| {
// Arrange
helper::git_init(".")?;
helper::git_commit("chore: init")?;
helper::git_commit("feat: feature")?;
helper::git_commit("fix: bug fix")?;
run_test_with_context(|_| {
// Arrange
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("feat: feature")?;
helper::git_commit("fix: bug fix")?;

// Act
Command::cargo_bin("cog")?
.arg("check")
// Assert
.assert()
.success()
.stdout(predicate::str::contains("No errored commits"));
Ok(())
})
// Act
Command::cargo_bin("cog")?
.arg("check")
// Assert
.assert()
.success()
.stdout(predicate::str::contains("No errored commits"));
Ok(())
})
}

#[test]
#[cfg(not(tarpaulin))]
fn cog_check_failure() -> Result<()> {
run_test_with_context(|_| {
// Arrange
helper::git_init(".")?;
helper::git_commit("chore: init")?;
helper::git_commit("toto: feature")?;
helper::git_commit("fix: bug fix")?;
run_test_with_context(|_| {
// Arrange
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("toto: feature")?;
helper::git_commit("fix: bug fix")?;

// Act
Command::cargo_bin("cog")?
.arg("check")
// Assert
.assert()
.failure()
.stderr(predicate::str::contains("Commit type `toto` not allowed"));
Ok(())
})
// Act
Command::cargo_bin("cog")?
.arg("check")
// Assert
.assert()
.failure()
.stderr(predicate::str::contains("Commit type `toto` not allowed"));
Ok(())
})
}

#[test]
#[cfg(not(tarpaulin))]
fn cog_check_from_latest_tag_ok() -> Result<()> {
run_test_with_context(|_| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("toto: errored commit")?;
helper::git_commit("feat: feature")?;
Expand All @@ -77,7 +76,7 @@ fn cog_check_from_latest_tag_ok() -> Result<()> {
fn cog_check_from_latest_tag_failure() -> Result<()> {
run_test_with_context(|_| {
// Arrange
helper::git_init(".")?;
helper::git_init()?;
helper::git_commit("chore: init")?;
helper::git_commit("toto: errored commit")?;
helper::git_commit("feat: feature")?;
Expand Down
13 changes: 8 additions & 5 deletions tests/cog_init_test_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn init_empty_repo_in_target_dir() -> Result<()> {
fn init_existing_repo() -> Result<()> {
run_test_with_context(|context| {
// Arrange
git_init_with_path("test_repo_existing")?;
git_init_and_set_current_path("test_repo_existing")?;
assert_file_exists(context.test_dir.join("test_repo_existing"));
helper::git_commit("chore: test commit")?;

Expand All @@ -51,9 +51,12 @@ fn init_existing_repo() -> Result<()> {
fn fail_if_config_exist() -> Result<()> {
run_test_with_context(|context| {
// Arrange
helper::git_init_with_path("test_repo_existing")?;
helper::git_init_and_set_current_path("test_repo_existing")?;
std::fs::write(
&context.test_dir.join("test_repo_existing").join(CONFIG_PATH),
&context
.test_dir
.join("test_repo_existing")
.join(CONFIG_PATH),
"[hooks]",
)?;
helper::git_commit("chore: test commit")?;
Expand Down Expand Up @@ -82,8 +85,8 @@ fn init_current_dir_with_no_arg() -> Result<()> {
Command::cargo_bin("cog")?
.arg("init")
// Assert
.assert().success();
.assert()
.success();
Ok(())
})

}

0 comments on commit a32a517

Please sign in to comment.