Skip to content

Commit 25e2b53

Browse files
committed
Move filters to comment defaults
1 parent ef834dd commit 25e2b53

File tree

5 files changed

+48
-81
lines changed

5 files changed

+48
-81
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
* `$DIR` and `RUSTLIB` replacements
2424
* `Config::edition` (replaced by `config.comment_defaults.base().edition`)
25+
* `Config::filter_stdout` (replaced by `config.comment_defaults.base().normalize_stdout`)
26+
* `Config::filter_stderr` (replaced by `config.comment_defaults.base().normalize_stderr`)
2527

2628
## [0.21.2] - 2023-09-27

src/config.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use regex::bytes::Regex;
22
use spanned::Span;
33

44
use crate::{
5-
dependencies::build_dependencies, per_test_config::Comments, CommandBuilder, Filter, Match,
6-
Mode, RustfixMode,
5+
dependencies::build_dependencies, per_test_config::Comments, CommandBuilder, Match, Mode,
6+
RustfixMode,
77
};
88
pub use color_eyre;
99
use color_eyre::eyre::Result;
@@ -23,14 +23,6 @@ pub struct Config {
2323
pub host: Option<String>,
2424
/// `None` to run on the host, otherwise a target triple
2525
pub target: Option<String>,
26-
/// Filters applied to stderr output before processing it.
27-
/// By default contains a filter for replacing backslashes in paths with
28-
/// regular slashes.
29-
/// On windows, contains a filter to remove `\r`.
30-
pub stderr_filters: Filter,
31-
/// Filters applied to stdout output before processing it.
32-
/// On windows, contains a filter to remove `\r`.
33-
pub stdout_filters: Filter,
3426
/// The folder in which to start searching for .rs files
3527
pub root_dir: PathBuf,
3628
/// The mode in which to run the tests.
@@ -74,23 +66,18 @@ impl Config {
7466
.base()
7567
.edition
7668
.set("2021".into(), Span::default());
69+
let filters = vec![
70+
(Match::PathBackslash, b"/".to_vec()),
71+
#[cfg(windows)]
72+
(Match::Exact(vec![b'\r']), b"".to_vec()),
73+
#[cfg(windows)]
74+
(Match::Exact(br"\\?\".to_vec()), b"".to_vec()),
75+
];
76+
let _ = comment_defaults.base().normalize_stderr = filters.clone();
77+
let _ = comment_defaults.base().normalize_stdout = filters;
7778
Self {
7879
host: None,
7980
target: None,
80-
stderr_filters: vec![
81-
(Match::PathBackslash, b"/"),
82-
#[cfg(windows)]
83-
(Match::Exact(vec![b'\r']), b""),
84-
#[cfg(windows)]
85-
(Match::Exact(br"\\?\".to_vec()), b""),
86-
],
87-
stdout_filters: vec![
88-
(Match::PathBackslash, b"/"),
89-
#[cfg(windows)]
90-
(Match::Exact(vec![b'\r']), b""),
91-
#[cfg(windows)]
92-
(Match::Exact(br"\\?\".to_vec()), b""),
93-
],
9481
root_dir: root_dir.into(),
9582
mode: Mode::Fail {
9683
require_patterns: true,
@@ -118,15 +105,16 @@ impl Config {
118105
/// Create a configuration for testing the output of running
119106
/// `cargo` on the test `Cargo.toml` files.
120107
pub fn cargo(root_dir: impl Into<PathBuf>) -> Self {
121-
Self {
108+
let mut this = Self {
122109
program: CommandBuilder::cargo(),
123-
comment_defaults: Comments::default(),
124110
mode: Mode::Fail {
125111
require_patterns: true,
126112
rustfix: RustfixMode::Disabled,
127113
},
128114
..Self::rustc(root_dir)
129-
}
115+
};
116+
this.comment_defaults.base().edition = Default::default();
117+
this
130118
}
131119

132120
/// Populate the config with the values from parsed command line arguments.
@@ -189,8 +177,10 @@ impl Config {
189177
replacement: &'static (impl AsRef<[u8]> + ?Sized),
190178
) {
191179
let pattern = path.canonicalize().unwrap();
192-
self.stderr_filters
193-
.push((pattern.parent().unwrap().into(), replacement.as_ref()));
180+
self.comment_defaults.base().normalize_stderr.push((
181+
pattern.parent().unwrap().into(),
182+
replacement.as_ref().to_owned(),
183+
));
194184
}
195185

196186
/// Replace all occurrences of a path in stdout with a byte string.
@@ -201,8 +191,10 @@ impl Config {
201191
replacement: &'static (impl AsRef<[u8]> + ?Sized),
202192
) {
203193
let pattern = path.canonicalize().unwrap();
204-
self.stdout_filters
205-
.push((pattern.parent().unwrap().into(), replacement.as_ref()));
194+
self.comment_defaults.base().normalize_stdout.push((
195+
pattern.parent().unwrap().into(),
196+
replacement.as_ref().to_owned(),
197+
));
206198
}
207199

208200
/// Replace all occurrences of a regex pattern in stderr/stdout with a byte string.
@@ -219,8 +211,10 @@ impl Config {
219211
pattern: &str,
220212
replacement: &'static (impl AsRef<[u8]> + ?Sized),
221213
) {
222-
self.stderr_filters
223-
.push((Regex::new(pattern).unwrap().into(), replacement.as_ref()));
214+
self.comment_defaults.base().normalize_stderr.push((
215+
Regex::new(pattern).unwrap().into(),
216+
replacement.as_ref().to_owned(),
217+
));
224218
}
225219

226220
/// Replace all occurrences of a regex pattern in stdout with a byte string.
@@ -230,8 +224,10 @@ impl Config {
230224
pattern: &str,
231225
replacement: &'static (impl AsRef<[u8]> + ?Sized),
232226
) {
233-
self.stdout_filters
234-
.push((Regex::new(pattern).unwrap().into(), replacement.as_ref()));
227+
self.comment_defaults.base().normalize_stdout.push((
228+
Regex::new(pattern).unwrap().into(),
229+
replacement.as_ref().to_owned(),
230+
));
235231
}
236232

237233
/// Compile dependencies and return the right flags

src/lib.rs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ impl From<Regex> for Match {
111111
}
112112
}
113113

114-
/// Replacements to apply to output files.
115-
pub type Filter = Vec<(Match, &'static [u8])>;
116-
117114
/// Run all tests as described in the config argument.
118115
/// Will additionally process command line arguments.
119116
pub fn run_tests(mut config: Config) -> Result<()> {
@@ -931,7 +928,6 @@ fn run_rustfix(
931928
path,
932929
&mut errors,
933930
"fixed",
934-
&Filter::default(),
935931
config,
936932
&rustfix_comments,
937933
revision,
@@ -1040,26 +1036,8 @@ fn check_test_output(
10401036
) {
10411037
// Check output files (if any)
10421038
// Check output files against actual output
1043-
check_output(
1044-
stderr,
1045-
path,
1046-
errors,
1047-
"stderr",
1048-
&config.stderr_filters,
1049-
config,
1050-
comments,
1051-
revision,
1052-
);
1053-
check_output(
1054-
stdout,
1055-
path,
1056-
errors,
1057-
"stdout",
1058-
&config.stdout_filters,
1059-
config,
1060-
comments,
1061-
revision,
1062-
);
1039+
check_output(stderr, path, errors, "stderr", config, comments, revision);
1040+
check_output(stdout, path, errors, "stdout", config, comments, revision);
10631041
}
10641042

10651043
fn check_annotations(
@@ -1197,13 +1175,12 @@ fn check_output(
11971175
path: &Path,
11981176
errors: &mut Errors,
11991177
kind: &'static str,
1200-
filters: &Filter,
12011178
config: &Config,
12021179
comments: &Comments,
12031180
revision: &str,
12041181
) -> PathBuf {
12051182
let target = config.target.as_ref().unwrap();
1206-
let output = normalize(output, filters, comments, revision, kind);
1183+
let output = normalize(output, comments, revision, kind);
12071184
let path = output_path(path, comments, revised(revision, kind), target, revision);
12081185
match &config.output_conflict_handling {
12091186
OutputConflictHandling::Error(bless_command) => {
@@ -1293,19 +1270,9 @@ fn get_pointer_width(triple: &str) -> u8 {
12931270
}
12941271
}
12951272

1296-
fn normalize(
1297-
text: &[u8],
1298-
filters: &Filter,
1299-
comments: &Comments,
1300-
revision: &str,
1301-
kind: &'static str,
1302-
) -> Vec<u8> {
1273+
fn normalize(text: &[u8], comments: &Comments, revision: &str, kind: &'static str) -> Vec<u8> {
13031274
let mut text = text.to_owned();
13041275

1305-
for (rule, replacement) in filters {
1306-
text = rule.replace_all(&text, replacement).into_owned();
1307-
}
1308-
13091276
for (from, to) in comments.for_revision(revision).flat_map(|r| match kind {
13101277
"fixed" => &[] as &[_],
13111278
"stderr" => &r.normalize_stderr,

src/parser.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
use bstr::{ByteSlice, Utf8Error};
99
use regex::bytes::Regex;
1010

11-
use crate::{rustc_stderr::Level, Error, Errored, Mode};
11+
use crate::{rustc_stderr::Level, Error, Errored, Match, Mode};
1212

1313
use color_eyre::eyre::{Context, Result};
1414

@@ -118,9 +118,9 @@ pub struct Revisioned {
118118
/// Additional env vars to set for the executable
119119
pub env_vars: Vec<(String, String)>,
120120
/// Normalizations to apply to the stderr output before emitting it to disk
121-
pub normalize_stderr: Vec<(Regex, Vec<u8>)>,
121+
pub normalize_stderr: Vec<(Match, Vec<u8>)>,
122122
/// Normalizations to apply to the stdout output before emitting it to disk
123-
pub normalize_stdout: Vec<(Regex, Vec<u8>)>,
123+
pub normalize_stdout: Vec<(Match, Vec<u8>)>,
124124
/// Arbitrary patterns to look for in the stderr.
125125
/// The error must be from another file, as errors from the current file must be
126126
/// checked via `error_matches`.
@@ -506,13 +506,13 @@ impl CommentParser<&mut Revisioned> {
506506
}
507507
}
508508
"normalize-stderr-test" => (this, args, _span){
509-
if let Some(res) = this.parse_normalize_test(args, "stderr") {
510-
this.normalize_stderr.push(res)
509+
if let Some((regex, replacement)) = this.parse_normalize_test(args, "stderr") {
510+
this.normalize_stderr.push((regex.into(), replacement))
511511
}
512512
}
513513
"normalize-stdout-test" => (this, args, _span){
514-
if let Some(res) = this.parse_normalize_test(args, "stdout") {
515-
this.normalize_stdout.push(res)
514+
if let Some((regex, replacement)) = this.parse_normalize_test(args, "stdout") {
515+
this.normalize_stdout.push((regex.into(), replacement))
516516
}
517517
}
518518
"error-pattern" => (this, _args, span){

tests/integration.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ fn main() -> Result<()> {
5252
// Insert the replacement filter at the start to make sure the filter for single backslashes
5353
// runs afterwards.
5454
config
55-
.stdout_filters
56-
.insert(0, (Match::Exact(b"\\\\".to_vec()), b"\\"));
55+
.comment_defaults
56+
.base()
57+
.normalize_stdout
58+
.insert(0, (Match::Exact(b"\\\\".to_vec()), b"\\".to_vec()));
5759
config.stdout_filter(r#"(panic.*)\.rs:[0-9]+:[0-9]+"#, "$1.rs");
5860
// We don't want to normalize lines starting with `+`, those are diffs of the inner ui_test
5961
// and normalizing these here doesn't make the "actual output differed from expected" go

0 commit comments

Comments
 (0)