Skip to content

Commit 1a1e440

Browse files
Artemis Livingstonesocial-anthrax
authored andcommitted
add new test and add fish script to add to path
1 parent d0608db commit 1a1e440

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
/local-rustup
1010
/snapcraft
1111
flake.lock
12+
.vscode/

src/cli/self_update/env.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
contains {cargo_bin} $fish_user_paths; or set -Ua fish_user_paths {cargo_bin}

src/cli/self_update/shell.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#![warn(clippy::all)]
2+
#![warn(clippy::pedantic)]
3+
#![allow(clippy::doc_markdown)]
4+
15
//! Paths and Unix shells
26
//!
37
//! MacOS, Linux, FreeBSD, and many other OS model their design on Unix,
@@ -95,7 +99,7 @@ pub(crate) trait UnixShell {
9599
// Gives rcs that should be written to.
96100
fn update_rcs(&self) -> Vec<PathBuf>;
97101

98-
// By defualt follows POSIX and sources <cargo home>/env
102+
// By default follows POSIX and sources <cargo home>/env
99103
fn add_to_path(&self) -> Result<(), anyhow::Error> {
100104
let source_cmd = self.source_string()?;
101105
let source_cmd_with_newline = format!("\n{}", &source_cmd);
@@ -249,13 +253,13 @@ impl Fish {
249253
#![allow(non_snake_case)]
250254
/// Gets fish vendor config location from `XDG_DATA_DIRS`
251255
/// Returns None if `XDG_DATA_DIRS` is not set or if there is no fis`fish/vendor_conf.d`.d directory found
252-
pub fn get_XDG_DATA_DIRS() -> Option<PathBuf> {
253-
#[cfg(test)]
254-
return None;
256+
pub fn get_vendor_config_from_XDG_DATA_DIRS() -> Option<PathBuf> {
257+
// Skip the directory during testing as we don't want to write into the XDG_DATA_DIRS by accident
258+
// TODO: Change the definition of XDG_DATA_DIRS in test so that doesn't happen
255259

260+
// TODO: Set up XDG DATA DIRS in a test to test the location being correct
256261
return process()
257262
.var("XDG_DATA_DIRS")
258-
// If var is found
259263
.map(|var| {
260264
var.split(':')
261265
.map(PathBuf::from)
@@ -274,14 +278,15 @@ impl UnixShell for Fish {
274278

275279
fn rcfiles(&self) -> Vec<PathBuf> {
276280
// As per https://fishshell.com/docs/current/language.html#configuration
277-
// Vendor config files should be written to `/fish/vendor_config.d/`
281+
// Vendor config files should be written to `/usr/share/fish/vendor_config.d/`
278282
// if that does not exist then it should be written to `/usr/share/fish/vendor_conf.d/`
279283
// otherwise it should be written to `$HOME/.config/fish/conf.d/ as per discussions in github issue #478
280284
[
281-
#[cfg(test)] // Write to test location so we don't pollute during testing.
282-
utils::home_dir().map(|home| home.join(".config/fish/conf.d/")),
283-
Self::get_XDG_DATA_DIRS(),
285+
// #[cfg(test)] // Write to test location so we don't pollute during testing.
286+
// utils::home_dir().map(|home| home.join(".config/fish/conf.d/")),
287+
Self::get_vendor_config_from_XDG_DATA_DIRS(),
284288
Some(PathBuf::from("/usr/share/fish/vendor_conf.d/")),
289+
Some(PathBuf::from("/usr/local/share/fish/vendor_conf.d/")),
285290
utils::home_dir().map(|home| home.join(".config/fish/conf.d/")),
286291
]
287292
.iter_mut()
@@ -291,17 +296,16 @@ impl UnixShell for Fish {
291296
}
292297

293298
fn update_rcs(&self) -> Vec<PathBuf> {
299+
// TODO: Change rcfiles to just read parent dirs
294300
self.rcfiles()
295301
.into_iter()
296302
.filter(|rc| {
297-
rc.metadata() // Returns error if path doesnt exist so separate check is not needed
298-
.map_or(false, |metadata| {
299-
dbg!(rc);
300-
dbg!(metadata.permissions());
301-
dbg!(metadata.permissions().readonly());
302-
dbg!();
303-
!metadata.permissions().readonly()
304-
})
303+
// We only want to check if the directory exists as in fish we create a new file for every applications env
304+
rc.parent().map_or(false, |parent| {
305+
parent
306+
.metadata() // Returns error if path doesn't exist so separate check is not needed
307+
.map_or(false, |metadata| !metadata.permissions().readonly())
308+
})
305309
})
306310
.collect()
307311
}
@@ -331,8 +335,8 @@ impl UnixShell for Fish {
331335

332336
fn env_script(&self) -> ShellScript {
333337
ShellScript {
334-
name: "env",
335-
content: include_str!("env.sh"),
338+
name: "env.fish",
339+
content: include_str!("env.fish"),
336340
}
337341
}
338342

@@ -346,13 +350,14 @@ impl UnixShell for Fish {
346350
}
347351

348352
pub(crate) fn legacy_paths() -> impl Iterator<Item = PathBuf> {
349-
let zprofiles = Zsh::zdotdir()
353+
let z_profiles = Zsh::zdotdir()
350354
.into_iter()
351355
.chain(utils::home_dir())
352356
.map(|d| d.join(".zprofile"));
353-
let profiles = [".bash_profile", ".profile"]
357+
358+
let bash_profiles = [".bash_profile", ".profile"]
354359
.iter()
355360
.filter_map(|rc| utils::home_dir().map(|d| d.join(rc)));
356361

357-
profiles.chain(zprofiles)
362+
bash_profiles.chain(z_profiles)
358363
}

tests/cli-paths.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ export PATH="$HOME/apple/bin"
7171
});
7272
}
7373

74+
#[test]
75+
fn install_creates_necessary_fish_scripts_new() {
76+
clitools::setup(Scenario::Empty, &|config| {
77+
let mut cmd = clitools::cmd(config, "rustup-init", &INIT_NONE[1..]);
78+
let rcs: Vec<PathBuf> = [".cargo/env.fish", ".config/fish/config.fish"]
79+
.iter()
80+
.map(|file| config.homedir.join(file))
81+
.collect();
82+
for file in &rcs {
83+
assert!(!file.exists());
84+
}
85+
cmd.env_remove("CARGO_HOME");
86+
cmd.env("SHELL", "fish");
87+
cmd.env("XDG_DATA_DIRS", ""); // Overwrite XDG as host shouldn't be affected by the test.
88+
assert!(cmd.output().unwrap().status.success());
89+
let expected = include_str!("../src/cli/self_update/env.fish")
90+
.replace("{cargo_bin}", "$HOME/.cargo/bin");
91+
92+
assert!(rcs
93+
.iter()
94+
.filter_map(|rc| fs::read_to_string(rc).ok()) // Read contents of files that exist
95+
.any(|rc_content| rc_content.contains(&expected)));
96+
})
97+
}
98+
7499
#[test]
75100
fn install_updates_bash_rcs() {
76101
clitools::setup(Scenario::Empty, &|config| {

tests/mock/clitools.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ pub fn setup(s: Scenario, f: &dyn Fn(&mut Config)) {
9999
env::remove_var("RUSTUP_TOOLCHAIN");
100100
env::remove_var("SHELL");
101101
env::remove_var("ZDOTDIR");
102-
// clap does its own terminal colour probing, and that isn't
102+
env::remove_var("XDG_DATA_DIRS");
103+
// clap does it's own terminal colour probing, and that isn't
103104
// trait-controllable, but it does honour the terminal. To avoid testing
104105
// claps code, lie about whatever terminal this process was started under.
105106
env::set_var("TERM", "dumb");

0 commit comments

Comments
 (0)