From ad41a5f3f6ead623d5f3f2524629e0c426a130be Mon Sep 17 00:00:00 2001 From: Paul Delafosse Date: Sun, 31 Oct 2021 17:43:47 +0100 Subject: [PATCH] test: add write_file test helper --- src/conventional/changelog/release.rs | 29 ++++++----- src/conventional/version.rs | 74 +++++++++++++++++---------- src/git/commit.rs | 8 +-- src/git/diff.rs | 10 ++-- src/git/repository.rs | 7 +-- src/lib.rs | 13 +++-- tests/lib_tests/init.rs | 4 +- 7 files changed, 89 insertions(+), 56 deletions(-) diff --git a/src/conventional/changelog/release.rs b/src/conventional/changelog/release.rs index de44800e..30fda538 100644 --- a/src/conventional/changelog/release.rs +++ b/src/conventional/changelog/release.rs @@ -47,7 +47,7 @@ impl<'a> From<&'a Footer> for ChangelogFooter<'a> { #[cfg(test)] mod test { - use chrono::Utc; + use chrono::NaiveDateTime; use conventional_commit_parser::commit::{CommitType, ConventionalCommit, Footer}; use git2::Oid; use indoc::indoc; @@ -58,25 +58,26 @@ mod test { use crate::conventional::commit::Commit; use crate::git::oid::OidOf; use crate::git::tag::Tag; + use anyhow::Result; #[test] - fn should_render_default_template() { + fn should_render_default_template() -> Result<()> { + let date = NaiveDateTime::parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S")?; + let paul_delafosse = "Paul Delafosse"; let a_commit_hash = "17f7e23081db15e9318aeb37529b1d473cf41cbe"; let version = Tag::new( "1.0.0", - Oid::from_str("9bb5facac5724bc81385fdd740fedbb49056da00").unwrap(), - ) - .unwrap(); + Oid::from_str("9bb5facac5724bc81385fdd740fedbb49056da00")?, + )?; let from = Tag::new( "0.1.0", - Oid::from_str("fae3a288a1bc69b14f85a1d5fe57cee1964acd60").unwrap(), - ) - .unwrap(); + Oid::from_str("fae3a288a1bc69b14f85a1d5fe57cee1964acd60")?, + )?; let version = Release { version: OidOf::Tag(version), from: OidOf::Tag(from), - date: Utc::now().naive_utc(), + date, commits: vec![ ChangelogCommit { author_username: Some("oknozor"), @@ -94,7 +95,7 @@ mod test { is_breaking_change: false, }, author: paul_delafosse.to_string(), - date: Utc::now().naive_utc(), + date, }, }, ChangelogCommit { @@ -113,7 +114,7 @@ mod test { is_breaking_change: false, }, author: paul_delafosse.to_string(), - date: Utc::now().naive_utc(), + date, }, }, ChangelogCommit { @@ -132,7 +133,7 @@ mod test { is_breaking_change: false, }, author: "James Delleck".to_string(), - date: Utc::now().naive_utc(), + date, }, }, ], @@ -143,7 +144,7 @@ mod test { assert_that!(changelog).is_ok().is_equal_to( indoc! { - "## 1.0.0 - 2021-10-31 + "## 1.0.0 - 2015-09-05 #### Bug Fixes - **(parser)** fix parser implementation - (17f7e23) - *oknozor* #### Features @@ -152,5 +153,7 @@ mod test { } .to_string(), ); + + Ok(()) } } diff --git a/src/conventional/version.rs b/src/conventional/version.rs index 5a2003e6..3abba2f5 100644 --- a/src/conventional/version.rs +++ b/src/conventional/version.rs @@ -23,7 +23,9 @@ impl VersionIncrement { VersionIncrement::Manual(version) => { Version::parse(version).map_err(|err| anyhow!(err)) } - VersionIncrement::Auto => self.create_version_from_commit_history(current_version), + VersionIncrement::Auto => { + VersionIncrement::create_version_from_commit_history(current_version) + } VersionIncrement::Major => Ok(Version::new(current_version.major + 1, 0, 0)), VersionIncrement::Patch => Ok(Version::new( current_version.major, @@ -38,7 +40,7 @@ impl VersionIncrement { } } - fn create_version_from_commit_history(&self, current_version: &Version) -> Result { + fn create_version_from_commit_history(current_version: &Version) -> Result { let repository = Repository::open(".")?; let changelog_start_oid = repository .get_latest_tag_oid() @@ -224,88 +226,108 @@ mod test { } #[test] - fn increment_minor_version_should_set_patch_to_zero() { - let version = Version::from_str("1.1.1").unwrap(); + fn increment_minor_version_should_set_patch_to_zero() -> Result<()> { + let version = Version::from_str("1.1.1")?; + + let bumped = VersionIncrement::Minor.bump(&version); - let bumped = VersionIncrement::Minor.bump(&version).unwrap(); + assert_that!(bumped) + .is_ok() + .is_equal_to(Version::from_str("1.2.0")?); - assert_that!(bumped).is_equal_to(Version::from_str("1.2.0").unwrap()) + Ok(()) } #[test] - fn increment_major_version_should_set_minor_and_patch_to_zero() { - let version = Version::from_str("1.1.1").unwrap(); + fn increment_major_version_should_set_minor_and_patch_to_zero() -> Result<()> { + let version = Version::from_str("1.1.1")?; - let bumped = VersionIncrement::Major.bump(&version).unwrap(); + let bumped = VersionIncrement::Major.bump(&version); - assert_that!(bumped).is_equal_to(Version::from_str("2.0.0").unwrap()) + assert_that!(bumped) + .is_ok() + .is_equal_to(Version::from_str("2.0.0")?); + + Ok(()) } #[test] - fn increment_should_strip_metadata() { - let version = Version::from_str("1.1.1-pre+10.1").unwrap(); + fn increment_should_strip_metadata() -> Result<()> { + let version = Version::from_str("1.1.1-pre+10.1")?; - let bumped = VersionIncrement::Patch.bump(&version).unwrap(); + let bumped = VersionIncrement::Patch.bump(&version); + + assert_that!(bumped) + .is_ok() + .is_equal_to(Version::from_str("1.1.2")?); - assert_that!(bumped).is_equal_to(Version::from_str("1.1.2").unwrap()) + Ok(()) } #[test] - fn should_get_next_auto_version_patch() { + fn should_get_next_auto_version_patch() -> Result<()> { let patch = Commit::commit_fixture(CommitType::BugFix, false); let version = VersionIncrement::version_increment_from_commit_history( - &Version::parse("1.0.0").unwrap(), + &Version::parse("1.0.0")?, &[patch], ); assert_that!(version) .is_ok() - .is_equal_to(VersionIncrement::Patch) + .is_equal_to(VersionIncrement::Patch); + + Ok(()) } #[test] - fn should_get_next_auto_version_breaking_changes() { + fn should_get_next_auto_version_breaking_changes() -> Result<()> { let feature = Commit::commit_fixture(CommitType::Feature, false); let breaking_change = Commit::commit_fixture(CommitType::Feature, true); let version = VersionIncrement::version_increment_from_commit_history( - &Version::parse("1.0.0").unwrap(), + &Version::parse("1.0.0")?, &[breaking_change, feature], ); assert_that!(version) .is_ok() - .is_equal_to(VersionIncrement::Major) + .is_equal_to(VersionIncrement::Major); + + Ok(()) } #[test] - fn should_get_next_auto_version_breaking_changes_on_initial_dev_version() { + fn should_get_next_auto_version_breaking_changes_on_initial_dev_version() -> Result<()> { let feature = Commit::commit_fixture(CommitType::Feature, false); let breaking_change = Commit::commit_fixture(CommitType::Feature, true); let version = VersionIncrement::version_increment_from_commit_history( - &Version::parse("0.1.0").unwrap(), + &Version::parse("0.1.0")?, &[breaking_change, feature], ); assert_that!(version) .is_ok() - .is_equal_to(VersionIncrement::Minor) + .is_equal_to(VersionIncrement::Minor); + + Ok(()) } #[test] - fn should_get_next_auto_version_minor() { + fn should_get_next_auto_version_minor() -> Result<()> { let patch = Commit::commit_fixture(CommitType::BugFix, false); let feature = Commit::commit_fixture(CommitType::Feature, false); let version = VersionIncrement::version_increment_from_commit_history( - &Version::parse("1.0.0").unwrap(), + &Version::parse("1.0.0")?, &[patch, feature], ); assert_that!(version) .is_ok() - .is_equal_to(VersionIncrement::Minor) + .is_equal_to(VersionIncrement::Minor); + + Ok(()) } } diff --git a/src/git/commit.rs b/src/git/commit.rs index 05062309..f560fd7e 100644 --- a/src/git/commit.rs +++ b/src/git/commit.rs @@ -45,7 +45,7 @@ mod test { #[test] fn create_commit_ok() -> Result<()> { run_test_with_context(|context| { - let repo = Repository::init(".")?; + let repo = Repository::init(&context.test_dir)?; std::fs::write(context.test_dir.join("file"), "changes")?; repo.add_all()?; @@ -56,8 +56,8 @@ mod test { #[test] fn not_create_empty_commit() -> Result<()> { - run_test_with_context(|_| { - let repo = Repository::init(".")?; + run_test_with_context(|context| { + let repo = Repository::init(&context.test_dir)?; assert_that!(repo.commit("feat: a test commit")).is_err(); Ok(()) @@ -67,7 +67,7 @@ mod test { #[test] fn not_create_empty_commit_with_unstaged_changed() -> Result<()> { run_test_with_context(|context| { - let repo = Repository::init(".")?; + let repo = Repository::init(&context.test_dir)?; std::fs::write(context.test_dir.join("file"), "changes")?; assert_that!(repo.commit("feat: a test commit")).is_err(); diff --git a/src/git/diff.rs b/src/git/diff.rs index e4eb4fb0..d03204b7 100644 --- a/src/git/diff.rs +++ b/src/git/diff.rs @@ -37,7 +37,7 @@ mod test { #[test] fn get_diff_some() -> Result<()> { run_test_with_context(|context| { - let repo = Repository::init(".")?; + let repo = Repository::init(&context.test_dir)?; std::fs::write(context.test_dir.join("file"), "changes")?; repo.add_all()?; @@ -49,7 +49,7 @@ mod test { #[test] fn get_diff_none() -> Result<()> { run_test_with_context(|context| { - let repo = Repository::init(".")?; + let repo = Repository::init(&context.test_dir)?; std::fs::write(context.test_dir.join("file"), "changes")?; assert!(repo.get_diff(false).is_none()); @@ -60,7 +60,7 @@ mod test { #[test] fn get_diff_include_untracked_some() -> Result<()> { run_test_with_context(|context| { - let repo = Repository::init(".")?; + let repo = Repository::init(&context.test_dir)?; std::fs::write(context.test_dir.join("file"), "changes")?; assert!(repo.get_diff(true).is_some()); @@ -70,8 +70,8 @@ mod test { #[test] fn get_diff_include_untracked_none() -> Result<()> { - run_test_with_context(|_| { - let repo = Repository::init(".")?; + run_test_with_context(|context| { + let repo = Repository::init(&context.test_dir)?; assert!(repo.get_diff(true).is_none()); Ok(()) diff --git a/src/git/repository.rs b/src/git/repository.rs index 5aa82d63..ac0936ab 100644 --- a/src/git/repository.rs +++ b/src/git/repository.rs @@ -116,7 +116,7 @@ mod test { #[test] fn get_repo_working_dir_some() -> Result<()> { run_test_with_context(|context| { - let repo = Repository::init(".")?; + let repo = Repository::init(&context.test_dir)?; let dir = context.test_dir.join("dir"); std::fs::create_dir(&dir)?; std::env::set_current_dir(&dir)?; @@ -129,14 +129,15 @@ mod test { // see: https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server #[test] fn open_bare_err() -> Result<()> { - run_test_with_context(|_| { + run_test_with_context(|context| { Command::new("git") .arg("init") + .arg(&context.test_dir) .arg("bare") .stdout(Stdio::inherit()) .output()?; - let repo = Repository::open("."); + let repo = Repository::open(&context.test_dir); assert_that!(repo).is_err(); Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 6db2de88..76a2e3af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -618,10 +618,9 @@ impl CocoGitto { #[cfg(test)] pub mod test_helpers { - use std::panic; - use std::path::PathBuf; - use anyhow::Result; + use std::panic; + use std::path::{Path, PathBuf}; use tempfile::TempDir; pub struct TestContext { @@ -629,6 +628,14 @@ pub mod test_helpers { pub test_dir: PathBuf, } + impl TestContext { + pub fn write_file>(&self, path: S, content: &str) -> Result<()> { + let path = self.test_dir.join(path); + std::fs::write(path, content)?; + Ok(()) + } + } + // Save the current directory in the test context // Change current dir to a temp directory // Execute the test in context diff --git a/tests/lib_tests/init.rs b/tests/lib_tests/init.rs index 1e03d6b0..d8589f94 100644 --- a/tests/lib_tests/init.rs +++ b/tests/lib_tests/init.rs @@ -8,9 +8,9 @@ use speculoos::prelude::*; #[test] fn should_init_a_cog_repository() -> Result<()> { // Arrange - run_test_with_context(|_| { + run_test_with_context(|context| { // Act - cocogitto::init(".")?; + cocogitto::init(&context.test_dir)?; // Assert assert_that(&Path::new("cog.toml")).exists();