Skip to content

Commit e89dd71

Browse files
committed
[no-color] - add unit test and doc
1 parent 43d27c1 commit e89dd71

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

man/exa.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ Specifies the number of spaces to print between an icon (see the ‘`--icons`’
224224

225225
Different terminals display icons differently, as they usually take up more than one character width on screen, so there’s no “standard” number of spaces that exa can use to separate an icon from text. One space may place the icon too close to the text, and two spaces may place it too far away. So the choice is left up to the user to configure depending on their terminal emulator.
226226

227+
## `NO_COLOR`
228+
229+
Disables ANSI colour in the output (regardless of its value). Can be overridden by `--color` option.
230+
231+
See `https://no-color.org/` for details.
232+
227233
## `LS_COLORS`, `EXA_COLORS`
228234

229235
Specifies the colour scheme used to highlight files based on their name and kind, as well as highlighting metadata and parts of the UI.

src/options/theme.rs

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ mod terminal_test {
9292
}
9393
};
9494

95+
($name:ident: $type:ident <- $inputs:expr, $env:expr; $stricts:expr => $result:expr) => {
96+
#[test]
97+
fn $name() {
98+
let env = $env;
99+
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, &env)) {
100+
assert_eq!(result, $result);
101+
}
102+
}
103+
};
104+
95105
($name:ident: $type:ident <- $inputs:expr; $stricts:expr => err $result:expr) => {
96106
#[test]
97107
fn $name() {
@@ -100,11 +110,39 @@ mod terminal_test {
100110
}
101111
}
102112
};
113+
114+
($name:ident: $type:ident <- $inputs:expr, $env:expr; $stricts:expr => err $result:expr) => {
115+
#[test]
116+
fn $name() {
117+
let env = $env;
118+
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, &env)) {
119+
assert_eq!(result.unwrap_err(), $result);
120+
}
121+
}
122+
};
103123
}
104124

105125
struct MockVars {
106126
ls: &'static str,
107127
exa: &'static str,
128+
no_color: &'static str,
129+
}
130+
131+
impl MockVars {
132+
fn empty() -> MockVars {
133+
return MockVars {
134+
ls: "",
135+
exa: "",
136+
no_color: "",
137+
};
138+
}
139+
fn with_no_color() -> MockVars {
140+
return MockVars {
141+
ls: "",
142+
exa: "",
143+
no_color: "true",
144+
};
145+
}
108146
}
109147

