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/support selecting multiple packages #43

Merged
merged 3 commits into from
Apr 24, 2024
Merged
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
57 changes: 43 additions & 14 deletions crates/cargo-codspeed/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::{ffi::OsString, process::exit};

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

use cargo::util::important_paths::find_root_manifest_for_wd;
use cargo::Config;
use clap::{Parser, Subcommand};
use cargo::{ops::Packages, util::important_paths::find_root_manifest_for_wd};
use clap::{Args, Parser, Subcommand};
use termcolor::Color;

use crate::build::build_benches;
Expand All @@ -16,15 +16,30 @@ struct Cli {
command: Commands,
}

/// Package selection flags
#[derive(Args)]
struct PackageSelection {
/// Select all packages in the workspace
#[arg(long)]
workspace: bool,
/// Exclude packages
#[arg(long)]
exclude: Vec<String>,
/// Package to select
#[arg(short, long)]
package: Vec<String>,
}
art049 marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Subcommand)]
enum Commands {
/// Build the benchmarks
Build {
/// Optional list of benchmarks to build (builds all benchmarks by default)
benches: Option<Vec<String>>,
/// Package to build benchmarks for (if using a workspace)
#[arg(short, long)]
package: Option<String>,

#[command(flatten)]
package_selection: PackageSelection,

/// Space or comma separated list of features to activate
#[arg(short = 'F', long)]
features: Option<String>,
Expand All @@ -33,9 +48,9 @@ enum Commands {
Run {
/// Optional list of benchmarks to run (run all found benchmarks by default)
benches: Option<Vec<String>>,
/// Package to build benchmarks for (if using a workspace)
#[arg(short, long)]
package: Option<String>,

#[command(flatten)]
package_selection: PackageSelection,
},
}

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

let res = match cli.command {
Commands::Build {
benches,
package,
package_selection,
features,
} => {
let features = features.map(|f| {
f.split(|c| c == ' ' || c == ',')
.map(|s| s.to_string())
.collect_vec()
});
build_benches(&workspace, benches, package, features)
let packages = Packages::from_flags(
package_selection.workspace,
package_selection.exclude,
package_selection.package,
)?;
build_benches(&ws, benches, packages, features)
}
Commands::Run {
benches,
package_selection,
} => {
let packages = Packages::from_flags(
package_selection.workspace,
package_selection.exclude,
package_selection.package,
)?;
run_benches(&ws, benches, packages)
}
Commands::Run { benches, package } => run_benches(&workspace, benches, package),
};

if let Err(e) = res {
workspace
.config()
ws.config()
.shell()
.status_with_color("Error", e.to_string(), Color::Red)?;
exit(1);
Expand Down
6 changes: 3 additions & 3 deletions crates/cargo-codspeed/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
helpers::{clear_dir, get_codspeed_target_dir, get_target_packages},
helpers::{clear_dir, get_codspeed_target_dir},
prelude::*,
};

Expand Down Expand Up @@ -58,10 +58,10 @@ struct BenchToBuild<'a> {
pub fn build_benches(
ws: &Workspace,
selected_benches: Option<Vec<String>>,
package_name: Option<String>,
packages: Packages,
features: Option<Vec<String>>,
) -> Result<()> {
let packages_to_build = get_target_packages(&package_name, ws)?;
let packages_to_build = packages.get_packages(ws)?;
let mut benches_to_build = vec![];
for package in packages_to_build.iter() {
let benches = package
Expand Down
24 changes: 0 additions & 24 deletions crates/cargo-codspeed/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::*;
use cargo::CargoResult;
use std::path::{Path, PathBuf};

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

/// Get the packages to run benchmarks for
/// If a package name is provided, only that package is a target
/// If no package name is provided,
/// and the current directory is a package then only that package is a target
/// Otherwise all packages in the workspace are targets
pub fn get_target_packages<'a>(
package_name: &Option<String>,
ws: &'a Workspace<'_>,
) -> Result<Vec<&'a cargo::core::Package>> {
let packages_to_run = if let Some(package) = package_name.as_ref() {
let p = ws
.members()
.find(|m| m.manifest().name().to_string().as_str() == package)
.ok_or(anyhow!("Package {} not found", package))?;
vec![p]
} else if let CargoResult::Ok(p) = ws.current() {
vec![p]
} else {
ws.members().collect::<Vec<_>>()
};
Ok(packages_to_run)
}

pub fn clear_dir<P>(dir: P) -> Result<()>
where
P: AsRef<Path>,
Expand Down
19 changes: 5 additions & 14 deletions crates/cargo-codspeed/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::{io, path::PathBuf};

use anyhow::anyhow;
use cargo::ops::Packages;
use termcolor::Color;

use crate::{
helpers::{get_codspeed_target_dir, get_target_packages},
prelude::*,
};
use crate::{helpers::get_codspeed_target_dir, prelude::*};

struct BenchToRun {
bench_path: PathBuf,
Expand All @@ -18,10 +16,10 @@ struct BenchToRun {
pub fn run_benches(
ws: &Workspace,
selected_bench_names: Option<Vec<String>>,
package: Option<String>,
packages: Packages,
) -> Result<()> {
let codspeed_target_dir = get_codspeed_target_dir(ws);
let packages_to_run = get_target_packages(&package, ws)?;
let packages_to_run = packages.get_packages(ws)?;
let mut benches: Vec<BenchToRun> = vec![];
for p in packages_to_run {
let package_name = p.manifest().name().to_string();
Expand Down Expand Up @@ -50,14 +48,7 @@ pub fn run_benches(
}

if benches.is_empty() {
if let Some(package) = package.as_ref() {
bail!(
"No benchmarks found. Run `cargo codspeed build -p {}` first.",
package
);
} else {
bail!("No benchmarks found. Run `cargo codspeed build` first.");
}
bail!("No benchmarks found. Run `cargo codspeed build` first.");
}

let mut to_run = vec![];
Expand Down
36 changes: 36 additions & 0 deletions crates/cargo-codspeed/tests/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,39 @@ fn test_workspace_build_both_and_run_all() {
.stderr(contains("Finished running 3 benchmark suite(s)"));
teardown(dir);
}

#[test]
fn test_workspace_build_both_and_run_all_explicitely() {
let dir = setup(DIR, Project::Workspace);
cargo_codspeed(&dir)
.arg("build")
.args(["--package", "package-a"])
.args(["--package", "package-b"])
.assert()
.success();
cargo_codspeed(&dir)
.arg("run")
.args(["--package", "package-a"])
.args(["--package", "package-b"])
.assert()
.success()
.stderr(contains("Finished running 3 benchmark suite(s)"));
teardown(dir);
}

#[test]
fn test_workspace_build_exclude() {
let dir = setup(DIR, Project::Workspace);
cargo_codspeed(&dir)
.arg("build")
.args(["--workspace", "--exclude", "package-b"])
.assert()
.success()
.stderr(contains("Finished built 1 benchmark suite(s)"));
cargo_codspeed(&dir)
.arg("run")
.assert()
.success()
.stderr(contains("Finished running 1 benchmark suite(s)"));
teardown(dir);
}