Skip to content

Commit 6a8dccc

Browse files
authored
feat(unit tests): respect color flag for tests (#23957)
* feat: disable test color on flag * chore: changelog * feat: generalized formating vrl diagnostics to a single lib function * fix: correct name for multiple diacgnostics * fix: period for the error event log
1 parent 6b807ef commit 6a8dccc

File tree

7 files changed

+56
-31
lines changed

7 files changed

+56
-31
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Disable ANSI color for `vector test` when running non-interactively. Honor `--color {auto|always|never}` and `VECTOR_COLOR`; VRL diagnostics no longer include ANSI sequences when color is disabled.
2+
3+
authors: VanjaRo

src/app.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ impl Application {
206206
opts.root.internal_log_rate_limit,
207207
);
208208

209+
// Set global color preference for downstream modules
210+
crate::set_global_color(color);
211+
209212
// Can only log this after initializing the logging subsystem
210213
if opts.root.openssl_no_probe {
211214
debug!(
@@ -511,7 +514,7 @@ pub fn build_runtime(threads: Option<usize>, thread_name: &str) -> Result<Runtim
511514
.unwrap_or_else(|_| panic!("double thread initialization"));
512515
rt_builder.worker_threads(threads);
513516

514-
debug!(messaged = "Building runtime.", worker_threads = threads);
517+
debug!(message = "Building runtime.", worker_threads = threads);
515518
Ok(rt_builder.build().expect("Unable to create async runtime"))
516519
}
517520

src/common/http/server_auth.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ use vector_lib::{
1717
use vrl::{
1818
compiler::{CompilationResult, CompileConfig, Program, runtime::Runtime},
1919
core::Value,
20-
diagnostic::Formatter,
2120
prelude::TypeState,
2221
value::{KeyString, ObjectMap},
2322
};
2423

24+
use crate::format_vrl_diagnostics;
25+
2526
use super::ErrorMessage;
2627

2728
/// Configuration of the authentication strategy for server mode sinks and sources.
@@ -159,16 +160,15 @@ impl HttpServerAuthConfig {
159160
program,
160161
warnings,
161162
config: _,
162-
} = compile_vrl(source, &functions, &state, config).map_err(|diagnostics| {
163-
Formatter::new(source, diagnostics).colored().to_string()
164-
})?;
163+
} = compile_vrl(source, &functions, &state, config)
164+
.map_err(|diagnostics| format_vrl_diagnostics(source, diagnostics))?;
165165

166166
if !program.final_type_info().result.is_boolean() {
167167
return Err("VRL conditions must return a boolean.".into());
168168
}
169169

170170
if !warnings.is_empty() {
171-
let warnings = Formatter::new(source, warnings).colored().to_string();
171+
let warnings = format_vrl_diagnostics(source, warnings);
172172
warn!(message = "VRL compilation warning.", %warnings);
173173
}
174174

src/conditions/vrl.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use vrl::{
44
CompilationResult, CompileConfig, Program, TypeState, VrlRuntime,
55
runtime::{Runtime, RuntimeResult, Terminate},
66
},
7-
diagnostic::Formatter,
87
value::Value,
98
};
109

1110
use crate::{
1211
conditions::{Condition, Conditional, ConditionalConfig},
1312
config::LogNamespace,
1413
event::{Event, TargetEvents, VrlTarget},
14+
format_vrl_diagnostics,
1515
internal_events::VrlConditionExecutionError,
1616
};
1717

@@ -64,18 +64,15 @@ impl ConditionalConfig for VrlConfig {
6464
program,
6565
warnings,
6666
config: _,
67-
} = compile_vrl(&self.source, &functions, &state, config).map_err(|diagnostics| {
68-
Formatter::new(&self.source, diagnostics)
69-
.colored()
70-
.to_string()
71-
})?;
67+
} = compile_vrl(&self.source, &functions, &state, config)
68+
.map_err(|diagnostics| format_vrl_diagnostics(&self.source, diagnostics))?;
7269

7370
if !program.final_type_info().result.is_boolean() {
7471
return Err("VRL conditions must return a boolean.".into());
7572
}
7673

7774
if !warnings.is_empty() {
78-
let warnings = Formatter::new(&self.source, warnings).colored().to_string();
75+
let warnings = format_vrl_diagnostics(&self.source, warnings);
7976
warn!(message = "VRL compilation warning.", %warnings);
8077
}
8178

