fix(git): check tag_disabled option (#4527)

* fix(git): check `tag_disabled` option

* Check in `map` and test fixes
This commit is contained in:
Loong Wang 2022-11-07 05:43:50 +08:00 committed by GitHub
parent 1a3d51fe76
commit fd165b96cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}) })
.map(|variable| match variable { .map(|variable| match variable {
"hash" => Some(Ok(git_hash(context.get_repo().ok()?, &config)?)), "hash" => Some(Ok(git_hash(context.get_repo().ok()?, &config)?)),
"tag" => Some(Ok(format!( "tag" if !config.tag_disabled => Some(Ok(format!(
"{}{}", "{}{}",
config.tag_symbol, config.tag_symbol,
git_tag(context.get_repo().ok()?, &config)? git_tag(context.get_repo().ok()?, &config)?
@ -201,10 +201,51 @@ mod tests {
repo_dir.close() repo_dir.close()
} }
#[test]
fn test_render_commit_hash_with_tag_disabled() -> io::Result<()> {
let repo_dir = fixture_repo(FixtureProvider::Git)?;
create_command("git")?
.args(&["tag", "v1", "-m", "Testing tags"])
.current_dir(&repo_dir.path())
.output()?;
let mut git_commit = create_command("git")?
.args(&["rev-parse", "HEAD"])
.current_dir(&repo_dir.path())
.output()?
.stdout;
git_commit.truncate(7);
let commit_output = str::from_utf8(&git_commit).unwrap().trim();
let actual = ModuleRenderer::new("git_commit")
.config(toml::toml! {
[git_commit]
only_detached = false
})
.path(&repo_dir.path())
.collect();
let expected = Some(format!(
"{} ",
Color::Green
.bold()
.paint(format!("({})", commit_output.trim()))
));
assert_eq!(expected, actual);
Ok(())
}
#[test] #[test]
fn test_render_commit_hash_with_tag_enabled() -> io::Result<()> { fn test_render_commit_hash_with_tag_enabled() -> io::Result<()> {
let repo_dir = fixture_repo(FixtureProvider::Git)?; let repo_dir = fixture_repo(FixtureProvider::Git)?;
create_command("git")?
.args(&["tag", "v1", "-m", "Testing tags"])
.current_dir(&repo_dir.path())
.output()?;
let mut git_commit = create_command("git")? let mut git_commit = create_command("git")?
.args(["rev-parse", "HEAD"]) .args(["rev-parse", "HEAD"])
.current_dir(repo_dir.path()) .current_dir(repo_dir.path())
@ -227,7 +268,7 @@ mod tests {
[git_commit] [git_commit]
only_detached = false only_detached = false
tag_disabled = false tag_disabled = false
tag_symbol = "" tag_symbol = " "
}) })
.path(repo_dir.path()) .path(repo_dir.path())
.collect(); .collect();