Add NO_COLOR support and CLI testing.

This commit is contained in:
Bauke 2022-10-31 15:20:38 +01:00
parent 16757cabce
commit 0743a982d1
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
7 changed files with 184 additions and 3 deletions

111
Cargo.lock generated
View File

@ -35,6 +35,20 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "assert_cmd"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5c2ca00549910ec251e3bd15f87aeeb206c9456b9a77b43ff6c97c54042a472"
dependencies = [
"bstr",
"doc-comment",
"predicates",
"predicates-core",
"predicates-tree",
"wait-timeout",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -88,7 +102,9 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
dependencies = [ dependencies = [
"lazy_static",
"memchr", "memchr",
"regex-automata",
] ]
[[package]] [[package]]
@ -314,6 +330,12 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
[[package]]
name = "difflib"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.5" version = "0.10.5"
@ -324,6 +346,18 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]] [[package]]
name = "encode_unicode" name = "encode_unicode"
version = "0.3.6" version = "0.3.6"
@ -416,12 +450,16 @@ dependencies = [
name = "hooked-cli" name = "hooked-cli"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"assert_cmd",
"clap", "clap",
"color-eyre", "color-eyre",
"hooked-library", "hooked-library",
"insta",
"owo-colors", "owo-colors",
"subprocess", "subprocess",
"supports-color",
"tera", "tera",
"test-case",
] ]
[[package]] [[package]]
@ -502,6 +540,21 @@ dependencies = [
"yaml-rust", "yaml-rust",
] ]
[[package]]
name = "is_ci"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.4" version = "1.0.4"
@ -724,6 +777,33 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "predicates"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5aab5be6e4732b473071984b3164dbbfb7a3674d30ea5ff44410b6bcd960c3c"
dependencies = [
"difflib",
"itertools",
"predicates-core",
]
[[package]]
name = "predicates-core"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb"
[[package]]
name = "predicates-tree"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032"
dependencies = [
"predicates-core",
"termtree",
]
[[package]] [[package]]
name = "proc-macro-error" name = "proc-macro-error"
version = "1.0.4" version = "1.0.4"
@ -807,6 +887,12 @@ dependencies = [
"regex-syntax", "regex-syntax",
] ]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.27" version = "0.6.27"
@ -928,6 +1014,16 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "supports-color"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4872ced36b91d47bae8a214a683fe54e7078875b399dfa251df346c9b547d1f9"
dependencies = [
"atty",
"is_ci",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.103" version = "1.0.103"
@ -980,6 +1076,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "termtree"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"
[[package]] [[package]]
name = "test-case" name = "test-case"
version = "2.2.2" version = "2.2.2"
@ -1177,6 +1279,15 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wait-timeout"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.3.2" version = "2.3.2"

View File

@ -15,6 +15,7 @@ path = "source/main.rs"
color-eyre = "0.6.2" color-eyre = "0.6.2"
owo-colors = "3.5.0" owo-colors = "3.5.0"
subprocess = "0.2.9" subprocess = "0.2.9"
supports-color = "1.3.0"
tera = "1.17.1" tera = "1.17.1"
[dependencies.clap] [dependencies.clap]
@ -24,3 +25,8 @@ version = "4.0.18"
[dependencies.hooked-library] [dependencies.hooked-library]
path = "../hooked-library" path = "../hooked-library"
version = "0.1.0" version = "0.1.0"
[dev-dependencies]
assert_cmd = "2.0.5"
insta = "1.21.0"
test-case = "2.2.2"

View File

@ -1,3 +1,5 @@
//! The `run` subcommand.
use std::{io::Read, process::exit}; use std::{io::Read, process::exit};
use { use {
@ -5,12 +7,22 @@ use {
hooked_library::{Config, ExitAction}, hooked_library::{Config, ExitAction},
owo_colors::{OwoColorize, Style}, owo_colors::{OwoColorize, Style},
subprocess::{Exec, Redirection}, subprocess::{Exec, Redirection},
supports_color::Stream,
}; };
/// The `run` subcommand.
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> { pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
let success_style = Style::new().bold().green(); let (success_style, warn_style, error_style) =
let warn_style = Style::new().bold().yellow(); if let Some(_support) = supports_color::on(Stream::Stdout) {
let error_style = Style::new().bold().red(); let shared_style = Style::new().bold();
(
shared_style.green(),
shared_style.yellow(),
shared_style.red(),
)
} else {
(Style::new(), Style::new(), Style::new())
};
if hook_type == "pre-commit" { if hook_type == "pre-commit" {
println!( println!(

21
hooked-cli/tests/run.rs Normal file
View File

@ -0,0 +1,21 @@
use {assert_cmd::Command, insta::assert_snapshot, test_case::test_case};
#[test_case("pre-commit", "pre-commit.toml", true; "pre-commit")]
fn test_hooked_run(hook_type: &str, config: &str, expect_failure: bool) {
let config = format!("tests/samples/{}", config);
let mut command = Command::cargo_bin("hooked").unwrap();
command
.args(["run", hook_type, "--config", &config])
.env("NO_COLOR", "1");
let assert = if expect_failure {
command.assert().failure().code(1)
} else {
command.assert().success()
};
let stderr = std::str::from_utf8(&assert.get_output().stderr).unwrap();
let stdout = std::str::from_utf8(&assert.get_output().stdout).unwrap();
assert_snapshot!(hook_type, format!("{}{}", stdout, stderr));
}

View File

@ -0,0 +1,14 @@
[general]
directory = "tests/samples"
[[pre_commit]]
script = "simple-echo.bash"
[[pre_commit]]
name = "Exit 1 Continue"
command = "echo 'on_failure=continue' && exit 1"
on_failure = "continue"
[[pre_commit]]
name = "Exit 1 Stop"
command = "echo 'Hey from stdout!' && echo 'Hey from stderr!' 1>&2 && exit 1"

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "Hey, $USER!"

View File

@ -0,0 +1,14 @@
---
source: hooked-cli/tests/run.rs
expression: "format!(\"{}{}\", stdout, stderr)"
---
Hooked: Running 3 pre-commit hooks.
✓ Unnamed Hook
⚠ Exit 1 Continue
on_failure=continue
✗ Exit 1 Stop
Hey from stdout!
Hey from stderr!