Skip to content

Commit

Permalink
Add unit-test-kind option to allow choosing bin or lib
Browse files Browse the repository at this point in the history
When running with the unit-test option, there was no way to choose
between a bin or lib target, so the result was always an error if
there were both types for the same target name. To allow running
unit tests in that case, this new option unit-test-kind may be used
to specify the type of target that we want.
  • Loading branch information
felipou authored and djc committed May 16, 2024
1 parent 594d254 commit 512f20c
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/bin/cargo-flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ use clap::{Args, Parser};

use flamegraph::Workload;

#[derive(Debug, Clone, Copy, clap::ValueEnum)]
#[clap(rename_all = "snake_case")]
enum UnitTestTargetKind {
Bin,
Lib,
}

#[derive(Args, Debug)]
struct Opt {
/// Build with the dev profile
Expand Down Expand Up @@ -42,6 +49,11 @@ struct Opt {
#[clap(long, group = "exec-args")]
unit_test: Option<Option<String>>,

/// Kind of target (lib or bin) when running with <unit-test> which is may be
/// required when we have two targets with the same name.
#[clap(long)]
unit_test_kind: Option<UnitTestTargetKind>,

/// Crate target to unit benchmark, <bench> may be omitted if crate only has one target
/// (currently profiles the test harness and all tests in the binary; test selection
/// can be passed as trailing arguments after `--` as separator)
Expand Down Expand Up @@ -436,8 +448,14 @@ fn main() -> anyhow::Result<()> {
opt.package = Some(target.package);
target.kind
} else if let Some(unit_test) = opt.unit_test {
let kinds = match opt.unit_test_kind {
Some(UnitTestTargetKind::Bin) => &["bin"][..], // get slice to help type inference
Some(UnitTestTargetKind::Lib) => &["lib"],
None => &["bin", "lib"],
};

let target = find_unique_target(
&["bin", "lib"],
kinds,
opt.package.as_deref(),
opt.manifest_path.as_deref(),
unit_test.as_deref(),
Expand Down

0 comments on commit 512f20c

Please sign in to comment.