Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic support for tests #94

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
basic support for tests
  • Loading branch information
gswirski committed Sep 20, 2023
commit 35ad0cb6afca09c4bdd49585573b672ba5869eee
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per my top-level comment I would call this argument --test, and then I would want to add some more documentation, explicitly pointing out that this only runs integration tests, and that if you want to filter a specific test than you should pass the test name to the target after --.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code, will work on docs next. Just a note that technically it doesn't have to be an integration test. All tests generate their binaries. When you run cargo test, you'll see something like

[...]
Running unittests src/main.rs (target/debug/deps/cargo_instruments-3cabce0a041cfeed)

which means that you can do:

cargo instruments -t alloc --test cargo-instruments -- opt::tests::features

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay that's great. In an ideal world then maybe you should be able to omit the target after --test, and we would run the "default test target", if such a thing exists, and if we can figure out how to get it from cargo? This might involve some splunking in the cargo crate internals to figure out what they do when you run cargo test.

#[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