From 0743a982d1ad8c2e15fc6c3e70b82654f5fdbdee Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 31 Oct 2022 15:20:38 +0100 Subject: [PATCH] Add NO_COLOR support and CLI testing. --- Cargo.lock | 111 ++++++++++++++++++ hooked-cli/Cargo.toml | 6 + hooked-cli/source/cli/run.rs | 18 ++- hooked-cli/tests/run.rs | 21 ++++ hooked-cli/tests/samples/pre-commit.toml | 14 +++ hooked-cli/tests/samples/simple-echo.bash | 3 + .../tests/snapshots/run__pre-commit.snap | 14 +++ 7 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 hooked-cli/tests/run.rs create mode 100644 hooked-cli/tests/samples/pre-commit.toml create mode 100755 hooked-cli/tests/samples/simple-echo.bash create mode 100644 hooked-cli/tests/snapshots/run__pre-commit.snap diff --git a/Cargo.lock b/Cargo.lock index 13993d8..4e43ad3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,6 +35,20 @@ dependencies = [ "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]] name = "atty" version = "0.2.14" @@ -88,7 +102,9 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" dependencies = [ + "lazy_static", "memchr", + "regex-automata", ] [[package]] @@ -314,6 +330,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "digest" version = "0.10.5" @@ -324,6 +346,18 @@ dependencies = [ "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]] name = "encode_unicode" version = "0.3.6" @@ -416,12 +450,16 @@ dependencies = [ name = "hooked-cli" version = "0.1.0" dependencies = [ + "assert_cmd", "clap", "color-eyre", "hooked-library", + "insta", "owo-colors", "subprocess", + "supports-color", "tera", + "test-case", ] [[package]] @@ -502,6 +540,21 @@ dependencies = [ "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]] name = "itoa" version = "1.0.4" @@ -724,6 +777,33 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "proc-macro-error" version = "1.0.4" @@ -807,6 +887,12 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-syntax" version = "0.6.27" @@ -928,6 +1014,16 @@ dependencies = [ "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]] name = "syn" version = "1.0.103" @@ -980,6 +1076,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "termtree" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b" + [[package]] name = "test-case" version = "2.2.2" @@ -1177,6 +1279,15 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "walkdir" version = "2.3.2" diff --git a/hooked-cli/Cargo.toml b/hooked-cli/Cargo.toml index 076bf6c..393fa10 100644 --- a/hooked-cli/Cargo.toml +++ b/hooked-cli/Cargo.toml @@ -15,6 +15,7 @@ path = "source/main.rs" color-eyre = "0.6.2" owo-colors = "3.5.0" subprocess = "0.2.9" +supports-color = "1.3.0" tera = "1.17.1" [dependencies.clap] @@ -24,3 +25,8 @@ version = "4.0.18" [dependencies.hooked-library] path = "../hooked-library" version = "0.1.0" + +[dev-dependencies] +assert_cmd = "2.0.5" +insta = "1.21.0" +test-case = "2.2.2" diff --git a/hooked-cli/source/cli/run.rs b/hooked-cli/source/cli/run.rs index 2d9bdb1..7b7c28c 100644 --- a/hooked-cli/source/cli/run.rs +++ b/hooked-cli/source/cli/run.rs @@ -1,3 +1,5 @@ +//! The `run` subcommand. + use std::{io::Read, process::exit}; use { @@ -5,12 +7,22 @@ use { hooked_library::{Config, ExitAction}, owo_colors::{OwoColorize, Style}, subprocess::{Exec, Redirection}, + supports_color::Stream, }; +/// The `run` subcommand. pub fn hooked_run(config: Config, hook_type: String) -> Result<()> { - let success_style = Style::new().bold().green(); - let warn_style = Style::new().bold().yellow(); - let error_style = Style::new().bold().red(); + let (success_style, warn_style, error_style) = + if let Some(_support) = supports_color::on(Stream::Stdout) { + 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" { println!( diff --git a/hooked-cli/tests/run.rs b/hooked-cli/tests/run.rs new file mode 100644 index 0000000..d1ca977 --- /dev/null +++ b/hooked-cli/tests/run.rs @@ -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)); +} diff --git a/hooked-cli/tests/samples/pre-commit.toml b/hooked-cli/tests/samples/pre-commit.toml new file mode 100644 index 0000000..436fdc8 --- /dev/null +++ b/hooked-cli/tests/samples/pre-commit.toml @@ -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" diff --git a/hooked-cli/tests/samples/simple-echo.bash b/hooked-cli/tests/samples/simple-echo.bash new file mode 100755 index 0000000..86835aa --- /dev/null +++ b/hooked-cli/tests/samples/simple-echo.bash @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo "Hey, $USER!" diff --git a/hooked-cli/tests/snapshots/run__pre-commit.snap b/hooked-cli/tests/snapshots/run__pre-commit.snap new file mode 100644 index 0000000..bb58415 --- /dev/null +++ b/hooked-cli/tests/snapshots/run__pre-commit.snap @@ -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! + +