Skip to content

Allow selecting toolchains to uninstall with glob pattern #2540

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

Closed
wants to merge 9 commits into from
Closed
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ download = {path = "download"}
error-chain = "0.12"
flate2 = "1"
git-testament = "0.1.4"
glob = "0.3"
home = {git = "https://github.com/rbtcollins/home", rev = "a243ee2fbee6022c57d56f5aa79aefe194eabe53"}
lazy_static = "1"
libc = "0.2"
Expand Down
1 change: 1 addition & 0 deletions src/cli/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ error_chain! {
Temp(temp::Error);
Io(io::Error);
Term(term::Error);
Glob(glob::PatternError);
}

errors {
Expand Down
20 changes: 17 additions & 3 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::process::Command;
use std::str::FromStr;

use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, Shell, SubCommand};
use glob::Pattern;

use super::common;
use super::errors::*;
Expand Down Expand Up @@ -1312,10 +1313,23 @@ fn toolchain_link(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
}

fn toolchain_remove(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
for toolchain in m.values_of("toolchain").unwrap() {
let toolchain = cfg.get_toolchain(toolchain, false)?;
toolchain.remove()?;
for pattern_str in m.values_of("toolchain").unwrap() {
let pattern = Pattern::new(&pattern_str)?;

let mut toolchains = cfg.get_toolchains_from_glob(pattern)?.peekable();

if toolchains.peek().is_some() {
// This pattern matched some toolchains, so remove each of the ones it matched.
for toolchain in toolchains {
toolchain.remove()?;
}
} else {
// It didn't match any toolchains, so treat it as a partial toolchain specifier.
let toolchain = cfg.get_toolchain(pattern_str, false)?;
toolchain.remove()?;
}
}

Ok(utils::ExitCode(0))
}

Expand Down
35 changes: 25 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::borrow::Cow;
use std::fmt::{self, Display};
use std::io;
use std::iter;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::sync::Arc;

use glob::Pattern;
use pgp::{Deserializable, SignedPublicKey};
use serde::Deserialize;

Expand Down Expand Up @@ -362,6 +364,16 @@ impl Cfg {
Toolchain::from(self, name)
}

pub fn get_toolchains_from_glob(
&self,
pattern: Pattern,
) -> Result<impl Iterator<Item = Toolchain<'_>>> {
Ok(self
.list_toolchains_iter()?
.filter(move |toolchain| pattern.matches(toolchain))
.map(move |toolchain| Toolchain::from(self, &toolchain).unwrap()))
}

pub fn verify_toolchain(&self, name: &str) -> Result<Toolchain<'_>> {
let toolchain = self.get_toolchain(name, false)?;
toolchain.verify()?;
Expand Down Expand Up @@ -694,18 +706,21 @@ impl Cfg {
}

pub fn list_toolchains(&self) -> Result<Vec<String>> {
if utils::is_directory(&self.toolchains_dir) {
let mut toolchains: Vec<_> = utils::read_dir("toolchains", &self.toolchains_dir)?
.filter_map(io::Result::ok)
.filter(|e| e.file_type().map(|f| !f.is_file()).unwrap_or(false))
.filter_map(|e| e.file_name().into_string().ok())
.collect();

utils::toolchain_sort(&mut toolchains);
let mut toolchains: Vec<_> = self.list_toolchains_iter()?.collect();
utils::toolchain_sort(&mut toolchains);
Ok(toolchains)
}

Ok(toolchains)
fn list_toolchains_iter(&self) -> Result<Box<dyn Iterator<Item = String>>> {
if utils::is_directory(&self.toolchains_dir) {
Ok(Box::new(
utils::read_dir("toolchains", &self.toolchains_dir)?
.filter_map(io::Result::ok)
.filter(|e| e.file_type().map(|f| !f.is_file()).unwrap_or(false))
.filter_map(|e| e.file_name().into_string().ok()),
))
} else {
Ok(Vec::new())
Ok(Box::new(iter::empty()))
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,21 @@ fn toolchain_uninstall_is_like_uninstall() {
});
}

#[test]
fn toolchain_uninstall_pattern() {
setup(&|config| {
expect_ok(config, &["rustup", "uninstall", "stable-*"]);
expect_ok(config, &["rustup", "uninstall", "nightly-*"]);
let mut cmd = clitools::cmd(config, "rustup", &["show"]);
clitools::env(config, &mut cmd);
let out = cmd.output().unwrap();
assert!(out.status.success());
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(!stdout.contains(for_host!("'stable-{}'")));
assert!(!stdout.contains(for_host!("'nightly-2015-01-01-{}'")));
});
}
Comment on lines +1227 to +1240
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied this test code from other tests and modified it. Is the test correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to start the test by installing and asserting that stable and nightly are there. Otherwise I think this is fine.


#[test]
fn toolchain_update_is_like_update_except_that_bare_install_is_an_error() {
setup(&|config| {
Expand Down
4 changes: 2 additions & 2 deletions tests/mock/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ pub fn setup(s: Scenario, f: &dyn Fn(&mut Config)) {
env::remove_var("RUSTUP_TOOLCHAIN");
env::remove_var("SHELL");
env::remove_var("ZDOTDIR");
// clap does it's own terminal colour probing, and that isn't
// clap does its own terminal colour probing, and that isn't
// trait-controllable, but it does honour the terminal. To avoid testing
// claps code, lie about whatever terminal this process was started under.
// clap's code, lie about whatever terminal this process was started under.
env::set_var("TERM", "dumb");

match env::var("RUSTUP_BACKTRACE") {
Expand Down