Skip to content

rustup check #1980

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

Merged
merged 2 commits into from
Sep 16, 2019
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
47 changes: 47 additions & 0 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn main() -> Result<()> {
},
("install", Some(m)) => update(cfg, m)?,
("update", Some(m)) => update(cfg, m)?,
("check", Some(_)) => check_updates(cfg)?,
("uninstall", Some(m)) => toolchain_remove(cfg, m)?,
("default", Some(m)) => default_(cfg, m)?,
("toolchain", Some(c)) => match c.subcommand() {
Expand Down Expand Up @@ -200,6 +201,10 @@ pub fn cli() -> App<'static, 'static> {
.takes_value(false),
),
)
.subcommand(
SubCommand::with_name("check")
.about("Check for updates to Rust toolchains")
)
.subcommand(
SubCommand::with_name("default")
.about("Set the default toolchain")
Expand Down Expand Up @@ -668,6 +673,48 @@ fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
Ok(())
}

fn check_updates(cfg: &Cfg) -> Result<()> {
let mut t = term2::stdout();
let channels = cfg.list_channels()?;

for channel in channels {
match channel {
(ref name, Ok(ref toolchain)) => {
let current_version = toolchain.show_version()?;
let dist_version = toolchain.show_dist_version()?;
let _ = t.attr(term2::Attr::Bold);
write!(t, "{} - ", name)?;
match (current_version, dist_version) {
(None, None) => {
let _ = t.fg(term2::color::BRIGHT_RED);
writeln!(t, "Cannot identify installed or update versions")?;
}
(Some(cv), None) => {
let _ = t.fg(term2::color::BRIGHT_GREEN);
write!(t, "Up to date")?;
let _ = t.reset();
writeln!(t, " : {}", cv)?;
}
(Some(cv), Some(dv)) => {
let _ = t.fg(term2::color::BRIGHT_YELLOW);
write!(t, "Update available")?;
let _ = t.reset();
writeln!(t, " : {} -> {}", cv, dv)?;
}
(None, Some(dv)) => {
let _ = t.fg(term2::color::BRIGHT_YELLOW);
write!(t, "Update available")?;
let _ = t.reset();
writeln!(t, " : (Unknown version) -> {}", dv)?;
}
}
}
(_, Err(err)) => return Err(err.into()),
}
}
Ok(())
}

