Skip to content
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
16 changes: 12 additions & 4 deletions bin/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
env,
io::{self, Write},
str,
};
Expand Down Expand Up @@ -46,6 +47,7 @@ fn write_stderr<T: Write>(
lint_result: &LintResult,
vfs: &ReadOnlyVfs,
) -> io::Result<()> {
let no_color = env::var("NO_COLOR").is_ok();
Comment thread
mightyiam marked this conversation as resolved.
let file_id = lint_result.file_id;
let src = str::from_utf8(vfs.get(file_id)).unwrap();
let path = vfs.file_path(file_id);
Expand All @@ -70,6 +72,7 @@ fn write_stderr<T: Write>(
CliReport::build(report_kind, src_id, offset)
.with_config(
CliConfig::default()
.with_color(!no_color)
.with_cross_gap(true)
.with_multiline_arrows(false)
.with_label_attach(LabelAttach::Middle)
Expand All @@ -78,11 +81,16 @@ fn write_stderr<T: Write>(
.with_message(report.note)
.with_code(report.code),
|cli_report, diagnostic| {
cli_report.with_label(
Label::new((src_id, range(diagnostic.at)))
let mut label = Label::new((src_id, range(diagnostic.at)));
if no_color {
label =
label.with_message(diagnostic.message.split('`').collect::<String>());
} else {
label = label
.with_message(colorize(&diagnostic.message))
.with_color(Color::Magenta),
)
.with_color(Color::Magenta);
}
Comment thread
mightyiam marked this conversation as resolved.
cli_report.with_label(label)
},
)
.finish()
Expand Down
23 changes: 20 additions & 3 deletions bin/tests/_utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::{io::Write, process::Command};
use std::{collections::HashMap, io::Write, process::Command};

use tempfile::NamedTempFile;

pub fn test_cli(expression: &str, args: &[&str]) -> anyhow::Result<String> {
pub fn test_cli(
expression: &str,
args: &[&str],
envs: HashMap<&str, &str>,
output_processing: OutputProcessing,
) -> anyhow::Result<String>
where
{
let mut fixture = NamedTempFile::with_suffix(".nix")?;
fixture.write_all(expression.as_bytes())?;
fixture.write_all(b"\n")?; // otherwise diff says there's no newline at end of file
Expand All @@ -12,11 +19,21 @@ pub fn test_cli(expression: &str, args: &[&str]) -> anyhow::Result<String> {
.arg("--")
.args(args)
.arg(fixture.path())
.envs(envs)
.output()?;

let stdout = strip_ansi_escapes::strip(output.stdout)?;
let stdout = match output_processing {
OutputProcessing::Unchanged => output.stdout,
OutputProcessing::StripAnsi => strip_ansi_escapes::strip(output.stdout)?,
};
let stdout = String::from_utf8(stdout)?;
let stdout = stdout.replace(fixture.path().to_str().unwrap(), "<temp_file_path>");

Ok(stdout)
}

#[allow(unused)]
pub enum OutputProcessing {
Unchanged,
StripAnsi,
}
36 changes: 36 additions & 0 deletions bin/tests/no_color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod _utils;

use crate::_utils::OutputProcessing;
use std::collections::HashMap;

const EXPR: &'static str = "let in null";

#[test]
fn test_output_with_color_ansi_escape() {
let stdout = _utils::test_cli(
EXPR,
&["check"],
HashMap::new(),
OutputProcessing::Unchanged,
)
.unwrap();
let snapshot = format!("{EXPR}\n---\n{stdout}");
insta::with_settings!({omit_expression => true}, {
insta::assert_snapshot!("output_with_color_ansi_escape", snapshot);
});
}

#[test]
fn test_output_without_color_ansi_escape() {
let stdout = _utils::test_cli(
EXPR,
&["check"],
HashMap::from([("NO_COLOR", "1")]),
OutputProcessing::Unchanged,
)
.unwrap();
let snapshot = format!("{EXPR}\n---\n{stdout}");
insta::with_settings!({omit_expression => true}, {
insta::assert_snapshot!("output_without_color_ansi_escape", snapshot);
});
}
12 changes: 12 additions & 0 deletions bin/tests/snapshots/no_color__output_with_color_ansi_escape.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: bin/tests/no_color.rs
---
let in null
---
[W02] Warning: Useless let-in expression
╭─[<temp_file_path>:1:1]
│
1 │ let in null
 · ─────┬─────
 · ╰─────── This let-in expression has no entries
───╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: bin/tests/no_color.rs
---
let in null
---
[W02] Warning: Useless let-in expression
╭─[<temp_file_path>:1:1]
1 │ let in null
· ─────┬─────
· ╰─────── This let-in expression has no entries
───╯
5 changes: 4 additions & 1 deletion macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ fn make_test(rule: &Ident, kind: TestKind, nix_expression: &Expr) -> proc_macro2
quote! {
#[test]
fn #test_ident() {
use std::collections::HashMap;
use crate::_utils::OutputProcessing;

let expression = #nix_expression;
let stdout = _utils::test_cli(expression, #args).unwrap();
let stdout = _utils::test_cli(expression, #args, HashMap::new(), OutputProcessing::StripAnsi).unwrap();
let snapshot = format!("{expression}\n---\n{stdout}");
insta::with_settings!({omit_expression => true}, {
insta::assert_snapshot!(#snap_name, snapshot);
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ useless_has_attr

All lints are enabled by default. Generate a minimal config
with `statix dump > statix.toml`.

### Color

Reports contain ANSI color escape codes unless the
[`NO_COLOR`](https://no-color.org/) environment variable is present.