Skip to content

Commit

Permalink
Merge pull request #74 from oli-obk/magic_run
Browse files Browse the repository at this point in the history
Handle out-dir setting per command
  • Loading branch information
oli-obk committed Jun 2, 2023
2 parents 4d8aebe + 757b6df commit 89f99d4
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 121 deletions.
4 changes: 2 additions & 2 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Dependencies {
}

fn cfgs(config: &Config) -> Result<Vec<Cfg>> {
let mut cmd = config.cfgs.build();
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)?;
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn build_dependencies(config: &mut Config) -> Result<Dependencies> {
let manifest_path = &manifest_path;
config.fill_host_and_target()?;
eprintln!(" Building test dependencies...");
let mut build = config.dependency_builder.build();
let mut build = config.dependency_builder.build(&config.out_dir);

if let Some(target) = &config.target {
build.arg(format!("--target={target}"));
Expand Down
83 changes: 28 additions & 55 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ pub struct Config {
/// 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.
pub out_dir: Option<PathBuf>,
/// Defaults to `target/ui` in the current directory.
pub out_dir: PathBuf,
/// The default edition to use on all tests
pub edition: Option<String>,
}
Expand Down Expand Up @@ -113,7 +114,7 @@ impl Default for Config {
dependency_builder: CommandBuilder::cargo(),
quiet: false,
num_test_threads: std::thread::available_parallelism().unwrap(),
out_dir: None,
out_dir: std::env::current_dir().unwrap().join("target/ui"),
edition: Some("2021".into()),
}
}
Expand Down Expand Up @@ -212,6 +213,8 @@ pub struct CommandBuilder {
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>,
/// 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>)>,
Expand All @@ -223,6 +226,7 @@ impl CommandBuilder {
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()),
envs: vec![],
}
}
Expand All @@ -235,6 +239,7 @@ impl CommandBuilder {
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()),
envs: vec![],
}
}
Expand All @@ -253,6 +258,7 @@ impl CommandBuilder {
Self {
program: cmd.into(),
args: vec![],
out_dir_flag: None,
envs: vec![],
}
}
Expand All @@ -266,16 +272,22 @@ impl CommandBuilder {
for arg in &self.0.args {
write!(f, " {arg:?}")?;
}
if let Some(flag) = &self.0.out_dir_flag {
write!(f, " {flag:?} OUT_DIR")?;
}
Ok(())
}
}
Display(self)
}

