cli: make crossterm not respect NO_COLOR

The `NO_COLOR` spec says that user-specified config is supposed to
override the `$NO_COLOR` environment variable, and we do correctly use
the `ColorFormatter` when `ui.color= "always"` is set in the user's
config. However, it turns out that `NO_COLOR=1` still resulted in no
color because `crossterm` also respects the variable, so the color
codes the `ColorFormatter` requested had no effect. Since `crossterm`
doesn't know about our user configs, it cannot decide whether to
respect `$NO_COLOR`, so let's tell `crossterm` to always use the
colors we tell it to use.
This commit is contained in:
Martin von Zweigbergk 2024-12-06 15:58:06 -08:00 committed by Martin von Zweigbergk
parent 23f9cc7a4f
commit 66723276fc
2 changed files with 4 additions and 3 deletions

View file

@ -3627,6 +3627,8 @@ impl CliRunner {
.fold(builder, |builder, config| builder.add_source(config))
.build()
.unwrap();
// Tell crossterm to ignore NO_COLOR (we check it ourselves)
crossterm::style::force_color_output(true);
let stacked_config = config_from_environment(config);
let mut ui = Ui::with_config(&stacked_config)
.expect("default config should be valid, env vars are stringly typed");

View file

@ -439,10 +439,9 @@ fn test_color_config() {
// Test that NO_COLOR does NOT override the request for color in the config file
test_env.add_env_var("NO_COLOR", "1");
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
// TODO: Should have color
insta::assert_snapshot!(stdout, @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
 0000000000000000000000000000000000000000
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
 0000000000000000000000000000000000000000
"###);
// Test that per-repo config overrides the user config.