use std::path::{Path, PathBuf};
use crate::config::Position;
use crate::context::SystemContext;
use crate::shell::{Shell, ShellKind};
pub struct Xonsh;
impl Shell for Xonsh {
fn kind(&self) -> ShellKind {
ShellKind::Xonsh
}
fn env_extension(&self) -> &'static str {
".xsh"
}
fn env_script(&self, dir: &Path, position: Position) -> String {
let dir = dir.display();
let insert = match position {
Position::Prepend => format!("$PATH.insert(0, \"{dir}\")"),
Position::Append => format!("$PATH.append(\"{dir}\")"),
};
format!(
r#"# Generated by onpath. Do not edit.
import os
if "{dir}" not in os.environ.get("PATH", "").split(os.pathsep):
{insert}
"#
)
}
fn source_line(&self, env_script_path: &Path) -> String {
format!("source \"{}\"", env_script_path.display())
}
fn rc_candidates(&self, ctx: &SystemContext) -> Vec<PathBuf> {
vec![ctx.home_dir().join(".xonshrc")]
}
fn primary_rc(&self, ctx: &SystemContext) -> PathBuf {
ctx.home_dir().join(".xonshrc")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn env_script_uses_python_syntax() {
let script = Xonsh.env_script(Path::new("/app/bin"), Position::Prepend);
assert!(script.contains("import os"));
assert!(script.contains("os.pathsep"));
assert!(script.contains("$PATH.insert(0,"));
}
#[test]
fn env_script_append() {
let script = Xonsh.env_script(Path::new("/app/bin"), Position::Append);
assert!(script.contains("$PATH.append("));
}
#[test]
fn primary_rc_is_xonshrc() {
let ctx = SystemContext::with_home(PathBuf::from("/home/user"));
assert_eq!(Xonsh.primary_rc(&ctx), PathBuf::from("/home/user/.xonshrc"));
}
}