diff options
Diffstat (limited to 'vendor/ui_test/src')
-rw-r--r-- | vendor/ui_test/src/cmd.rs | 120 | ||||
-rw-r--r-- | vendor/ui_test/src/config.rs | 196 | ||||
-rw-r--r-- | vendor/ui_test/src/dependencies.rs | 183 | ||||
-rw-r--r-- | vendor/ui_test/src/diff.rs | 175 | ||||
-rw-r--r-- | vendor/ui_test/src/error.rs | 72 | ||||
-rw-r--r-- | vendor/ui_test/src/github_actions.rs | 94 | ||||
-rw-r--r-- | vendor/ui_test/src/lib.rs | 1054 | ||||
-rw-r--r-- | vendor/ui_test/src/mode.rs | 84 | ||||
-rw-r--r-- | vendor/ui_test/src/parser.rs | 648 | ||||
-rw-r--r-- | vendor/ui_test/src/parser/tests.rs | 137 | ||||
-rw-r--r-- | vendor/ui_test/src/rustc_stderr.rs | 160 | ||||
-rw-r--r-- | vendor/ui_test/src/status_emitter.rs | 577 | ||||
-rw-r--r-- | vendor/ui_test/src/tests.rs | 338 |
13 files changed, 3838 insertions, 0 deletions
diff --git a/vendor/ui_test/src/cmd.rs b/vendor/ui_test/src/cmd.rs new file mode 100644 index 000000000..63d055e28 --- /dev/null +++ b/vendor/ui_test/src/cmd.rs @@ -0,0 +1,120 @@ +use std::{ + ffi::OsString, + path::{Path, PathBuf}, + process::Command, +}; + +#[derive(Debug, Clone)] +/// A command, its args and its environment. Used for +/// the main command, the dependency builder and the cfg-reader. +pub struct CommandBuilder { + /// Path to the binary. + pub program: PathBuf, + /// Arguments to the binary. + pub args: Vec<OsString>, + /// A flag to prefix before the path to where output files should be written. + pub out_dir_flag: Option<OsString>, + /// A flag to set as the last flag in the command, so the `build` caller can + /// append the filename themselves. + pub input_file_flag: Option<OsString>, + /// Environment variables passed to the binary that is executed. + /// The environment variable is removed if the second tuple field is `None` + pub envs: Vec<(OsString, Option<OsString>)>, +} + +impl CommandBuilder { + /// Uses the `CARGO` env var or just a program named `cargo` and the argument `build`. + pub fn cargo() -> Self { + Self { + program: PathBuf::from(std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into())), + args: vec!["build".into()], + out_dir_flag: Some("--target-dir".into()), + input_file_flag: Some("--manifest-path".into()), + envs: vec![], + } + } + + /// Uses the `RUSTC` env var or just a program named `rustc` and the argument `--error-format=json`. + /// + /// Take care to only append unless you actually meant to overwrite the defaults. + /// Overwriting the defaults may make `//~ ERROR` style comments stop working. + pub fn rustc() -> Self { + Self { + program: PathBuf::from(std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into())), + args: vec!["--error-format=json".into()], + out_dir_flag: Some("--out-dir".into()), + input_file_flag: None, + envs: vec![], + } + } + + /// Same as [`rustc`], but with arguments for obtaining the cfgs. + pub fn cfgs() -> Self { + Self { + args: vec!["--print".into(), "cfg".into()], + ..Self::rustc() + } + } + + /// Build a `CommandBuilder` for a command without any argumemnts. + /// You can still add arguments later. + pub fn cmd(cmd: impl Into<PathBuf>) -> Self { + Self { + program: cmd.into(), + args: vec![], + out_dir_flag: None, + input_file_flag: None, + envs: vec![], + } + } + + /// Render the command like you'd use it on a command line. + pub fn display(&self) -> impl std::fmt::Display + '_ { + struct Display<'a>(&'a CommandBuilder); + impl std::fmt::Display for Display<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for (var, val) in &self.0.envs { + if let Some(val) = val { + write!(f, "{var:?}={val:?} ")?; + } + } + self.0.program.display().fmt(f)?; + for arg in &self.0.args { + write!(f, " {arg:?}")?; + } + if let Some(flag) = &self.0.out_dir_flag { + write!(f, " {flag:?} OUT_DIR")?; + } + if let Some(flag) = &self.0.input_file_flag { + write!(f, " {flag:?}")?; + } + Ok(()) + } + } + Display(self) + } + + /// Create a command with the given settings. + pub fn build(&self, out_dir: &Path) -> Command { + let mut cmd = Command::new(&self.program); + cmd.args(self.args.iter()); + if let Some(flag) = &self.out_dir_flag { + cmd.arg(flag).arg(out_dir); + } + if let Some(flag) = &self.input_file_flag { + cmd.arg(flag); + } + self.apply_env(&mut cmd); + cmd + } + + pub(crate) fn apply_env(&self, cmd: &mut Command) { + for (var, val) in self.envs.iter() { + if let Some(val) = val { + cmd.env(var, val); + } else { + cmd.env_remove(var); + } + } + } +} diff --git a/vendor/ui_test/src/config.rs b/vendor/ui_test/src/config.rs new file mode 100644 index 000000000..6f24cce90 --- /dev/null +++ b/vendor/ui_test/src/config.rs @@ -0,0 +1,196 @@ +use regex::bytes::Regex; + +use crate::{dependencies::build_dependencies, CommandBuilder, Filter, Match, Mode}; +pub use color_eyre; +use color_eyre::eyre::Result; +use std::{ + ffi::OsString, + num::NonZeroUsize, + path::{Path, PathBuf}, +}; + +#[derive(Debug, Clone)] +/// Central datastructure containing all information to run the tests. +pub struct Config { + /// Arguments passed to the binary that is executed. + /// These arguments are passed *after* the args inserted via `//@compile-flags:`. + pub trailing_args: Vec<OsString>, + /// Host triple; usually will be auto-detected. + 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 with regular slashes. + /// On windows, contains a filter to replace `\n` with `\r\n`. + pub stderr_filters: Filter, + /// Filters applied to stdout output before processing it. + /// On windows, contains a filter to replace `\n` with `\r\n`. + 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 + pub cfgs: CommandBuilder, + /// What to do in case the stdout/stderr output differs from the expected one. + /// By default, errors in case of conflict, but emits a message informing the user + /// that running `cargo test -- -- --bless` will automatically overwrite the + /// `.stdout` and `.stderr` files with the latest output. + pub output_conflict_handling: OutputConflictHandling, + /// Path to a `Cargo.toml` that describes which dependencies the tests can access. + pub dependencies_crate_manifest_path: Option<PathBuf>, + /// The command to run can be changed from `cargo` to any custom command to build the + /// dependencies in `dependencies_crate_manifest_path` + pub dependency_builder: CommandBuilder, + /// How many threads to use for running tests. Defaults to number of cores + pub num_test_threads: NonZeroUsize, + /// 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>, +} + +impl Config { + /// Create a configuration for testing the output of running + /// `rustc` on the test files. + pub fn rustc(root_dir: PathBuf) -> Self { + Self { + trailing_args: vec![], + host: None, + target: None, + stderr_filters: vec![ + (Match::Exact(vec![b'\\']), b"/"), + #[cfg(windows)] + (Match::Exact(vec![b'\r']), b""), + ], + stdout_filters: vec![ + #[cfg(windows)] + (Match::Exact(vec![b'\r']), b""), + ], + root_dir, + mode: Mode::Fail { + require_patterns: true, + }, + program: CommandBuilder::rustc(), + cfgs: CommandBuilder::cfgs(), + output_conflict_handling: OutputConflictHandling::Error( + "cargo test -- -- --bless".into(), + ), + dependencies_crate_manifest_path: None, + dependency_builder: CommandBuilder::cargo(), + num_test_threads: std::thread::available_parallelism().unwrap(), + out_dir: std::env::current_dir().unwrap().join("target/ui"), + edition: Some("2021".into()), + } + } + + /// Create a configuration for testing the output of running + /// `cargo` on the test `Cargo.toml` files. + pub fn cargo(root_dir: PathBuf) -> Self { + Self { + program: CommandBuilder::cargo(), + edition: None, + ..Self::rustc(root_dir) + } + } + + /// Replace all occurrences of a path in stderr with a byte string. + pub fn path_stderr_filter( + &mut self, + path: &Path, + replacement: &'static (impl AsRef<[u8]> + ?Sized), + ) { + let pattern = path.canonicalize().unwrap(); + self.stderr_filters + .push((pattern.parent().unwrap().into(), replacement.as_ref())); + } + + /// Replace all occurrences of a regex pattern in stderr with a byte string. + pub fn stderr_filter( + &mut self, + pattern: &str, + replacement: &'static (impl AsRef<[u8]> + ?Sized), + ) { + self.stderr_filters + .push((Regex::new(pattern).unwrap().into(), replacement.as_ref())); + } + + /// Replace all occurrences of a regex pattern in stdout with a byte string. + pub fn stdout_filter( + &mut self, + pattern: &str, + replacement: &'static (impl AsRef<[u8]> + ?Sized), + ) { + self.stdout_filters + .push((Regex::new(pattern).unwrap().into(), replacement.as_ref())); + } + + pub(crate) fn build_dependencies_and_link_them(&mut self) -> Result<()> { + let dependencies = build_dependencies(self)?; + for (name, artifacts) in dependencies.dependencies { + for dependency in artifacts { + self.program.args.push("--extern".into()); + let mut dep = OsString::from(&name); + dep.push("="); + dep.push(dependency); + self.program.args.push(dep); + } + } + for import_path in dependencies.import_paths { + self.program.args.push("-L".into()); + self.program.args.push(import_path.into()); + } + Ok(()) + } + + /// Make sure we have the host and target triples. + pub fn fill_host_and_target(&mut self) -> Result<()> { + if self.host.is_none() { + self.host = Some( + rustc_version::VersionMeta::for_command(std::process::Command::new( + &self.program.program, + )) + .map_err(|err| { + color_eyre::eyre::Report::new(err).wrap_err(format!( + "failed to parse rustc version info: {}", + self.program.display() + )) + })? + .host, + ); + } + if self.target.is_none() { + self.target = Some(self.host.clone().unwrap()); + } + Ok(()) + } + + pub(crate) fn has_asm_support(&self) -> bool { + static ASM_SUPPORTED_ARCHS: &[&str] = &[ + "x86", "x86_64", "arm", "aarch64", "riscv32", + "riscv64", + // These targets require an additional asm_experimental_arch feature. + // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32", + ]; + ASM_SUPPORTED_ARCHS + .iter() + .any(|arch| self.target.as_ref().unwrap().contains(arch)) + } +} + +#[derive(Debug, Clone)] +/// The different options for what to do when stdout/stderr files differ from the actual output. +pub enum OutputConflictHandling { + /// The default: emit a diff of the expected/actual output. + /// + /// The string should be a command that can be executed to bless all tests. + Error(String), + /// Ignore mismatches in the stderr/stdout files. + Ignore, + /// Instead of erroring if the stderr/stdout differs from the expected + /// automatically replace it with the found output (after applying filters). + Bless, +} diff --git a/vendor/ui_test/src/dependencies.rs b/vendor/ui_test/src/dependencies.rs new file mode 100644 index 000000000..800a3a447 --- /dev/null +++ b/vendor/ui_test/src/dependencies.rs @@ -0,0 +1,183 @@ +use cargo_metadata::{camino::Utf8PathBuf, DependencyKind}; +use cargo_platform::Cfg; +use color_eyre::eyre::{bail, Result}; +use std::{ + collections::{HashMap, HashSet}, + path::PathBuf, + process::Command, + str::FromStr, +}; + +use crate::{Config, Mode, OutputConflictHandling}; + +#[derive(Default, Debug)] +pub struct Dependencies { + /// All paths that must be imported with `-L dependency=`. This is for + /// finding proc macros run on the host and dependencies for the target. + pub import_paths: Vec<PathBuf>, + /// The name as chosen in the `Cargo.toml` and its corresponding rmeta file. + pub dependencies: Vec<(String, Vec<Utf8PathBuf>)>, +} + +fn cfgs(config: &Config) -> Result<Vec<Cfg>> { + let mut cmd = config.cfgs.build(&config.out_dir); + cmd.arg("--target").arg(config.target.as_ref().unwrap()); + let output = cmd.output()?; + let stdout = String::from_utf8(output.stdout)?; + + if !output.status.success() { + let stderr = String::from_utf8(output.stderr)?; + bail!( + "failed to obtain `cfg` information from {cmd:?}:\nstderr:\n{stderr}\n\nstdout:{stdout}" + ); + } + let mut cfgs = vec![]; + + for line in stdout.lines() { + cfgs.push(Cfg::from_str(line)?); + } + + Ok(cfgs) +} + +/// Compiles dependencies and returns the crate names and corresponding rmeta files. +pub fn build_dependencies(config: &mut Config) -> Result<Dependencies> { + let manifest_path = match &config.dependencies_crate_manifest_path { + Some(path) => path.to_owned(), + None => return Ok(Default::default()), + }; + let manifest_path = &manifest_path; + config.fill_host_and_target()?; + eprintln!(" Building test dependencies..."); + let mut build = config.dependency_builder.build(&config.out_dir); + build.arg(manifest_path); + + if let Some(target) = &config.target { + build.arg(format!("--target={target}")); + } + + // 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) { + (_, Mode::Yolo) => {} + (OutputConflictHandling::Error(_), _) => { + cmd.arg("--locked"); + } + _ => {} + }; + + set_locking(&mut build); + build.arg("--message-format=json"); + + let output = build.output()?; + + if !output.status.success() { + let stdout = String::from_utf8(output.stdout)?; + let stderr = String::from_utf8(output.stderr)?; + bail!("failed to compile dependencies:\ncommand: {build:?}\nstderr:\n{stderr}\n\nstdout:{stdout}"); + } + + // Collect all artifacts generated + let artifact_output = output.stdout; + let artifact_output = String::from_utf8(artifact_output)?; + let mut import_paths: HashSet<PathBuf> = HashSet::new(); + let mut artifacts: HashMap<_, _> = artifact_output + .lines() + .filter_map(|line| { + let message = serde_json::from_str::<cargo_metadata::Message>(line).ok()?; + if let cargo_metadata::Message::CompilerArtifact(artifact) = message { + for filename in &artifact.filenames { + import_paths.insert(filename.parent().unwrap().into()); + } + Some((artifact.package_id, artifact.filenames)) + } else { + None + } + }) + .collect(); + + // Check which crates are mentioned in the crate itself + let mut metadata = cargo_metadata::MetadataCommand::new().cargo_command(); + metadata.arg("--manifest-path").arg(manifest_path); + config.dependency_builder.apply_env(&mut metadata); + set_locking(&mut metadata); + let output = metadata.output()?; + + if !output.status.success() { + let stdout = String::from_utf8(output.stdout)?; + let stderr = String::from_utf8(output.stderr)?; + bail!("failed to run cargo-metadata:\nstderr:\n{stderr}\n\nstdout:{stdout}"); + } + + let output = output.stdout; + let output = String::from_utf8(output)?; + + let cfg = cfgs(config)?; + + for line in output.lines() { + if !line.starts_with('{') { + continue; + } + let metadata: cargo_metadata::Metadata = serde_json::from_str(line)?; + // Only take artifacts that are defined in the Cargo.toml + + // First, find the root artifact + let root = metadata + .packages + .iter() + .find(|package| { + package.manifest_path.as_std_path().canonicalize().unwrap() + == manifest_path.canonicalize().unwrap() + }) + .unwrap(); + + // Then go over all of its dependencies + let dependencies = root + .dependencies + .iter() + .filter(|dep| matches!(dep.kind, DependencyKind::Normal)) + // Only consider dependencies that are enabled on the current target + .filter(|dep| match &dep.target { + Some(platform) => platform.matches(config.target.as_ref().unwrap(), &cfg), + None => true, + }) + .map(|dep| { + let package = metadata + .packages + .iter() + .find(|&p| p.name == dep.name && dep.req.matches(&p.version)) + .expect("dependency does not exist"); + ( + package, + dep.rename.clone().unwrap_or_else(|| package.name.clone()), + ) + }) + // Also expose the root crate + .chain(std::iter::once((root, root.name.clone()))) + .filter_map(|(package, name)| { + // Get the id for the package matching the version requirement of the dep + let id = &package.id; + // Return the name chosen in `Cargo.toml` and the path to the corresponding artifact + match artifacts.remove(id) { + Some(artifacts) => Some((name.replace('-', "_"), artifacts)), + None => { + if name == root.name { + // If there are no artifacts, this is the root crate and it is being built as a binary/test + // instead of a library. We simply add no artifacts, meaning you can't depend on functions + // and types declared in the root crate. + None + } else { + panic!("no artifact found for `{name}`(`{id}`):`\n{artifact_output}") + } + } + } + }) + .collect(); + let import_paths = import_paths.into_iter().collect(); + return Ok(Dependencies { + dependencies, + import_paths, + }); + } + + bail!("no json found in cargo-metadata output") +} diff --git a/vendor/ui_test/src/diff.rs b/vendor/ui_test/src/diff.rs new file mode 100644 index 000000000..916645dd1 --- /dev/null +++ b/vendor/ui_test/src/diff.rs @@ -0,0 +1,175 @@ +use colored::*; +use diff::{chars, lines, Result, Result::*}; + +#[derive(Default)] +struct DiffState<'a> { + /// Whether we've already printed something, so we should print starting context, too. + print_start_context: bool, + /// When we skip lines, remember the last `CONTEXT` ones to + /// display after the "skipped N lines" message + skipped_lines: Vec<&'a str>, + /// When we see a removed line, we don't print it, we + /// keep it around to compare it with the next added line. + prev_left: Option<&'a str>, +} + +/// How many lines of context are displayed around the actual diffs +const CONTEXT: usize = 2; + +impl<'a> DiffState<'a> { + /// Print `... n lines skipped ...` followed by the last `CONTEXT` lines. + fn print_end_skip(&self, skipped: usize) { + self.print_skipped_msg(skipped); + for line in self.skipped_lines.iter().rev().take(CONTEXT).rev() { + eprintln!(" {line}"); + } + } + + fn print_skipped_msg(&self, skipped: usize) { + match skipped { + // When the amount of skipped lines is exactly `CONTEXT * 2`, we already + // print all the context and don't actually skip anything. + 0 => {} + // Instead of writing a line saying we skipped one line, print that one line + 1 => eprintln!(" {}", self.skipped_lines[CONTEXT]), + _ => eprintln!("... {skipped} lines skipped ..."), + } + } + + /// Print an initial `CONTEXT` amount of lines. + fn print_start_skip(&self) { + for line in self.skipped_lines.iter().take(CONTEXT) { + eprintln!(" {line}"); + } + } + + fn print_skip(&mut self) { + let half = self.skipped_lines.len() / 2; + if !self.print_start_context { + self.print_start_context = true; + self.print_end_skip(self.skipped_lines.len().saturating_sub(CONTEXT)); + } else if half < CONTEXT { + // Print all the skipped lines if the amount of context desired is less than the amount of lines + for line in self.skipped_lines.drain(..) { + eprintln!(" {line}"); + } + } else { + self.print_start_skip(); + let skipped = self.skipped_lines.len() - CONTEXT * 2; + self.print_end_skip(skipped); + } + self.skipped_lines.clear(); + } + + fn skip(&mut self, line: &'a str) { + self.skipped_lines.push(line); + } + + fn print_prev(&mut self) { + if let Some(l) = self.prev_left.take() { + self.print_left(l); + } + } + + fn print_left(&self, l: &str) { + eprintln!("{}{}", "-".red(), l.red()); + } + + fn print_right(&self, r: &str) { + eprintln!("{}{}", "+".green(), r.green()); + } + + fn row(&mut self, row: Result<&'a str>) { + match row { + Left(l) => { + self.print_skip(); + self.print_prev(); + self.prev_left = Some(l); + } + Both(l, _) => { + self.print_prev(); + self.skip(l); + } + Right(r) => { + // When there's an added line after a removed line, we'll want to special case some print cases. + // FIXME(oli-obk): also do special printing modes when there are multiple lines that only have minor changes. + if let Some(l) = self.prev_left.take() { + let diff = chars(l, r); + let mut seen_l = false; + let mut seen_r = false; + for char in &diff { + match char { + Left(l) if !l.is_whitespace() => seen_l = true, + Right(r) if !r.is_whitespace() => seen_r = true, + _ => {} + } + } + if seen_l && seen_r { + // The line both adds and removes chars, print both lines, but highlight their differences instead of + // drawing the entire line in red/green. + eprint!("{}", "-".red()); + for char in &diff { + match *char { + Left(l) => eprint!("{}", l.to_string().red()), + Right(_) => {} + Both(l, _) => eprint!("{l}"), + } + } + eprintln!(); + eprint!("{}", "+".green()); + for char in diff { + match char { + Left(_) => {} + Right(r) => eprint!("{}", r.to_string().green()), + Both(l, _) => eprint!("{l}"), + } + } + eprintln!(); + } else { + // The line only adds or only removes chars, print a single line highlighting their differences. + eprint!("{}", "~".yellow()); + for char in diff { + match char { + Left(l) => eprint!("{}", l.to_string().red()), + Both(l, _) => eprint!("{l}"), + Right(r) => eprint!("{}", r.to_string().green()), + } + } + eprintln!(); + } + } else { + self.print_skip(); + self.print_right(r); + } + } + } + } + + fn finish(self) { + self.print_start_skip(); + self.print_skipped_msg(self.skipped_lines.len().saturating_sub(CONTEXT)); + eprintln!() + } +} + +pub fn print_diff(expected: &[u8], actual: &[u8]) { + let expected_str = String::from_utf8_lossy(expected); + let actual_str = String::from_utf8_lossy(actual); + + if expected_str.as_bytes() != expected || actual_str.as_bytes() != actual { + eprintln!( + "{}", + "Non-UTF8 characters in output, diff may be imprecise.".red() + ); + } + + let pat = |c: char| c.is_whitespace() && c != ' ' && c != '\n' && c != '\r'; + let expected_str = expected_str.replace(pat, "░"); + let actual_str = actual_str.replace(pat, "░"); + + let mut state = DiffState::default(); + for row in lines(&expected_str, &actual_str) { + state.row(row); + } + state.finish(); +} diff --git a/vendor/ui_test/src/error.rs b/vendor/ui_test/src/error.rs new file mode 100644 index 000000000..18ce52ecc --- /dev/null +++ b/vendor/ui_test/src/error.rs @@ -0,0 +1,72 @@ +use crate::{parser::Pattern, rustc_stderr::Message, Mode}; +use std::{path::PathBuf, process::ExitStatus}; + +/// All the ways in which a test can fail. +#[derive(Debug)] +pub enum Error { + /// Got an invalid exit status for the given mode. + ExitStatus { + /// The expected mode. + mode: Mode, + /// The exit status of the command. + status: ExitStatus, + /// The expected exit status as set in the file or derived from the mode. + expected: i32, + }, + /// A pattern was declared but had no matching error. + PatternNotFound { + /// The pattern that was missing an error + pattern: Pattern, + /// The line in which the pattern was defined. + definition_line: usize, + }, + /// A ui test checking for failure does not have any failure patterns + NoPatternsFound, + /// A ui test checking for success has failure patterns + PatternFoundInPassTest, + /// Stderr/Stdout differed from the `.stderr`/`.stdout` file present. + OutputDiffers { + /// The file containing the expected output that differs from the actual output. + path: PathBuf, + /// The output from the command. + actual: Vec<u8>, + /// The contents of the file. + expected: Vec<u8>, + /// A command, that when run, causes the output to get blessed instead of erroring. + bless_command: String, + }, + /// There were errors that don't have a pattern. + ErrorsWithoutPattern { + /// The main message of the error. + msgs: Vec<Message>, + /// File and line information of the error. + path: Option<(PathBuf, usize)>, + }, + /// A comment failed to parse. + InvalidComment { + /// The comment + msg: String, + /// THe line in which it was defined. + line: usize, + }, + /// A subcommand (e.g. rustfix) of a test failed. + Command { + /// The name of the subcommand (e.g. "rustfix"). + kind: String, + /// The exit status of the command. + status: ExitStatus, + }, + /// This catches crashes of ui tests and reports them along the failed test. + Bug(String), + /// An auxiliary build failed with its own set of errors. + Aux { + /// Path to the aux file. + path: PathBuf, + /// The errors that occurred during the build of the aux file. + errors: Vec<Error>, + /// The line in which the aux file was requested to be built. + line: usize, + }, +} + +pub(crate) type Errors = Vec<Error>; diff --git a/vendor/ui_test/src/github_actions.rs b/vendor/ui_test/src/github_actions.rs new file mode 100644 index 000000000..cad9bf569 --- /dev/null +++ b/vendor/ui_test/src/github_actions.rs @@ -0,0 +1,94 @@ +//! An interface to github actions workflow commands. + +use std::fmt::{Debug, Write}; + +/// Shows an error message directly in a github diff view on drop. +pub struct Error { + file: String, + line: usize, + title: String, + message: String, +} +impl Error { + /// Set a line for this error. By default the message is shown at the top of the file. + pub fn line(mut self, line: usize) -> Self { + self.line = line; + self + } +} + +/// Create an error to be shown for the given file and with the given title. +pub fn error(file: impl std::fmt::Display, title: impl Into<String>) -> Error { + Error { + file: file.to_string(), + line: 0, + title: title.into(), + message: String::new(), + } +} + +impl Write for Error { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + self.message.write_str(s) + } +} + +impl Drop for Error { + fn drop(&mut self) { + if std::env::var_os("GITHUB_ACTION").is_some() { + let Error { + file, + line, + title, + message, + } = self; + let message = message.trim(); + let message = if message.is_empty() { + "::no message".into() + } else { + format!("::{}", github_action_multiline_escape(message)) + }; + eprintln!("::error file={file},line={line},title={title}{message}"); + eprintln!("error file={file},line={line},title={title}{message}"); + } + } +} + +/// Append to the summary file that will be shown for the entire CI run. +pub fn summary() -> Option<impl std::io::Write> { + let path = std::env::var_os("GITHUB_STEP_SUMMARY")?; + Some(std::fs::OpenOptions::new().append(true).open(path).unwrap()) +} + +fn github_action_multiline_escape(s: &str) -> String { + s.replace('%', "%25") + .replace('\n', "%0A") + .replace('\r', "%0D") +} + +/// All github actions log messages from this call to the Drop of the return value +/// will be grouped and hidden by default in logs. Note that nesting these does +/// not really work. +pub fn group(name: impl std::fmt::Display) -> Group { + if std::env::var_os("GITHUB_ACTION").is_some() { + eprintln!("::group::{name}"); + } + Group(()) +} + +/// A guard that closes the current github actions log group on drop. +pub struct Group(()); + +impl Debug for Group { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("a handle that will close the github action group on drop") + } +} + +impl Drop for Group { + fn drop(&mut self) { + if std::env::var_os("GITHUB_ACTION").is_some() { + eprintln!("::endgroup::"); + } + } +} diff --git a/vendor/ui_test/src/lib.rs b/vendor/ui_test/src/lib.rs new file mode 100644 index 000000000..7837d0e62 --- /dev/null +++ b/vendor/ui_test/src/lib.rs @@ -0,0 +1,1054 @@ +#![allow( + clippy::enum_variant_names, + clippy::useless_format, + clippy::too_many_arguments, + rustc::internal +)] +#![deny(missing_docs)] + +//! A crate to run the Rust compiler (or other binaries) and test their command line output. + +use bstr::ByteSlice; +pub use color_eyre; +use color_eyre::eyre::{eyre, Result}; +use crossbeam_channel::unbounded; +use parser::{ErrorMatch, Revisioned}; +use regex::bytes::Regex; +use rustc_stderr::{Diagnostics, Level, Message}; +use status_emitter::StatusEmitter; +use std::borrow::Cow; +use std::collections::{HashSet, VecDeque}; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::thread; + +use crate::parser::{Comments, Condition}; + +mod cmd; +mod config; +mod dependencies; +mod diff; +mod error; +pub mod github_actions; +mod mode; +mod parser; +mod rustc_stderr; +pub mod status_emitter; +#[cfg(test)] +mod tests; + +pub use cmd::*; +pub use config::*; +pub use error::*; +pub use mode::*; + +/// A filter's match rule. +#[derive(Clone, Debug)] +pub enum Match { + /// If the regex matches, the filter applies + Regex(Regex), + /// If the exact byte sequence is found, the filter applies + Exact(Vec<u8>), +} +impl Match { + fn replace_all<'a>(&self, text: &'a [u8], replacement: &[u8]) -> Cow<'a, [u8]> { + match self { + Match::Regex(regex) => regex.replace_all(text, replacement), + Match::Exact(needle) => text.replace(needle, replacement).into(), + } + } +} + +impl From<&'_ Path> for Match { + fn from(v: &Path) -> Self { + let mut v = v.display().to_string(); + // Normalize away windows canonicalized paths. + if v.starts_with(r#"\\?\"#) { + v.drain(0..4); + } + let mut v = v.into_bytes(); + // Normalize paths on windows to use slashes instead of backslashes, + // So that paths are rendered the same on all systems. + for c in &mut v { + if *c == b'\\' { + *c = b'/'; + } + } + Self::Exact(v) + } +} + +impl From<Regex> for Match { + fn from(v: Regex) -> Self { + Self::Regex(v) + } +} + +/// Replacements to apply to output files. +pub type Filter = Vec<(Match, &'static [u8])>; + +/// Run all tests as described in the config argument. +pub fn run_tests(config: Config) -> Result<()> { + eprintln!(" Compiler: {}", config.program.display()); + + let name = config.root_dir.display().to_string(); + + run_tests_generic( + config, + default_file_filter, + default_per_file_config, + (status_emitter::Text, status_emitter::Gha::<true> { name }), + ) +} + +/// The filter used by `run_tests` to only run on `.rs` files. +pub fn default_file_filter(path: &Path) -> bool { + path.extension().map(|ext| ext == "rs").unwrap_or(false) +} + +/// The default per-file config used by `run_tests`. +pub fn default_per_file_config(config: &Config, path: &Path) -> Option<Config> { + let mut config = config.clone(); + // Heuristic: + // * if the file contains `#[test]`, automatically pass `--cfg test`. + // * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`. + // This avoids having to spam `fn main() {}` in almost every test. + let file_contents = std::fs::read(path).unwrap(); + if file_contents.find(b"#[proc_macro]").is_some() + || file_contents.find(b"#[proc_macro_attribute]").is_some() + || file_contents.find(b"#[proc_macro_derive]").is_some() + { + config.program.args.push("--crate-type=proc-macro".into()) + } else if file_contents.find(b"#[test]").is_some() { + config.program.args.push("--test".into()); + } else if file_contents.find(b"fn main()").is_none() + && file_contents.find(b"#[start]").is_none() + { + config.program.args.push("--crate-type=lib".into()); + } + Some(config) +} + +/// Create a command for running a single file, with the settings from the `config` argument. +/// Ignores various settings from `Config` that relate to finding test files. +pub fn test_command(mut config: Config, path: &Path) -> Result<Command> { + config.build_dependencies_and_link_them()?; + + let comments = + Comments::parse_file(path)?.map_err(|errors| color_eyre::eyre::eyre!("{errors:#?}"))?; + let mut errors = vec![]; + let result = build_command(path, &config, "", &comments, &mut errors); + assert!(errors.is_empty(), "{errors:#?}"); + Ok(result) +} + +#[allow(clippy::large_enum_variant)] +/// The possible results a single test can have. +pub enum TestResult { + /// The test passed + Ok, + /// The test was ignored due to a rule (`//@only-*` or `//@ignore-*`) + Ignored, + /// The test was filtered with the `file_filter` argument. + Filtered, + /// The test failed. + Errored { + /// Command that failed + command: Command, + /// The errors that were encountered. + errors: Vec<Error>, + /// The full stderr of the test run. + stderr: Vec<u8>, + }, +} + +struct TestRun { + result: TestResult, + path: PathBuf, + revision: String, +} + +/// A version of `run_tests` that allows more fine-grained control over running tests. +pub fn run_tests_generic( + mut config: Config, + file_filter: impl Fn(&Path) -> bool + Sync, + per_file_config: impl Fn(&Config, &Path) -> Option<Config> + Sync, + mut status_emitter: impl StatusEmitter + Send, +) -> Result<()> { + config.fill_host_and_target()?; + + config.build_dependencies_and_link_them()?; + + // A channel for files to process + let (submit, receive) = unbounded(); + + let mut results = vec![]; + + thread::scope(|s| -> Result<()> { + // Create a thread that is in charge of walking the directory and submitting jobs. + // It closes the channel when it is done. + s.spawn(|| { + let mut todo = VecDeque::new(); + todo.push_back(config.root_dir.clone()); + while let Some(path) = todo.pop_front() { + if path.is_dir() { + if path.file_name().unwrap() == "auxiliary" { + continue; + } + // Enqueue everything inside this directory. + // We want it sorted, to have some control over scheduling of slow tests. + let mut entries = std::fs::read_dir(path) + .unwrap() + .collect::<Result<Vec<_>, _>>() + .unwrap(); + entries.sort_by_key(|e| e.file_name()); + for entry in entries { + todo.push_back(entry.path()); + } + } else if file_filter(&path) { + // Forward .rs files to the test workers. + submit.send(path).unwrap(); + } + } + // There will be no more jobs. This signals the workers to quit. + // (This also ensures `submit` is moved into this closure.) + drop(submit); + }); + + // A channel for the messages emitted by the individual test threads. + // Used to produce live updates while running the tests. + let (finished_files_sender, finished_files_recv) = unbounded::<TestRun>(); + + s.spawn(|| { + for run in finished_files_recv { + status_emitter.test_result(&run.path, &run.revision, &run.result); + + results.push(run); + } + }); + + let mut threads = vec![]; + + // Create N worker threads that receive files to test. + for _ in 0..config.num_test_threads.get() { + let finished_files_sender = finished_files_sender.clone(); + threads.push(s.spawn(|| -> Result<()> { + let finished_files_sender = finished_files_sender; + for path in &receive { + let maybe_config; + let config = match per_file_config(&config, &path) { + None => &config, + Some(config) => { + maybe_config = config; + &maybe_config + } + }; + let result = + match std::panic::catch_unwind(|| parse_and_test_file(&path, config)) { + Ok(res) => res, + Err(err) => { + finished_files_sender.send(TestRun { + result: TestResult::Errored { + command: Command::new("<unknown>"), + errors: vec![Error::Bug( + *Box::<dyn std::any::Any + Send + 'static>::downcast::< + String, + >(err) + .unwrap(), + )], + stderr: vec![], + }, + path, + revision: String::new(), + })?; + continue; + } + }; + for result in result { + finished_files_sender.send(result)?; + } + } + Ok(()) + })); + } + + for thread in threads { + thread.join().unwrap()?; + } + Ok(()) + })?; + + let mut failures = vec![]; + let mut succeeded = 0; + let mut ignored = 0; + let mut filtered = 0; + + for run in results { + match run.result { + TestResult::Ok => succeeded += 1, + TestResult::Ignored => ignored += 1, + TestResult::Filtered => filtered += 1, + TestResult::Errored { + command, + errors, + stderr, + } => failures.push((run.path, command, run.revision, errors, stderr)), + } + } + + let mut failure_emitter = status_emitter.finalize(failures.len(), succeeded, ignored, filtered); + for (path, command, revision, errors, stderr) in &failures { + let _guard = status_emitter.failed_test(revision, path, command, stderr); + failure_emitter.test_failure(path, revision, errors); + } + + if failures.is_empty() { + Ok(()) + } else { + Err(eyre!("tests failed")) + } +} + +fn parse_and_test_file(path: &Path, config: &Config) -> Vec<TestRun> { + let comments = match parse_comments_in_file(path) { + Ok(comments) => comments, + Err((stderr, errors)) => { + return vec![TestRun { + result: TestResult::Errored { + command: Command::new("parse comments"), + errors, + stderr, + }, + path: path.into(), + revision: "".into(), + }] + } + }; + // Run the test for all revisions + comments + .revisions + .clone() + .unwrap_or_else(|| vec![String::new()]) + .into_iter() + .map(|revision| { + // Ignore file if only/ignore rules do (not) apply + if !test_file_conditions(&comments, config, &revision) { + return TestRun { + result: TestResult::Ignored, + path: path.into(), + revision, + }; + } + let (command, errors, stderr) = run_test(path, config, &revision, &comments); + let result = if errors.is_empty() { + TestResult::Ok + } else { + TestResult::Errored { + command, + errors, + stderr, + } + }; + TestRun { + result, + revision, + path: path.into(), + } + }) + .collect() +} + +fn parse_comments_in_file(path: &Path) -> Result<Comments, (Vec<u8>, Vec<Error>)> { + match Comments::parse_file(path) { + Ok(Ok(comments)) => Ok(comments), + Ok(Err(errors)) => Err((vec![], errors)), + Err(err) => Err((format!("{err:?}").into(), vec![])), + } +} + +fn build_command( + path: &Path, + config: &Config, + revision: &str, + comments: &Comments, + errors: &mut Vec<Error>, +) -> Command { + let mut cmd = config.program.build(&config.out_dir); + cmd.arg(path); + if !revision.is_empty() { + cmd.arg(format!("--cfg={revision}")); + } + for arg in comments + .for_revision(revision) + .flat_map(|r| r.compile_flags.iter()) + { + cmd.arg(arg); + } + let edition = comments.edition(errors, revision, config); + if let Some((edition, _)) = edition { + cmd.arg("--edition").arg(edition); + } + cmd.args(config.trailing_args.iter()); + cmd.envs( + comments + .for_revision(revision) + .flat_map(|r| r.env_vars.iter()) + .map(|(k, v)| (k, v)), + ); + + cmd +} + +fn build_aux( + aux_file: &Path, + path: &Path, + config: &Config, + revision: &str, + comments: &Comments, + kind: &str, + aux: &Path, + extra_args: &mut Vec<String>, +) -> std::result::Result<(), (Command, Vec<Error>, Vec<u8>)> { + let comments = match parse_comments_in_file(aux_file) { + Ok(comments) => comments, + Err((msg, mut errors)) => { + return Err(( + build_command(path, config, revision, comments, &mut errors), + errors, + msg, + )) + } + }; + assert_eq!(comments.revisions, None); + + let mut config = config.clone(); + + // Strip any `crate-type` flags from the args, as we need to set our own, + // and they may conflict (e.g. `lib` vs `proc-macro`); + let mut prev_was_crate_type = false; + config.program.args.retain(|arg| { + if prev_was_crate_type { + prev_was_crate_type = false; + return false; + } + if arg == "--test" { + false + } else if arg == "--crate-type" { + prev_was_crate_type = true; + false + } else if let Some(arg) = arg.to_str() { + !arg.starts_with("--crate-type=") + } else { + true + } + }); + + // Put aux builds into a separate directory per test so that + // tests running in parallel but building the same aux build don't conflict. + // FIXME: put aux builds into the regular build queue. + config.out_dir = config.out_dir.join(path.with_extension("")); + + let mut errors = vec![]; + + let mut aux_cmd = build_command(aux_file, &config, revision, &comments, &mut errors); + + if !errors.is_empty() { + return Err((aux_cmd, errors, vec![])); + } + + let current_extra_args = + build_aux_files(aux_file, aux_file.parent().unwrap(), &comments, "", &config)?; + // Make sure we see our dependencies + aux_cmd.args(current_extra_args.iter()); + // Make sure our dependents also see our dependencies. + extra_args.extend(current_extra_args); + + aux_cmd.arg("--crate-type").arg(kind); + aux_cmd.arg("--emit=link"); + let filename = aux.file_stem().unwrap().to_str().unwrap(); + let output = aux_cmd.output().unwrap(); + if !output.status.success() { + let error = Error::Command { + kind: "compilation of aux build failed".to_string(), + status: output.status, + }; + return Err(( + aux_cmd, + vec![error], + rustc_stderr::process(path, &output.stderr).rendered, + )); + } + + // Now run the command again to fetch the output filenames + aux_cmd.arg("--print").arg("file-names"); + let output = aux_cmd.output().unwrap(); + assert!(output.status.success()); + + for file in output.stdout.lines() { + let file = std::str::from_utf8(file).unwrap(); + let crate_name = filename.replace('-', "_"); + let path = config.out_dir.join(file); + extra_args.push("--extern".into()); + extra_args.push(format!("{crate_name}={}", path.display())); + // Help cargo find the crates added with `--extern`. + extra_args.push("-L".into()); + extra_args.push(config.out_dir.display().to_string()); + } + Ok(()) +} + +fn run_test( + path: &Path, + config: &Config, + revision: &str, + comments: &Comments, +) -> (Command, Errors, Vec<u8>) { + let extra_args = match build_aux_files( + path, + &path.parent().unwrap().join("auxiliary"), + comments, + revision, + config, + ) { + Ok(value) => value, + Err(value) => return value, + }; + + let mut errors = vec![]; + + let mut cmd = build_command(path, config, revision, comments, &mut errors); + cmd.args(&extra_args); + + let output = cmd + .output() + .unwrap_or_else(|err| panic!("could not execute {cmd:?}: {err}")); + let mode = config.mode.maybe_override(comments, revision, &mut errors); + let status_check = mode.ok(output.status); + if status_check.is_empty() && matches!(mode, Mode::Run { .. }) { + let cmd = run_test_binary(mode, path, revision, comments, cmd, config, &mut errors); + return (cmd, errors, vec![]); + } + errors.extend(status_check); + if output.status.code() == Some(101) && !matches!(config.mode, Mode::Panic | Mode::Yolo) { + let stderr = String::from_utf8_lossy(&output.stderr); + let stdout = String::from_utf8_lossy(&output.stdout); + errors.push(Error::Bug(format!( + "test panicked: stderr:\n{stderr}\nstdout:\n{stdout}", + ))); + return (cmd, errors, vec![]); + } + // Always remove annotation comments from stderr. + let diagnostics = rustc_stderr::process(path, &output.stderr); + let rustfixed = matches!(mode, Mode::Fix).then(|| { + run_rustfix( + &output.stderr, + path, + comments, + revision, + config, + extra_args, + &mut errors, + ) + }); + let stderr = check_test_result( + path, + config, + revision, + comments, + &mut errors, + &output.stdout, + diagnostics, + ); + if let Some((mut rustfix, rustfix_path)) = rustfixed { + // picking the crate name from the file name is problematic when `.revision_name` is inserted + rustfix.arg("--crate-name").arg( + path.file_stem() + .unwrap() + .to_str() + .unwrap() + .replace('-', "_"), + ); + let output = rustfix.output().unwrap(); + if !output.status.success() { + errors.push(Error::Command { + kind: "rustfix".into(), + status: output.status, + }); + return ( + rustfix, + errors, + rustc_stderr::process(&rustfix_path, &output.stderr).rendered, + ); + } + } + (cmd, errors, stderr) +} + +fn build_aux_files( + path: &Path, + aux_dir: &Path, + comments: &Comments, + revision: &str, + config: &Config, +) -> Result<Vec<String>, (Command, Vec<Error>, Vec<u8>)> { + let mut extra_args = vec![]; + for rev in comments.for_revision(revision) { + for (aux, kind, line) in &rev.aux_builds { + let aux_file = if aux.starts_with("..") { + aux_dir.parent().unwrap().join(aux) + } else { + aux_dir.join(aux) + }; + if let Err((command, errors, msg)) = build_aux( + &aux_file, + path, + config, + revision, + comments, + kind, + aux, + &mut extra_args, + ) { + return Err(( + command, + vec![Error::Aux { + path: aux_file, + errors, + line: *line, + }], + msg, + )); + } + } + } + Ok(extra_args) +} + +fn run_test_binary( + mode: Mode, + path: &Path, + revision: &str, + comments: &Comments, + mut cmd: Command, + config: &Config, + errors: &mut Vec<Error>, +) -> Command { + cmd.arg("--print").arg("file-names"); + let output = cmd.output().unwrap(); + assert!(output.status.success()); + + let mut files = output.stdout.lines(); + let file = files.next().unwrap(); + assert_eq!(files.next(), None); + let file = std::str::from_utf8(file).unwrap(); + let exe = config.out_dir.join(file); + let mut exe = Command::new(exe); + let output = exe.output().unwrap(); + + check_test_output( + path, + errors, + revision, + config, + comments, + &output.stdout, + &output.stderr, + ); + + errors.extend(mode.ok(output.status)); + + exe +} + +fn run_rustfix( + stderr: &[u8], + path: &Path, + comments: &Comments, + revision: &str, + config: &Config, + extra_args: Vec<String>, + errors: &mut Vec<Error>, +) -> (Command, PathBuf) { + let input = std::str::from_utf8(stderr).unwrap(); + let suggestions = rustfix::get_suggestions_from_json( + input, + &HashSet::new(), + if let Mode::Yolo = config.mode { + rustfix::Filter::Everything + } else { + rustfix::Filter::MachineApplicableOnly + }, + ) + .unwrap_or_else(|err| { + panic!("could not deserialize diagnostics json for rustfix {err}:{input}") + }); + let fixed_code = + rustfix::apply_suggestions(&std::fs::read_to_string(path).unwrap(), &suggestions) + .unwrap_or_else(|e| { + panic!( + "failed to apply suggestions for {:?} with rustfix: {e}", + path.display() + ) + }); + let edition = comments.edition(errors, revision, config); + let rustfix_comments = Comments { + revisions: None, + revisioned: std::iter::once(( + vec![], + Revisioned { + ignore: vec![], + only: vec![], + stderr_per_bitwidth: false, + compile_flags: comments + .for_revision(revision) + .flat_map(|r| r.compile_flags.iter().cloned()) + .collect(), + env_vars: comments + .for_revision(revision) + .flat_map(|r| r.env_vars.iter().cloned()) + .collect(), + normalize_stderr: vec![], + error_in_other_files: vec![], + error_matches: vec![], + require_annotations_for_level: None, + aux_builds: comments + .for_revision(revision) + .flat_map(|r| r.aux_builds.iter().cloned()) + .collect(), + edition, + mode: Some((Mode::Pass, 0)), + needs_asm_support: false, + }, + )) + .collect(), + }; + let path = check_output( + fixed_code.as_bytes(), + path, + errors, + revised(revision, "fixed"), + &Filter::default(), + config, + &rustfix_comments, + revision, + ); + + let mut cmd = build_command(&path, config, revision, &rustfix_comments, errors); + cmd.args(extra_args); + (cmd, path) +} + +fn revised(revision: &str, extension: &str) -> String { + if revision.is_empty() { + extension.to_string() + } else { + format!("{revision}.{extension}") + } +} + +fn check_test_result( + path: &Path, + config: &Config, + revision: &str, + comments: &Comments, + errors: &mut Errors, + stdout: &[u8], + diagnostics: Diagnostics, +) -> Vec<u8> { + check_test_output( + path, + errors, + revision, + config, + comments, + stdout, + &diagnostics.rendered, + ); + // Check error annotations in the source against output + check_annotations( + diagnostics.messages, + diagnostics.messages_from_unknown_file_or_line, + path, + errors, + config, + revision, + comments, + ); + diagnostics.rendered +} + +fn check_test_output( + path: &Path, + errors: &mut Vec<Error>, + revision: &str, + config: &Config, + comments: &Comments, + stdout: &[u8], + stderr: &[u8], +) { + // Check output files (if any) + // Check output files against actual output + check_output( + stderr, + path, + errors, + revised(revision, "stderr"), + &config.stderr_filters, + config, + comments, + revision, + ); + check_output( + stdout, + path, + errors, + revised(revision, "stdout"), + &config.stdout_filters, + config, + comments, + revision, + ); +} + +fn check_annotations( + mut messages: Vec<Vec<Message>>, + mut messages_from_unknown_file_or_line: Vec<Message>, + path: &Path, + errors: &mut Errors, + config: &Config, + revision: &str, + comments: &Comments, +) { + let error_patterns = comments + .for_revision(revision) + .flat_map(|r| r.error_in_other_files.iter()); + + let mut seen_error_match = false; + for (error_pattern, definition_line) in error_patterns { + seen_error_match = true; + // first check the diagnostics messages outside of our file. We check this first, so that + // you can mix in-file annotations with //@error-in-other-file annotations, even if there is overlap + // in the messages. + if let Some(i) = messages_from_unknown_file_or_line + .iter() + .position(|msg| error_pattern.matches(&msg.message)) + { + messages_from_unknown_file_or_line.remove(i); + } else { + errors.push(Error::PatternNotFound { + pattern: error_pattern.clone(), + definition_line: *definition_line, + }); + } + } + + // The order on `Level` is such that `Error` is the highest level. + // We will ensure that *all* diagnostics of level at least `lowest_annotation_level` + // are matched. + let mut lowest_annotation_level = Level::Error; + for &ErrorMatch { + ref pattern, + definition_line, + line, + level, + } in comments + .for_revision(revision) + .flat_map(|r| r.error_matches.iter()) + { + seen_error_match = true; + // If we found a diagnostic with a level annotation, make sure that all + // diagnostics of that level have annotations, even if we don't end up finding a matching diagnostic + // for this pattern. + lowest_annotation_level = std::cmp::min(lowest_annotation_level, level); + + if let Some(msgs) = messages.get_mut(line) { + let found = msgs + .iter() + .position(|msg| pattern.matches(&msg.message) && msg.level == level); + if let Some(found) = found { + msgs.remove(found); + continue; + } + } + + errors.push(Error::PatternNotFound { + pattern: pattern.clone(), + definition_line, + }); + } + + let required_annotation_level = comments + .find_one_for_revision( + revision, + |r| r.require_annotations_for_level, + |_| { + errors.push(Error::InvalidComment { + msg: "`require_annotations_for_level` specified twice for same revision".into(), + line: 0, + }) + }, + ) + .unwrap_or(lowest_annotation_level); + let filter = |mut msgs: Vec<Message>| -> Vec<_> { + msgs.retain(|msg| msg.level >= required_annotation_level); + msgs + }; + + let mode = config.mode.maybe_override(comments, revision, errors); + + if !matches!(config.mode, Mode::Yolo) { + let messages_from_unknown_file_or_line = filter(messages_from_unknown_file_or_line); + if !messages_from_unknown_file_or_line.is_empty() { + errors.push(Error::ErrorsWithoutPattern { + path: None, + msgs: messages_from_unknown_file_or_line, + }); + } + + for (line, msgs) in messages.into_iter().enumerate() { + let msgs = filter(msgs); + if !msgs.is_empty() { + errors.push(Error::ErrorsWithoutPattern { + path: Some((path.to_path_buf(), line)), + msgs, + }); + } + } + } + + match (mode, seen_error_match) { + (Mode::Pass, true) | (Mode::Panic, true) => errors.push(Error::PatternFoundInPassTest), + ( + Mode::Fail { + require_patterns: true, + }, + false, + ) => errors.push(Error::NoPatternsFound), + _ => {} + } +} + +fn check_output( + output: &[u8], + path: &Path, + errors: &mut Errors, + kind: String, + filters: &Filter, + config: &Config, + comments: &Comments, + revision: &str, +) -> PathBuf { + let target = config.target.as_ref().unwrap(); + let output = normalize(path, output, filters, comments, revision); + let path = output_path(path, comments, kind, target, revision); + match &config.output_conflict_handling { + OutputConflictHandling::Bless => { + if output.is_empty() { + let _ = std::fs::remove_file(&path); + } else { + std::fs::write(&path, &output).unwrap(); + } + } + OutputConflictHandling::Error(bless_command) => { + let expected_output = std::fs::read(&path).unwrap_or_default(); + if output != expected_output { + errors.push(Error::OutputDiffers { + path: path.clone(), + actual: output, + expected: expected_output, + bless_command: bless_command.clone(), + }); + } + } + OutputConflictHandling::Ignore => {} + } + path +} + +fn output_path( + path: &Path, + comments: &Comments, + kind: String, + target: &str, + revision: &str, +) -> PathBuf { + if comments + .for_revision(revision) + .any(|r| r.stderr_per_bitwidth) + { + return path.with_extension(format!("{}bit.{kind}", get_pointer_width(target))); + } + path.with_extension(kind) +} + +fn test_condition(condition: &Condition, config: &Config) -> bool { + let target = config.target.as_ref().unwrap(); + match condition { + Condition::Bitwidth(bits) => get_pointer_width(target) == *bits, + Condition::Target(t) => target.contains(t), + Condition::Host(t) => config.host.as_ref().unwrap().contains(t), + Condition::OnHost => target == config.host.as_ref().unwrap(), + } +} + +/// Returns whether according to the in-file conditions, this file should be run. +fn test_file_conditions(comments: &Comments, config: &Config, revision: &str) -> bool { + if comments + .for_revision(revision) + .flat_map(|r| r.ignore.iter()) + .any(|c| test_condition(c, config)) + { + return false; + } + if comments + .for_revision(revision) + .any(|r| r.needs_asm_support && !config.has_asm_support()) + { + return false; + } + comments + .for_revision(revision) + .flat_map(|r| r.only.iter()) + .all(|c| test_condition(c, config)) +} + +// Taken 1:1 from compiletest-rs +fn get_pointer_width(triple: &str) -> u8 { + if (triple.contains("64") && !triple.ends_with("gnux32") && !triple.ends_with("gnu_ilp32")) + || triple.starts_with("s390x") + { + 64 + } else if triple.starts_with("avr") { + 16 + } else { + 32 + } +} + +fn normalize( + path: &Path, + text: &[u8], + filters: &Filter, + comments: &Comments, + revision: &str, +) -> Vec<u8> { + // Useless paths + let path_filter = (Match::from(path.parent().unwrap()), b"$DIR" as &[u8]); + let filters = filters.iter().chain(std::iter::once(&path_filter)); + let mut text = text.to_owned(); + if let Some(lib_path) = option_env!("RUSTC_LIB_PATH") { + text = text.replace(lib_path, "RUSTLIB"); + } + + for (regex, replacement) in filters { + text = regex.replace_all(&text, replacement).into_owned(); + } + + for (from, to) in comments + .for_revision(revision) + .flat_map(|r| r.normalize_stderr.iter()) + { + text = from.replace_all(&text, to).into_owned(); + } + text +} diff --git a/vendor/ui_test/src/mode.rs b/vendor/ui_test/src/mode.rs new file mode 100644 index 000000000..9c4ce8c60 --- /dev/null +++ b/vendor/ui_test/src/mode.rs @@ -0,0 +1,84 @@ +use super::Error; +use super::Errors; +use crate::parser::Comments; +use std::fmt::Display; +use std::process::ExitStatus; + +#[derive(Copy, Clone, Debug)] +/// Decides what is expected of each test's exit status. +pub enum Mode { + /// The test fails with an error, but passes after running rustfix + Fix, + /// The test passes a full execution of the rustc driver + Pass, + /// The test produces an executable binary that can get executed on the host + Run { + /// The expected exit code + exit_code: i32, + }, + /// The rustc driver panicked + Panic, + /// The rustc driver emitted an error + Fail { + /// Whether failing tests must have error patterns. Set to false if you just care about .stderr output. + require_patterns: bool, + }, + /// Run the tests, but always pass them as long as all annotations are satisfied and stderr files match. + Yolo, +} + +impl Mode { + pub(crate) fn ok(self, status: ExitStatus) -> Errors { + let expected = match self { + Mode::Run { exit_code } => exit_code, + Mode::Pass => 0, + Mode::Panic => 101, + Mode::Fail { .. } => 1, + Mode::Fix | Mode::Yolo => return vec![], + }; + if status.code() == Some(expected) { + vec![] + } else { + vec![Error::ExitStatus { + mode: self, + status, + expected, + }] + } + } + pub(crate) fn maybe_override( + self, + comments: &Comments, + revision: &str, + errors: &mut Vec<Error>, + ) -> Self { + comments + .find_one_for_revision( + revision, + |r| r.mode.as_ref(), + |&(_, line)| { + errors.push(Error::InvalidComment { + msg: "multiple mode changes found".into(), + line, + }) + }, + ) + .map(|&(mode, _)| mode) + .unwrap_or(self) + } +} + +impl Display for Mode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Mode::Run { exit_code } => write!(f, "run({exit_code})"), + Mode::Pass => write!(f, "pass"), + Mode::Panic => write!(f, "panic"), + Mode::Fail { + require_patterns: _, + } => write!(f, "fail"), + Mode::Yolo => write!(f, "yolo"), + Mode::Fix => write!(f, "fix"), + } + } +} diff --git a/vendor/ui_test/src/parser.rs b/vendor/ui_test/src/parser.rs new file mode 100644 index 000000000..d2b510e1b --- /dev/null +++ b/vendor/ui_test/src/parser.rs @@ -0,0 +1,648 @@ +use std::{ + collections::HashMap, + path::{Path, PathBuf}, +}; + +use bstr::{ByteSlice, Utf8Error}; +use regex::bytes::Regex; + +use crate::{rustc_stderr::Level, Error, Mode}; + +use color_eyre::eyre::{Context, Result}; + +#[cfg(test)] +mod tests; + +/// This crate supports various magic comments that get parsed as file-specific +/// configuration values. This struct parses them all in one go and then they +/// get processed by their respective use sites. +#[derive(Default, Debug)] +pub(crate) struct Comments { + /// List of revision names to execute. Can only be speicified once + pub revisions: Option<Vec<String>>, + /// Comments that are only available under specific revisions. + /// The defaults are in key `vec![]` + pub revisioned: HashMap<Vec<String>, Revisioned>, +} + +impl Comments { + /// Check that a comment isn't specified twice across multiple differently revisioned statements. + /// e.g. `//@[foo, bar] error-in-other-file: bop` and `//@[foo, baz] error-in-other-file boop` would end up + /// specifying two error patterns that are available in revision `foo`. + pub fn find_one_for_revision<'a, T: 'a>( + &'a self, + revision: &'a str, + f: impl Fn(&'a Revisioned) -> Option<T>, + error: impl FnOnce(T), + ) -> Option<T> { + let mut rev = self.for_revision(revision).filter_map(f); + let result = rev.next(); + if let Some(next) = rev.next() { + error(next); + } + result + } + + /// Returns an iterator over all revisioned comments that match the revision. + pub fn for_revision<'a>(&'a self, revision: &'a str) -> impl Iterator<Item = &'a Revisioned> { + self.revisioned.iter().filter_map(move |(k, v)| { + if k.is_empty() || k.iter().any(|rev| rev == revision) { + Some(v) + } else { + None + } + }) + } + + pub(crate) fn edition( + &self, + errors: &mut Vec<Error>, + revision: &str, + config: &crate::Config, + ) -> Option<(String, usize)> { + self.find_one_for_revision( + revision, + |r| r.edition.as_ref(), + |&(_, line)| { + errors.push(Error::InvalidComment { + msg: "`edition` specified twice".into(), + line, + }) + }, + ) + .cloned() + .or(config.edition.clone().map(|e| (e, 0))) + } +} + +#[derive(Default, Debug)] +/// Comments that can be filtered for specific revisions. +pub(crate) struct Revisioned { + /// Don't run this test if any of these filters apply + pub ignore: Vec<Condition>, + /// Only run this test if all of these filters apply + pub only: Vec<Condition>, + /// Generate one .stderr file per bit width, by prepending with `.64bit` and similar + pub stderr_per_bitwidth: bool, + /// Additional flags to pass to the executable + pub compile_flags: Vec<String>, + /// Additional env vars to set for the executable + pub env_vars: Vec<(String, String)>, + /// Normalizations to apply to the stderr output before emitting it to disk + pub normalize_stderr: Vec<(Regex, Vec<u8>)>, + /// Arbitrary patterns to look for in the stderr. + /// The error must be from another file, as errors from the current file must be + /// checked via `error_matches`. + pub error_in_other_files: Vec<(Pattern, usize)>, + pub error_matches: Vec<ErrorMatch>, + /// Ignore diagnostics below this level. + /// `None` means pick the lowest level from the `error_pattern`s. + pub require_annotations_for_level: Option<Level>, + pub aux_builds: Vec<(PathBuf, String, usize)>, + pub edition: Option<(String, usize)>, + /// Overwrites the mode from `Config`. + pub mode: Option<(Mode, usize)>, + pub needs_asm_support: bool, +} + +#[derive(Debug)] +struct CommentParser<T> { + /// The comments being built. + comments: T, + /// Any errors that ocurred during comment parsing. + errors: Vec<Error>, + /// The line currently being parsed. + line: usize, +} + +impl<T> std::ops::Deref for CommentParser<T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.comments + } +} + +impl<T> std::ops::DerefMut for CommentParser<T> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.comments + } +} + +/// The conditions used for "ignore" and "only" filters. +#[derive(Debug)] +pub(crate) enum Condition { + /// The given string must appear in the host triple. + Host(String), + /// The given string must appear in the target triple. + Target(String), + /// Tests that the bitwidth is the given one. + Bitwidth(u8), + /// Tests that the target is the host. + OnHost, +} + +#[derive(Debug, Clone)] +/// An error pattern parsed from a `//~` comment. +pub enum Pattern { + SubString(String), + Regex(Regex), +} + +#[derive(Debug)] +pub(crate) struct ErrorMatch { + pub pattern: Pattern, + pub level: Level, + /// The line where the message was defined, for reporting issues with it (e.g. in case it wasn't found). + pub definition_line: usize, + /// The line this pattern is expecting to find a message in. + pub line: usize, +} + +impl Condition { + fn parse(c: &str) -> std::result::Result<Self, String> { + if c == "on-host" { + Ok(Condition::OnHost) + } else if let Some(bits) = c.strip_suffix("bit") { + let bits: u8 = bits.parse().map_err(|_err| { + format!("invalid ignore/only filter ending in 'bit': {c:?} is not a valid bitwdith") + })?; + Ok(Condition::Bitwidth(bits)) + } else if let Some(triple_substr) = c.strip_prefix("target-") { + Ok(Condition::Target(triple_substr.to_owned())) + } else if let Some(triple_substr) = c.strip_prefix("host-") { + Ok(Condition::Host(triple_substr.to_owned())) + } else { + Err(format!( + "`{c}` is not a valid condition, expected `on-host`, /[0-9]+bit/, /host-.*/, or /target-.*/" + )) + } + } +} + +impl Comments { + pub(crate) fn parse_file(path: &Path) -> Result<std::result::Result<Self, Vec<Error>>> { + let content = + std::fs::read(path).wrap_err_with(|| format!("failed to read {}", path.display()))?; + Ok(Self::parse(&content)) + } + + /// Parse comments in `content`. + /// `path` is only used to emit diagnostics if parsing fails. + pub(crate) fn parse( + content: &(impl AsRef<[u8]> + ?Sized), + ) -> std::result::Result<Self, Vec<Error>> { + let mut parser = CommentParser { + comments: Comments::default(), + errors: vec![], + line: 0, + }; + + let mut fallthrough_to = None; // The line that a `|` will refer to. + for (l, line) in content.as_ref().lines().enumerate() { + let l = l + 1; // enumerate starts at 0, but line numbers start at 1 + parser.line = l; + match parser.parse_checked_line(&mut fallthrough_to, line) { + Ok(()) => {} + Err(e) => parser.errors.push(Error::InvalidComment { + msg: format!("Comment is not utf8: {e:?}"), + line: l, + }), + } + } + if parser.errors.is_empty() { + Ok(parser.comments) + } else { + Err(parser.errors) + } + } +} + +impl CommentParser<Comments> { + fn parse_checked_line( + &mut self, + fallthrough_to: &mut Option<usize>, + line: &[u8], + ) -> std::result::Result<(), Utf8Error> { + if let Some(command) = line.strip_prefix(b"//@") { + self.parse_command(command.trim().to_str()?) + } else if let Some((_, pattern)) = line.split_once_str("//~") { + let (revisions, pattern) = self.parse_revisions(pattern.to_str()?); + self.revisioned(revisions, |this| { + this.parse_pattern(pattern, fallthrough_to) + }) + } else { + *fallthrough_to = None; + for pos in line.find_iter("//") { + let rest = &line[pos + 2..]; + for rest in std::iter::once(rest).chain(rest.strip_prefix(b" ")) { + if let Some('@' | '~' | '[' | ']' | '^' | '|') = rest.chars().next() { + self.errors.push(Error::InvalidComment { + msg: format!( + "comment looks suspiciously like a test suite command: `{}`\n\ + All `//@` test suite commands must be at the start of the line.\n\ + The `//` must be directly followed by `@` or `~`.", + rest.to_str()?, + ), + line: self.line, + }) + } else { + let mut parser = Self { + line: 0, + errors: vec![], + comments: Comments::default(), + }; + parser.parse_command(rest.to_str()?); + if parser.errors.is_empty() { + self.errors.push(Error::InvalidComment { + msg: "a compiletest-rs style comment was detected.\n\ + Please use text that could not also be interpreted as a command,\n\ + and prefix all actual commands with `//@`" + .into(), + line: self.line, + }); + } + } + } + } + } + Ok(()) + } +} + +impl<CommentsType> CommentParser<CommentsType> { + fn error(&mut self, s: impl Into<String>) { + self.errors.push(Error::InvalidComment { + msg: s.into(), + line: self.line, + }); + } + + fn check(&mut self, cond: bool, s: impl Into<String>) { + if !cond { + self.error(s); + } + } + + fn check_some<T>(&mut self, opt: Option<T>, s: impl Into<String>) -> Option<T> { + self.check(opt.is_some(), s); + opt + } +} + +impl CommentParser<Comments> { + fn parse_command(&mut self, command: &str) { + let (revisions, command) = self.parse_revisions(command); + + // Commands are letters or dashes, grab everything until the first character that is neither of those. + let (command, args) = match command + .char_indices() + .find_map(|(i, c)| (!c.is_alphanumeric() && c != '-' && c != '_').then_some(i)) + { + None => (command, ""), + Some(i) => { + let (command, args) = command.split_at(i); + let mut args = args.chars(); + // Commands are separated from their arguments by ':' or ' ' + let next = args + .next() + .expect("the `position` above guarantees that there is at least one char"); + self.check( + next == ':', + "test command must be followed by `:` (or end the line)", + ); + (command, args.as_str().trim()) + } + }; + + if command == "revisions" { + self.check( + revisions.is_empty(), + "revisions cannot be declared under a revision", + ); + self.check(self.revisions.is_none(), "cannot specify `revisions` twice"); + self.revisions = Some(args.split_whitespace().map(|s| s.to_string()).collect()); + return; + } + self.revisioned(revisions, |this| this.parse_command(command, args)); + } + + fn revisioned( + &mut self, + revisions: Vec<String>, + f: impl FnOnce(&mut CommentParser<&mut Revisioned>), + ) { + let mut this = CommentParser { + errors: std::mem::take(&mut self.errors), + line: self.line, + comments: self.revisioned.entry(revisions).or_default(), + }; + f(&mut this); + self.errors = this.errors; + } +} + +impl CommentParser<&mut Revisioned> { + fn parse_command(&mut self, command: &str, args: &str) { + match command { + "compile-flags" => { + self.compile_flags + .extend(args.split_whitespace().map(|s| s.to_string())); + } + "rustc-env" => { + for env in args.split_whitespace() { + if let Some((k, v)) = self.check_some( + env.split_once('='), + "environment variables must be key/value pairs separated by a `=`", + ) { + self.env_vars.push((k.to_string(), v.to_string())); + } + } + } + "normalize-stderr-test" => { + let (from, rest) = self.parse_str(args); + + let to = match rest.strip_prefix("->") { + Some(v) => v, + None => { + self.error("normalize-stderr-test needs a pattern and replacement separated by `->`"); + return; + }, + }.trim_start(); + let (to, rest) = self.parse_str(to); + + self.check( + rest.is_empty(), + format!("trailing text after pattern replacement: {rest}"), + ); + + if let Some(regex) = self.parse_regex(from) { + self.normalize_stderr + .push((regex, to.as_bytes().to_owned())) + } + } + "error-pattern" => { + self.error("`error-pattern` has been renamed to `error-in-other-file`"); + } + "error-in-other-file" => { + let pat = self.parse_error_pattern(args.trim()); + let line = self.line; + self.error_in_other_files.push((pat, line)); + } + "stderr-per-bitwidth" => { + // args are ignored (can be used as comment) + self.check( + !self.stderr_per_bitwidth, + "cannot specify `stderr-per-bitwidth` twice", + ); + self.stderr_per_bitwidth = true; + } + "run-rustfix" => { + // args are ignored (can be used as comment) + self.check( + self.mode.is_none(), + "cannot specify test mode changes twice", + ); + self.mode = Some((Mode::Fix, self.line)) + } + "needs-asm-support" => { + // args are ignored (can be used as comment) + self.check( + !self.needs_asm_support, + "cannot specify `needs-asm-support` twice", + ); + self.needs_asm_support = true; + } + "aux-build" => { + let (name, kind) = args.split_once(':').unwrap_or((args, "lib")); + let line = self.line; + self.aux_builds.push((name.into(), kind.into(), line)); + } + "edition" => { + self.check(self.edition.is_none(), "cannot specify `edition` twice"); + self.edition = Some((args.into(), self.line)) + } + "check-pass" => { + // args are ignored (can be used as comment) + self.check( + self.mode.is_none(), + "cannot specify test mode changes twice", + ); + self.mode = Some((Mode::Pass, self.line)) + } + "run" => { + self.check( + self.mode.is_none(), + "cannot specify test mode changes twice", + ); + let mut set = |exit_code| self.mode = Some((Mode::Run { exit_code }, self.line)); + if args.is_empty() { + set(0); + } else { + match args.parse() { + Ok(exit_code) => set(exit_code), + Err(err) => self.error(err.to_string()), + } + } + } + "require-annotations-for-level" => { + self.check( + self.require_annotations_for_level.is_none(), + "cannot specify `require-annotations-for-level` twice", + ); + match args.trim().parse() { + Ok(it) => self.require_annotations_for_level = Some(it), + Err(msg) => self.error(msg), + } + } + command => { + if let Some(s) = command.strip_prefix("ignore-") { + // args are ignored (can be used as comment) + match Condition::parse(s) { + Ok(cond) => self.ignore.push(cond), + Err(msg) => self.error(msg), + } + return; + } + + if let Some(s) = command.strip_prefix("only-") { + // args are ignored (can be used as comment) + match Condition::parse(s) { + Ok(cond) => self.only.push(cond), + Err(msg) => self.error(msg), + } + return; + } + self.error(format!("unknown command `{command}`")); + } + } + } +} + +impl<CommentsType> CommentParser<CommentsType> { + fn parse_regex(&mut self, regex: &str) -> Option<Regex> { + match Regex::new(regex) { + Ok(regex) => Some(regex), + Err(err) => { + self.error(format!("invalid regex: {err:?}")); + None + } + } + } + + /// Parses a string literal. `s` has to start with `"`; everything until the next `"` is + /// returned in the first component. `\` can be used to escape arbitrary character. + /// Second return component is the rest of the string with leading whitespace removed. + fn parse_str<'a>(&mut self, s: &'a str) -> (&'a str, &'a str) { + let mut chars = s.char_indices(); + match chars.next() { + Some((_, '"')) => { + let s = chars.as_str(); + let mut escaped = false; + for (i, c) in chars { + if escaped { + // Accept any character as literal after a `\`. + escaped = false; + } else if c == '"' { + return (&s[..(i - 1)], s[i..].trim_start()); + } else { + escaped = c == '\\'; + } + } + self.error(format!("no closing quotes found for {s}")); + (s, "") + } + Some((_, c)) => { + self.error(format!("expected `\"`, got `{c}`")); + (s, "") + } + None => { + self.error("expected quoted string, but found end of line"); + (s, "") + } + } + } + + // parse something like \[[a-z]+(,[a-z]+)*\] + fn parse_revisions<'a>(&mut self, pattern: &'a str) -> (Vec<String>, &'a str) { + match pattern.chars().next() { + Some('[') => { + // revisions + let s = &pattern[1..]; + let end = s.char_indices().find_map(|(i, c)| match c { + ']' => Some(i), + _ => None, + }); + let Some(end) = end else { + self.error("`[` without corresponding `]`"); + return (vec![], pattern); + }; + let (revision, pattern) = s.split_at(end); + ( + revision.split(',').map(|s| s.trim().to_string()).collect(), + // 1.. because `split_at` includes the separator + pattern[1..].trim_start(), + ) + } + _ => (vec![], pattern), + } + } +} + +impl CommentParser<&mut Revisioned> { + // parse something like (\[[a-z]+(,[a-z]+)*\])?(?P<offset>\||[\^]+)? *(?P<level>ERROR|HELP|WARN|NOTE): (?P<text>.*) + fn parse_pattern(&mut self, pattern: &str, fallthrough_to: &mut Option<usize>) { + let (match_line, pattern) = match pattern.chars().next() { + Some('|') => ( + match fallthrough_to { + Some(fallthrough) => *fallthrough, + None => { + self.error("`//~|` pattern without preceding line"); + return; + } + }, + &pattern[1..], + ), + Some('^') => { + let offset = pattern.chars().take_while(|&c| c == '^').count(); + (self.line - offset, &pattern[offset..]) + } + Some(_) => (self.line, pattern), + None => { + self.error("no pattern specified"); + return; + } + }; + + let pattern = pattern.trim_start(); + let offset = match pattern.chars().position(|c| !c.is_ascii_alphabetic()) { + Some(offset) => offset, + None => { + self.error("pattern without level"); + return; + } + }; + + let level = match pattern[..offset].parse() { + Ok(level) => level, + Err(msg) => { + self.error(msg); + return; + } + }; + let pattern = &pattern[offset..]; + let pattern = match pattern.strip_prefix(':') { + Some(offset) => offset, + None => { + self.error("no `:` after level found"); + return; + } + }; + + let pattern = pattern.trim(); + + self.check(!pattern.is_empty(), "no pattern specified"); + + let pattern = self.parse_error_pattern(pattern); + + *fallthrough_to = Some(match_line); + + let definition_line = self.line; + self.error_matches.push(ErrorMatch { + pattern, + level, + definition_line, + line: match_line, + }); + } +} + +impl Pattern { + pub(crate) fn matches(&self, message: &str) -> bool { + match self { + Pattern::SubString(s) => message.contains(s), + Pattern::Regex(r) => r.is_match(message.as_bytes()), + } + } +} + +impl<CommentsType> CommentParser<CommentsType> { + fn parse_error_pattern(&mut self, pattern: &str) -> Pattern { + if let Some(regex) = pattern.strip_prefix('/') { + match regex.strip_suffix('/') { + Some(regex) => match self.parse_regex(regex) { + Some(regex) => Pattern::Regex(regex), + None => Pattern::SubString(pattern.to_string()), + }, + None => { + self.error( + "expected regex pattern due to leading `/`, but found no closing `/`", + ); + Pattern::SubString(pattern.to_string()) + } + } + } else { + Pattern::SubString(pattern.to_string()) + } + } +} diff --git a/vendor/ui_test/src/parser/tests.rs b/vendor/ui_test/src/parser/tests.rs new file mode 100644 index 000000000..1263d28ce --- /dev/null +++ b/vendor/ui_test/src/parser/tests.rs @@ -0,0 +1,137 @@ +use crate::{ + parser::{Condition, Pattern}, + Error, +}; + +use super::Comments; + +#[test] +fn parse_simple_comment() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address $HEX is unallocated) +} + "; + let comments = Comments::parse(s).unwrap(); + println!("parsed comments: {:#?}", comments); + assert_eq!(comments.revisioned.len(), 1); + let revisioned = &comments.revisioned[&vec![]]; + assert_eq!(revisioned.error_matches[0].definition_line, 5); + match &revisioned.error_matches[0].pattern { + Pattern::SubString(s) => { + assert_eq!( + s, + "encountered a dangling reference (address $HEX is unallocated)" + ) + } + other => panic!("expected substring, got {other:?}"), + } +} + +#[test] +fn parse_missing_level() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ encountered a dangling reference (address $HEX is unallocated) +} + "; + let errors = Comments::parse(s).unwrap_err(); + println!("parsed comments: {:#?}", errors); + assert_eq!(errors.len(), 1); + match &errors[0] { + Error::InvalidComment { msg, line: 5 } => assert_eq!(msg, "unknown level `encountered`"), + _ => unreachable!(), + } +} + +#[test] +fn parse_slash_slash_at() { + let s = r" +//@ error-in-other-file: foomp +use std::mem; + + "; + let comments = Comments::parse(s).unwrap(); + println!("parsed comments: {:#?}", comments); + assert_eq!(comments.revisioned.len(), 1); + let revisioned = &comments.revisioned[&vec![]]; + let pat = &revisioned.error_in_other_files[0]; + assert_eq!(format!("{:?}", pat.0), r#"SubString("foomp")"#); + assert_eq!(pat.1, 2); +} + +#[test] +fn parse_regex_error_pattern() { + let s = r" +//@ error-in-other-file: /foomp/ +use std::mem; + + "; + let comments = Comments::parse(s).unwrap(); + println!("parsed comments: {:#?}", comments); + assert_eq!(comments.revisioned.len(), 1); + let revisioned = &comments.revisioned[&vec![]]; + let pat = &revisioned.error_in_other_files[0]; + assert_eq!(format!("{:?}", pat.0), r#"Regex(foomp)"#); + assert_eq!(pat.1, 2); +} + +#[test] +fn parse_slash_slash_at_fail() { + let s = r" +//@ error-patttern foomp +use std::mem; + + "; + let errors = Comments::parse(s).unwrap_err(); + println!("parsed comments: {:#?}", errors); + assert_eq!(errors.len(), 2); + match &errors[0] { + Error::InvalidComment { msg, line: 2 } => { + assert!(msg.contains("must be followed by `:`")) + } + _ => unreachable!(), + } + match &errors[1] { + Error::InvalidComment { msg, line: 2 } => { + assert_eq!(msg, "unknown command `error-patttern`"); + } + _ => unreachable!(), + } +} + +#[test] +fn missing_colon_fail() { + let s = r" +//@stderr-per-bitwidth hello +use std::mem; + + "; + let errors = Comments::parse(s).unwrap_err(); + println!("parsed comments: {:#?}", errors); + assert_eq!(errors.len(), 1); + match &errors[0] { + Error::InvalidComment { msg, line: 2 } => { + assert!(msg.contains("must be followed by `:`")) + } + _ => unreachable!(), + } +} + +#[test] +fn parse_x86_64() { + let s = r"//@ only-target-x86_64-unknown-linux"; + let comments = Comments::parse(s).unwrap(); + println!("parsed comments: {:#?}", comments); + assert_eq!(comments.revisioned.len(), 1); + let revisioned = &comments.revisioned[&vec![]]; + assert_eq!(revisioned.only.len(), 1); + match &revisioned.only[0] { + Condition::Target(t) => assert_eq!(t, "x86_64-unknown-linux"), + _ => unreachable!(), + } +} diff --git a/vendor/ui_test/src/rustc_stderr.rs b/vendor/ui_test/src/rustc_stderr.rs new file mode 100644 index 000000000..64e5928c5 --- /dev/null +++ b/vendor/ui_test/src/rustc_stderr.rs @@ -0,0 +1,160 @@ +use std::path::{Path, PathBuf}; + +use bstr::ByteSlice; +use regex::Regex; + +#[derive(serde::Deserialize, Debug)] +struct RustcMessage { + rendered: Option<String>, + spans: Vec<Span>, + level: String, + message: String, + children: Vec<RustcMessage>, +} + +#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] +pub(crate) enum Level { + Ice = 5, + Error = 4, + Warn = 3, + Help = 2, + Note = 1, + /// Only used for "For more information about this error, try `rustc --explain EXXXX`". + FailureNote = 0, +} + +#[derive(Debug)] +/// A diagnostic message. +pub struct Message { + pub(crate) level: Level, + pub(crate) message: String, +} + +/// Information about macro expansion. +#[derive(serde::Deserialize, Debug)] +struct Expansion { + span: Span, +} + +#[derive(serde::Deserialize, Debug)] +struct Span { + line_start: usize, + file_name: PathBuf, + is_primary: bool, + expansion: Option<Box<Expansion>>, +} + +impl std::str::FromStr for Level { + type Err = String; + fn from_str(s: &str) -> Result<Self, Self::Err> { + match s { + "ERROR" | "error" => Ok(Self::Error), + "WARN" | "warning" => Ok(Self::Warn), + "HELP" | "help" => Ok(Self::Help), + "NOTE" | "note" => Ok(Self::Note), + "failure-note" => Ok(Self::FailureNote), + "error: internal compiler error" => Ok(Self::Ice), + _ => Err(format!("unknown level `{s}`")), + } + } +} + +#[derive(Debug)] +pub(crate) struct Diagnostics { + /// Rendered and concatenated version of all diagnostics. + /// This is equivalent to non-json diagnostics. + pub rendered: Vec<u8>, + /// Per line, a list of messages for that line. + pub messages: Vec<Vec<Message>>, + /// Messages not on any line (usually because they are from libstd) + pub messages_from_unknown_file_or_line: Vec<Message>, +} + +impl RustcMessage { + fn line(&self, file: &Path) -> Option<usize> { + let span = |primary| self.spans.iter().find_map(|span| span.line(file, primary)); + span(true).or_else(|| span(false)) + } + + /// Put the message and its children into the line-indexed list. + fn insert_recursive( + self, + file: &Path, + messages: &mut Vec<Vec<Message>>, + messages_from_unknown_file_or_line: &mut Vec<Message>, + line: Option<usize>, + ) { + let line = self.line(file).or(line); + let msg = Message { + level: self.level.parse().unwrap(), + message: self.message, + }; + if let Some(line) = line { + if messages.len() <= line { + messages.resize_with(line + 1, Vec::new); + } + messages[line].push(msg); + // All other messages go into the general bin, unless they are specifically of the + // "aborting due to X previous errors" variety, as we never want to match those. They + // only count the number of errors and provide no useful information about the tests. + } else if !(msg.message.starts_with("aborting due to") + && msg.message.contains("previous error")) + { + messages_from_unknown_file_or_line.push(msg); + } + for child in self.children { + child.insert_recursive(file, messages, messages_from_unknown_file_or_line, line) + } + } +} + +impl Span { + /// Returns the most expanded line number *in the given file*, if possible. + fn line(&self, file: &Path, primary: bool) -> Option<usize> { + if let Some(exp) = &self.expansion { + if let Some(line) = exp.span.line(file, primary && !self.is_primary) { + return Some(line); + } + } + ((!primary || self.is_primary) && self.file_name == file).then_some(self.line_start) + } +} + +pub(crate) fn filter_annotations_from_rendered(rendered: &str) -> std::borrow::Cow<'_, str> { + let annotations = Regex::new(r" *//(\[[a-z,]+\])?~.*").unwrap(); + annotations.replace_all(rendered, "") +} + +pub(crate) fn process(file: &Path, stderr: &[u8]) -> Diagnostics { + let mut rendered = Vec::new(); + let mut messages = vec![]; + let mut messages_from_unknown_file_or_line = vec![]; + for (line_number, line) in stderr.lines_with_terminator().enumerate() { + if line.starts_with_str(b"{") { + match serde_json::from_slice::<RustcMessage>(line) { + Ok(msg) => { + rendered.extend( + filter_annotations_from_rendered(msg.rendered.as_ref().unwrap()).as_bytes(), + ); + msg.insert_recursive( + file, + &mut messages, + &mut messages_from_unknown_file_or_line, + None, + ); + } + Err(err) => { + panic!("failed to parse rustc JSON output at line {line_number}: {err}") + } + } + } else { + // FIXME: do we want to throw interpreter stderr into a separate file? + rendered.extend(line); + } + } + Diagnostics { + rendered, + messages, + messages_from_unknown_file_or_line, + } +} diff --git a/vendor/ui_test/src/status_emitter.rs b/vendor/ui_test/src/status_emitter.rs new file mode 100644 index 000000000..a277a9808 --- /dev/null +++ b/vendor/ui_test/src/status_emitter.rs @@ -0,0 +1,577 @@ +//! Variaous schemes for reporting messages during testing or after testing is done. + +use bstr::ByteSlice; +use colored::Colorize; + +use crate::{github_actions, parser::Pattern, rustc_stderr::Message, Error, Errors, TestResult}; +use std::{ + fmt::{Debug, Write as _}, + io::Write as _, + path::Path, + process::Command, +}; + +/// A generic way to handle the output of this crate. +pub trait StatusEmitter: Sync { + /// Invoked before each failed test prints its errors along with a drop guard that can + /// gets invoked afterwards. + fn failed_test<'a>( + &'a self, + revision: &'a str, + path: &'a Path, + cmd: &'a Command, + stderr: &'a [u8], + ) -> Box<dyn Debug + 'a>; + + /// A test has finished, handle the result immediately. + fn test_result(&mut self, _path: &Path, _revision: &str, _result: &TestResult) {} + + /// Create a report about the entire test run at the end. + #[allow(clippy::type_complexity)] + fn finalize( + &self, + failed: usize, + succeeded: usize, + ignored: usize, + filtered: usize, + ) -> Box<dyn Summary>; +} + +/// Report a summary at the end of a test run. +pub trait Summary { + /// A test has finished, handle the result. + fn test_failure(&mut self, _path: &Path, _revision: &str, _errors: &Errors) {} +} + +impl Summary for () {} + +/// A human readable output emitter. +pub struct Text; +impl StatusEmitter for Text { + fn failed_test<'a>( + &self, + revision: &str, + path: &Path, + cmd: &Command, + stderr: &'a [u8], + ) -> Box<dyn Debug + 'a> { + eprintln!(); + let path = path.display().to_string(); + eprint!("{}", path.underline().bold()); + let revision = if revision.is_empty() { + String::new() + } else { + format!(" (revision `{revision}`)") + }; + eprint!("{revision}"); + eprint!(" {}", "FAILED:".red().bold()); + eprintln!(); + eprintln!("command: {cmd:?}"); + eprintln!(); + + #[derive(Debug)] + struct Guard<'a>(&'a [u8]); + impl<'a> Drop for Guard<'a> { + fn drop(&mut self) { + eprintln!("full stderr:"); + std::io::stderr().write_all(self.0).unwrap(); + eprintln!(); + eprintln!(); + } + } + Box::new(Guard(stderr)) + } + + fn test_result(&mut self, path: &Path, revision: &str, result: &TestResult) { + let result = match result { + TestResult::Ok => "ok".green(), + TestResult::Errored { .. } => "FAILED".red().bold(), + TestResult::Ignored => "ignored (in-test comment)".yellow(), + TestResult::Filtered => return, + }; + eprint!( + "{}{} ... ", + path.display(), + if revision.is_empty() { + "".into() + } else { + format!(" ({revision})") + } + ); + eprintln!("{result}"); + } + + fn finalize( + &self, + failures: usize, + succeeded: usize, + ignored: usize, + filtered: usize, + ) -> Box<dyn Summary> { + // Print all errors in a single thread to show reliable output + if failures == 0 { + eprintln!(); + eprintln!( + "test result: {}. {} tests passed, {} ignored, {} filtered out", + "ok".green(), + succeeded.to_string().green(), + ignored.to_string().yellow(), + filtered.to_string().yellow(), + ); + eprintln!(); + Box::new(()) + } else { + struct Summarizer { + failures: Vec<String>, + succeeded: usize, + ignored: usize, + filtered: usize, + } + + impl Summary for Summarizer { + fn test_failure(&mut self, path: &Path, revision: &str, errors: &Errors) { + for error in errors { + print_error(error, &path.display().to_string()); + } + + self.failures.push(if revision.is_empty() { + format!(" {}", path.display()) + } else { + format!(" {} (revision {revision})", path.display()) + }); + } + } + + impl Drop for Summarizer { + fn drop(&mut self) { + eprintln!("{}", "FAILURES:".red().underline().bold()); + for line in &self.failures { + eprintln!("{line}"); + } + eprintln!(); + eprintln!( + "test result: {}. {} tests failed, {} tests passed, {} ignored, {} filtered out", + "FAIL".red(), + self.failures.len().to_string().red().bold(), + self.succeeded.to_string().green(), + self.ignored.to_string().yellow(), + self.filtered.to_string().yellow(), + ); + } + } + Box::new(Summarizer { + failures: vec![], + succeeded, + ignored, + filtered, + }) + } + } +} + +fn print_error(error: &Error, path: &str) { + match error { + Error::ExitStatus { + mode, + status, + expected, + } => { + eprintln!("{mode} test got {status}, but expected {expected}") + } + Error::Command { kind, status } => { + eprintln!("{kind} failed with {status}"); + } + Error::PatternNotFound { + pattern, + definition_line, + } => { + match pattern { + Pattern::SubString(s) => { + eprintln!("substring `{s}` {} in stderr output", "not found".red()) + } + Pattern::Regex(r) => { + eprintln!("`/{r}/` does {} stderr output", "not match".red()) + } + } + eprintln!( + "expected because of pattern here: {}", + format!("{path}:{definition_line}").bold() + ); + } + Error::NoPatternsFound => { + eprintln!("{}", "no error patterns found in fail test".red()); + } + Error::PatternFoundInPassTest => { + eprintln!("{}", "error pattern found in pass test".red()) + } + Error::OutputDiffers { + path: output_path, + actual, + expected, + bless_command, + } => { + eprintln!("{}", "actual output differed from expected".underline()); + eprintln!( + "Execute `{}` to update `{}` to the actual output", + bless_command, + output_path.display() + ); + eprintln!("{}", format!("--- {}", output_path.display()).red()); + eprintln!("{}", "+++ <stderr output>".green()); + crate::diff::print_diff(expected, actual); + } + Error::ErrorsWithoutPattern { path: None, msgs } => { + eprintln!( + "There were {} unmatched diagnostics that occurred outside the testfile and had no pattern", + msgs.len(), + ); + for Message { level, message } in msgs { + eprintln!(" {level:?}: {message}") + } + } + Error::ErrorsWithoutPattern { + path: Some((path, line)), + msgs, + } => { + let path = path.display(); + eprintln!( + "There were {} unmatched diagnostics at {path}:{line}", + msgs.len(), + ); + for Message { level, message } in msgs { + eprintln!(" {level:?}: {message}") + } + } + Error::InvalidComment { msg, line } => { + eprintln!("Could not parse comment in {path}:{line} because\n{msg}",) + } + Error::Bug(msg) => { + eprintln!("A bug in `ui_test` occurred: {msg}"); + } + Error::Aux { + path: aux_path, + errors, + line, + } => { + eprintln!("Aux build from {path}:{line} failed"); + for error in errors { + print_error(error, &aux_path.display().to_string()); + } + } + } + eprintln!(); +} + +fn gha_error(error: &Error, path: &str, revision: &str) { + match error { + Error::ExitStatus { + mode, + status, + expected, + } => { + github_actions::error( + path, + format!("{mode} test{revision} got {status}, but expected {expected}"), + ); + } + Error::Command { kind, status } => { + github_actions::error(path, format!("{kind}{revision} failed with {status}")); + } + Error::PatternNotFound { + pattern: _, + definition_line, + } => { + github_actions::error(path, format!("Pattern not found{revision}")) + .line(*definition_line); + } + Error::NoPatternsFound => { + github_actions::error( + path, + format!("no error patterns found in fail test{revision}"), + ); + } + Error::PatternFoundInPassTest => { + github_actions::error(path, format!("error pattern found in pass test{revision}")); + } + Error::OutputDiffers { + path: output_path, + actual, + expected, + bless_command: _, + } => { + let mut err = github_actions::error( + if expected.is_empty() { + path.to_owned() + } else { + output_path.display().to_string() + }, + "actual output differs from expected", + ); + writeln!(err, "```diff").unwrap(); + let mut seen_diff_line = Some(0); + for r in ::diff::lines(expected.to_str().unwrap(), actual.to_str().unwrap()) { + if let Some(line) = &mut seen_diff_line { + *line += 1; + } + let mut seen_diff = || { + if let Some(line) = seen_diff_line.take() { + writeln!(err, "{line} unchanged lines skipped").unwrap(); + } + }; + match r { + ::diff::Result::Both(l, r) => { + if l != r { + seen_diff(); + writeln!(err, "-{l}").unwrap(); + writeln!(err, "+{r}").unwrap(); + } else if seen_diff_line.is_none() { + writeln!(err, " {l}").unwrap() + } + } + ::diff::Result::Left(l) => { + seen_diff(); + writeln!(err, "-{l}").unwrap(); + } + ::diff::Result::Right(r) => { + seen_diff(); + writeln!(err, "+{r}").unwrap(); + } + } + } + writeln!(err, "```").unwrap(); + } + Error::ErrorsWithoutPattern { path: None, msgs } => { + let mut err = github_actions::error( + path, + format!("Unmatched diagnostics outside the testfile{revision}"), + ); + for Message { level, message } in msgs { + writeln!(err, "{level:?}: {message}").unwrap(); + } + } + Error::ErrorsWithoutPattern { + path: Some((path, line)), + msgs, + } => { + let path = path.display(); + let mut err = github_actions::error(&path, format!("Unmatched diagnostics{revision}")) + .line(*line); + for Message { level, message } in msgs { + writeln!(err, "{level:?}: {message}").unwrap(); + } + } + Error::InvalidComment { msg, line } => { + let mut err = + github_actions::error(path, format!("Could not parse comment")).line(*line); + writeln!(err, "{msg}").unwrap(); + } + Error::Bug(_) => {} + Error::Aux { + path: aux_path, + errors, + line, + } => { + github_actions::error(path, format!("Aux build failed")).line(*line); + for error in errors { + gha_error(error, &aux_path.display().to_string(), "") + } + } + } + eprintln!(); +} + +/// Just print some dots instead of a whole line per run test. +#[derive(Default)] +pub struct Quiet { + n: usize, +} + +impl StatusEmitter for Quiet { + fn test_result(&mut self, _path: &Path, _revision: &str, result: &TestResult) { + // Humans start counting at 1 + self.n += 1; + match result { + TestResult::Ok => eprint!("{}", ".".green()), + TestResult::Errored { .. } => eprint!("{}", "F".red().bold()), + TestResult::Ignored => eprint!("{}", "i".yellow()), + TestResult::Filtered => {} + } + if self.n % 100 == 0 { + eprintln!(" {}", self.n); + } + } + + fn failed_test<'a>( + &'a self, + revision: &'a str, + path: &'a Path, + cmd: &'a Command, + stderr: &'a [u8], + ) -> Box<dyn Debug + 'a> { + Text.failed_test(revision, path, cmd, stderr) + } + + fn finalize( + &self, + failed: usize, + succeeded: usize, + ignored: usize, + filtered: usize, + ) -> Box<dyn Summary> { + Text.finalize(failed, succeeded, ignored, filtered) + } +} + +/// Emits Github Actions Workspace commands to show the failures directly in the github diff view. +/// If the const generic `GROUP` boolean is `true`, also emit `::group` commands. +pub struct Gha<const GROUP: bool> { + /// Show a specific name for the final summary. + pub name: String, +} + +impl<const GROUP: bool> StatusEmitter for Gha<GROUP> { + fn failed_test( + &self, + revision: &str, + path: &Path, + _cmd: &Command, + _stderr: &[u8], + ) -> Box<dyn Debug> { + if GROUP { + Box::new(github_actions::group(format_args!( + "{}:{revision}", + path.display() + ))) + } else { + Box::new(()) + } + } + + fn test_result(&mut self, _path: &Path, _revision: &str, _result: &TestResult) {} + + fn finalize( + &self, + _failures: usize, + succeeded: usize, + ignored: usize, + filtered: usize, + ) -> Box<dyn Summary> { + struct Summarizer<const GROUP: bool> { + failures: Vec<String>, + succeeded: usize, + ignored: usize, + filtered: usize, + name: String, + } + + impl<const GROUP: bool> Summary for Summarizer<GROUP> { + fn test_failure(&mut self, path: &Path, revision: &str, errors: &Errors) { + let revision = if revision.is_empty() { + "".to_string() + } else { + format!(" (revision: {revision})") + }; + for error in errors { + gha_error(error, &path.display().to_string(), &revision); + } + self.failures.push(format!("{}{revision}", path.display())); + } + } + impl<const GROUP: bool> Drop for Summarizer<GROUP> { + fn drop(&mut self) { + if let Some(mut file) = github_actions::summary() { + writeln!(file, "### {}", self.name).unwrap(); + for line in &self.failures { + writeln!(file, "* {line}").unwrap(); + } + writeln!(file).unwrap(); + writeln!(file, "| failed | passed | ignored | filtered out |").unwrap(); + writeln!(file, "| --- | --- | --- | --- |").unwrap(); + writeln!( + file, + "| {} | {} | {} | {} |", + self.failures.len(), + self.succeeded, + self.ignored, + self.filtered, + ) + .unwrap(); + } + } + } + + Box::new(Summarizer::<GROUP> { + failures: vec![], + succeeded, + ignored, + filtered, + name: self.name.clone(), + }) + } +} + +impl<T: StatusEmitter, U: StatusEmitter> StatusEmitter for (T, U) { + fn failed_test<'a>( + &'a self, + revision: &'a str, + path: &'a Path, + cmd: &'a Command, + stderr: &'a [u8], + ) -> Box<dyn Debug + 'a> { + Box::new(( + self.0.failed_test(revision, path, cmd, stderr), + self.1.failed_test(revision, path, cmd, stderr), + )) + } + + fn test_result(&mut self, path: &Path, revision: &str, result: &TestResult) { + self.0.test_result(path, revision, result); + self.1.test_result(path, revision, result); + } + + fn finalize( + &self, + failures: usize, + succeeded: usize, + ignored: usize, + filtered: usize, + ) -> Box<dyn Summary> { + Box::new(( + self.1.finalize(failures, succeeded, ignored, filtered), + self.0.finalize(failures, succeeded, ignored, filtered), + )) + } +} + +impl<T: StatusEmitter + ?Sized> StatusEmitter for Box<T> { + fn failed_test<'a>( + &'a self, + revision: &'a str, + path: &'a Path, + cmd: &'a Command, + stderr: &'a [u8], + ) -> Box<dyn Debug + 'a> { + (**self).failed_test(revision, path, cmd, stderr) + } + + fn test_result(&mut self, path: &Path, revision: &str, result: &TestResult) { + (**self).test_result(path, revision, result); + } + + fn finalize( + &self, + failures: usize, + succeeded: usize, + ignored: usize, + filtered: usize, + ) -> Box<dyn Summary> { + (**self).finalize(failures, succeeded, ignored, filtered) + } +} + +impl Summary for (Box<dyn Summary>, Box<dyn Summary>) { + fn test_failure(&mut self, path: &Path, revision: &str, errors: &Errors) { + self.0.test_failure(path, revision, errors); + self.1.test_failure(path, revision, errors); + } +} diff --git a/vendor/ui_test/src/tests.rs b/vendor/ui_test/src/tests.rs new file mode 100644 index 000000000..4aca1d2d0 --- /dev/null +++ b/vendor/ui_test/src/tests.rs @@ -0,0 +1,338 @@ +use std::path::{Path, PathBuf}; + +use crate::rustc_stderr::Level; +use crate::rustc_stderr::Message; + +use super::*; + +fn config() -> Config { + Config { + root_dir: PathBuf::from("$RUSTROOT"), + program: CommandBuilder::cmd("cake"), + ..Config::rustc(PathBuf::new()) + } +} + +#[test] +fn issue_2156() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address $HEX is unallocated) +} + "; + let comments = Comments::parse(s).unwrap(); + let mut errors = vec![]; + let config = config(); + let messages = vec![ + vec![], vec![], vec![], vec![], vec![], + vec![ + Message { + message:"Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + } + ] + ]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + [Error::PatternNotFound { + definition_line: 5, .. + }, Error::ErrorsWithoutPattern { + path: Some((_, 5)), .. + }] => {} + _ => panic!("{:#?}", errors), + } +} + +#[test] +fn find_pattern() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address 0x10 is unallocated) +} + "; + let comments = Comments::parse(s).unwrap(); + let config = config(); + { + let messages = vec![vec![], vec![], vec![], vec![], vec![], vec![ + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + } + ] + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + [] => {} + _ => panic!("{:#?}", errors), + } + } + + // only difference to above is a wrong line number + { + let messages = vec![vec![], vec![], vec![], vec![], vec![ + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + } + ] + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + [Error::PatternNotFound { + definition_line: 5, .. + }, Error::ErrorsWithoutPattern { + path: Some((_, 4)), .. + }] => {} + _ => panic!("not the expected error: {:#?}", errors), + } + } + + // only difference to first is a wrong level + { + let messages = vec![ + vec![], vec![], vec![], vec![], vec![], + vec![ + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Note, + } + ] + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + // Note no `ErrorsWithoutPattern`, because there are no `//~NOTE` in the test file, so we ignore them + [Error::PatternNotFound { + definition_line: 5, .. + }] => {} + _ => panic!("not the expected error: {:#?}", errors), + } + } +} + +#[test] +fn duplicate_pattern() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address 0x10 is unallocated) + //~^ ERROR: encountered a dangling reference (address 0x10 is unallocated) +} + "; + let comments = Comments::parse(s).unwrap(); + let config = config(); + let messages = vec![ + vec![], vec![], vec![], vec![], vec![], + vec![ + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + } + ] + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + [Error::PatternNotFound { + definition_line: 6, .. + }] => {} + _ => panic!("{:#?}", errors), + } +} + +#[test] +fn missing_pattern() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address 0x10 is unallocated) +} + "; + let comments = Comments::parse(s).unwrap(); + let config = config(); + let messages = vec![ + vec![], vec![], vec![], vec![], vec![], + vec![ + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + }, + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + } + ] + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + [Error::ErrorsWithoutPattern { + path: Some((_, 5)), .. + }] => {} + _ => panic!("{:#?}", errors), + } +} + +#[test] +fn missing_warn_pattern() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address 0x10 is unallocated) + //~^ WARN: cake +} + "; + let comments = Comments::parse(s).unwrap(); + let config = config(); + let messages= vec![ + vec![], + vec![], + vec![], + vec![], + vec![], + vec![ + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + }, + Message { + message: "kaboom".to_string(), + level: Level::Warn, + }, + Message { + message: "cake".to_string(), + level: Level::Warn, + }, + ], + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + [Error::ErrorsWithoutPattern { + path: Some((_, 5)), + msgs, + .. + }] => match &msgs[..] { + [Message { + message, + level: Level::Warn, + }] if message == "kaboom" => {} + _ => panic!("{:#?}", msgs), + }, + _ => panic!("{:#?}", errors), + } +} + +#[test] +fn missing_implicit_warn_pattern() { + let s = r" +use std::mem; +//@require-annotations-for-level: ERROR +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address 0x10 is unallocated) + //~^ WARN: cake +} + "; + let comments = Comments::parse(s).unwrap(); + let config = config(); + let messages = vec![ + vec![], + vec![], + vec![], + vec![], + vec![], + vec![ + Message { + message: "Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated)".to_string(), + level: Level::Error, + }, + Message { + message: "kaboom".to_string(), + level: Level::Warn, + }, + Message { + message: "cake".to_string(), + level: Level::Warn, + }, + ], + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ); + match &errors[..] { + [] => {} + _ => panic!("{:#?}", errors), + } +} |