feat: Show git_status counts (#434)

The git_status module can show the count of files next to their respective symbols.
This commit is contained in:
Brian Low
2019-10-26 00:20:20 -06:00
committed by Matan Kushner
parent 59148ac4ad
commit 2710d02709
6 changed files with 523 additions and 187 deletions
+277 -97
View File
@@ -1,6 +1,7 @@
use ansi_term::Color;
use ansi_term::{ANSIStrings, Color};
use std::fs::{self, File};
use std::io;
use std::path::PathBuf;
use std::process::Command;
use crate::common::{self, TestCommand};
@@ -22,11 +23,7 @@ fn barrier() {
fn shows_behind() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
behind(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -45,11 +42,7 @@ fn shows_behind() -> io::Result<()> {
fn shows_behind_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
behind(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
@@ -73,12 +66,7 @@ fn shows_ahead() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
File::create(repo_dir.join("readme.md"))?.sync_all()?;
Command::new("git")
.args(&["commit", "-am", "Update readme"])
.current_dir(&repo_dir)
.output()?;
barrier();
ahead(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -98,12 +86,7 @@ fn shows_ahead_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
File::create(repo_dir.join("readme.md"))?.sync_all()?;
Command::new("git")
.args(&["commit", "-am", "Update readme"])
.current_dir(&repo_dir)
.output()?;
barrier();
ahead(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
@@ -126,19 +109,7 @@ fn shows_ahead_with_count() -> io::Result<()> {
fn shows_diverged() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
fs::write(repo_dir.join("Cargo.toml"), " ")?;
Command::new("git")
.args(&["commit", "-am", "Update readme"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
diverge(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -157,19 +128,7 @@ fn shows_diverged() -> io::Result<()> {
fn shows_diverged_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
fs::write(repo_dir.join("Cargo.toml"), " ")?;
Command::new("git")
.args(&["commit", "-am", "Update readme"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
diverge(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
@@ -195,33 +154,7 @@ fn shows_diverged_with_count() -> io::Result<()> {
fn shows_conflicted() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
fs::write(repo_dir.join("readme.md"), "# goodbye")?;
barrier();
Command::new("git")
.args(&["add", "."])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Command::new("git")
.args(&["commit", "-m", "Change readme"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Command::new("git")
.args(&["pull", "--rebase"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
create_conflict(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -235,12 +168,35 @@ fn shows_conflicted() -> io::Result<()> {
Ok(())
}
#[test]
#[ignore]
fn shows_conflicted_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
create_conflict(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
[git_status]
conflicted_count.enabled = true
})
.arg("--path")
.arg(repo_dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = Color::Red.bold().paint(format!("[{}] ", "=1")).to_string();
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
fn shows_untracked_file() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
File::create(repo_dir.join("license"))?.sync_all()?;
create_untracked(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -254,12 +210,35 @@ fn shows_untracked_file() -> io::Result<()> {
Ok(())
}
#[test]
#[ignore]
fn shows_untracked_file_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
create_untracked(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
[git_status]
untracked_count.enabled = true
})
.arg("--path")
.arg(repo_dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = Color::Red.bold().paint(format!("[{}] ", "?1")).to_string();
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
fn doesnt_show_untracked_file_if_disabled() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
File::create(repo_dir.join("license"))?.sync_all()?;
create_untracked(&repo_dir)?;
Command::new("git")
.args(&["config", "status.showUntrackedFiles", "no"])
@@ -318,7 +297,7 @@ fn shows_stashed() -> io::Result<()> {
fn shows_modified() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
File::create(repo_dir.join("readme.md"))?.sync_all()?;
create_modified(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -332,18 +311,35 @@ fn shows_modified() -> io::Result<()> {
Ok(())
}
#[test]
#[ignore]
fn shows_modified_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
create_modified(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
[git_status]
modified_count.enabled = true
})
.arg("--path")
.arg(repo_dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = Color::Red.bold().paint(format!("[{}] ", "!1")).to_string();
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
fn shows_staged_file() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
File::create(repo_dir.join("license"))?.sync_all()?;
Command::new("git")
.args(&["add", "."])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
create_staged(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -357,21 +353,43 @@ fn shows_staged_file() -> io::Result<()> {
Ok(())
}
#[test]
#[ignore]
fn shows_staged_file_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
create_staged(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
[git_status]
staged_count.enabled = true
staged_count.style = "green"
})
.arg("--path")
.arg(repo_dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = format!(
"{}",
ANSIStrings(&[
Color::Red.bold().paint("[+"),
Color::Green.paint("1"),
Color::Red.bold().paint("] "),
])
);
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
fn shows_renamed_file() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
Command::new("git")
.args(&["mv", "readme.md", "readme.md.bak"])
.current_dir(repo_dir.as_path())
.output()?;
Command::new("git")
.args(&["add", "-A"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
create_renamed(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -385,12 +403,35 @@ fn shows_renamed_file() -> io::Result<()> {
Ok(())
}
#[test]
#[ignore]
fn shows_renamed_file_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
create_renamed(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
[git_status]
renamed_count.enabled = true
})
.arg("--path")
.arg(repo_dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = Color::Red.bold().paint(format!("[{}] ", "»1")).to_string();
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
fn shows_deleted_file() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
fs::remove_file(repo_dir.join("readme.md"))?;
create_deleted(&repo_dir)?;
let output = common::render_module("git_status")
.arg("--path")
@@ -404,6 +445,29 @@ fn shows_deleted_file() -> io::Result<()> {
Ok(())
}
#[test]
#[ignore]
fn shows_deleted_file_with_count() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;
create_deleted(&repo_dir)?;
let output = common::render_module("git_status")
.use_config(toml::toml! {
[git_status]
deleted_count.enabled = true
})
.arg("--path")
.arg(repo_dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = Color::Red.bold().paint(format!("[{}] ", "✘1")).to_string();
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
fn prefix() -> io::Result<()> {
@@ -445,3 +509,119 @@ fn suffix() -> io::Result<()> {
assert!(actual.ends_with(&expected));
Ok(())
}
fn ahead(repo_dir: &PathBuf) -> io::Result<()> {
File::create(repo_dir.join("readme.md"))?.sync_all()?;
Command::new("git")
.args(&["commit", "-am", "Update readme"])
.current_dir(&repo_dir)
.output()?;
barrier();
Ok(())
}
fn behind(repo_dir: &PathBuf) -> io::Result<()> {
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Ok(())
}
fn diverge(repo_dir: &PathBuf) -> io::Result<()> {
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
fs::write(repo_dir.join("Cargo.toml"), " ")?;
Command::new("git")
.args(&["commit", "-am", "Update readme"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Ok(())
}
fn create_conflict(repo_dir: &PathBuf) -> io::Result<()> {
Command::new("git")
.args(&["reset", "--hard", "HEAD^"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
fs::write(repo_dir.join("readme.md"), "# goodbye")?;
Command::new("git")
.args(&["add", "."])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Command::new("git")
.args(&["commit", "-m", "Change readme"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Command::new("git")
.args(&["pull", "--rebase"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Ok(())
}
fn create_untracked(repo_dir: &PathBuf) -> io::Result<()> {
File::create(repo_dir.join("license"))?.sync_all()?;
Ok(())
}
fn create_modified(repo_dir: &PathBuf) -> io::Result<()> {
File::create(repo_dir.join("readme.md"))?.sync_all()?;
Ok(())
}
fn create_staged(repo_dir: &PathBuf) -> io::Result<()> {
File::create(repo_dir.join("license"))?.sync_all()?;
Command::new("git")
.args(&["add", "."])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Ok(())
}
fn create_renamed(repo_dir: &PathBuf) -> io::Result<()> {
Command::new("git")
.args(&["mv", "readme.md", "readme.md.bak"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Command::new("git")
.args(&["add", "-A"])
.current_dir(repo_dir.as_path())
.output()?;
barrier();
Ok(())
}
fn create_deleted(repo_dir: &PathBuf) -> io::Result<()> {
fs::remove_file(repo_dir.join("readme.md"))?;
Ok(())
}