Skip to content

Commit 556203f

Browse files
committed
feat(cargo-codspeed): support advanced package selection flags
1 parent 39b45ef commit 556203f

File tree

5 files changed

+87
-55
lines changed

5 files changed

+87
-55
lines changed

crates/cargo-codspeed/src/app.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::{ffi::OsString, process::exit};
22

33
use crate::{prelude::*, run::run_benches};
44

5-
use cargo::util::important_paths::find_root_manifest_for_wd;
65
use cargo::Config;
7-
use clap::{Parser, Subcommand};
6+
use cargo::{ops::Packages, util::important_paths::find_root_manifest_for_wd};
7+
use clap::{Args, Parser, Subcommand};
88
use termcolor::Color;
99

1010
use crate::build::build_benches;
@@ -16,15 +16,30 @@ struct Cli {
1616
command: Commands,
1717
}
1818

19+
/// Package selection flags
20+
#[derive(Args)]
21+
struct PackageSelection {
22+
/// Select all packages in the workspace
23+
#[arg(long)]
24+
workspace: bool,
25+
/// Exclude packages
26+
#[arg(long)]
27+
exclude: Vec<String>,
28+
/// Package to select
29+
#[arg(short, long)]
30+
package: Vec<String>,
31+
}
32+
1933
#[derive(Subcommand)]
2034
enum Commands {
2135
/// Build the benchmarks
2236
Build {
2337
/// Optional list of benchmarks to build (builds all benchmarks by default)
2438
benches: Option<Vec<String>>,
25-
/// Package to build benchmarks for (if using a workspace)
26-
#[arg(short, long)]
27-
package: Option<String>,
39+
40+
#[command(flatten)]
41+
package_selection: PackageSelection,
42+
2843
/// Space or comma separated list of features to activate
2944
#[arg(short = 'F', long)]
3045
features: Option<String>,
@@ -33,9 +48,9 @@ enum Commands {
3348
Run {
3449
/// Optional list of benchmarks to run (run all found benchmarks by default)
3550
benches: Option<Vec<String>>,
36-
/// Package to build benchmarks for (if using a workspace)
37-
#[arg(short, long)]
38-
package: Option<String>,
51+
52+
#[command(flatten)]
53+
package_selection: PackageSelection,
3954
},
4055
}
4156

@@ -50,27 +65,41 @@ pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> {
5065
let cli = Cli::try_parse_from(args)?;
5166
let cargo_config = get_cargo_config()?;
5267
let manifest_path = find_root_manifest_for_wd(cargo_config.cwd())?;
53-
let workspace = Workspace::new(&manifest_path, &cargo_config)?;
68+
let ws = Workspace::new(&manifest_path, &cargo_config)?;
5469

5570
let res = match cli.command {
5671
Commands::Build {
5772
benches,
58-
package,
73+
package_selection,
5974
features,
6075
} => {
6176
let features = features.map(|f| {
6277
f.split(|c| c == ' ' || c == ',')
6378
.map(|s| s.to_string())
6479
.collect_vec()
6580
});
66-
build_benches(&workspace, benches, package, features)
81+
let packages = Packages::from_flags(
82+
package_selection.workspace,
83+
package_selection.exclude,
84+
package_selection.package,
85+
)?;
86+
build_benches(&ws, benches, packages, features)
87+
}
88+
Commands::Run {
89+
benches,
90+
package_selection,
91+
} => {
92+
let packages = Packages::from_flags(
93+
package_selection.workspace,
94+
package_selection.exclude,
95+
package_selection.package,
96+
)?;
97+
run_benches(&ws, benches, packages)
6798
}
68-
Commands::Run { benches, package } => run_benches(&workspace, benches, package),
6999
};
70100

71101
if let Err(e) = res {
72-
workspace
73-
.config()
102+
ws.config()
74103
.shell()
75104
.status_with_color("Error", e.to_string(), Color::Red)?;
76105
exit(1);

crates/cargo-codspeed/src/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
helpers::{clear_dir, get_codspeed_target_dir, get_target_packages},
2+
helpers::{clear_dir, get_codspeed_target_dir},
33
prelude::*,
44
};
55

@@ -58,10 +58,10 @@ struct BenchToBuild<'a> {
5858
pub fn build_benches(
5959
ws: &Workspace,
6060
selected_benches: Option<Vec<String>>,
61-
package_name: Option<String>,
61+
packages: Packages,
6262
features: Option<Vec<String>>,
6363
) -> Result<()> {
64-
let packages_to_build = get_target_packages(&package_name, ws)?;
64+
let packages_to_build = packages.get_packages(ws)?;
6565
let mut benches_to_build = vec![];
6666
for package in packages_to_build.iter() {
6767
let benches = package

crates/cargo-codspeed/src/helpers.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::prelude::*;
2-
use cargo::CargoResult;
32
use std::path::{Path, PathBuf};
43

54
pub fn get_codspeed_target_dir(ws: &Workspace) -> PathBuf {
@@ -9,29 +8,6 @@ pub fn get_codspeed_target_dir(ws: &Workspace) -> PathBuf {
98
.join("codspeed")
109
}
1110

12-
/// Get the packages to run benchmarks for
13-
/// If a package name is provided, only that package is a target
14-
/// If no package name is provided,
15-
/// and the current directory is a package then only that package is a target
16-
/// Otherwise all packages in the workspace are targets
17-
pub fn get_target_packages<'a>(
18-
package_name: &Option<String>,
19-
ws: &'a Workspace<'_>,
20-
) -> Result<Vec<&'a cargo::core::Package>> {
21-
let packages_to_run = if let Some(package) = package_name.as_ref() {
22-
let p = ws
23-
.members()
24-
.find(|m| m.manifest().name().to_string().as_str() == package)
25-
.ok_or(anyhow!("Package {} not found", package))?;
26-
vec![p]
27-
} else if let CargoResult::Ok(p) = ws.current() {
28-
vec![p]
29-
} else {
30-
ws.members().collect::<Vec<_>>()
31-
};
32-
Ok(packages_to_run)
33-
}
34-
3511
pub fn clear_dir<P>(dir: P) -> Result<()>
3612
where
3713
P: AsRef<Path>,

crates/cargo-codspeed/src/run.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::{io, path::PathBuf};
22

33
use anyhow::anyhow;
4+
use cargo::ops::Packages;
45
use termcolor::Color;
56

6-
use crate::{
7-
helpers::{get_codspeed_target_dir, get_target_packages},
8-
prelude::*,
9-
};
7+
use crate::{helpers::get_codspeed_target_dir, prelude::*};
108

119
struct BenchToRun {
1210
bench_path: PathBuf,
@@ -18,10 +16,10 @@ struct BenchToRun {
1816
pub fn run_benches(
1917
ws: &Workspace,
2018
selected_bench_names: Option<Vec<String>>,
21-
package: Option<String>,
19+
packages: Packages,
2220
) -> Result<()> {
2321
let codspeed_target_dir = get_codspeed_target_dir(ws);
24-
let packages_to_run = get_target_packages(&package, ws)?;
22+
let packages_to_run = packages.get_packages(ws)?;
2523
let mut benches: Vec<BenchToRun> = vec![];
2624
for p in packages_to_run {
2725
let package_name = p.manifest().name().to_string();
@@ -50,14 +48,7 @@ pub fn run_benches(
5048
}
5149

5250
if benches.is_empty() {
53-
if let Some(package) = package.as_ref() {
54-
bail!(
55-
"No benchmarks found. Run `cargo codspeed build -p {}` first.",
56-
package
57-
);
58-
} else {
59-
bail!("No benchmarks found. Run `cargo codspeed build` first.");
60-
}
51+
bail!("No benchmarks found. Run `cargo codspeed build` first.");
6152
}
6253

6354
let mut to_run = vec![];

crates/cargo-codspeed/tests/workspace.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,39 @@ fn test_workspace_build_both_and_run_all() {
110110
.stderr(contains("Finished running 3 benchmark suite(s)"));
111111
teardown(dir);
112112
}
113+
114+
#[test]
115+
fn test_workspace_build_both_and_run_all_explicitely() {
116+
let dir = setup(DIR, Project::Workspace);
117+
cargo_codspeed(&dir)
118+
.arg("build")
119+
.args(["--package", "package-a"])
120+
.args(["--package", "package-b"])
121+
.assert()
122+
.success();
123+
cargo_codspeed(&dir)
124+
.arg("run")
125+
.args(["--package", "package-a"])
126+
.args(["--package", "package-b"])
127+
.assert()
128+
.success()
129+
.stderr(contains("Finished running 3 benchmark suite(s)"));
130+
teardown(dir);
131+
}
132+
133+
#[test]
134+
fn test_workspace_build_exclude() {
135+
let dir = setup(DIR, Project::Workspace);
136+
cargo_codspeed(&dir)
137+
.arg("build")
138+
.args(["--workspace", "--exclude", "package-b"])
139+
.assert()
140+
.success()
141+
.stderr(contains("Finished built 1 benchmark suite(s)"));
142+
cargo_codspeed(&dir)
143+
.arg("run")
144+
.assert()
145+
.success()
146+
.stderr(contains("Finished running 1 benchmark suite(s)"));
147+
teardown(dir);
148+
}

0 commit comments

Comments
 (0)