Skip to content

Commit 651fa8e

Browse files
committed
Handle updating multiple rc files from one shell
1 parent b6cc0f9 commit 651fa8e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/cli/self_update/unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn do_add_to_path() -> Result<()> {
8787

8888
for rc in sh.update_rcs() {
8989
let cmd_to_write = match utils::read_file("rcfile", &rc) {
90-
Ok(contents) if contents.contains(&source_cmd) => break,
90+
Ok(contents) if contents.contains(&source_cmd) => continue,
9191
Ok(contents) if !contents.ends_with('\n') => &source_cmd_with_newline,
9292
_ => &source_cmd,
9393
};

tests/cli-paths.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ export PATH="$HOME/apple/bin"
156156
let profile = config.homedir.join(".profile");
157157
let fake_rc_modified = FAKE_RC.strip_suffix('\n').expect("Should end in a newline");
158158
raw::write_file(&profile, fake_rc_modified).unwrap();
159+
// Run once to to add the configuration
159160
expect_ok(config, &INIT_NONE);
161+
// Run twice to test that the process is idempotent
160162
expect_ok(config, &INIT_NONE);
161163

162164
let new_profile = fs::read_to_string(&profile).unwrap();
@@ -165,6 +167,32 @@ export PATH="$HOME/apple/bin"
165167
});
166168
}
167169

170+
171+
#[test]
172+
fn install_adds_path_to_multiple_rc_files() {
173+
clitools::setup(Scenario::Empty, &|config| {
174+
// Two RC files that are both from the same shell
175+
let bash_profile = config.homedir.join(".bash_profile");
176+
let bashrc = config.homedir.join(".bashrc");
177+
178+
let expected = FAKE_RC.to_owned() + &source(config.cargodir.display(), POSIX_SH);
179+
180+
// The order that the two files are processed isn't known, so test both orders
181+
for [path1, path2] in &[[&bash_profile, &bashrc], [&bashrc, &bash_profile]] {
182+
183+
raw::write_file(&path1, &expected).unwrap();
184+
raw::write_file(&path2, FAKE_RC).unwrap();
185+
186+
expect_ok(config, &INIT_NONE);
187+
188+
let new1 = fs::read_to_string(&path1).unwrap();
189+
assert_eq!(new1, expected);
190+
let new2 = fs::read_to_string(&path2).unwrap();
191+
assert_eq!(new2, expected);
192+
}
193+
});
194+
}
195+
168196
#[test]
169197
fn uninstall_removes_source_from_rcs() {
170198
clitools::setup(Scenario::Empty, &|config| {

0 commit comments

Comments
 (0)