Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default test settings #183

Merged
merged 8 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

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

### Fixed

### Changed

* crate-private span handling was passed off to the `spanned` crate, improving some diagnostics along the way.

### Removed

* `$DIR` and `RUSTLIB` replacements
* `Config::edition` (replaced by `config.comment_defaults.base().edition`)
* `Config::filter_stdout` (replaced by `config.comment_defaults.base().normalize_stdout`)
* `Config::filter_stderr` (replaced by `config.comment_defaults.base().normalize_stderr`)
* `Config::mode` (replaced by `config.comment_defaults.base().mode`)

## [0.21.2] - 2023-09-27
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ui_test"
version = "0.21.2"
version = "0.22.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "A test framework for testing rustc diagnostics output"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A smaller version of compiletest-rs
A stable version of compiletest-rs

## Magic behavior

Expand Down
99 changes: 52 additions & 47 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use regex::bytes::Regex;
use spanned::{Span, Spanned};

use crate::{dependencies::build_dependencies, CommandBuilder, Filter, Match, Mode, RustfixMode};
use crate::{
dependencies::build_dependencies, per_test_config::Comments, CommandBuilder, Match, Mode,
RustfixMode,
};
pub use color_eyre;
use color_eyre::eyre::Result;
use std::{
Expand All @@ -19,18 +23,8 @@ pub struct Config {
pub host: Option<String>,
/// `None` to run on the host, otherwise a target triple
pub target: Option<String>,
/// Filters applied to stderr output before processing it.
/// By default contains a filter for replacing backslashes in paths with
/// regular slashes.
/// On windows, contains a filter to remove `\r`.
pub stderr_filters: Filter,
/// Filters applied to stdout output before processing it.
/// On windows, contains a filter to remove `\r`.
pub stdout_filters: Filter,
/// The folder in which to start searching for .rs files
pub root_dir: PathBuf,
/// The mode in which to run the tests.
pub mode: Mode,
/// The binary to actually execute.
pub program: CommandBuilder,
/// The command to run to obtain the cfgs that the output is supposed to
Expand All @@ -45,8 +39,6 @@ pub struct Config {
/// Where to dump files like the binaries compiled from tests.
/// Defaults to `target/ui` in the current directory.
pub out_dir: PathBuf,
/// The default edition to use on all tests.
pub edition: Option<String>,
/// Skip test files whose names contain any of these entries.
pub skip_files: Vec<String>,
/// Only test files whose names contain any of these entries.
Expand All @@ -59,34 +51,37 @@ pub struct Config {
pub run_only_ignored: bool,
/// Filters must match exactly instead of just checking for substrings.
pub filter_exact: bool,
/// The default settings settable via `@` comments
pub comment_defaults: Comments,
}

impl Config {
/// Create a configuration for testing the output of running
/// `rustc` on the test files.
pub fn rustc(root_dir: impl Into<PathBuf>) -> Self {
let mut comment_defaults = Comments::default();
let _ = comment_defaults
.base()
.edition
.set("2021".into(), Span::default());
let filters = vec![
(Match::PathBackslash, b"/".to_vec()),
#[cfg(windows)]
(Match::Exact(vec![b'\r']), b"".to_vec()),
#[cfg(windows)]
(Match::Exact(br"\\?\".to_vec()), b"".to_vec()),
];
comment_defaults.base().normalize_stderr = filters.clone();
comment_defaults.base().normalize_stdout = filters;
comment_defaults.base().mode = Spanned::dummy(Mode::Fail {
require_patterns: true,
rustfix: RustfixMode::MachineApplicable,
})
.into();
Self {
host: None,
target: None,
stderr_filters: vec![
(Match::PathBackslash, b"/"),
#[cfg(windows)]
(Match::Exact(vec![b'\r']), b""),
#[cfg(windows)]
(Match::Exact(br"\\?\".to_vec()), b""),
],
stdout_filters: vec![
(Match::PathBackslash, b"/"),
#[cfg(windows)]
(Match::Exact(vec![b'\r']), b""),
#[cfg(windows)]
(Match::Exact(br"\\?\".to_vec()), b""),
],
root_dir: root_dir.into(),
mode: Mode::Fail {
require_patterns: true,
rustfix: RustfixMode::MachineApplicable,
},
program: CommandBuilder::rustc(),
cfgs: CommandBuilder::cfgs(),
output_conflict_handling: OutputConflictHandling::Bless,
Expand All @@ -96,28 +91,30 @@ impl Config {
.map(PathBuf::from)
.unwrap_or_else(|| std::env::current_dir().unwrap().join("target"))
.join("ui"),
edition: Some("2021".into()),
skip_files: Vec::new(),
filter_files: Vec::new(),
threads: None,
list: false,
run_only_ignored: false,
filter_exact: false,
comment_defaults,
}
}

/// Create a configuration for testing the output of running
/// `cargo` on the test `Cargo.toml` files.
pub fn cargo(root_dir: impl Into<PathBuf>) -> Self {
Self {
let mut this = Self {
program: CommandBuilder::cargo(),
edition: None,
mode: Mode::Fail {
require_patterns: true,
rustfix: RustfixMode::Disabled,
},
..Self::rustc(root_dir)
}
};
this.comment_defaults.base().edition = Default::default();
this.comment_defaults.base().mode = Spanned::dummy(Mode::Fail {
require_patterns: true,
rustfix: RustfixMode::Disabled,
})
.into();
this
}

/// Populate the config with the values from parsed command line arguments.
Expand Down Expand Up @@ -180,8 +177,10 @@ impl Config {
replacement: &'static (impl AsRef<[u8]> + ?Sized),
) {
let pattern = path.canonicalize().unwrap();
self.stderr_filters
.push((pattern.parent().unwrap().into(), replacement.as_ref()));
self.comment_defaults.base().normalize_stderr.push((
pattern.parent().unwrap().into(),
replacement.as_ref().to_owned(),
));
}

/// Replace all occurrences of a path in stdout with a byte string.
Expand All @@ -192,8 +191,10 @@ impl Config {
replacement: &'static (impl AsRef<[u8]> + ?Sized),
) {
let pattern = path.canonicalize().unwrap();
self.stdout_filters
.push((pattern.parent().unwrap().into(), replacement.as_ref()));
self.comment_defaults.base().normalize_stdout.push((
pattern.parent().unwrap().into(),
replacement.as_ref().to_owned(),
));
}

/// Replace all occurrences of a regex pattern in stderr/stdout with a byte string.
Expand All @@ -210,8 +211,10 @@ impl Config {
pattern: &str,
replacement: &'static (impl AsRef<[u8]> + ?Sized),
) {
self.stderr_filters
.push((Regex::new(pattern).unwrap().into(), replacement.as_ref()));
self.comment_defaults.base().normalize_stderr.push((
Regex::new(pattern).unwrap().into(),
replacement.as_ref().to_owned(),
));
}

/// Replace all occurrences of a regex pattern in stdout with a byte string.
Expand All @@ -221,8 +224,10 @@ impl Config {
pattern: &str,
replacement: &'static (impl AsRef<[u8]> + ?Sized),
) {
self.stdout_filters
.push((Regex::new(pattern).unwrap().into(), replacement.as_ref()));
self.comment_defaults.base().normalize_stdout.push((
Regex::new(pattern).unwrap().into(),
replacement.as_ref().to_owned(),
));
}

/// Compile dependencies and return the right flags
Expand Down
10 changes: 9 additions & 1 deletion src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ pub(crate) fn build_dependencies(config: &Config) -> Result<Dependencies> {
}

// Reusable closure for setting up the environment both for artifact generation and `cargo_metadata`
let set_locking = |cmd: &mut Command| match (&config.output_conflict_handling, &config.mode) {
let set_locking = |cmd: &mut Command| match (
&config.output_conflict_handling,
config
.comment_defaults
.base_immut()
.mode
.as_deref()
.unwrap(),
) {
(_, Mode::Yolo { .. }) => {}
(OutputConflictHandling::Error(_), _) => {
cmd.arg("--locked");
Expand Down
4 changes: 3 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum Error {
/// A ui test checking for success has failure patterns
PatternFoundInPassTest {
/// Span of a flag changing the mode (if changed from default).
mode: Option<Span>,
mode: Span,
/// Span of the pattern
span: Span,
},
Expand Down Expand Up @@ -59,6 +59,8 @@ pub enum Error {
/// The character range in which it was defined.
span: Span,
},
/// An invalid setting was used.
ConfigError(String),
/// Conflicting comments
MultipleRevisionsWithResults {
/// The comment being looked for
Expand Down
Loading