Skip to content

Commit

Permalink
fix: Fallback to sh for non POSIX shells
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinblackman committed Jul 5, 2024
1 parent 1dd54c4 commit 7281bbb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/shims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
56 changes: 55 additions & 1 deletion tests/shims_test.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit 7281bbb

Please sign in to comment.