From 35ad0cb6afca09c4bdd49585573b672ba5869eee Mon Sep 17 00:00:00 2001 From: Grzegorz Swirski Date: Wed, 20 Sep 2023 21:18:16 +0200 Subject: [PATCH 1/5] 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 } From 7100ce12107732c1dedcea05344620d1e52857f0 Mon Sep 17 00:00:00 2001 From: Grzegorz Swirski Date: Wed, 20 Sep 2023 21:34:48 +0200 Subject: [PATCH 2/5] formatting --- src/app.rs | 9 +++------ src/opt.rs | 1 - 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/app.rs b/src/app.rs index 53714f3..d6cbd86 100644 --- a/src/app.rs +++ b/src/app.rs @@ -59,10 +59,9 @@ 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 = @@ -139,7 +138,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::Verbose); + workspace.config().shell().set_verbosity(Verbosity::Normal); let compile_options = make_compile_opts(cargo_options, workspace.config())?; let result = cargo::ops::compile(workspace, &compile_options)?; @@ -155,9 +154,7 @@ fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result Date: Sun, 24 Sep 2023 22:42:50 +0200 Subject: [PATCH 3/5] simplify test interface --- src/app.rs | 21 ++++++++++----------- src/opt.rs | 15 +++++---------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/app.rs b/src/app.rs index d6cbd86..f853bb9 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(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()?; @@ -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) @@ -150,13 +146,16 @@ fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result Ok(unit_output.path.clone()), @@ -188,11 +187,11 @@ fn make_compile_opts(cargo_options: &CargoOpts, cfg: &Config) -> Result (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!(), }; @@ -201,7 +200,7 @@ 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")] + #[structopt(long, group = "target", value_name = "NAME")] test: Option, /// Pass --release to cargo @@ -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) @@ -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), } } } @@ -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 } From 189854724f04f39d0cf2a3119c637b91999764b2 Mon Sep 17 00:00:00 2001 From: Grzegorz Swirski Date: Sun, 24 Sep 2023 22:47:36 +0200 Subject: [PATCH 4/5] remove println --- src/app.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/app.rs b/src/app.rs index f853bb9..f9371c7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -150,10 +150,7 @@ fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result Date: Sun, 1 Oct 2023 21:53:26 +0200 Subject: [PATCH 5/5] update README --- README.md | 14 ++++++++++++-- src/app.rs | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 11fa8d5..10d20de 100644 --- a/README.md +++ b/README.md @@ -77,8 +77,8 @@ $ nix-shell --command 'cargo install cargo-instruments' --pure -p \ `cargo-instruments` requires a binary target to run. By default, it will try to build the current crate's `main.rs`. You can specify an alternative binary by -using the `--bin` or `--example` flags, or a benchmark target with the `--bench` -flag. +using the `--bin` or `--example` flags, a test target with the `--test` flag, +or a benchmark target with the `--bench` flag. Assuming your crate has one binary target named `mybin`, and you want to profile using the `Allocations` Instruments template: @@ -154,6 +154,7 @@ OPTIONS: -p, --package Specify package for example/bin/bench --profile Pass --profile NAME to cargo -t, --template