| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "bundle" | 
					
						
							| 
									
										
										
										
											2025-03-24 21:55:47 +08:00
										 |  |  | require "bundle/vscode_extension_installer" | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  | require "extend/kernel" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe Homebrew::Bundle::VscodeExtensionInstaller do | 
					
						
							|  |  |  |   context "when VSCode is not installed" do | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       described_class.reset! | 
					
						
							|  |  |  |       allow(Homebrew::Bundle).to receive_messages(vscode_installed?: false, cask_installed?: true) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "tries to install vscode" do | 
					
						
							|  |  |  |       expect(Homebrew::Bundle).to \ | 
					
						
							|  |  |  |         receive(:system).with(HOMEBREW_BREW_FILE, "install", "--cask", "visual-studio-code", verbose: false) | 
					
						
							|  |  |  |                         .and_return(true) | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |       expect { described_class.preinstall!("foo") }.to raise_error(RuntimeError) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "when VSCode is installed" do | 
					
						
							|  |  |  |     before do | 
					
						
							| 
									
										
										
										
											2025-03-19 11:42:37 +00:00
										 |  |  |       allow(Homebrew::Bundle).to receive(:which_vscode).and_return(Pathname("code")) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when extension is installed" do | 
					
						
							|  |  |  |       before do | 
					
						
							|  |  |  |         allow(described_class).to receive(:installed_extensions).and_return(["foo"]) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "skips" do | 
					
						
							|  |  |  |         expect(Homebrew::Bundle).not_to receive(:system) | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |         expect(described_class.preinstall!("foo")).to be(false) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "skips ignoring case" do | 
					
						
							|  |  |  |         expect(Homebrew::Bundle).not_to receive(:system) | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |         expect(described_class.preinstall!("Foo")).to be(false) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when extension is not installed" do | 
					
						
							|  |  |  |       before do | 
					
						
							|  |  |  |         allow(described_class).to receive(:installed_extensions).and_return([]) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "installs extension" do | 
					
						
							| 
									
										
										
										
											2025-03-19 11:42:37 +00:00
										 |  |  |         expect(Homebrew::Bundle).to \ | 
					
						
							|  |  |  |           receive(:system).with(Pathname("code"), "--install-extension", "foo", verbose: false).and_return(true) | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |         expect(described_class.preinstall!("foo")).to be(true) | 
					
						
							|  |  |  |         expect(described_class.install!("foo")).to be(true) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "installs extension when euid != uid and Process::UID.re_exchangeable? returns true" do | 
					
						
							|  |  |  |         expect(Process).to receive(:euid).and_return(1).once | 
					
						
							|  |  |  |         expect(Process::UID).to receive(:re_exchangeable?).and_return(true).once | 
					
						
							|  |  |  |         expect(Process::UID).to receive(:re_exchange).twice | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-19 11:42:37 +00:00
										 |  |  |         expect(Homebrew::Bundle).to \ | 
					
						
							|  |  |  |           receive(:system).with(Pathname("code"), "--install-extension", "foo", verbose: false).and_return(true) | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |         expect(described_class.preinstall!("foo")).to be(true) | 
					
						
							|  |  |  |         expect(described_class.install!("foo")).to be(true) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "installs extension when euid != uid and Process::UID.re_exchangeable? returns false" do | 
					
						
							|  |  |  |         expect(Process).to receive(:euid).and_return(1).once | 
					
						
							|  |  |  |         expect(Process::UID).to receive(:re_exchangeable?).and_return(false).once | 
					
						
							|  |  |  |         expect(Process::Sys).to receive(:seteuid).twice | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-19 11:42:37 +00:00
										 |  |  |         expect(Homebrew::Bundle).to \ | 
					
						
							|  |  |  |           receive(:system).with(Pathname("code"), "--install-extension", "foo", verbose: false).and_return(true) | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |         expect(described_class.preinstall!("foo")).to be(true) | 
					
						
							|  |  |  |         expect(described_class.install!("foo")).to be(true) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |