diff --git a/src/shims.rs b/src/shims.rs index 4b102d2..3c54484 100644 --- a/src/shims.rs +++ b/src/shims.rs @@ -12,13 +12,17 @@ use crate::metadata; fn create_shim(binary: &str, bin_path: path::PathBuf) -> Result<()> { use std::os::unix::prelude::OpenOptionsExt; - let shell = env::var("SHELL") + let mut shell = env::var("SHELL") .unwrap_or("bash".to_string()) .split('/') .last() .unwrap() .to_string(); + if !vec!["bash", "zsh", "sh"].contains(&&*shell) { + shell = "sh".to_string(); + } + let script = format!( r#"#!/usr/bin/env {shell} diff --git a/tests/shims_test.rs b/tests/shims_test.rs index 3157b56..197cd0b 100644 --- a/tests/shims_test.rs +++ b/tests/shims_test.rs @@ -1,20 +1,74 @@ +use anyhow::Result; +use std::fs; +use cargo_run_bin::metadata; + +fn clean_shims() -> Result<()> { + let bin_dir = metadata::get_project_root()?.join(".bin/.shims"); + if bin_dir.exists() { + fs::remove_dir_all(&bin_dir)?; + } + + return Ok(()); +} + mod sync_shims { - use cargo_run_bin::metadata; + use std::fs; use cargo_run_bin::shims::sync; + use cargo_run_bin::metadata; + use super::clean_shims; #[test] #[cfg(target_family = "unix")] fn it_creates_shims() { + clean_shims().unwrap(); let res = sync(); let exists = metadata::get_project_root() .unwrap() .join(".bin/.shims/hello-world-first") .exists(); + clean_shims().unwrap(); assert!(res.is_ok()); assert!(exists); } + #[test] + #[cfg(target_family = "unix")] + fn it_creates_shims_for_zsh() { + std::env::set_var("SHELL", "zsh"); + clean_shims().unwrap(); + let res = sync(); + assert!(res.is_ok()); + + let shim_path = metadata::get_project_root() + .unwrap() + .join(".bin/.shims/hello-world-first"); + + let content = fs::read_to_string(shim_path).unwrap(); + + clean_shims().unwrap(); + assert!(content.starts_with("#!/usr/bin/env zsh")); + } + + #[test] + #[cfg(target_family = "unix")] + fn it_creates_shims_falling_back_to_sh() { + std::env::set_var("SHELL", "fish"); + clean_shims().unwrap(); + let res = sync(); + assert!(res.is_ok()); + + let shim_path = metadata::get_project_root() + .unwrap() + .join(".bin/.shims/hello-world-first"); + + let content = fs::read_to_string(shim_path).unwrap(); + + clean_shims().unwrap(); + assert!(content.starts_with("#!/usr/bin/env sh")); + } + + #[test] #[cfg(not(target_family = "unix"))] fn it_creates_shims() {