Skip to content

Commit 809d586

Browse files
committed
Allow overwriting defaults for per-test comments
Demos this with `edition`
1 parent 596f418 commit 809d586

File tree

13 files changed

+52
-25
lines changed

13 files changed

+52
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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

@@ -20,5 +21,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2021
### Removed
2122

2223
* `$DIR` and `RUSTLIB` replacements
24+
* `Config::edition` (replaced by `config.comment_defaults.base().edition`)
2325

2426
## [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"

src/config.rs

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

3-
use crate::{dependencies::build_dependencies, CommandBuilder, Filter, Match, Mode, RustfixMode};
4+
use crate::{
5+
dependencies::build_dependencies, per_test_config::Comments, CommandBuilder, Filter, Match,
6+
Mode, RustfixMode,
7+
};
48
pub use color_eyre;
59
use color_eyre::eyre::Result;
610
use std::{
@@ -45,8 +49,6 @@ pub struct Config {
4549
/// Where to dump files like the binaries compiled from tests.
4650
/// Defaults to `target/ui` in the current directory.
4751
pub out_dir: PathBuf,
48-
/// The default edition to use on all tests.
49-
pub edition: Option<String>,
5052
/// Skip test files whose names contain any of these entries.
5153
pub skip_files: Vec<String>,
5254
/// Only test files whose names contain any of these entries.
@@ -59,12 +61,19 @@ pub struct Config {
5961
pub run_only_ignored: bool,
6062
/// Filters must match exactly instead of just checking for substrings.
6163
pub filter_exact: bool,
64+
/// The default settings settable via `@` comments
65+
pub comment_defaults: Comments,
6266
}
6367

6468
impl Config {
6569
/// Create a configuration for testing the output of running
6670
/// `rustc` on the test files.
6771
pub fn rustc(root_dir: impl Into<PathBuf>) -> Self {
72+
let mut comment_defaults = Comments::default();
73+
let _ = comment_defaults
74+
.base()
75+
.edition
76+
.set("2021".into(), Span::default());
6877
Self {
6978
host: None,
7079
target: None,
@@ -96,13 +105,13 @@ impl Config {
96105
.map(PathBuf::from)
97106
.unwrap_or_else(|| std::env::current_dir().unwrap().join("target"))
98107
.join("ui"),
99-
edition: Some("2021".into()),
100108
skip_files: Vec::new(),
101109
filter_files: Vec::new(),
102110
threads: None,
103111
list: false,
104112
run_only_ignored: false,
105113
filter_exact: false,
114+
comment_defaults,
106115
}
107116
}
108117

@@ -111,7 +120,7 @@ impl Config {
111120
pub fn cargo(root_dir: impl Into<PathBuf>) -> Self {
112121
Self {
113122
program: CommandBuilder::cargo(),
114-
edition: None,
123+
comment_defaults: Default::default(),
115124
mode: Mode::Fail {
116125
require_patterns: true,
117126
rustfix: RustfixMode::Disabled,

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ fn build_command(
501501
{
502502
cmd.arg(arg);
503503
}
504-
let edition = comments.edition(revision, config)?;
504+
let edition = comments.edition(revision, &config.comment_defaults)?;
505505

506506
if let Some(edition) = edition {
507507
cmd.arg("--edition").arg(&*edition);
@@ -878,7 +878,7 @@ fn run_rustfix(
878878
stdout: stdout.into(),
879879
})?;
880880

881-
let edition = comments.edition(revision, config)?;
881+
let edition = comments.edition(revision, &config.comment_defaults)?;
882882
let edition = edition
883883
.map(|mwl| {
884884
let line = mwl.span().unwrap_or_default();

src/parser.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod tests;
2121
/// This crate supports various magic comments that get parsed as file-specific
2222
/// configuration values. This struct parses them all in one go and then they
2323
/// get processed by their respective use sites.
24-
#[derive(Default, Debug)]
24+
#[derive(Debug, Clone)]
2525
pub struct Comments {
2626
/// List of revision names to execute. Can only be specified once
2727
pub revisions: Option<Vec<String>>,
@@ -30,6 +30,17 @@ pub struct Comments {
3030
pub revisioned: HashMap<Vec<String>, Revisioned>,
3131
}
3232

33+
impl Default for Comments {
34+
fn default() -> Self {
35+
let mut this = Self {
36+
revisions: Default::default(),
37+
revisioned: Default::default(),
38+
};
39+
this.revisioned.insert(vec![], Revisioned::default());
40+
this
41+
}
42+
}
43+
3344
impl Comments {
3445
/// Check that a comment isn't specified twice across multiple differently revisioned statements.
3546
/// e.g. `//@[foo, bar] error-in-other-file: bop` and `//@[foo, baz] error-in-other-file boop` would end up
@@ -80,19 +91,26 @@ impl Comments {
8091
pub(crate) fn edition(
8192
&self,
8293
revision: &str,
83-
config: &crate::Config,
94+
default: &Self,
8495
) -> Result<Option<MaybeSpanned<String>>, Errored> {
8596
let edition =
8697
self.find_one_for_revision(revision, "`edition` annotations", |r| r.edition.clone())?;
98+
let default = default
99+
.find_one_for_revision(revision, "`edition` annotations", |r| r.edition.clone())?;
87100
let edition = edition
88101
.into_inner()
89-
.map(MaybeSpanned::from)
90-
.or(config.edition.clone().map(MaybeSpanned::new_config));
102+
.or(default.into_inner())
103+
.map(MaybeSpanned::from);
91104
Ok(edition)
92105
}
106+
107+
/// The comments set for all revisions
108+
pub fn base(&mut self) -> &mut Revisioned {
109+
self.revisioned.get_mut(&[][..]).unwrap()
110+
}
93111
}
94112

95-
#[derive(Debug)]
113+
#[derive(Debug, Clone, Default)]
96114
/// Comments that can be filtered for specific revisions.
97115
pub struct Revisioned {
98116
/// The character range in which this revisioned item was first added.
@@ -158,7 +176,7 @@ impl<T> std::ops::DerefMut for CommentParser<T> {
158176
}
159177

160178
/// The conditions used for "ignore" and "only" filters.
161-
#[derive(Debug)]
179+
#[derive(Debug, Clone)]
162180
pub enum Condition {
163181
/// The given string must appear in the host triple.
164182
Host(String),
@@ -177,7 +195,7 @@ pub enum Pattern {
177195
Regex(Regex),
178196
}
179197

180-
#[derive(Debug)]
198+
#[derive(Debug, Clone)]
181199
pub(crate) struct ErrorMatch {
182200
pub pattern: Spanned<Pattern>,
183201
pub level: Level,

tests/integration.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use ui_test::*;
66
fn main() -> Result<()> {
77
let path = Path::new(file!()).parent().unwrap();
88
let root_dir = path.join("integrations");
9-
let mut config = Config {
10-
..Config::cargo(root_dir.clone())
11-
};
9+
let mut config = Config::cargo(root_dir.clone());
1210
let args = Args::test()?;
1311
config.with_args(&args, true);
1412

tests/integrations/basic-bin/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.

tests/integrations/basic-fail-mode/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.

tests/integrations/basic-fail/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.

0 commit comments

Comments
 (0)