Skip to content

Commit

Permalink
test(changelog): add github template test
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Delafosse authored and oknozor committed Nov 30, 2021
1 parent ad41a5f commit e1f219b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 88 deletions.
193 changes: 115 additions & 78 deletions src/conventional/changelog/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,86 +62,14 @@ mod test {

#[test]
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")?,
)?;
let from = Tag::new(
"0.1.0",
Oid::from_str("fae3a288a1bc69b14f85a1d5fe57cee1964acd60")?,
)?;
let version = Release {
version: OidOf::Tag(version),
from: OidOf::Tag(from),
date,
commits: vec![
ChangelogCommit {
author_username: Some("oknozor"),
commit: Commit {
oid: a_commit_hash.to_string(),
message: ConventionalCommit {
commit_type: CommitType::BugFix,
scope: Some("parser".to_string()),
summary: "fix parser implementation".to_string(),
body: Some("the body".to_string()),
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
}],
is_breaking_change: false,
},
author: paul_delafosse.to_string(),
date,
},
},
ChangelogCommit {
author_username: None,
commit: Commit {
oid: a_commit_hash.to_string(),
message: ConventionalCommit {
commit_type: CommitType::Feature,
scope: None,
summary: "awesome feature".to_string(),
body: Some("the body".to_string()),
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
}],
is_breaking_change: false,
},
author: paul_delafosse.to_string(),
date,
},
},
ChangelogCommit {
author_username: Some("oknozor"),
commit: Commit {
oid: a_commit_hash.to_string(),
message: ConventionalCommit {
commit_type: CommitType::Feature,
scope: Some("parser".to_string()),
summary: "implement the changelog generator".to_string(),
body: Some("the body".to_string()),
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
}],
is_breaking_change: false,
},
author: "James Delleck".to_string(),
date,
},
},
],
};

// Arrange
let release = Release::fixture();
let renderer = Renderer::default();
let changelog = renderer.render(&version);

// Act
let changelog = renderer.render(&release);

// Assert
assert_that!(changelog).is_ok().is_equal_to(
indoc! {
"## 1.0.0 - 2015-09-05
Expand All @@ -156,4 +84,113 @@ mod test {

Ok(())
}

#[test]
fn should_render_github_template() -> Result<()> {
// Arrange
let release = Release::fixture();
let renderer = Renderer::github();

// Act
let changelog = renderer.render(&release);

// Assert
assert_that!(changelog).is_ok().is_equal_to(
indoc! {
"## [1.0.0](https://github.com/oknozor/cocogitto/compare/0.1.0..1.0.0) - 2015-09-05
#### Bug Fixes
- **(parser)** fix parser implementation - ([17f7e23](https://github.com/oknozor/cocogitto/commit/17f7e23081db15e9318aeb37529b1d473cf41cbe)) - [@oknozor](https://github.com/oknozor)
#### Features
- **(parser)** implement the changelog generator - ([17f7e23](https://github.com/oknozor/cocogitto/commit/17f7e23081db15e9318aeb37529b1d473cf41cbe)) - [@oknozor](https://github.com/oknozor)
- awesome feature - ([17f7e23](https://github.com/oknozor/cocogitto/commit/17f7e23081db15e9318aeb37529b1d473cf41cbe)) - Paul Delafosse"
}
.to_string(),
);

Ok(())
}

impl Release<'_> {
pub fn fixture() -> Release<'static> {
let date =
NaiveDateTime::parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S").unwrap();

let paul_delafosse = "Paul Delafosse";
let a_commit_hash = "17f7e23081db15e9318aeb37529b1d473cf41cbe";
let version = Tag::new(
"1.0.0",
Oid::from_str("9bb5facac5724bc81385fdd740fedbb49056da00").unwrap(),
)
.unwrap();
let from = Tag::new(
"0.1.0",
Oid::from_str("fae3a288a1bc69b14f85a1d5fe57cee1964acd60").unwrap(),
)
.unwrap();
Release {
version: OidOf::Tag(version),
from: OidOf::Tag(from),
date,
commits: vec![
ChangelogCommit {
author_username: Some("oknozor"),
commit: Commit {
oid: a_commit_hash.to_string(),
message: ConventionalCommit {
commit_type: CommitType::BugFix,
scope: Some("parser".to_string()),
summary: "fix parser implementation".to_string(),
body: Some("the body".to_string()),
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
}],
is_breaking_change: false,
},
author: paul_delafosse.to_string(),
date,
},
},
ChangelogCommit {
author_username: None,
commit: Commit {
oid: a_commit_hash.to_string(),
message: ConventionalCommit {
commit_type: CommitType::Feature,
scope: None,
summary: "awesome feature".to_string(),
body: Some("the body".to_string()),
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
}],
is_breaking_change: false,
},
author: paul_delafosse.to_string(),
date,
},
},
ChangelogCommit {
author_username: Some("oknozor"),
commit: Commit {
oid: a_commit_hash.to_string(),
message: ConventionalCommit {
commit_type: CommitType::Feature,
scope: Some("parser".to_string()),
summary: "implement the changelog generator".to_string(),
body: Some("the body".to_string()),
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
}],
is_breaking_change: false,
},
author: "James Delleck".to_string(),
date,
},
},
],
}
}
}
}
26 changes: 18 additions & 8 deletions src/conventional/changelog/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
use std::collections::HashMap;