fn update(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let self_update = !m.is_present("no-self-update") && !self_update::NEVER_SELF_UPDATE;
if let Some(names) = m.values_of("toolchain") {
Expand Down
22 changes: 14 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,22 +390,28 @@ impl Cfg {
}
}

pub fn update_all_channels(
&self,
force_update: bool,
) -> Result<Vec<(String, Result<UpdateStatus>)>> {
pub fn list_channels(&self) -> Result<Vec<(String, Result<Toolchain<'_>>)>> {
let toolchains = self.list_toolchains()?;

// Convert the toolchain strings to Toolchain values
let toolchains = toolchains.into_iter();
let toolchains = toolchains.map(|n| (n.clone(), self.get_toolchain(&n, true)));

// Filter out toolchains that don't track a release channel
let toolchains = toolchains
.filter(|&(_, ref t)| t.as_ref().map(Toolchain::is_tracking).unwrap_or(false));
Ok(toolchains
.filter(|&(_, ref t)| t.as_ref().map(Toolchain::is_tracking).unwrap_or(false))
.collect())
}

pub fn update_all_channels(
&self,
force_update: bool,
) -> Result<Vec<(String, Result<UpdateStatus>)>> {
let channels = self.list_channels()?;
let channels = channels.into_iter();

// Update toolchains and collect the results
let toolchains = toolchains.map(|(n, t)| {
let channels = channels.map(|(n, t)| {
let t = t.and_then(|t| {
let t = t.install_from_dist(force_update);
if let Err(ref e) = t {
Expand All @@ -417,7 +423,7 @@ impl Cfg {
(n, t)
});

Ok(toolchains.collect())
Ok(channels.collect())
}

pub fn check_metadata_version(&self) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ fn update_from_dist_<'a>(
}
}

fn dl_v2_manifest<'a>(
pub fn dl_v2_manifest<'a>(
download: DownloadCfg<'a>,
update_hash: Option<&Path>,
toolchain: &ToolchainDesc,
Expand Down
30 changes: 30 additions & 0 deletions src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,36 @@ impl<'a> Toolchain<'a> {
})
}

pub fn show_version(&self) -> Result<Option<String>> {
if !self.exists() {
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
}

let toolchain = &self.name;
let toolchain = ToolchainDesc::from_str(toolchain)?;

let prefix = InstallPrefix::from(self.path.to_owned());
let manifestation = Manifestation::open(prefix, toolchain.target.clone())?;

match manifestation.load_manifest()? {
Some(manifest) => Ok(Some(manifest.get_rust_version()?.to_string())),
None => Ok(None),
}
}

pub fn show_dist_version(&self) -> Result<Option<String>> {
let update_hash = self.update_hash()?;

match crate::dist::dist::dl_v2_manifest(
self.download_cfg(),
update_hash.as_ref().map(|p| &**p),
&self.desc()?,
)? {
Some((manifest, _)) => Ok(Some(manifest.get_rust_version()?.to_string())),
None => Ok(None),
}
}

pub fn list_components(&self) -> Result<Vec<ComponentStatus>> {
if !self.exists() {
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
Expand Down
96 changes: 90 additions & 6 deletions tests/cli-exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
pub mod mock;

use crate::mock::clitools::{
self, expect_err_ex, expect_ok, expect_ok_ex, this_host_triple, Config, Scenario,
self, expect_err_ex, expect_ok, expect_ok_ex, expect_stdout_ok, set_current_dist_date,
this_host_triple, Config, Scenario,
};

macro_rules! for_host {
Expand All @@ -14,7 +15,7 @@ macro_rules! for_host {
}

fn setup(f: &dyn Fn(&mut Config)) {
clitools::setup(Scenario::SimpleV2, f);
clitools::setup(Scenario::ArchivesV2, f);
}

#[test]
Expand All @@ -31,7 +32,7 @@ fn update() {
),
for_host!(
r"info: syncing channel updates for 'nightly-{0}'
info: latest update on 2015-01-02, rust version 1.3.0
info: latest update on 2015-01-02, rust version 1.3.0 (hash-n-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand Down Expand Up @@ -68,6 +69,89 @@ fn update_again() {
});
}

#[test]
fn check_updates_none() {
setup(&|config| {
set_current_dist_date(config, "2015-01-01");
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
expect_ok(config, &["rustup", "update", "nightly", "--no-self-update"]);
expect_stdout_ok(
config,
&["rustup", "check"],
for_host!(
r"stable-{0} - Up to date : 1.0.0 (hash-s-1)
beta-{0} - Up to date : 1.1.0 (hash-b-1)
nightly-{0} - Up to date : 1.2.0 (hash-n-1)
"
),
);
})
}

#[test]
fn check_updates_some() {
setup(&|config| {
set_current_dist_date(config, "2015-01-01");
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
expect_ok(config, &["rustup", "update", "nightly", "--no-self-update"]);
set_current_dist_date(config, "2015-01-02");
expect_stdout_ok(
config,
&["rustup", "check"],
for_host!(
r"stable-{0} - Update available : 1.0.0 (hash-s-1) -> 1.1.0 (hash-s-2)
beta-{0} - Update available : 1.1.0 (hash-b-1) -> 1.2.0 (hash-b-2)
nightly-{0} - Update available : 1.2.0 (hash-n-1) -> 1.3.0 (hash-n-2)
"
),
);
})
}

#[test]
fn check_updates_with_update() {
setup(&|config| {
set_current_dist_date(config, "2015-01-01");
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
expect_ok(config, &["rustup", "update", "nightly", "--no-self-update"]);
expect_stdout_ok(
config,
&["rustup", "check"],
for_host!(
r"stable-{0} - Up to date : 1.0.0 (hash-s-1)
beta-{0} - Up to date : 1.1.0 (hash-b-1)
nightly-{0} - Up to date : 1.2.0 (hash-n-1)
"
),
);
set_current_dist_date(config, "2015-01-02");
expect_stdout_ok(
config,
&["rustup", "check"],
for_host!(
r"stable-{0} - Update available : 1.0.0 (hash-s-1) -> 1.1.0 (hash-s-2)
beta-{0} - Update available : 1.1.0 (hash-b-1) -> 1.2.0 (hash-b-2)
nightly-{0} - Update available : 1.2.0 (hash-n-1) -> 1.3.0 (hash-n-2)
"
),
);
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
expect_stdout_ok(
config,
&["rustup", "check"],
for_host!(
r"stable-{0} - Update available : 1.0.0 (hash-s-1) -> 1.1.0 (hash-s-2)
beta-{0} - Up to date : 1.2.0 (hash-b-2)
nightly-{0} - Update available : 1.2.0 (hash-n-1) -> 1.3.0 (hash-n-2)
"
),
);
})
}

#[test]
fn default() {
setup(&|config| {
Expand All @@ -82,7 +166,7 @@ fn default() {
),
for_host!(
r"info: syncing channel updates for 'nightly-{0}'
info: latest update on 2015-01-02, rust version 1.3.0
info: latest update on 2015-01-02, rust version 1.3.0 (hash-n-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand Down Expand Up @@ -342,7 +426,7 @@ fn update_invalid_toolchain() {
&["rustup", "update", "nightly-2016-03-1"],
r"",
r"info: syncing channel updates for 'nightly-2016-03-1'
info: latest update on 2015-01-02, rust version 1.3.0
info: latest update on 2015-01-02, rust version 1.3.0 (hash-n-2)
error: target '2016-03-1' not found in channel. Perhaps check https://forge.rust-lang.org/platform-support.html for available targets
",
);
Expand All @@ -357,7 +441,7 @@ fn default_invalid_toolchain() {
&["rustup", "default", "nightly-2016-03-1"],
r"",
r"info: syncing channel updates for 'nightly-2016-03-1'
info: latest update on 2015-01-02, rust version 1.3.0
info: latest update on 2015-01-02, rust version 1.3.0 (hash-n-2)
error: target '2016-03-1' not found in channel. Perhaps check https://forge.rust-lang.org/platform-support.html for available targets
",
);
Expand Down
14 changes: 7 additions & 7 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn rustup_stable() {
),
for_host!(
r"info: syncing channel updates for 'stable-{0}'
info: latest update on 2015-01-02, rust version 1.1.0
info: latest update on 2015-01-02, rust version 1.1.0 (hash-s-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand Down Expand Up @@ -108,7 +108,7 @@ fn rustup_all_channels() {
),
for_host!(
r"info: syncing channel updates for 'stable-{0}'
info: latest update on 2015-01-02, rust version 1.1.0
info: latest update on 2015-01-02, rust version 1.1.0 (hash-s-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand All @@ -122,7 +122,7 @@ info: installing component 'cargo'
info: installing component 'rust-std'
info: installing component 'rust-docs'
info: syncing channel updates for 'beta-{0}'
info: latest update on 2015-01-02, rust version 1.2.0
info: latest update on 2015-01-02, rust version 1.2.0 (hash-b-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand All @@ -136,7 +136,7 @@ info: installing component 'cargo'
info: installing component 'rust-std'
info: installing component 'rust-docs'
info: syncing channel updates for 'nightly-{0}'
info: latest update on 2015-01-02, rust version 1.3.0
info: latest update on 2015-01-02, rust version 1.3.0 (hash-n-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand Down Expand Up @@ -177,7 +177,7 @@ fn rustup_some_channels_up_to_date() {
),
for_host!(
r"info: syncing channel updates for 'stable-{0}'
info: latest update on 2015-01-02, rust version 1.1.0
info: latest update on 2015-01-02, rust version 1.1.0 (hash-s-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand All @@ -192,7 +192,7 @@ info: installing component 'rust-std'
info: installing component 'rust-docs'
info: syncing channel updates for 'beta-{0}'
info: syncing channel updates for 'nightly-{0}'
info: latest update on 2015-01-02, rust version 1.3.0
info: latest update on 2015-01-02, rust version 1.3.0 (hash-n-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand Down Expand Up @@ -240,7 +240,7 @@ fn default() {
),
for_host!(
r"info: syncing channel updates for 'nightly-{0}'
info: latest update on 2015-01-02, rust version 1.3.0
info: latest update on 2015-01-02, rust version 1.3.0 (hash-n-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand Down
2 changes: 1 addition & 1 deletion tests/cli-self-upd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ fn first_install_exact() {
",
for_host!(
r"info: syncing channel updates for 'stable-{0}'
info: latest update on 2015-01-02, rust version 1.1.0
info: latest update on 2015-01-02, rust version 1.1.0 (hash-s-2)
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
Expand Down
Loading