Honor the NO_COLOR environment variable.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded

This commit is contained in:
Tom Alexander 2023-08-25 04:25:39 -04:00
parent 5134cece7b
commit 45e16fea2d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 43 additions and 15 deletions

View File

@ -7,6 +7,8 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
: ${SHELL:="NO"} # or YES to launch a shell instead of running the test : ${SHELL:="NO"} # or YES to launch a shell instead of running the test
: ${TRACE:="NO"} # or YES to send traces to jaeger : ${TRACE:="NO"} # or YES to send traces to jaeger
: ${BACKTRACE:="NO"} # or YES to print a rust backtrace when panicking : ${BACKTRACE:="NO"} # or YES to print a rust backtrace when panicking
: ${NO_COLOR:=""} # Set to anything to disable color output
cd "$DIR/../" cd "$DIR/../"
REALPATH=$(command -v uu-realpath || command -v realpath) REALPATH=$(command -v uu-realpath || command -v realpath)
@ -25,6 +27,10 @@ function launch_container {
local additional_flags=() local additional_flags=()
local additional_args=() local additional_args=()
if [ "$NO_COLOR" != "" ]; then
additional_flags+=(--env "NO_COLOR=$NO_COLOR")
fi
if [ "$SHELL" != "YES" ]; then if [ "$SHELL" != "YES" ]; then
additional_args+=(cargo run) additional_args+=(cargo run)
else else

View File

@ -4,6 +4,8 @@ set -euo pipefail
IFS=$'\n\t' IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
: ${NO_COLOR:=""} # Set to anything to disable color output
cd "$DIR/../" cd "$DIR/../"
REALPATH=$(command -v uu-realpath || command -v realpath) REALPATH=$(command -v uu-realpath || command -v realpath)
MAKE=$(command -v gmake || command -v make) MAKE=$(command -v gmake || command -v make)
@ -40,7 +42,11 @@ function get_test_names {
function launch_container { function launch_container {
local test="$1" local test="$1"
local additional_args=() local additional_flags=()
if [ "$NO_COLOR" != "" ]; then
additional_flags+=(--env "NO_COLOR=$NO_COLOR")
fi
local init_script=$(cat <<EOF local init_script=$(cat <<EOF
set -euo pipefail set -euo pipefail
@ -50,7 +56,7 @@ cargo test --no-fail-fast --lib --test test_loader "$test" -- --show-output
EOF EOF
) )
docker run --init --rm -v "$($REALPATH ./):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source organic-test sh -c "$init_script" docker run "${additional_flags[@]}" --init --rm -v "$($REALPATH ./):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source organic-test sh -c "$init_script"
} }

View File

@ -131,26 +131,42 @@ impl DiffResult {
} }
fn foreground_color(red: u8, green: u8, blue: u8) -> String { fn foreground_color(red: u8, green: u8, blue: u8) -> String {
format!( if DiffResult::should_use_color() {
"\x1b[38;2;{red};{green};{blue}m", format!(
red = red, "\x1b[38;2;{red};{green};{blue}m",
green = green, red = red,
blue = blue green = green,
) blue = blue
)
} else {
String::new()
}
} }
#[allow(dead_code)] #[allow(dead_code)]
fn background_color(red: u8, green: u8, blue: u8) -> String { fn background_color(red: u8, green: u8, blue: u8) -> String {
format!( if DiffResult::should_use_color() {
"\x1b[48;2;{red};{green};{blue}m", format!(
red = red, "\x1b[48;2;{red};{green};{blue}m",
green = green, red = red,
blue = blue green = green,
) blue = blue
)
} else {
String::new()
}
} }
fn reset_color() -> &'static str { fn reset_color() -> &'static str {
"\x1b[0m" if DiffResult::should_use_color() {
"\x1b[0m"
} else {
""
}
}
fn should_use_color() -> bool {
!std::env::var("NO_COLOR").is_ok_and(|val| !val.is_empty())
} }
} }