110148
// Test impl that just returns the value it has.
@@ -116,6 +154,9 @@ mod terminal_test {
116154
else if name == vars::EXA_COLORS && ! self.exa.is_empty() {
117155
Some(OsString::from(self.exa.clone()))
118156
}
157+
else if name == vars::NO_COLOR && ! self.no_color.is_empty() {
158+
Some(OsString::from(self.no_color.clone()))
159+
}
119160
else {
120161
None
121162
}
@@ -125,32 +166,33 @@ mod terminal_test {
125166

126167

127168
// Default
128-
test!(empty: UseColours <- []; Both => Ok(UseColours::Automatic));
169+
test!(empty: UseColours <- [], MockVars::empty(); Both => Ok(UseColours::Automatic));
170+
test!(empty_with_no_color: UseColours <- [], MockVars::with_no_color(); Both => Ok(UseColours::Never));
129171

130172
// --colour
131-
test!(u_always: UseColours <- ["--colour=always"]; Both => Ok(UseColours::Always));
132-
test!(u_auto: UseColours <- ["--colour", "auto"]; Both => Ok(UseColours::Automatic));
133-
test!(u_never: UseColours <- ["--colour=never"]; Both => Ok(UseColours::Never));
173+
test!(u_always: UseColours <- ["--colour=always"], MockVars::empty(); Both => Ok(UseColours::Always));
174+
test!(u_auto: UseColours <- ["--colour", "auto"], MockVars::empty(); Both => Ok(UseColours::Automatic));
175+
test!(u_never: UseColours <- ["--colour=never"], MockVars::empty(); Both => Ok(UseColours::Never));
134176

135177
// --color
136-
test!(no_u_always: UseColours <- ["--color", "always"]; Both => Ok(UseColours::Always));
137-
test!(no_u_auto: UseColours <- ["--color=auto"]; Both => Ok(UseColours::Automatic));
138-
test!(no_u_never: UseColours <- ["--color", "never"]; Both => Ok(UseColours::Never));
178+
test!(no_u_always: UseColours <- ["--color", "always"], MockVars::empty(); Both => Ok(UseColours::Always));
179+
test!(no_u_auto: UseColours <- ["--color=auto"], MockVars::empty(); Both => Ok(UseColours::Automatic));
180+
test!(no_u_never: UseColours <- ["--color", "never"], MockVars::empty(); Both => Ok(UseColours::Never));
139181

140182
// Errors
141-
test!(no_u_error: UseColours <- ["--color=upstream"]; Both => err OptionsError::BadArgument(&flags::COLOR, OsString::from("upstream"))); // the error is for --color
142-
test!(u_error: UseColours <- ["--colour=lovers"]; Both => err OptionsError::BadArgument(&flags::COLOR, OsString::from("lovers"))); // and so is this one!
183+
test!(no_u_error: UseColours <- ["--color=upstream"], MockVars::empty(); Both => err OptionsError::BadArgument(&flags::COLOR, OsString::from("upstream"))); // the error is for --color
184+
test!(u_error: UseColours <- ["--colour=lovers"], MockVars::empty(); Both => err OptionsError::BadArgument(&flags::COLOR, OsString::from("lovers"))); // and so is this one!
143185

144186
// Overriding
145-
test!(overridden_1: UseColours <- ["--colour=auto", "--colour=never"]; Last => Ok(UseColours::Never));
146-
test!(overridden_2: UseColours <- ["--color=auto", "--colour=never"]; Last => Ok(UseColours::Never));
147-
test!(overridden_3: UseColours <- ["--colour=auto", "--color=never"]; Last => Ok(UseColours::Never));
148-
test!(overridden_4: UseColours <- ["--color=auto", "--color=never"]; Last => Ok(UseColours::Never));
149-
150-
test!(overridden_5: UseColours <- ["--colour=auto", "--colour=never"]; Complain => err OptionsError::Duplicate(Flag::Long("colour"), Flag::Long("colour")));
151-
test!(overridden_6: UseColours <- ["--color=auto", "--colour=never"]; Complain => err OptionsError::Duplicate(Flag::Long("color"), Flag::Long("colour")));
152-
test!(overridden_7: UseColours <- ["--colour=auto", "--color=never"]; Complain => err OptionsError::Duplicate(Flag::Long("colour"), Flag::Long("color")));
153-
test!(overridden_8: UseColours <- ["--color=auto", "--color=never"]; Complain => err OptionsError::Duplicate(Flag::Long("color"), Flag::Long("color")));
187+
test!(overridden_1: UseColours <- ["--colour=auto", "--colour=never"], MockVars::empty(); Last => Ok(UseColours::Never));
188+
test!(overridden_2: UseColours <- ["--color=auto", "--colour=never"], MockVars::empty(); Last => Ok(UseColours::Never));
189+
test!(overridden_3: UseColours <- ["--colour=auto", "--color=never"], MockVars::empty(); Last => Ok(UseColours::Never));
190+
test!(overridden_4: UseColours <- ["--color=auto", "--color=never"], MockVars::empty(); Last => Ok(UseColours::Never));
191+
192+
test!(overridden_5: UseColours <- ["--colour=auto", "--colour=never"], MockVars::empty(); Complain => err OptionsError::Duplicate(Flag::Long("colour"), Flag::Long("colour")));
193+
test!(overridden_6: UseColours <- ["--color=auto", "--colour=never"], MockVars::empty(); Complain => err OptionsError::Duplicate(Flag::Long("color"), Flag::Long("colour")));
194+
test!(overridden_7: UseColours <- ["--colour=auto", "--color=never"], MockVars::empty(); Complain => err OptionsError::Duplicate(Flag::Long("colour"), Flag::Long("color")));
195+
test!(overridden_8: UseColours <- ["--color=auto", "--color=never"], MockVars::empty(); Complain => err OptionsError::Duplicate(Flag::Long("color"), Flag::Long("color")));
154196

155197
test!(scale_1: ColourScale <- ["--color-scale", "--colour-scale"]; Last => Ok(ColourScale::Gradient));
156198
test!(scale_2: ColourScale <- ["--color-scale", ]; Last => Ok(ColourScale::Gradient));

0 commit comments

Comments
 (0)