| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  | require "utils/shell" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Utils::Shell do | 
					
						
							| 
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 |  |  |   describe "::profile" do | 
					
						
							| 
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 |  |  |     it "returns ~/.profile by default" do | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |       ENV["SHELL"] = "/bin/another_shell" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.profile).to eq("~/.profile") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 |  |  |     it "returns ~/.profile for sh" do | 
					
						
							| 
									
										
										
										
											2018-10-20 08:53:19 +02:00
										 |  |  |       ENV["SHELL"] = "/bin/sh" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.profile).to eq("~/.profile") | 
					
						
							| 
									
										
										
										
											2018-10-20 08:53:19 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 |  |  |     it "returns ~/.profile for Bash" do | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |       ENV["SHELL"] = "/bin/bash" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.profile).to eq("~/.profile") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 12:03:01 -04:00
										 |  |  |     it "returns /tmp/.zshrc for Zsh if ZDOTDIR is /tmp" do | 
					
						
							|  |  |  |       ENV["SHELL"] = "/bin/zsh" | 
					
						
							|  |  |  |       ENV["ZDOTDIR"] = "/tmp" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.profile).to eq("/tmp/.zshrc") | 
					
						
							| 
									
										
										
										
											2019-10-06 12:03:01 -04:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     it "returns ~/.zshrc for Zsh" do | 
					
						
							|  |  |  |       ENV["SHELL"] = "/bin/zsh" | 
					
						
							| 
									
										
										
										
											2019-10-06 12:03:01 -04:00
										 |  |  |       ENV["ZDOTDIR"] = nil | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.profile).to eq("~/.zshrc") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns ~/.kshrc for Ksh" do | 
					
						
							|  |  |  |       ENV["SHELL"] = "/bin/ksh" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.profile).to eq("~/.kshrc") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 |  |  |   describe "::from_path" do | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     it "supports a raw command name" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.from_path("bash")).to eq(:bash) | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "supports full paths" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.from_path("/bin/bash")).to eq(:bash) | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "supports versions" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.from_path("zsh-5.2")).to eq(:zsh) | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "strips newlines" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.from_path("zsh-5.2\n")).to eq(:zsh) | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns nil when input is invalid" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.from_path("")).to be nil | 
					
						
							|  |  |  |       expect(described_class.from_path("@@@@@@")).to be nil | 
					
						
							|  |  |  |       expect(described_class.from_path("invalid_shell-4.2")).to be nil | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "::sh_quote" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |     expect(described_class.send(:sh_quote, "")).to eq("''") | 
					
						
							|  |  |  |     expect(described_class.send(:sh_quote, "\\")).to eq("\\\\") | 
					
						
							|  |  |  |     expect(described_class.send(:sh_quote, "\n")).to eq("'\n'") | 
					
						
							|  |  |  |     expect(described_class.send(:sh_quote, "$")).to eq("\\$") | 
					
						
							|  |  |  |     expect(described_class.send(:sh_quote, "word")).to eq("word") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "::csh_quote" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |     expect(described_class.send(:csh_quote, "")).to eq("''") | 
					
						
							|  |  |  |     expect(described_class.send(:csh_quote, "\\")).to eq("\\\\") | 
					
						
							| 
									
										
										
										
											2020-11-13 09:49:06 -05:00
										 |  |  |     # NOTE: this test is different than for sh | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |     expect(described_class.send(:csh_quote, "\n")).to eq("'\\\n'") | 
					
						
							|  |  |  |     expect(described_class.send(:csh_quote, "$")).to eq("\\$") | 
					
						
							|  |  |  |     expect(described_class.send(:csh_quote, "word")).to eq("word") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 |  |  |   describe "::prepend_path_in_profile" do | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     let(:path) { "/my/path" } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 17:31:11 -04:00
										 |  |  |     it "supports tcsh" do | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |       ENV["SHELL"] = "/bin/tcsh" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.prepend_path_in_profile(path)) | 
					
						
							| 
									
										
										
										
											2018-05-12 11:47:12 -05:00
										 |  |  |         .to eq("echo 'setenv PATH #{path}:$PATH' >> #{shell_profile}") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "supports Bash" do | 
					
						
							|  |  |  |       ENV["SHELL"] = "/bin/bash" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.prepend_path_in_profile(path)) | 
					
						
							| 
									
										
										
										
											2018-05-12 11:47:12 -05:00
										 |  |  |         .to eq("echo 'export PATH=\"#{path}:$PATH\"' >> #{shell_profile}") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 17:31:11 -04:00
										 |  |  |     it "supports fish" do | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |       ENV["SHELL"] = "/usr/local/bin/fish" | 
					
						
							| 
									
										
										
										
											2018-05-12 11:47:12 -05:00
										 |  |  |       ENV["fish_user_paths"] = "/some/path" | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.prepend_path_in_profile(path)) | 
					
						
							| 
									
										
										
										
											2018-05-12 11:47:12 -05:00
										 |  |  |         .to eq("echo 'set -g fish_user_paths \"#{path}\" $fish_user_paths' >> #{shell_profile}") | 
					
						
							| 
									
										
										
										
											2017-02-17 18:35:26 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |