Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only emit ANSI escapes when process is terminal #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ echo "Starting build at: ${start} on ${host_name}"
export RUST_BACKTRACE="full"

cargo deny check
cargo +nightly fmt --all -- --check
cargo build --verbose
cargo test --verbose --all-features
cargo clippy --workspace --all-targets --all-features -- --deny warnings
18 changes: 13 additions & 5 deletions src/macros_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::io::IsTerminal as _;
use std::ops::Range;

use codespan_reporting::diagnostic::{Diagnostic, Label};
Expand Down Expand Up @@ -89,13 +90,20 @@ pub fn format_error<'a>(json: &'a Value, error: &Error<'a>) -> String {
)
.with_message(error.to_string())]);

let output = Vec::<u8>::new();
let mut writer = termcolor::Ansi::new(output);
let config = term::Config::default();
let bytes = Vec::<u8>::new();

let bytes = if std::io::stderr().is_terminal() {
let mut writer = termcolor::Ansi::new(bytes);
term::emit(&mut writer, &config, &files, &diagnostic).unwrap();
writer.into_inner()
} else {
let mut writer = termcolor::NoColor::new(bytes);
term::emit(&mut writer, &config, &files, &diagnostic).unwrap();
writer.into_inner()
};

term::emit(&mut writer, &config, &files, &diagnostic).unwrap();

String::from_utf8(writer.into_inner()).unwrap()
String::from_utf8(bytes).unwrap()
}

/// Serialize a JSON [Value] and keeps the span information of each
Expand Down
29 changes: 23 additions & 6 deletions tests/error_msg.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
use std::any::Any;
use std::io::IsTerminal as _;

use assert_json::{assert_json, validators};
use indoc::indoc;

macro_rules! assert_panic_output {
($output:expr, $($assert:tt)+) => {{
($expected_output:expr, $($assert:tt)+) => {{
let out_result = std::panic::catch_unwind(|| $($assert)+);
let out_err = out_result.err().unwrap();
assert!(out_err.is::<String>());
let out = out_err.downcast_ref::<String>().unwrap();
let out = String::from_utf8(strip_ansi_escapes::strip(out.clone().into_bytes())).unwrap();
assert!(out.contains($output.trim()), "\n\texpected:\n{}\n\tgot:\n{}", $output.trim(), out)
let err = out_result_to_string(out_result);
let expected_output = $expected_output.trim();
assert!(err.contains(expected_output), "\n\texpected:\n{expected_output}\n\tgot:\n{err}")
}};
}

#[expect(unsafe_code)]
fn out_result_to_string(result: Result<(), Box<dyn Any + Send>>) -> String {
let err = result.unwrap_err();
let s = err
.downcast::<String>()
.expect("the assert output should be a String");

// ANSI escapes should only be written when `assert_json!` is called from a terminal
if std::io::stderr().is_terminal() {
let bytes = strip_ansi_escapes::strip(s.into_bytes());
unsafe { String::from_utf8_unchecked(bytes) }
} else {
*s
}
}

#[test]
fn primitive_invalid_type() {
let expected_output = indoc! {r"
Expand Down