Skip to content

Commit

Permalink
fix: make footer serialization and deserialization symmetric
Browse files Browse the repository at this point in the history
Ref: #139
  • Loading branch information
Paul Delafosse authored and oknozor committed Nov 30, 2021
1 parent e1f219b commit 04befc1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ which = "^4"
lazy_static = "^1"
toml = "^0"
structopt = { version = "^0", optional = true }
conventional_commit_parser = "0.5.2"
conventional_commit_parser = "0.9.0"
pest = "2.1.3"
pest_derive = "2.1.0"
tera = "1.12.1"
Expand Down
24 changes: 17 additions & 7 deletions src/bin/coco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::Write;
use cocogitto::{CocoGitto, COMMITS_METADATA};

use anyhow::{bail, Result};
use conventional_commit_parser::commit::Footer;
use conventional_commit_parser::commit::Separator;
use itertools::Itertools;
use structopt::clap::{AppSettings, Shell};
use structopt::StructOpt;
Expand Down Expand Up @@ -117,18 +117,28 @@ fn main() -> Result<()> {
.filter(|&line| !line.trim_start().starts_with('#'))
.join("\n");

let mut cc = conventional_commit_parser::parse(content.trim())?;
let cc = conventional_commit_parser::parse(content.trim())?;

let footer = if !cc.footers.is_empty() {
let Footer { token, content } = cc.footers.swap_remove(0);
Some(format!("{}: {}", token, content.trim()))
} else {
let footers: Option<String> = if cc.footers.is_empty() {
None
} else {
Some(
cc.footers
.iter()
.map(|footer| {
let separator = match footer.token_separator {
Separator::Colon => ": ",
Separator::Hash => " #",
};
format!("{}{}{}", footer.token, separator, footer.content)
})
.join("\n"),
)
};

(
cc.body.map(|s| s.trim().to_string()),
footer,
footers,
cc.is_breaking_change || cli.breaking_change,
)
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/conventional/changelog/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ mod test {
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
..Default::default()
}],
is_breaking_change: false,
},
Expand All @@ -163,6 +164,7 @@ mod test {
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
..Default::default()
}],
is_breaking_change: false,
},
Expand All @@ -182,6 +184,7 @@ mod test {
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
..Default::default()
}],
is_breaking_change: false,
},
Expand Down
1 change: 1 addition & 0 deletions src/conventional/changelog/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mod test {
footers: vec![Footer {
token: "token".to_string(),
content: "content".to_string(),
..Default::default()
}],
is_breaking_change: false,
},
Expand Down
44 changes: 43 additions & 1 deletion src/conventional/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ mod test {
use crate::conventional::commit::{verify, Commit};

use chrono::NaiveDateTime;
use conventional_commit_parser::commit::{CommitType, ConventionalCommit};

use conventional_commit_parser::commit::{CommitType, ConventionalCommit, Footer, Separator};
use indoc::indoc;
use speculoos::prelude::*;

#[test]
Expand All @@ -232,6 +234,46 @@ mod test {
assert_that!(commit.footers).is_empty();
}

#[test]
fn should_map_conventional_commit_message_with_multiple_scope_to_struct() {
// Arrange
let message = indoc!(
"feat(database): add postgresql driver
The body
footer: 123
footer2 #456"
);

// Act
let commit = conventional_commit_parser::parse(message);

// Assert
let commit = commit.unwrap();
assert_that!(commit.commit_type).is_equal_to(CommitType::Feature);
assert_that!(commit.scope).is_equal_to(Some("database".to_owned()));
assert_that!(commit.summary).is_equal_to("add postgresql driver".to_owned());
assert_that!(commit.is_breaking_change).is_false();
assert_that!(commit.body)
.is_some()
.is_equal_to("The body".to_string());
assert_that!(commit.footers).is_equal_to(vec![
Footer {
token: "footer".to_string(),
content: "123".to_string(),
..Default::default()
},
Footer {
token: "footer2".to_string(),
content: "456".to_string(),
token_separator: Separator::Hash,
},
]);

assert_that!(commit.to_string()).is_equal_to(&message.to_string())
}

#[test]
fn should_verify_message_ok() {
// Arrange
Expand Down

0 comments on commit 04befc1

Please sign in to comment.