Skip to content

Commit 663c9c2

Browse files
committed
fix: adapt testing and CI to the newly optional std feature for
env_filter Some parts of the tests are skipped with no_std as they require `snapbox::str!` which does not support no_std.
1 parent 9c70eaf commit 663c9c2

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ jobs:
7272
- uses: Swatinem/rust-cache@v2
7373
- uses: taiki-e/install-action@cargo-hack
7474
- name: Default features
75-
run: cargo hack check --each-feature --locked --rust-version --ignore-private --workspace --all-targets --keep-going
75+
run:
76+
- cargo hack check --each-feature --locked --rust-version --ignore-private --package env_logger --all-targets --keep-going
77+
# When the std feature is enabled, a lower MSRV of 1.71 is sufficient and was the MSRV for the crate before std was made optional.
78+
# To make sure we keep it that way, check configurations with std enabled and disabled separately and with their respective MSRVs.
79+
# "--at-least-one-of" requires "--feature-powerset", which can be made to emulate "--each-feature" with "--depth 1".
80+
- cargo hack check --feature-powerset --depth 1 --locked --version-range 1.71..=1.71 --ignore-private --package env_filter --all-targets --keep-going --at-least-one-of std,default,regex
81+
- cargo hack check --no-default-features --locked --version-range 1.81..=1.81 --ignore-private --package env_filter --all-targets --keep-going
7682
minimal-versions:
7783
name: Minimal versions
7884
strategy:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ unstable-kv = ["kv"]
135135

136136
[dependencies]
137137
log = { version = "0.4.29", features = ["std"] }
138-
env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false }
138+
env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false, features = ["std"] }
139139
jiff = { version = "0.2.22", default-features = false, features = ["std"], optional = true }
140140
anstream = { version = "1.0.0", default-features = false, features = ["wincon"], optional = true }
141141
anstyle = { version = "1.0.13", optional = true }

crates/env_filter/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ keywords = ["logging", "log", "logger"]
99
repository.workspace = true
1010
license.workspace = true
1111
edition.workspace = true
12-
rust-version.workspace = true
1312
include.workspace = true
1413

1514
[package.metadata.docs.rs]

crates/env_filter/src/filter.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use log::{LevelFilter, Metadata, Record};
55

