Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix aux builds in case of non-default target dir paths #152

Merged
merged 9 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ui_test"
version = "0.17.0"
version = "0.18.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "A test framework for testing rustc diagnostics output"
Expand Down
26 changes: 1 addition & 25 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use color_eyre;
use color_eyre::eyre::Result;
use std::{
ffi::OsString,
path::{Component, Path, PathBuf, Prefix},
path::{Path, PathBuf},
};

mod args;
Expand Down Expand Up @@ -218,30 +218,6 @@ impl Config {
.iter()
.any(|arch| self.target.as_ref().unwrap().contains(arch))
}

/// Remove the common prefix of this path and the `root_dir`.
pub(crate) fn strip_path_prefix<'a>(
&self,
path: &'a Path,
) -> impl Iterator<Item = Component<'a>> {
let mut components = path.components();
for c in self.out_dir.components() {
let deverbatimize = |c| match c {
Component::Prefix(prefix) => Err(match prefix.kind() {
Prefix::VerbatimUNC(a, b) => Prefix::UNC(a, b),
Prefix::VerbatimDisk(d) => Prefix::Disk(d),
other => other,
}),
c => Ok(c),
};
let c2 = components.next();
if Some(deverbatimize(c)) == c2.map(deverbatimize) {
continue;
}
return c2.into_iter().chain(components);
}
None.into_iter().chain(components)
}
}

#[derive(Debug, Clone)]
Expand Down
5 changes: 5 additions & 0 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl<'a> BuildManager<'a> {
command: Command::new(format!("{what:?}")),
errors: vec![],
stderr: b"previous build failed".to_vec(),
stdout: vec![],
});
}
let mut lock = self.cache.write().unwrap();
Expand All @@ -254,6 +255,7 @@ impl<'a> BuildManager<'a> {
command: Command::new(format!("{what:?}")),
errors: vec![],
stderr: b"previous build failed".to_vec(),
stdout: vec![],
});
}
entry.get().clone()
Expand All @@ -280,6 +282,7 @@ impl<'a> BuildManager<'a> {
command: Command::new(format!("{what:?}")),
errors: vec![],
stderr: format!("{e:?}").into_bytes(),
stdout: vec![],
});
Err(())
}
Expand All @@ -299,6 +302,7 @@ impl<'a> BuildManager<'a> {
command: Command::new(what.description()),
errors: vec![],
stderr: vec![],
stdout: vec![],
}),
);
res
Expand All @@ -309,6 +313,7 @@ impl<'a> BuildManager<'a> {
command: Command::new(what.description()),
errors: vec![],
stderr: b"previous build failed".to_vec(),
stdout: vec![],
})
})
}
Expand Down
75 changes: 64 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::borrow::Cow;
use std::collections::{HashSet, VecDeque};
use std::ffi::OsString;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf, Prefix};
use std::process::{Command, ExitStatus};
use std::thread;

Expand Down Expand Up @@ -205,6 +205,8 @@ pub struct Errored {
errors: Vec<Error>,
/// The full stderr of the test run.
stderr: Vec<u8>,
/// The full stdout of the test run.
stdout: Vec<u8>,
}

struct TestRun {
Expand Down Expand Up @@ -290,6 +292,7 @@ pub fn run_tests_generic(
.unwrap(),
)],
stderr: vec![],
stdout: vec![],
}),
status,
})?;
Expand Down Expand Up @@ -332,10 +335,11 @@ pub fn run_tests_generic(
command,
errors,
stderr,
stdout,
},
) in &failures
{
let _guard = status.failed_test(command, stderr);
let _guard = status.failed_test(command, stderr, stdout);
failure_emitter.test_failure(status, errors);
}

Expand Down Expand Up @@ -434,6 +438,7 @@ fn parse_comments(file_contents: &[u8]) -> Result<Comments, Errored> {
command: Command::new("parse comments"),
errors,
stderr: vec![],
stdout: vec![],
}),
}
}
Expand Down Expand Up @@ -476,7 +481,12 @@ fn build_aux(
config: &Config,
build_manager: &BuildManager<'_>,
) -> std::result::Result<Vec<OsString>, Errored> {
let file_contents = std::fs::read(aux_file).unwrap();
let file_contents = std::fs::read(aux_file).map_err(|err| Errored {
command: Command::new(format!("reading aux file `{}`", aux_file.display())),
errors: vec![],
stderr: err.to_string().into_bytes(),
stdout: vec![],
})?;
let comments = parse_comments(&file_contents)?;
assert_eq!(
comments.revisions, None,
Expand Down Expand Up @@ -509,7 +519,7 @@ fn build_aux(

// Put aux builds into a separate directory per path so that multiple aux files
// from different directories (but with the same file name) don't collide.
let relative = config.strip_path_prefix(aux_file.parent().unwrap());
let relative = strip_path_prefix(aux_file.parent().unwrap(), &config.out_dir);

config.out_dir.extend(relative);

Expand Down Expand Up @@ -537,6 +547,7 @@ fn build_aux(
command: aux_cmd,
errors: vec![error],
stderr: rustc_stderr::process(aux_file, &output.stderr).rendered,
stdout: output.stdout,
});
}

Expand Down Expand Up @@ -601,14 +612,17 @@ impl dyn TestStatus {
"test panicked: stderr:\n{stderr}\nstdout:\n{stdout}",
))],
stderr: vec![],
stdout: vec![],
});
}
}
}
check_test_result(
cmd, *mode, path, config, revision, comments, status, stdout, &stderr,
cmd, *mode, path, config, revision, comments, status, &stdout, &stderr,
)?;
run_rustfix(
&stderr, &stdout, path, comments, revision, config, *mode, extra_args,
)?;
run_rustfix(&stderr, path, comments, revision, config, *mode, extra_args)?;
Ok(TestOk::Ok)
}

