From 35ad0cb6afca09c4bdd49585573b672ba5869eee Mon Sep 17 00:00:00 2001 From: Grzegorz Swirski Date: Wed, 20 Sep 2023 21:18:16 +0200 Subject: [PATCH] basic support for tests --- src/app.rs | 31 +++++++++++++++++++++++-------- src/opt.rs | 16 +++++++++++++++- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/app.rs b/src/app.rs index b161d9b..53714f3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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()?; @@ -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) @@ -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 { 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)?; @@ -146,6 +151,15 @@ fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result Ok(unit_output.path.clone()), @@ -177,10 +191,11 @@ fn make_compile_opts(cargo_options: &CargoOpts, cfg: &Config) -> Result (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!(), }; @@ -188,8 +203,8 @@ fn make_compile_opts(cargo_options: &CargoOpts, cfg: &Config) -> Result, + /// Test harness target to run + #[structopt(long, group = "target", value_name = "NAME")] + harness: Option, + + /// Test target to run + #[structopt(long, value_name = "NAME")] + test: Option, + /// Pass --release to cargo #[structopt(long, conflicts_with = "profile")] release: bool, @@ -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) @@ -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), } } } @@ -192,7 +202,7 @@ 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()) @@ -200,6 +210,10 @@ 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 { Target::Main }