Skip to content

Commit 963dfb2

Browse files
authored
Merge pull request #183 from oli-obk/default_test_settings
Default test settings
2 parents 10b5d58 + 5d48b7b commit 963dfb2

26 files changed

+294
-266
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* Started maintaining a changelog
13+
* `Config::comment_defaults` allows setting `//@` comments for all tests
1314

1415
### Fixed
1516

1617
### Changed
1718

19+
* crate-private span handling was passed off to the `spanned` crate, improving some diagnostics along the way.
20+
1821
### Removed
1922

2023
* `$DIR` and `RUSTLIB` replacements
24+
* `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`)
27+
* `Config::mode` (replaced by `config.comment_defaults.base().mode`)
2128

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

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ui_test"
3-
version = "0.21.2"
3+
version = "0.22.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "A test framework for testing rustc diagnostics output"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
A smaller version of compiletest-rs
1+
A stable version of compiletest-rs
22

33
## Magic behavior
44

src/config.rs

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use regex::bytes::Regex;
2+
use spanned::{Span, Spanned};
23

3-
use crate::{dependencies::build_dependencies, CommandBuilder, Filter, Match, Mode, RustfixMode};
4+
use crate::{
5+
dependencies::build_dependencies, per_test_config::Comments, CommandBuilder, Match, Mode,
6+
RustfixMode,
7+
};
48
pub use color_eyre;
59
use color_eyre::eyre::Result;
610
use std::{
@@ -19,18 +23,8 @@ pub struct Config {
1923
pub host: Option<String>,
2024
/// `None` to run on the host, otherwise a target triple
2125
pub target: Option<String>,
22-
/// Filters applied to stderr output before processing it.
23-
/// By default contains a filter for replacing backslashes in paths with
24-
/// regular slashes.
25-
/// On windows, contains a filter to remove `\r`.
26-
pub stderr_filters: Filter,
27-
/// Filters applied to stdout output before processing it.
28-
/// On windows, contains a filter to remove `\r`.
29-
pub stdout_filters: Filter,
3026
/// The folder in which to start searching for .rs files
3127
pub root_dir: PathBuf,
32-
/// The mode in which to run the tests.
33-
pub mode: Mode,
3428
/// The binary to actually execute.
3529
pub program: CommandBuilder,
3630
/// The command to run to obtain the cfgs that the output is supposed to
@@ -45,8 +39,6 @@ pub struct Config {
4539
/// Where to dump files like the binaries compiled from tests.
4640
/// Defaults to `target/ui` in the current directory.
4741
pub out_dir: PathBuf,
48-
/// The default edition to use on all tests.
49-
pub edition: Option<String>,
5042
/// Skip test files whose names contain any of these entries.
5143
pub skip_files: Vec<String>,
5244
/// Only test files whose names contain any of these entries.
@@ -59,34 +51,37 @@ pub struct Config {
5951
pub run_only_ignored: bool,
6052
/// Filters must match exactly instead of just checking for substrings.
6153
pub filter_exact: bool,
54+
/// The default settings settable via `@` comments
55+
pub comment_defaults: Comments,
6256
}
6357

6458
impl Config {
6559
/// Create a configuration for testing the output of running
6660
/// `rustc` on the test files.
6761
pub fn rustc(root_dir: impl Into<PathBuf>) -> Self {
62+
let mut comment_defaults = Comments::default();
63+
let _ = comment_defaults
64+
.base()
65+
.edition
66+
.set("2021".into(), Span::default());
67+
let filters = vec![
68+
(Match::PathBackslash, b"/".to_vec()),
69+
#[cfg(windows)]
70+
(Match::Exact(vec![b'\r']), b"".to_vec()),
71+
#[cfg(windows)]
72+
(Match::Exact(br"\\?\".to_vec()), b"".to_vec()),
73+
];
74+
comment_defaults.base().normalize_stderr = filters.clone();
75+
comment_defaults.base().normalize_stdout = filters;
76+
comment_defaults.base().mode = Spanned::dummy(Mode::Fail {
77+
require_patterns: true,
78+
rustfix: RustfixMode::MachineApplicable,
79+
})
80+
.into();
6881
Self {
6982
host: None,
7083
target: None,
71-
stderr_filters: vec![
72-
(Match::PathBackslash, b"/"),
73-
#[cfg(windows)]
74-
(Match::Exact(vec![b'\r']), b""),
75-
#[cfg(windows)]
76-
(Match::Exact(br"\\?\".to_vec()), b""),
77-
],
78-
stdout_filters: vec![
79-
(Match::PathBackslash, b"/"),
80-
#[cfg(windows)]
81-
(Match::Exact(vec![b'\r']), b""),
82-
#[cfg(windows)]
83-
(Match::Exact(br"\\?\".to_vec()), b""),
84-
],
8584
root_dir: root_dir.into(),
86-
mode: Mode::Fail {
87-
require_patterns: true,
88-
rustfix: RustfixMode::MachineApplicable,
89-
},
9085
program: CommandBuilder::rustc(),
9186
cfgs: CommandBuilder::cfgs(),
9287
output_conflict_handling: OutputConflictHandling::Bless,
@@ -96,28 +91,30 @@ impl Config {
9691
.map(PathBuf::from)
9792
.unwrap_or_else(|| std::env::current_dir().unwrap().join("target"))
9893
.join("ui"),
99-
edition: Some("2021".into()),
10094
skip_files: Vec::new(),
10195
filter_files: Vec::new(),
10296
threads: None,
10397
list: false,
10498
run_only_ignored: false,
10599
filter_exact: false,
100+
comment_defaults,
106101
}
107102
}
108103

109104
/// Create a configuration for testing the output of running
110105
/// `cargo` on the test `Cargo.toml` files.
111106
pub fn cargo(root_dir: impl Into<PathBuf>) -> Self {
112-
Self {
107+
let mut this = Self {
113108
program: CommandBuilder::cargo(),
114-
edition: None,
115-
mode: Mode::Fail {
116-
require_patterns: true,
117-
rustfix: RustfixMode::Disabled,
118-
},
119109
..Self::rustc(root_dir)
120-
}
110+
};
111+
this.comment_defaults.base().edition = Default::default();
112+
this.comment_defaults.base().mode = Spanned::dummy(Mode::Fail {
113+
require_patterns: true,
114+
rustfix: RustfixMode::Disabled,
115+
})
116+
.into();
117+
this
121118
}
122119

123120
/// Populate the config with the values from parsed command line arguments.
@@ -180,8 +177,10 @@ impl Config {
180177
replacement: &'static (impl AsRef<[u8]> + ?Sized),
181178
) {
182179
let pattern = path.canonicalize().unwrap();
183-
self.stderr_filters
184-
.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+
));
185184
}
186185

187186
/// Replace all occurrences of a path in stdout with a byte string.
@@ -192,8 +191,10 @@ impl Config {
192191
replacement: &'static (impl AsRef<[u8]> + ?Sized),
193192
) {
194193
let pattern = path.canonicalize().unwrap();
195-
self.stdout_filters
196-
.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+
));
197198
}
198199

199200
/// Replace all occurrences of a regex pattern in stderr/stdout with a byte string.
@@ -210,8 +211,10 @@ impl Config {
210211
pattern: &str,
211212
replacement: &'static (impl AsRef<[u8]> + ?Sized),
212213
) {
213-
self.stderr_filters
214-
.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+
));
215218
}
216219

217220
/// Replace all occurrences of a regex pattern in stdout with a byte string.
@@ -221,8 +224,10 @@ impl Config {
221224
pattern: &str,
222225
replacement: &'static (impl AsRef<[u8]> + ?Sized),
223226
) {
224-
self.stdout_filters
225-
.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+
));
226231
}
227232

228233
/// Compile dependencies and return the right flags

src/dependencies.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ pub(crate) fn build_dependencies(config: &Config) -> Result<Dependencies> {
5959
}
6060

6161
// Reusable closure for setting up the environment both for artifact generation and `cargo_metadata`
62-
let set_locking = |cmd: &mut Command| match (&config.output_conflict_handling, &config.mode) {
62+
let set_locking = |cmd: &mut Command| match (
63+
&config.output_conflict_handling,
64+
config
65+
.comment_defaults
66+
.base_immut()
67+
.mode
68+
.as_deref()
69+
.unwrap(),
70+
) {
6371
(_, Mode::Yolo { .. }) => {}
6472
(OutputConflictHandling::Error(_), _) => {
6573
cmd.arg("--locked");

src/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub enum Error {
3030
/// A ui test checking for success has failure patterns
3131
PatternFoundInPassTest {
3232
/// Span of a flag changing the mode (if changed from default).
33-
mode: Option<Span>,
33+
mode: Span,
3434
/// Span of the pattern
3535
span: Span,
3636
},
@@ -59,6 +59,8 @@ pub enum Error {
5959
/// The character range in which it was defined.
6060
span: Span,
6161
},
62+
/// An invalid setting was used.
63+
ConfigError(String),
6264
/// Conflicting comments
6365
MultipleRevisionsWithResults {
6466
/// The comment being looked for

0 commit comments

Comments
 (0)