Skip to content

Commit

Permalink
test: add changelog generation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 18, 2020
1 parent 35085f2 commit de60c0b
Showing 1 changed file with 82 additions and 4 deletions.
86 changes: 82 additions & 4 deletions src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ impl ChangelogWriter {
}

fn replace(&mut self) -> Result<()> {
let mut content = Changelog::header();
let mut content = Changelog::default_header();
content.push_str(&self.changelog.markdown(false));
content.push_str(Changelog::footer());
content.push_str(Changelog::default_footer());

fs::write(&self.path, content).map_err(|err| anyhow!(err))
}
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Changelog {
out
}

fn header() -> String {
fn default_header() -> String {
let title = "# Changelog";
let link = "[conventional commits]";
format!(
Expand All @@ -112,7 +112,85 @@ impl Changelog {
)
}

fn footer() -> &'static str {
fn default_footer() -> &'static str {
"- - -\n\nThis changelog was generated by [cocogitto](https://github.com/oknozor/cocogitto)."
}
}

#[cfg(test)]
mod test {
use crate::changelog::Changelog;
use crate::commit::{Commit, CommitMessage, CommitType};
use anyhow::Result;
use chrono::Utc;
use git2::Oid;

#[test]
fn should_generate_changelog() -> Result<()> {
// Arrange
let mut ch = Changelog {
from: Oid::from_str("5375e15770ddf8821d0c1ad393d315e243014c15")?,
to: Oid::from_str("35085f20c5293fc8830e4e44a9bb487f98734f73")?,
date: Utc::now().date().naive_local().to_string(),
commits: vec![
Commit {
shorthand: "2134".to_string(),
message: CommitMessage {
commit_type: CommitType::Feature,
scope: None,
body: None,
footer: None,
description: "this is a commit message".to_string(),
is_breaking_change: false,
},
author: "coco".to_string(),
date: Utc::now().naive_local(),
},
Commit {
shorthand: "4321".to_string(),
message: CommitMessage {
commit_type: CommitType::Feature,
scope: None,
body: None,
footer: None,
description: "this is an other commit message".to_string(),
is_breaking_change: false,
},
author: "cogi".to_string(),
date: Utc::now().naive_local(),
},
],
};

// Act
let content = ch.markdown(false);

// Assert
assert!(content.contains("2134 - this is a commit message - coco"));
assert!(content.contains("4321 - this is an other commit message - cogi"));
assert!(content.contains("## 5375e1..35085f -"));
assert!(content.contains("### Features"));
assert!(!content.contains("### Tests"));
Ok(())
}

#[test]
fn should_generate_empty_changelog() -> Result<()> {
// Arrange
let mut ch = Changelog {
from: Oid::from_str("5375e15770ddf8821d0c1ad393d315e243014c15")?,
to: Oid::from_str("35085f20c5293fc8830e4e44a9bb487f98734f73")?,
date: Utc::now().date().naive_local().to_string(),
commits: vec![],
};

// Act
let content = ch.markdown(false);

// Assert
println!("{}", content);
assert!(content.contains("## 5375e1..35085f"));
assert!(!content.contains("### Features"));
Ok(())
}
}

0 comments on commit de60c0b

Please sign in to comment.