Skip to content

Commit

Permalink
🐛 fix: ignore comment lines in commit messages (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
welpo committed Feb 9, 2024
1 parent 1ebd6ca commit 821d6c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/lint.rs
Expand Up @@ -23,7 +23,7 @@ pub fn run_lint_on_each_line(
let mut errors = Vec::new();

for line in non_empty_lines.clone() {
match run_lint(&line.to_string(), config) {
match run_lint(line, config) {
Ok(parsed_commit) => parsed_commits.push(parsed_commit),
Err(error) => {
error!("{}", error);
Expand All @@ -47,11 +47,12 @@ pub fn run_lint_on_each_line(

/// Lints and parses the given commit message.
/// Returns a `ParsedCommit` struct if the commit is valid, or an error message if it is not.
pub fn run_lint(commit: &String, config: &Config) -> Result<ParsedCommit, SumiError> {
pub fn run_lint(raw_commit: &str, config: &Config) -> Result<ParsedCommit, SumiError> {
let commit = preprocess_commit_message(raw_commit);
info!("💬 Input: \"{}\"", commit);
let mut non_fatal_errors: Vec<SumiError> = Vec::new();
let parsed_commit = handle_parsing(commit, config, &mut non_fatal_errors)?;
let errors = validate_commit(commit, &parsed_commit, config);
let parsed_commit = handle_parsing(&commit, config, &mut non_fatal_errors)?;
let errors = validate_commit(&commit, &parsed_commit, config);
non_fatal_errors.extend(errors);
if non_fatal_errors.is_empty() {
handle_success(&parsed_commit, config)?;
Expand All @@ -60,6 +61,15 @@ pub fn run_lint(commit: &String, config: &Config) -> Result<ParsedCommit, SumiEr
handle_failure(&non_fatal_errors)
}

fn preprocess_commit_message(commit: &str) -> String {
// Remove comments.
commit
.lines()
.filter(|line| !line.trim_start().starts_with('#'))
.collect::<Vec<&str>>()
.join("\n")
}

fn validate_commit(
commit: &String,
parsed_commit: &ParsedCommit,
Expand Down
10 changes: 5 additions & 5 deletions tests/lint/test_display.rs
Expand Up @@ -4,7 +4,7 @@ use assert_cmd::Command;
use predicates::prelude::*;
use serde_json::Value;

const COMMIT_MESSAGE: &str = "🐛 fix(Scope)!: short description\n\nLonger body description\n\nBREAKING CHANGE: breaking description\nFooter1: value1\nFooter2: value2\n\n#123, ce6df36";
const COMMIT_MESSAGE: &str = "🐛 fix(Scope)!: short description\n\nLonger body description\n\nBREAKING CHANGE: breaking description\nFooter1: value1\nFooter2: value2\n\nFixes #123, ce6df36";

fn assert_table_output(cmd: &mut Command) {
cmd.assert()
Expand Down Expand Up @@ -64,7 +64,7 @@ fn success_display_format_toml() {
fn success_display_format_json() {
let mut cmd = run_isolated_git_sumi("");

let commit_message = "✨ feat(CrimeAndPunishment): Add Raskolnikov's internal monologue\n\nIntroduces the new chapter where Raskolnikov wrestles with his conscience.\n\nBREAKING CHANGE: New perspective on morality introduced.\nTranslator: Garnett\nPublisher: Penguin\n#1866";
let commit_message = "✨ feat(CrimeAndPunishment): Add Raskolnikov's internal monologue\n\nIntroduces the new chapter where Raskolnikov wrestles with his conscience.\n\nBREAKING CHANGE: New perspective on morality introduced.\nTranslator: Garnett\nPublisher: Penguin\nFixes #1866";

let output = cmd
.arg("-dCGqf")
Expand Down Expand Up @@ -92,7 +92,8 @@ fn success_display_format_json() {
let expected_footers: Value = serde_json::json!([
"BREAKING CHANGE:New perspective on morality introduced.",
"Translator:Garnett",
"Publisher:Penguin\n#1866"
"Publisher:Penguin",
"Fixes #1866"
]);
assert_eq!(parsed["footers"], expected_footers);
assert_eq!(parsed["is_breaking"], true);
Expand Down Expand Up @@ -137,12 +138,11 @@ fn success_display_format_markdown() {
.stdout(contains("| Key"))
.stdout(contains("| Value"))
.stdout(contains("| Gitmoji | 🐛"))
.stdout(contains("|----------------------|----------------------------------------------------------------------|"))
.stdout(contains("| Scope | Scope"))
.stdout(contains("| Description | short description"))
.stdout(contains("| Body | Longer body description "))
.stdout(contains(
"| Footers | BREAKING CHANGE:breaking description, Footer1:value1, Footer2:value2 |",
"| Footers | BREAKING CHANGE:breaking description, Footer1:value1, Footer2:value2, Fixes #123, ce6df36 |",
))
.stdout(contains("| Is breaking | true "))
.stdout(contains("| Breaking description | breaking description "))
Expand Down
10 changes: 10 additions & 0 deletions tests/lint/test_single_rule.rs
Expand Up @@ -105,6 +105,16 @@ fn error_invalid_whitespace() {
}
}

#[test]
fn success_ignore_comments() {
let mut cmd = run_isolated_git_sumi("");
cmd.arg("-W")
// If the comment weren't ignored, git-sumi would complain about the adjacent spaces in the second line.
.arg("feat: adds feature\n\n# modified: src/lib.rs")
.assert()
.success();
}

#[test]
fn success_non_letter_cant_be_capitalised() {
let test_cases = [" space", "✅ emoji", "1 number"];
Expand Down

0 comments on commit 821d6c6

Please sign in to comment.