66
use crate::enabled;
77
use crate::parse_spec;
8+
use crate::parser::ParseResult;
89
use crate::Directive;
910
use crate::FilterOp;
1011
use crate::ParseError;
@@ -97,18 +98,20 @@ impl Builder {
9798
/// See the [Enabling Logging] section for more details.
9899
///
99100
/// [Enabling Logging]: ../index.html#enabling-logging
100-
#[cfg(feature = "std")]
101101
pub fn parse(&mut self, filters: &str) -> &mut Self {
102102
#![allow(clippy::print_stderr)] // compatibility
103103

104-
let crate::parser::ParseResult {
104+
let ParseResult {
105105
directives,
106106
filter,
107107
errors,
108108
} = parse_spec(filters);
109109

110110
for error in errors {
111+
#[cfg(feature = "std")]
111112
eprintln!("warning: {error}, ignoring it");
113+
#[cfg(not(feature = "std"))]
114+
log::warn!("{error}, ignoring it");
112115
}
113116

114117
self.filter = filter;
@@ -258,8 +261,9 @@ impl fmt::Debug for Filter {
258261

259262
#[cfg(test)]
260263
mod tests {
264+
use alloc::{borrow::ToOwned, vec, vec::Vec};
265+
261266
use log::{Level, LevelFilter};
262-
use snapbox::{assert_data_eq, str};
263267

264268
use super::{enabled, Builder, Directive, Filter};
265269

@@ -486,10 +490,12 @@ mod tests {
486490

487491
#[test]
488492
fn try_parse_invalid_filter() {
493+
#[allow(unused_variables)]
489494
let error = Builder::new().try_parse("info,crate1=invalid").unwrap_err();
490-
assert_data_eq!(
495+
#[cfg(feature = "std")]
496+
snapbox::assert_data_eq!(
491497
error,
492-
str!["error parsing logger filter: invalid logging spec 'invalid'"]
498+
snapbox::str!["error parsing logger filter: invalid logging spec 'invalid'"]
493499
);
494500
}
495501

crates/env_filter/src/parser.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ pub(crate) fn parse_spec(spec: &str) -> ParseResult {
120120

121121
#[cfg(test)]
122122
mod tests {
123+
use alloc::{borrow::ToOwned, string::ToString};
124+
123125
use crate::ParseError;
124126
use log::LevelFilter;
125-
use snapbox::{assert_data_eq, str, Data, IntoData};
127+
#[cfg(feature = "std")]
128+
use snapbox::{assert_data_eq, str};
129+
use snapbox::{Data, IntoData};
126130

127131
use super::{parse_spec, ParseResult};
128132

@@ -169,6 +173,7 @@ mod tests {
169173
assert!(filter.is_none());
170174

171175
assert_eq!(errors.len(), 1);
176+
#[cfg(feature = "std")]
172177
assert_data_eq!(
173178
&errors[0],
174179
str!["invalid logging spec 'crate1::mod1=warn=info'"]
@@ -190,6 +195,7 @@ mod tests {
190195
assert!(filter.is_none());
191196

192197
assert_eq!(errors.len(), 1);
198+
#[cfg(feature = "std")]
193199
assert_data_eq!(&errors[0], str!["invalid logging spec 'noNumber'"]);
194200
}
195201

@@ -208,6 +214,7 @@ mod tests {
208214
assert!(filter.is_none());
209215

210216
assert_eq!(errors.len(), 1);
217+
#[cfg(feature = "std")]
211218
assert_data_eq!(&errors[0], str!["invalid logging spec 'wrong'"]);
212219
}
213220

@@ -226,6 +233,7 @@ mod tests {
226233
assert!(filter.is_none());
227234

228235
assert_eq!(errors.len(), 1);
236+
#[cfg(feature = "std")]
229237
assert_data_eq!(&errors[0], str!["invalid logging spec 'wrong'"]);
230238
}
231239

@@ -399,6 +407,7 @@ mod tests {
399407
assert!(filter.is_some() && filter.unwrap().to_string() == "a.c");
400408

401409
assert_eq!(errors.len(), 1);
410+
#[cfg(feature = "std")]
402411
assert_data_eq!(
403412
&errors[0],
404413
str!["invalid logging spec 'crate1::mod1=error=warn'"]
@@ -430,6 +439,7 @@ mod tests {
430439
assert!(filter.is_none());
431440

432441
assert_eq!(errors.len(), 1);
442+
#[cfg(feature = "std")]
433443
assert_data_eq!(
434444
&errors[0],
435445
str!["invalid logging spec 'debug/abc/a.c' (too many '/'s)"]
@@ -451,14 +461,17 @@ mod tests {
451461
assert!(filter.is_none());
452462

453463
assert_eq!(errors.len(), 2);
454-
assert_data_eq!(
455-
&errors[0],
456-
str!["invalid logging spec 'crate1::mod1=warn=info'"]
457-
);
458-
assert_data_eq!(
459-
&errors[1],
460-
str!["invalid logging spec 'crate3=error=error'"]
461-
);
464+
#[cfg(feature = "std")]
465+
{
466+
assert_data_eq!(
467+
&errors[0],
468+
str!["invalid logging spec 'crate1::mod1=warn=info'"]
469+
);
470+
assert_data_eq!(
471+
&errors[1],
472+
str!["invalid logging spec 'crate3=error=error'"]
473+
);
474+
}
462475
}
463476

464477
#[test]
@@ -476,8 +489,11 @@ mod tests {
476489
assert!(filter.is_none());
477490

478491
assert_eq!(errors.len(), 2);
479-
assert_data_eq!(&errors[0], str!["invalid logging spec 'noNumber'"]);
480-
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
492+
#[cfg(feature = "std")]
493+
{
494+
assert_data_eq!(&errors[0], str!["invalid logging spec 'noNumber'"]);
495+
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
496+
}
481497
}
482498

483499
#[test]
@@ -495,18 +511,23 @@ mod tests {
495511
assert!(filter.is_none());
496512

497513
assert_eq!(errors.len(), 2);
498-
assert_data_eq!(
499-
&errors[0],
500-
str!["invalid logging spec 'crate1::mod1=debug=info'"]
501-
);
502-
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
514+
#[cfg(feature = "std")]
515+
{
516+
assert_data_eq!(
517+
&errors[0],
518+
str!["invalid logging spec 'crate1::mod1=debug=info'"]
519+
);
520+
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
521+
}
503522
}
504523

505524
#[test]
506525
fn parse_error_message_single_error() {
526+
#[allow(unused_variables)]
507527
let error = parse_spec("crate1::mod1=debug=info,crate2=debug")
508528
.ok()
509529
.unwrap_err();
530+
#[cfg(feature = "std")]
510531
assert_data_eq!(
511532
error,
512533
str!["error parsing logger filter: invalid logging spec 'crate1::mod1=debug=info'"]
@@ -515,9 +536,11 @@ mod tests {
515536

516537
#[test]
517538
fn parse_error_message_multiple_errors() {
539+
#[allow(unused_variables)]
518540
let error = parse_spec("crate1::mod1=debug=info,crate2=debug,crate3=invalid")
519541
.ok()
520542
.unwrap_err();
543+
#[cfg(feature = "std")]
521544
assert_data_eq!(
522545
error,
523546
str!["error parsing logger filter: invalid logging spec 'crate1::mod1=debug=info'"]

0 commit comments

Comments
 (0)