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

Allow cargo install --version as well (preferred) #4637

Merged
merged 2 commits into from
Oct 19, 2017
Merged
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
12 changes: 9 additions & 3 deletions src/bin/install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo::ops;
use cargo::core::{SourceId, GitReference};
use cargo::util::{CliResult, Config, ToUrl};
use cargo::util::{CargoError, CliResult, Config, ToUrl};

#[derive(Deserialize)]
pub struct Options {
Expand All @@ -24,6 +24,7 @@ pub struct Options {

arg_crate: Vec<String>,
flag_vers: Option<String>,
flag_version: Option<String>,

flag_git: Option<String>,
flag_branch: Option<String>,
Expand All @@ -43,7 +44,8 @@ Usage:
cargo install [options] --list

Specifying what crate to install:
--vers VERS Specify a version to install from crates.io
--vers VERSION Specify a version to install from crates.io
--version VERSION
--git URL Git URL to install the specified crate from
--branch BRANCH Branch to use when installing from git
--tag TAG Tag to use when installing from git
Expand Down Expand Up @@ -151,7 +153,11 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
};

let krates = options.arg_crate.iter().map(|s| &s[..]).collect::<Vec<_>>();
let vers = options.flag_vers.as_ref().map(|s| &s[..]);
let vers = match (&options.flag_vers, &options.flag_version) {
(&Some(_), &Some(_)) => return Err(CargoError::from("Invalid arguments.").into()),
(&Some(ref v), _) | (_, &Some(ref v)) => Some(v.as_ref()),
_ => None,
};
let root = options.flag_root.as_ref().map(|s| &s[..]);

if options.flag_list {
Expand Down
22 changes: 22 additions & 0 deletions tests/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,28 @@ fn vers_precise() {
"));
}

#[test]
fn version_too() {
pkg("foo", "0.1.1");
pkg("foo", "0.1.2");

assert_that(cargo_process("install").arg("foo").arg("--version").arg("0.1.1"),
execs().with_status(0).with_stderr_contains("\
[DOWNLOADING] foo v0.1.1 (registry [..])
"));
}

#[test]
fn not_both_vers_and_version() {
pkg("foo", "0.1.1");
pkg("foo", "0.1.2");

assert_that(cargo_process("install").arg("foo").arg("--version").arg("0.1.1").arg("--vers").arg("0.1.2"),
execs().with_status(101).with_stderr_contains("\
error: Invalid arguments.
"));
}

#[test]
fn legacy_version_requirement() {
pkg("foo", "0.1.1");
Expand Down