@@ -136,25 +133,21 @@ impl Conditional for Vrl {
136133

137134
let value_result = result.map_err(|err| match err {
138135
Terminate::Abort(err) => {
139-
let err = Formatter::new(
136+
let err = format_vrl_diagnostics(
140137
&self.source,
141138
vrl::diagnostic::Diagnostic::from(
142139
Box::new(err) as Box<dyn vrl::diagnostic::DiagnosticMessage>
143140
),
144-
)
145-
.colored()
146-
.to_string();
141+
);
147142
format!("source execution aborted: {err}")
148143
}
149144
Terminate::Error(err) => {
150-
let err = Formatter::new(
145+
let err = format_vrl_diagnostics(
151146
&self.source,
152147
vrl::diagnostic::Diagnostic::from(
153148
Box::new(err) as Box<dyn vrl::diagnostic::DiagnosticMessage>
154149
),
155-
)
156-
.colored()
157-
.to_string();
150+
);
158151
format!("source execution failed: {err}")
159152
}
160153
});

src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub use source_sender::SourceSender;
133133
pub use vector_lib::{Error, Result, event, metrics, schema, shutdown, tcp, tls};
134134

135135
static APP_NAME_SLUG: std::sync::OnceLock<String> = std::sync::OnceLock::new();
136+
static USE_COLOR: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
136137

137138
/// The name used to identify this Vector application.
138139
///
@@ -151,6 +152,33 @@ pub fn get_slugified_app_name() -> String {
151152
.clone()
152153
}
153154

155+
/// Sets the global color preference for diagnostics and CLI output.
156+
/// This should be called once during application startup.
157+
pub fn set_global_color(enabled: bool) {
158+
if let Err(e) = USE_COLOR.set(enabled) {
159+
error!(message = "Failed to set global color.", %e);
160+
}
161+
}
162+
163+
/// Returns true if color output is globally enabled.
164+
/// Defaults to false if not set.
165+
pub fn use_color() -> bool {
166+
*USE_COLOR.get_or_init(|| false)
167+
}
168+
169+
/// Formats VRL diagnostics honoring the global color setting.
170+
pub fn format_vrl_diagnostics(
171+
source: &str,
172+
diagnostics: impl Into<vrl::diagnostic::DiagnosticList>,
173+
) -> String {
174+
let formatter = vrl::diagnostic::Formatter::new(source, diagnostics);
175+
if use_color() {
176+
formatter.colored().to_string()
177+
} else {
178+
formatter.to_string()
179+
}
180+
}
181+
154182
/// The current version of Vector in simplified format.
155183
/// `<version-number>-nightly`.
156184
pub fn vector_version() -> impl std::fmt::Display {

src/sources/http_client/client.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ use vector_lib::{
2323
};
2424
use vrl::{
2525
compiler::{CompileConfig, Function, Program, runtime::Runtime},
26-
diagnostic::Formatter,
2726
prelude::TypeState,
2827
};
2928

3029
use crate::{
3130
codecs::{Decoder, DecodingConfig},
3231
config::{SourceConfig, SourceContext},
32+
format_vrl_diagnostics,
3333
http::{Auth, ParamType, ParameterValue, QueryParameterValue, QueryParameters},
3434
serde::{default_decoding, default_framing_message_based},
3535
sources,
@@ -248,17 +248,14 @@ impl Query {
248248
match compile_vrl(param.value(), functions, &state, config) {
249249
Ok(compilation_result) => {
250250
if !compilation_result.warnings.is_empty() {
251-
let warnings = Formatter::new(param.value(), compilation_result.warnings)
252-
.colored()
253-
.to_string();
251+
let warnings =
252+
format_vrl_diagnostics(param.value(), compilation_result.warnings);
254253
warn!(message = "VRL compilation warnings.", %warnings, internal_log_rate_limit = true);
255254
}
256255
Some(compilation_result.program)
257256
}
258257
Err(diagnostics) => {
259-
let error = Formatter::new(param.value(), diagnostics)
260-
.colored()
261-
.to_string();
258+
let error = format_vrl_diagnostics(param.value(), diagnostics);
262259
warn!(message = "VRL compilation failed.", %error, internal_log_rate_limit = true);
263260
None
264261
}

src/transforms/remap.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use vrl::{
2424
runtime::{Runtime, Terminate},
2525
state::ExternalEnv,
2626
},
27-
diagnostic::{DiagnosticMessage, Formatter, Note},
27+
diagnostic::{DiagnosticMessage, Note},
2828
path,
2929
path::ValuePath,
3030
value::{Kind, Value},
@@ -37,6 +37,7 @@ use crate::{
3737
TransformOutput, log_schema,
3838
},
3939
event::{Event, TargetEvents, VrlTarget},
40+
format_vrl_diagnostics,
4041
internal_events::{RemapMappingAbort, RemapMappingError},
4142
schema,
4243
transforms::{SyncTransform, Transform, TransformOutputsBuf},
@@ -228,11 +229,11 @@ impl RemapConfig {
228229
config.set_custom(MeaningList::default());
229230

230231
let res = compile_vrl(&source, &functions, &state, config)
231-
.map_err(|diagnostics| Formatter::new(&source, diagnostics).colored().to_string())
232+
.map_err(|diagnostics| format_vrl_diagnostics(&source, diagnostics))
232233
.map(|result| {
233234
(
234235
result.program,
235-
Formatter::new(&source, result.warnings).to_string(),
236+
format_vrl_diagnostics(&source, result.warnings),
236237
result.config.get_custom::<MeaningList>().unwrap().clone(),
237238
)
238239
});

0 commit comments

Comments
 (0)