sauce/shell/kinds/
bash.rs1use crate::shell::utilities::{escape, qualify_binary_path};
2use crate::shell::Shell;
3use std::fmt::Write;
4
5pub struct Bash;
6
7impl Shell for Bash {
8 fn name(&self) -> &'static str {
9 "bash"
10 }
11
12 fn init(&self, binary: &str, autoload_hook: bool, autoload_args: &str) -> String {
13 let mut init = format!(
14 include_str!("bash_init.sh"),
15 binary,
16 qualify_binary_path(binary),
17 );
18
19 if autoload_hook {
20 write!(
21 init,
22 include_str!("bash_init_autoload.sh"),
23 binary, autoload_args,
24 )
25 .ok();
26 }
27
28 init
29 }
30
31 fn set_var(&self, var: &str, value: &str) -> String {
32 format!("export {}={}", var, escape(value))
33 }
34
35 fn set_alias(&self, var: &str, value: &str) -> String {
36 format!("alias {}={}", var, escape(value))
37 }
38
39 fn set_function(&self, var: &str, value: &str) -> String {
40 format!("function {} {{\n {}\n}}", var, value.replace('\n', "\n "))
41 }
42
43 fn unset_var(&self, var: &str) -> String {
44 format!("unset {}", var)
45 }
46
47 fn unset_alias(&self, var: &str) -> String {
48 format!("unalias {} 2>/dev/null", var)
49 }
50
51 fn unset_function(&self, var: &str) -> String {
52 format!("unset -f {}", var)
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 mod edit {
59 use std::ffi::OsString;
60
61 use super::super::*;
62 use pretty_assertions::assert_eq;
63
64 #[test]
65 fn it_edits_path() {
66 let shell = Bash {};
67 let output = shell.edit(Some(OsString::from("foo")), "foo/bar");
68 assert_eq!(output, Some(r#"foo 'foo/bar'"#.to_string()));
69 }
70 }
71
72 mod init {
73 use super::super::*;
74 use pretty_assertions::assert_eq;
75
76 #[test]
77 fn it_defaults() {
78 let shell = Bash {};
79 let output = shell.init("foo", false, "");
80 assert_eq!(
81 output,
82 "function foo {\n eval \"$(command foo --shell bash \"$@\")\"\n}\n"
83 );
84 }
85
86 #[test]
87 fn it_autoloads() {
88 let shell = Bash {};
89 let output = shell.init("foo", true, "");
90 assert_eq!(output.contains("--autoload"), true);
91 }
92 }
93
94 mod set_var {
95 use super::super::*;
96 use pretty_assertions::assert_eq;
97
98 #[test]
99 fn it_works() {
100 let shell = Bash {};
101 let output = shell.set_var("foo", "bar");
102 assert_eq!(output, "export foo=bar");
103 }
104 }
105
106 mod set_alias {
107 use super::super::*;
108 use pretty_assertions::assert_eq;
109
110 #[test]
111 fn it_works() {
112 let shell = Bash {};
113 let output = shell.set_alias("foo", "bar");
114 assert_eq!(output, "alias foo=bar");
115 }
116 }
117
118 mod set_function {
119 use super::super::*;
120 use pretty_assertions::assert_eq;
121
122 #[test]
123 fn it_works() {
124 let shell = Bash {};
125 let output = shell.set_function("foo", "bar");
126 assert_eq!(output, "function foo {\n bar\n}");
127 }
128 }
129
130 mod unset_var {
131 use super::super::*;
132 use pretty_assertions::assert_eq;
133
134 #[test]
135 fn it_works() {
136 let shell = Bash {};
137 let output = shell.unset_var("foo");
138 assert_eq!(output, "unset foo");
139 }
140 }
141
142 mod unset_alias {
143 use super::super::*;
144 use pretty_assertions::assert_eq;
145
146 #[test]
147 fn it_works() {
148 let shell = Bash {};
149 let output = shell.unset_alias("foo");
150 assert_eq!(output, "unalias foo 2>/dev/null");
151 }
152 }
153
154 mod unset_function {
155 use super::super::*;
156 use pretty_assertions::assert_eq;
157
158 #[test]
159 fn it_works() {
160 let shell = Bash {};
161 let output = shell.unset_function("foo");
162 assert_eq!(output, "unset -f foo");
163 }
164 }
165}