Skip to content

Commit

Permalink
basic support for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gswirski committed Sep 20, 2023
1 parent 23faeab commit 35ad0cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
31 changes: 23 additions & 8 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(app_config: AppConfig) -> Result<()> {
pub(crate) fn run(mut app_config: AppConfig) -> Result<()> {
// 1. Detect the type of Xcode Instruments installation
let xctrace_tool = instruments::XcodeInstruments::detect()?;

Expand Down Expand Up @@ -59,6 +59,11 @@ pub(crate) fn run(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 @@ -134,7 +139,7 @@ fn codesign(path: &Path, workspace: &Workspace) -> Result<()> {
/// the path to the built executable.
fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result<PathBuf> {
use cargo::core::shell::Verbosity;
workspace.config().shell().set_verbosity(Verbosity::Normal);
workspace.config().shell().set_verbosity(Verbosity::Verbose);

let compile_options = make_compile_opts(cargo_options, workspace.config())?;
let result = cargo::ops::compile(workspace, &compile_options)?;
Expand All @@ -146,6 +151,15 @@ 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 {
result
.tests
.iter()
.find(|unit_output| {
unit_output.unit.target.name() == harness
})
.map(|unit_output| unit_output.path.clone())
.ok_or_else(|| anyhow!("no test '{}'", harness))
} else {
match result.binaries.as_slice() {
[unit_output] => Ok(unit_output.path.clone()),
Expand Down Expand Up @@ -177,19 +191,20 @@ 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) = match &cargo_options.target {
Target::Bin(bin) => (vec![bin.clone()], vec![], vec![]),
Target::Example(bin) => (vec![], vec![bin.clone()], vec![]),
Target::Bench(bin) => (vec![], vec![], vec![bin.clone()]),
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()]),
_ => unreachable!(),
};

compile_options.filter = CompileFilter::from_raw_arguments(
false,
bins,
false,
Vec::new(),
false,
vec![],
true,
examples,
false,
benches,
Expand Down
16 changes: 15 additions & 1 deletion src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ 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")]
test: Option<String>,

/// Pass --release to cargo
#[structopt(long, conflicts_with = "profile")]
release: bool,
Expand Down Expand Up @@ -119,6 +127,7 @@ pub(crate) enum Target {
Example(String),
Bin(String),
Bench(String),
Test(String, String),
}

/// The package in which to look for the specified target (example/bin/bench)
Expand Down Expand Up @@ -155,6 +164,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),
}
}
}
Expand Down Expand Up @@ -192,14 +202,18 @@ impl AppConfig {
}
}

// valid target: --example, --bin, --bench
// valid target: --example, --bin, --bench, --harness
fn get_target(&self) -> Target {
if let Some(ref example) = self.example {
Target::Example(example.clone())
} else if let Some(ref bin) = self.bin {
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 {
Target::Main
}
Expand Down

0 comments on commit 35ad0cb

Please sign in to comment.