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 all commits
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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -154,6 +154,7 @@ OPTIONS:
-p, --package <NAME> Specify package for example/bin/bench
--profile <NAME> Pass --profile NAME to cargo
-t, --template <TEMPLATE> Specify the instruments template to run
--test <NAME> Test target to run
--time-limit <MILLIS> Limit recording time to the specified value (in milliseconds)
-o, --output <PATH> Output .trace file to the given path

Expand Down Expand Up @@ -219,6 +220,15 @@ $ cargo instruments -t alloc
$ cargo instruments -t Allocations --example my_example --time-limit 10000 --open
```

```shell
# Profile a single test scanario with the File Activity template
#
# You will find `your_library_name` name by running `cargo test` and checking the 'Running' line.
# e.g. Running unittests src/main.rs (target/debug/deps/your_library_name-3cabce0a041cfeed)
# Optionally, you can provide a specific test case after the -- marker
$ cargo instruments -t io --test your_library_name -- opt::tests::specific_test
```

## Resources

[Instruments Help][instruments]
Expand Down
20 changes: 14 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ 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 test) = cargo_options.target {
result
.tests
.iter()
.find(|unit_output| unit_output.unit.target.name() == test)
.map(|unit_output| unit_output.path.clone())
.ok_or_else(|| anyhow!("no test '{}'", test))
} else {
match result.binaries.as_slice() {
[unit_output] => Ok(unit_output.path.clone()),
Expand Down Expand Up @@ -177,19 +184,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) => (vec![], vec![], vec![], vec![bin.clone()]),
_ => unreachable!(),
};

compile_options.filter = CompileFilter::from_raw_arguments(
false,
bins,
false,
Vec::new(),
false,
vec![],
!tests.is_empty(),
examples,
false,
benches,
Expand Down
10 changes: 9 additions & 1 deletion src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub(crate) struct AppConfig {
#[structopt(long, group = "target", value_name = "NAME")]
bench: Option<String>,

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

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

/// The package in which to look for the specified target (example/bin/bench)
Expand Down Expand Up @@ -155,6 +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(test) => write!(f, "test {}", test),
}
}
}
Expand Down Expand Up @@ -192,14 +198,16 @@ 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 test) = self.test {
Target::Test(test.clone())
} else {
Target::Main
}
Expand Down
Loading