Skip to content

Commit

Permalink
llvm-cov: parse "git config core.commentChar"
Browse files Browse the repository at this point in the history
  • Loading branch information
commonquail committed Oct 22, 2023
1 parent 825588d commit ac62abc
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,30 @@ impl Config {

let cfg = Config {
width: width as usize,
comment_char: Config::comment_char_from_git(),
comment_char: parse_git_config_commentchar(git_config_commentchar()),
};

Ok(cfg)
}
}

fn comment_char_from_git() -> char {
use std::process::Command;
fn git_config_commentchar() -> Result<Vec<u8>, io::Error> {
std::process::Command::new("git")
.args(&["config", "core.commentChar"])
.output()
.map(|o| o.stdout)
}

let output: Vec<u8> = Command::new("git")
.args(&["config", "core.commentChar"])
.output()
.map(|o| o.stdout)
.unwrap_or_else(|_| "#".into());

// The setting is either unset, "auto", or precisely 1 ASCII character;
// Git won't commit with an invalid configuration value. "auto" support
// can be added on-demand, it requires at least 2 passes.
if output.is_empty() || output == b"auto" {
'#'
} else {
output[0].into()
}
fn parse_git_config_commentchar(git_output: Result<Vec<u8>, io::Error>) -> char {
let output: Vec<u8> = git_output.unwrap_or_else(|_| "#".into());

// The setting is either unset, "auto", or precisely 1 ASCII character;
// Git won't commit with an invalid configuration value. "auto" support
// can be added on-demand, it requires at least 2 passes.
if output.is_empty() || output == b"auto" {
'#'
} else {
output[0].into()
}
}

Expand Down Expand Up @@ -618,6 +619,26 @@ mod tests {
}
}

#[test]
fn parses_git_config_commentchar() {
let matrix = vec![
(Ok("".into()), '#'),
(Ok("auto".into()), '#'),
(Ok("#".into()), '#'),
(Ok("xy".into()), 'x'),
(Err(io::Error::from(io::ErrorKind::PermissionDenied)), '#'),
];
let (actual, expected): (Vec<_>, Vec<_>) = matrix
.into_iter()
.map(|(input, expected)| {
let x = format!("{:?}", &input);
let actual = parse_git_config_commentchar(input);
((x.clone(), actual), (x, expected))
})
.unzip();
assert_eq!(expected, actual);
}

#[test]
fn arg_width_0_exits_with_code_1() {
let output = run_debug_binary_no_input(target_binary_with_width("0"));
Expand Down

0 comments on commit ac62abc

Please sign in to comment.