From 592b0e93c8694d5c6ef3a4056ef6d6c79fd8c086 Mon Sep 17 00:00:00 2001 From: fernandorocagonzalez Date: Tue, 17 Nov 2026 16:22:10 +0100 Subject: [PATCH] test: cover the git log line that does not parse Rust 1.100 counts the implicit else of the if let in commits() as its own line, and no test ever produced a log line without four fields, so coverage dropped to 99.76% and the test check fails on main as well as on every PR. The parsing moves to parse_log, and a unit test feeds it a malformed line, so the skip is actually checked rather than just counted. --- src/cli.rs | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 0276c0f..850ac07 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -257,21 +257,7 @@ impl GitPort for CliGit { } let log = local(log_args).await?; if log.status.success() { - for line in String::from_utf8_lossy(&log.stdout).lines() { - // The format above always gives four fields; a line that - // does not is skipped rather than half-read. - let mut fields = line.splitn(4, '\u{1f}'); - if let (Some(revision), Some(author), Some(at), Some(message)) = - (fields.next(), fields.next(), fields.next(), fields.next()) - { - range.commits.push(Commit { - revision: revision.to_string(), - author: author.to_string(), - message: message.to_string(), - at: at.parse().unwrap_or(0), - }); - } - } + range.commits = parse_log(&String::from_utf8_lossy(&log.stdout)); } if range.commits.len() > max { range.commits.truncate(max); @@ -402,6 +388,27 @@ impl GitPort for CliGit { // An I/O error running a program, as a GitError naming the program. One // function for every call site, rather than a closure at each: a program that // fails to start fails the same way wherever it is started. +// One commit per line of `git log --format=%H%x1f%an%x1f%ct%x1f%s`. That +// format always gives four fields; a line that does not is skipped rather than +// half-read. +fn parse_log(stdout: &str) -> Vec { + let mut commits = Vec::new(); + for line in stdout.lines() { + let mut fields = line.splitn(4, '\u{1f}'); + if let (Some(revision), Some(author), Some(at), Some(message)) = + (fields.next(), fields.next(), fields.next(), fields.next()) + { + commits.push(Commit { + revision: revision.to_string(), + author: author.to_string(), + message: message.to_string(), + at: at.parse().unwrap_or(0), + }); + } + } + commits +} + fn io_fail(what: &'static str) -> impl FnOnce(std::io::Error) -> GitError { move |e| GitError { message: format!("{what}: {e}"), @@ -507,6 +514,16 @@ async fn run_git(args: &[&str], envs: &[(String, String)]) -> Result<(), GitErro mod tests { use super::*; + #[test] + fn a_log_line_without_four_fields_is_skipped() { + let commits = parse_log("abc\u{1f}ana\u{1f}17\u{1f}fix it\nnot a commit line\n"); + assert_eq!(commits.len(), 1); + assert_eq!(commits[0].revision, "abc"); + assert_eq!(commits[0].author, "ana"); + assert_eq!(commits[0].at, 17); + assert_eq!(commits[0].message, "fix it"); + } + #[test] fn no_credentials_means_no_env() { assert!(auth_env(&HashMap::new()).is_empty());