/// Create a command with the given settings.
pub fn build(&self) -> Command {
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);
}
self.apply_env(&mut cmd);
cmd
}
Expand Down Expand Up @@ -393,14 +405,7 @@ pub fn test_command(mut config: Config, path: &Path) -> Result<Command> {
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,
config.out_dir.as_deref(),
&mut errors,
);
let result = build_command(path, &config, "", &comments, &mut errors);
assert!(errors.is_empty(), "{errors:#?}");
Ok(result)
}
Expand Down Expand Up @@ -719,14 +724,9 @@ fn build_command(
config: &Config,
revision: &str,
comments: &Comments,
out_dir: Option<&Path>,
errors: &mut Vec<Error>,
) -> Command {
let mut cmd = config.program.build();
if let Some(out_dir) = out_dir {
cmd.arg("--out-dir");
cmd.arg(out_dir);
}
let mut cmd = config.program.build(&config.out_dir);
cmd.arg(path);
if !revision.is_empty() {
cmd.arg(format!("--cfg={revision}"));
Expand Down Expand Up @@ -766,40 +766,31 @@ fn build_aux(
Ok(comments) => comments,
Err((msg, mut errors)) => {
return Err((
build_command(path, config, revision, comments, None, &mut errors),
build_command(path, config, revision, comments, &mut errors),
errors,
msg,
))
}
};
assert_eq!(comments.revisions, None);

let mut config = config.clone();

// 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.
let out_dir = config
.out_dir
.clone()
.unwrap_or_default()
.join(path.with_extension(""));
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,
Some(&out_dir),
&mut errors,
);
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)?;
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.
Expand Down Expand Up @@ -829,12 +820,12 @@ fn build_aux(
for file in output.stdout.lines() {
let file = std::str::from_utf8(file).unwrap();
let crate_name = filename.replace('-', "_");
let path = out_dir.join(file);
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(out_dir.display().to_string());
extra_args.push(config.out_dir.display().to_string());
}
Ok(())
}
Expand All @@ -858,14 +849,7 @@ fn run_test(

let mut errors = vec![];

let mut cmd = build_command(
path,
config,
revision,
comments,
config.out_dir.as_deref(),
&mut errors,
);
let mut cmd = build_command(path, config, revision, comments, &mut errors);
cmd.args(&extra_args);

let output = cmd
Expand Down Expand Up @@ -982,7 +966,6 @@ fn run_test_binary(
config: &Config,
errors: &mut Vec<Error>,
) -> Command {
let out_dir = config.out_dir.as_deref();
cmd.arg("--print").arg("file-names");
let output = cmd.output().unwrap();
assert!(output.status.success());
Expand All @@ -991,10 +974,7 @@ fn run_test_binary(
let file = files.next().unwrap();
assert_eq!(files.next(), None);
let file = std::str::from_utf8(file).unwrap();
let exe = match out_dir {
None => PathBuf::from(file),
Some(path) => path.join(file),
};
let exe = config.out_dir.join(file);
let mut exe = Command::new(exe);
let output = exe.output().unwrap();

Expand Down Expand Up @@ -1086,14 +1066,7 @@ fn run_rustfix(
revision,
);

let mut cmd = build_command(
&path,
config,
revision,
&rustfix_comments,
config.out_dir.as_deref(),
errors,
);
let mut cmd = build_command(&path, config, revision, &rustfix_comments, errors);
cmd.args(extra_args);
(cmd, path)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn run(name: &str, mode: Mode) -> Result<()> {
config.stderr_filter("\\.exe", b"");
config.stderr_filter(r#"(panic.*)\.rs:[0-9]+:[0-9]+"#, "$1.rs");
config.stderr_filter(" [0-9]: .*", "");
config.stderr_filter("/target/[^/]+/debug", "/target/$$TRIPLE/debug");
config.stderr_filter("/target/[^/]+/[^/]+/debug", "/target/$$TMP/$$TRIPLE/debug");
config.stderr_filter("(command: )\"[^<rp][^\"]+", "$1\"$$CMD");

run_tests_generic(
Expand Down
1 change: 1 addition & 0 deletions tests/integrations/basic-bin/Cargo.lock

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

1 change: 1 addition & 0 deletions tests/integrations/basic-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dev-dependencies]
ui_test = { path = "../../.."}
tempfile = "3.3.0"

[[test]]
name = "ui_tests"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: extern location for basic_bin is of an unknown type: $DIR/tests/integrations/basic-bin/../../../target/$TRIPLE/debug/basic_bin
error: extern location for basic_bin is of an unknown type: $DIR/tests/integrations/basic-bin/../../../target/$TMP/$TRIPLE/debug/basic_bin
--> $DIR/foomp.rs:1:5
|
1 | use basic_bin::add;
Expand Down
12 changes: 7 additions & 5 deletions tests/integrations/basic-bin/tests/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ fn main() -> ui_test::color_eyre::Result<()> {
if std::env::var_os("BLESS").is_some() {
config.output_conflict_handling = OutputConflictHandling::Bless
}
config
.dependency_builder
.envs
.push(("CARGO_TARGET_DIR".into(), Some(path.into())));
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stdout_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stderr_filter(r"[^ ]*/\.?cargo/registry/.*/", "$$CARGO_REGISTRY");
config.stderr_filter(r"\.exe", "");
config.stderr_filter("/target/[^/]+/debug", "/target/$$TRIPLE/debug");
config.stderr_filter("/target/[^/]+/[^/]+/debug", "/target/$$TMP/$$TRIPLE/debug");
config.path_stderr_filter(&std::path::Path::new(path), "$DIR");

// hide binaries generated for successfully passing tests
let tmp_dir = tempfile::tempdir_in(path)?;
let tmp_dir = tmp_dir.path();
config.out_dir = tmp_dir.into();
config.path_stderr_filter(tmp_dir, "$$TMP");

run_tests_generic(
config,
default_file_filter,
Expand Down
16 changes: 8 additions & 8 deletions tests/integrations/basic-fail-mode/tests/run_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use ui_test::*;
fn run_file() -> Result<()> {
let mut config = Config::default();

let tmp_dir = tempfile::tempdir()?;
config.out_dir = Some(tmp_dir.path().into());
let tmp_dir = tempfile::tempdir_in(env!("CARGO_TARGET_TMPDIR"))?;
let tmp_dir = tmp_dir.path();
config.out_dir = tmp_dir.into();
config.path_stderr_filter(tmp_dir, "$TMP");

let mut result = ui_test::test_command(
config,
Expand Down Expand Up @@ -43,16 +45,14 @@ fn run_file_no_deps() -> Result<()> {

let mut config = Config::default();

let tmp_dir = tempfile::tempdir()?;
config.out_dir = Some(tmp_dir.path().into());
let tmp_dir = tempfile::tempdir_in(path)?;
let tmp_dir = tmp_dir.path();
config.out_dir = tmp_dir.into();
config.path_stderr_filter(tmp_dir, "$TMP");

// Don't build a binary, we only provide .rmeta dependencies for now
config.program.args.push("--emit=metadata".into());
config.dependencies_crate_manifest_path = Some("Cargo.toml".into());
config
.dependency_builder
.envs
.push(("CARGO_TARGET_DIR".into(), Some(path.into())));

let mut result = ui_test::test_command(
config,
Expand Down
4 changes: 0 additions & 4 deletions tests/integrations/basic-fail-mode/tests/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ fn main() -> ui_test::color_eyre::Result<()> {
if std::env::var_os("BLESS").is_some() {
config.output_conflict_handling = OutputConflictHandling::Bless
}
config
.dependency_builder
.envs
.push(("CARGO_TARGET_DIR".into(), Some(path.into())));
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stdout_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stderr_filter(r"[^ ]*/\.?cargo/registry/.*/", "$$CARGO_REGISTRY");
Expand Down
Loading

0 comments on commit 89f99d4

Please sign in to comment.