From 8d3df275c873503623a85b9c6aa4fc05d67aa68d Mon Sep 17 00:00:00 2001 From: Charlie Saunders Date: Sun, 30 Jun 2019 16:52:12 -0400 Subject: [PATCH 1/4] Fail with a more specific message if the shell profile file can't be modified. --- src/cli/errors.rs | 6 ++++++ src/cli/self_update.rs | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/cli/errors.rs b/src/cli/errors.rs index eb2dd1999f..5eaddbc452 100644 --- a/src/cli/errors.rs +++ b/src/cli/errors.rs @@ -57,5 +57,11 @@ error_chain! { ) display("`rustup target add {}` includes `all`", t.join(" ")) } + WritingShellProfile { + path: PathBuf, + } { + description("could not amend shell profile") + display("could not amend shell profile: '{}'", path.display()) + } } } diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index f559161338..552a7bd718 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -1107,7 +1107,9 @@ fn do_add_to_path(methods: &[PathUpdateMethod]) -> Result<()> { }; let addition = format!("\n{}", shell_export_string()?); if !file.contains(&addition) { - utils::append_file("rcfile", rcpath, &addition)?; + utils::append_file("rcfile", rcpath, &addition).chain_err(|| ErrorKind::WritingShellProfile { + path: rcpath.to_path_buf(), + })?; } } else { unreachable!() From 789199d67c5ec7e7aa37313722fa0953524f019b Mon Sep 17 00:00:00 2001 From: Charlie Saunders Date: Sun, 30 Jun 2019 16:55:57 -0400 Subject: [PATCH 2/4] Fix formatting. --- src/cli/self_update.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 552a7bd718..e0365d0b32 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -1107,8 +1107,10 @@ fn do_add_to_path(methods: &[PathUpdateMethod]) -> Result<()> { }; let addition = format!("\n{}", shell_export_string()?); if !file.contains(&addition) { - utils::append_file("rcfile", rcpath, &addition).chain_err(|| ErrorKind::WritingShellProfile { - path: rcpath.to_path_buf(), + utils::append_file("rcfile", rcpath, &addition).chain_err(|| { + ErrorKind::WritingShellProfile { + path: rcpath.to_path_buf(), + } })?; } } else { From f9b862423d564ad0a5565c0890bfed30ae79b12e Mon Sep 17 00:00:00 2001 From: Charlie Saunders Date: Sun, 30 Jun 2019 22:21:31 -0400 Subject: [PATCH 3/4] Add test for read-only rc profiles. --- tests/cli-self-upd.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/cli-self-upd.rs b/tests/cli-self-upd.rs index 01e040c08e..19970eb831 100644 --- a/tests/cli-self-upd.rs +++ b/tests/cli-self-upd.rs @@ -343,6 +343,24 @@ fn install_does_not_add_path_to_bash_profile_that_doesnt_exist() { }); } +#[test] +#[cfg(unix)] +fn install_errors_when_rc_file_cannot_be_updated() { + setup(&|config| { + let rc = config.homedir.join(".bash_profile"); + fs::File::create(&rc).unwrap(); + let mut perms = fs::metadata(&rc).unwrap().permissions(); + perms.set_readonly(true); + fs::set_permissions(&rc, perms).unwrap(); + + expect_err( + config, + &["rustup-init", "-y"], + "amend shell", + ); + }); +} + #[test] #[cfg(unix)] fn install_with_zsh_adds_path_to_zprofile() { From 188c2b1af879db3dd0b11a241d05a98f1582d730 Mon Sep 17 00:00:00 2001 From: Charlie Saunders Date: Sun, 30 Jun 2019 22:23:02 -0400 Subject: [PATCH 4/4] Fix formatting. --- tests/cli-self-upd.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/cli-self-upd.rs b/tests/cli-self-upd.rs index 19970eb831..1f8c1351f3 100644 --- a/tests/cli-self-upd.rs +++ b/tests/cli-self-upd.rs @@ -353,11 +353,7 @@ fn install_errors_when_rc_file_cannot_be_updated() { perms.set_readonly(true); fs::set_permissions(&rc, perms).unwrap(); - expect_err( - config, - &["rustup-init", "-y"], - "amend shell", - ); + expect_err(config, &["rustup-init", "-y"], "amend shell"); }); }