Expand Down Expand Up @@ -647,9 +661,19 @@ fn build_aux_files(
build_manager
.build(
Build::Aux {
aux_file: config
.strip_path_prefix(&aux_file.canonicalize().unwrap())
.collect(),
aux_file: strip_path_prefix(
&aux_file.canonicalize().map_err(|err| Errored {
command: Command::new(format!(
"canonicalizing path `{}`",
aux_file.display()
)),
errors: vec![],
stderr: err.to_string().into_bytes(),
stdout: vec![],
})?,
&std::env::current_dir().unwrap(),
)
.collect(),
},
config,
)
Expand All @@ -658,6 +682,7 @@ fn build_aux_files(
command,
errors,
stderr,
stdout,
}| Errored {
command,
errors: vec![Error::Aux {
Expand All @@ -666,6 +691,7 @@ fn build_aux_files(
line,
}],
stderr,
stdout,
},
)?,
);
Expand Down Expand Up @@ -714,12 +740,14 @@ fn run_test_binary(
command: exe,
errors,
stderr: vec![],
stdout: vec![],
})
}
}

fn run_rustfix(
stderr: &[u8],
stdout: &[u8],
path: &Path,
comments: &Comments,
revision: &str,
Expand Down Expand Up @@ -773,6 +801,7 @@ fn run_rustfix(
command: Command::new(format!("rustfix {}", path.display())),
errors: vec![Error::Rustfix(err)],
stderr: stderr.into(),
stdout: stdout.into(),
})?;

let edition = comments.edition(revision, config)?;
Expand Down Expand Up @@ -836,6 +865,7 @@ fn run_rustfix(
command: Command::new(format!("checking {}", path.display())),
errors,
stderr: vec![],
stdout: vec![],
});
}

Expand Down Expand Up @@ -864,6 +894,7 @@ fn run_rustfix(
status: output.status,
}],
stderr: rustc_stderr::process(&rustfix_path, &output.stderr).rendered,
stdout: output.stdout,
})
}
}
Expand All @@ -884,7 +915,7 @@ fn check_test_result(
revision: &str,
comments: &Comments,
status: ExitStatus,
stdout: Vec<u8>,
stdout: &[u8],
stderr: &[u8],
) -> Result<(), Errored> {
let mut errors = vec![];
Expand All @@ -897,7 +928,7 @@ fn check_test_result(
revision,
config,
comments,
&stdout,
stdout,
&diagnostics.rendered,
);
// Check error annotations in the source against output
Expand All @@ -917,6 +948,7 @@ fn check_test_result(
command,
errors,
stderr: diagnostics.rendered,
stdout: stdout.into(),
})
}
}
Expand Down Expand Up @@ -1202,3 +1234,24 @@ fn normalize(
}
text
}
/// Remove the common prefix of this path and the `root_dir`.
fn strip_path_prefix<'a>(path: &'a Path, prefix: &Path) -> impl Iterator<Item = Component<'a>> {
let mut components = path.components();
for c in prefix.components() {
// Windows has some funky paths. This is probably wrong, but works well in practice.
let deverbatimize = |c| match c {
Component::Prefix(prefix) => Err(match prefix.kind() {
Prefix::VerbatimUNC(a, b) => Prefix::UNC(a, b),
Prefix::VerbatimDisk(d) => Prefix::Disk(d),
other => other,
}),
c => Ok(c),
};
let c2 = components.next();
if Some(deverbatimize(c)) == c2.map(deverbatimize) {
continue;
}
return c2.into_iter().chain(components);
}
None.into_iter().chain(components)
}
1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl Comments {
lines: errors,
}],
stderr: vec![],
stdout: vec![],
})
}
}
Expand Down
Loading