use itertools::Itertools;
use tera::{get_json_pointer, to_value, try_get_value, Context, Tera, Value};

use crate::conventional::changelog::release::Release;
use crate::SETTINGS;
use itertools::Itertools;

const DEFAULT_TEMPLATE: &[u8] = include_bytes!("template/simple");
const DEFAULT_TEMPLATE_NAME: &str = "default_template";
const GITHUB_TEMPLATE: &[u8] = include_bytes!("template/github");
const GITHUB_TEMPLATE_NAME: &str = "github_template";

#[derive(Debug)]
pub struct Renderer {
tera: Tera,
template_name: String,
}

impl Default for Renderer {
fn default() -> Self {
let template = String::from_utf8_lossy(DEFAULT_TEMPLATE);
Self::new(template.as_ref()).unwrap()
Self::new(template.as_ref(), DEFAULT_TEMPLATE_NAME).unwrap()
}
}

impl Renderer {
pub fn github() -> Self {
let template = String::from_utf8_lossy(GITHUB_TEMPLATE);
Self::new(template.as_ref()).unwrap()
Self::new(template.as_ref(), GITHUB_TEMPLATE_NAME).unwrap()
}

pub fn new(template: &str) -> Result<Self, tera::Error> {
pub fn new(template: &str, template_name: &str) -> Result<Self, tera::Error> {
let mut tera = Tera::default();
tera.add_raw_template("default_changelog", template)?;

tera.add_raw_template(template_name, template.as_ref())?;
tera.register_filter("upper_first", Self::upper_first_filter);
tera.register_filter("unscoped", Self::unscoped);
Ok(Renderer { tera })

Ok(Renderer {
tera,
template_name: template_name.into(),
})
}

pub(crate) fn render(&self, version: &Release) -> Result<String, tera::Error> {
Expand All @@ -54,7 +63,7 @@ impl Renderer {
context.extend(template_context);

self.tera
.render("default_changelog", &context)
.render(&self.template_name, &context)
.map(|changelog| {
changelog
.lines()
Expand All @@ -75,7 +84,8 @@ impl Renderer {
Ok(tera::to_value(&s)?)
}

pub fn unscoped(value: &Value, args: &HashMap<String, Value>) -> Result<Value, tera::Error> {
// filter commit with no scope
fn unscoped(value: &Value, args: &HashMap<String, Value>) -> Result<Value, tera::Error> {
let mut arr = try_get_value!("unscoped", "scope", Vec<Value>, value);
if arr.is_empty() {
return Ok(arr.into());
Expand Down
5 changes: 4 additions & 1 deletion src/conventional/changelog/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ impl Serialize for OidOf {
{
let mut oidof = serializer.serialize_struct("OidOf", 3)?;
match self {
OidOf::Tag(tag) => oidof.serialize_field("tag", &tag.to_string_with_prefix())?,
OidOf::Tag(tag) => {
oidof.serialize_field("tag", &tag.to_string_with_prefix())?;
oidof.serialize_field("id", &tag.oid().to_string())?
}
OidOf::Head(oid) | OidOf::Other(oid) => {
oidof.serialize_field("id", &oid.to_string())?
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub fn init<S: AsRef<Path> + ?Sized>(path: &S) -> Result<()> {
.map_err(|err| anyhow!("Could not write file `{:?}`:{}", &settings_path, err))?;
}

// TODO: add cog.toml only
repository
.add_all()
.map_err(|err| anyhow!("Could not add file to repository index:{}", err))?;
Expand Down

0 comments on commit e1f219b

Please sign in to comment.