Skip to content

Commit

Permalink
feat: Add --rust-version flag
Browse files Browse the repository at this point in the history
Fixes #196
  • Loading branch information
epage authored and taiki-e committed Sep 6, 2023
1 parent 6decbf3 commit 58d1d91
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ OPTIONS:

This flag can only be used together with either --features or --include-features.

--rust-version
Perform commands on `package.rust-version`.

This cannot be used with --version-range.

--version-range [START]..[=END]
Perform commands on a specified (inclusive) range of Rust versions.

Expand Down
37 changes: 32 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl Args {
let mut keep_going = false;
let mut print_command_list = false;
let mut no_manifest_path = false;
let mut rust_version = false;
let mut version_range = None;
let mut version_step = None;

Expand Down Expand Up @@ -236,6 +237,7 @@ impl Args {

Long("manifest-path") => parse_opt!(manifest_path, false),
Long("depth") => parse_opt!(depth, false),
Long("rust-version") => parse_flag!(rust_version),
Long("version-range") => parse_opt!(version_range, false),
Long("version-step") => parse_opt!(version_step, false),

Expand Down Expand Up @@ -507,11 +509,28 @@ impl Args {
}
}

if version_range.is_some() {
let rustup = Rustup::new();
if rustup.version < 23 {
bail!("--version-range requires rustup 1.23 or later");
let version_range = match (version_range, rust_version) {
(Some(_), true) => {
conflicts("--version-range", "--rust-version")?;
unreachable!()
}
(Some(version_range), false) => {
let rustup = Rustup::new();
if rustup.version < 23 {
bail!("--version-range requires rustup 1.23 or later");
}
Some(version_range.parse()?)
}
(None, true) => {
let rustup = Rustup::new();
if rustup.version < 23 {
bail!("--rust-version requires rustup 1.23 or later");
}
Some(VersionRange::msrv())
}
(None, false) => None,
};
if version_range.is_some() {
} else {
if version_step.is_some() {
requires("--version-step", &["--version-range"])?;
Expand All @@ -520,7 +539,6 @@ impl Args {
requires("--clean-per-version", &["--version-range"])?;
}
}
let version_range = version_range.map(|v| v.parse()).transpose()?;

if no_dev_deps {
info!(
Expand Down Expand Up @@ -728,6 +746,15 @@ const HELP: &[HelpText<'_>] = &[
"Skip passing --features flag to `cargo` if that feature does not exist in the package",
&["This flag can only be used together with either --features or --include-features."],
),
(
"",
"--rust-version",
"",
"Perform commands on `package.rust-version`",
&[
"This cannot be used with --version-range.",
],
),
(
"",
"--version-range",
Expand Down
6 changes: 6 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub(crate) struct VersionRange {
pub(crate) end_inclusive: MaybeVersion,
}

impl VersionRange {
pub(crate) fn msrv() -> Self {
Self { start_inclusive: MaybeVersion::Msrv, end_inclusive: MaybeVersion::Msrv }
}
}

impl fmt::Display for VersionRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let MaybeVersion::Version(start) = self.start_inclusive {
Expand Down
5 changes: 4 additions & 1 deletion tests/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ pub fn cargo_hack<O: AsRef<OsStr>>(args: impl AsRef<[O]>) -> Command {
let mut cmd = cargo_bin_exe();
cmd.arg("hack");
if let Some(toolchain) = test_version() {
if !args.iter().any(|a| a.as_ref().to_str().unwrap().starts_with("--version-range")) {
if !args.iter().any(|a| {
let s = a.as_ref().to_str().unwrap();
s.starts_with("--version-range") || s.starts_with("--rust-version")
}) {
cmd.arg(format!("--version-range=1.{toolchain}..=1.{toolchain}"));
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ OPTIONS:

This flag can only be used together with either --features or --include-features.

--rust-version
Perform commands on `package.rust-version`.

This cannot be used with --version-range.

--version-range [START]..[=END]
Perform commands on a specified (inclusive) range of Rust versions.

Expand Down
1 change: 1 addition & 0 deletions tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ OPTIONS:
--ignore-private Skip to perform on `publish = false` packages
--ignore-unknown-features Skip passing --features flag to `cargo` if that feature
does not exist in the package
--rust-version Perform commands on `package.rust-version`
--version-range [START]..[=END] Perform commands on a specified (inclusive) range of Rust
versions
--version-step <NUM> Specify the version interval of --version-range (default
Expand Down
10 changes: 10 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,16 @@ fn version_range() {
);
}

#[cfg_attr(windows, ignore)] // rustup bug: https://github.com/rust-lang/rustup/issues/3036
#[test]
fn rust_version() {
cargo_hack(["check", "--rust-version"]).assert_success("rust-version").stderr_contains(
"
running `cargo +1.64 check` on real (1/1)
",
);
}

#[cfg_attr(windows, ignore)] // rustup bug: https://github.com/rust-lang/rustup/issues/3036
#[test]
fn multi_target() {
Expand Down

0 comments on commit 58d1d91

Please sign in to comment.