Skip to content

Commit

Permalink
simplify test interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gswirski committed Sep 24, 2023
1 parent 7100ce1 commit 93c52ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
21 changes: 10 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::instruments;
use crate::opt::{AppConfig, CargoOpts, Target};

/// Main entrance point, after args have been parsed.
pub(crate) fn run(mut app_config: AppConfig) -> Result<()> {
pub(crate) fn run(app_config: AppConfig) -> Result<()> {
// 1. Detect the type of Xcode Instruments installation
let xctrace_tool = instruments::XcodeInstruments::detect()?;

Expand Down Expand Up @@ -59,10 +59,6 @@ pub(crate) fn run(mut app_config: AppConfig) -> Result<()> {
#[cfg(target_arch = "aarch64")]
codesign(&target_filepath, &workspace)?;

if let Target::Test(_, ref tests) = cargo_options.target {
app_config.target_args.insert(0, tests.clone());
}

// 4. Profile the built target, will display menu if no template was selected
let trace_filepath =
match instruments::profile_target(&target_filepath, &xctrace_tool, &app_config, &workspace)
Expand Down Expand Up @@ -150,13 +146,16 @@ fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result<Path
.find(|unit_output| unit_output.unit.target.name() == bench)
.map(|unit_output| unit_output.path.clone())
.ok_or_else(|| anyhow!("no benchmark '{}'", bench))
} else if let Target::Test(ref harness, _) = cargo_options.target {
} else if let Target::Test(ref test) = cargo_options.target {
result
.tests
.iter()
.find(|unit_output| unit_output.unit.target.name() == harness)
.find(|unit_output| {
println!("testing output {}", unit_output.unit.target.name());
unit_output.unit.target.name() == test
})
.map(|unit_output| unit_output.path.clone())
.ok_or_else(|| anyhow!("no test '{}'", harness))
.ok_or_else(|| anyhow!("no test '{}'", test))
} else {
match result.binaries.as_slice() {
[unit_output] => Ok(unit_output.path.clone()),
Expand Down Expand Up @@ -188,11 +187,11 @@ fn make_compile_opts(cargo_options: &CargoOpts, cfg: &Config) -> Result<CompileO
compile_options.spec = cargo_options.package.clone().into();

if cargo_options.target != Target::Main {
let (bins, examples, benches, _tests) = match &cargo_options.target {
let (bins, examples, benches, tests) = match &cargo_options.target {
Target::Bin(bin) => (vec![bin.clone()], vec![], vec![], vec![]),
Target::Example(bin) => (vec![], vec![bin.clone()], vec![], vec![]),
Target::Bench(bin) => (vec![], vec![], vec![bin.clone()], vec![]),
Target::Test(bin, _test) => (vec![], vec![], vec![], vec![bin.clone()]),
Target::Test(bin) => (vec![], vec![], vec![], vec![bin.clone()]),
_ => unreachable!(),
};

Expand All @@ -201,7 +200,7 @@ fn make_compile_opts(cargo_options: &CargoOpts, cfg: &Config) -> Result<CompileO
bins,
false,
vec![],
true,
!tests.is_empty(),
examples,
false,
benches,
Expand Down
15 changes: 5 additions & 10 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ pub(crate) struct AppConfig {
#[structopt(long, group = "target", value_name = "NAME")]
bench: Option<String>,

/// Test harness target to run
#[structopt(long, group = "target", value_name = "NAME")]
harness: Option<String>,

/// Test target to run
#[structopt(long, value_name = "NAME")]
#[structopt(long, group = "target", value_name = "NAME")]
test: Option<String>,

/// Pass --release to cargo
Expand Down Expand Up @@ -127,7 +123,7 @@ pub(crate) enum Target {
Example(String),
Bin(String),
Bench(String),
Test(String, String),
Test(String),
}

/// The package in which to look for the specified target (example/bin/bench)
Expand Down Expand Up @@ -164,7 +160,7 @@ impl fmt::Display for Target {
Target::Example(bin) => write!(f, "examples/{}.rs", bin),
Target::Bin(bin) => write!(f, "bin/{}.rs", bin),
Target::Bench(bench) => write!(f, "bench {}", bench),
Target::Test(harness, test) => write!(f, "test {} {}", harness, test),
Target::Test(test) => write!(f, "test {}", test),
}
}
}
Expand Down Expand Up @@ -210,9 +206,8 @@ impl AppConfig {
Target::Bin(bin.clone())
} else if let Some(ref bench) = self.bench {
Target::Bench(bench.clone())
} else if let Some(ref harness) = self.harness {
let test = self.test.clone().unwrap_or_default();
Target::Test(harness.clone(), test)
} else if let Some(ref test) = self.test {
Target::Test(test.clone())
} else {
Target::Main
}
Expand Down

0 comments on commit 93c52ba

Please sign in to comment.