Skip to content

Commit

Permalink
test: add write_file test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent 5ce5187 commit ad41a5f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 56 deletions.
29 changes: 16 additions & 13 deletions src/conventional/changelog/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"),
Expand All @@ -94,7 +95,7 @@ mod test {
is_breaking_change: false,
},
author: paul_delafosse.to_string(),
date: Utc::now().naive_utc(),
date,
},
},
ChangelogCommit {
Expand All @@ -113,7 +114,7 @@ mod test {
is_breaking_change: false,
},
author: paul_delafosse.to_string(),
date: Utc::now().naive_utc(),
date,
},
},
ChangelogCommit {
Expand All @@ -132,7 +133,7 @@ mod test {
is_breaking_change: false,
},
author: "James Delleck".to_string(),
date: Utc::now().naive_utc(),
date,
},
},
],
Expand All @@ -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
Expand All @@ -152,5 +153,7 @@ mod test {
}
.to_string(),
);

Ok(())
}
}
74 changes: 48 additions & 26 deletions src/conventional/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,7 +40,7 @@ impl VersionIncrement {
}
}

fn create_version_from_commit_history(&self, current_version: &Version) -> Result<Version> {
fn create_version_from_commit_history(current_version: &Version) -> Result<Version> {
let repository = Repository::open(".")?;
let changelog_start_oid = repository
.get_latest_tag_oid()
Expand Down Expand Up @@ -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(())
}
}
8 changes: 4 additions & 4 deletions src/git/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand 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(())
Expand All @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions src/git/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand 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());
Expand All @@ -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());
Expand All @@ -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(())
Expand Down
7 changes: 4 additions & 3 deletions src/git/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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(())
Expand Down
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,17 +618,24 @@ 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 {
pub current_dir: PathBuf,
pub test_dir: PathBuf,
}

impl TestContext {
pub fn write_file<S: AsRef<Path>>(&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
Expand Down
4 changes: 2 additions & 2 deletions tests/lib_tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ad41a5f

Please sign in to comment.