| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "utils/autoremove" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Utils::Autoremove do | 
					
						
							|  |  |  |   shared_context "with formulae for dependency testing" do | 
					
						
							|  |  |  |     let(:formula_with_deps) do | 
					
						
							|  |  |  |       formula "zero" do | 
					
						
							|  |  |  |         url "zero-1.0" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         depends_on "three" => :build | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |     let(:first_formula_dep) do | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |       formula "one" do | 
					
						
							|  |  |  |         url "one-1.1" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |     let(:second_formula_dep) do | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |       formula "two" do | 
					
						
							|  |  |  |         url "two-1.1" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let(:formula_is_build_dep) do | 
					
						
							|  |  |  |       formula "three" do | 
					
						
							|  |  |  |         url "three-1.1" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let(:formulae) do | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         formula_with_deps, | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |         first_formula_dep, | 
					
						
							|  |  |  |         second_formula_dep, | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |         formula_is_build_dep, | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let(:tab_from_keg) { double } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |       allow(formula_with_deps).to receive(:runtime_formula_dependencies).and_return([first_formula_dep, | 
					
						
							|  |  |  |                                                                                      second_formula_dep]) | 
					
						
							|  |  |  |       allow(first_formula_dep).to receive(:runtime_formula_dependencies).and_return([second_formula_dep]) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       allow(Tab).to receive(:for_keg).and_return(tab_from_keg) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::formulae_with_no_formula_dependents" do | 
					
						
							|  |  |  |     include_context "with formulae for dependency testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       allow(Formulary).to receive(:factory).with("three").and_return(formula_is_build_dep) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when formulae are bottles" do | 
					
						
							|  |  |  |       it "filters out runtime dependencies" do | 
					
						
							|  |  |  |         allow(tab_from_keg).to receive(:poured_from_bottle).and_return(true) | 
					
						
							|  |  |  |         expect(described_class.send(:formulae_with_no_formula_dependents, formulae)) | 
					
						
							| 
									
										
										
										
											2023-03-03 22:13:41 +00:00
										 |  |  |           .to eq([formula_with_deps, formula_is_build_dep]) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when formulae are built from source" do | 
					
						
							|  |  |  |       it "filters out runtime and build dependencies" do | 
					
						
							|  |  |  |         allow(tab_from_keg).to receive(:poured_from_bottle).and_return(false) | 
					
						
							|  |  |  |         expect(described_class.send(:formulae_with_no_formula_dependents, formulae)) | 
					
						
							| 
									
										
										
										
											2023-03-03 22:13:41 +00:00
										 |  |  |           .to eq([formula_with_deps]) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::unused_formulae_with_no_formula_dependents" do | 
					
						
							|  |  |  |     include_context "with formulae for dependency testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       allow(tab_from_keg).to receive(:poured_from_bottle).and_return(true) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     specify "installed on request" do | 
					
						
							|  |  |  |       allow(tab_from_keg).to receive(:installed_on_request).and_return(true) | 
					
						
							|  |  |  |       expect(described_class.send(:unused_formulae_with_no_formula_dependents, formulae)) | 
					
						
							| 
									
										
										
										
											2023-03-03 22:13:41 +00:00
										 |  |  |         .to eq([]) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     specify "not installed on request" do | 
					
						
							|  |  |  |       allow(tab_from_keg).to receive(:installed_on_request).and_return(false) | 
					
						
							|  |  |  |       expect(described_class.send(:unused_formulae_with_no_formula_dependents, formulae)) | 
					
						
							| 
									
										
										
										
											2023-03-03 22:13:41 +00:00
										 |  |  |         .to match_array(formulae) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   shared_context "with formulae and casks for dependency testing" do | 
					
						
							|  |  |  |     include_context "with formulae for dependency testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     require "cask/cask_loader" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let(:cask_one_dep) do | 
					
						
							|  |  |  |       Cask::CaskLoader.load(+<<-RUBY)
 | 
					
						
							|  |  |  |         cask "red" do | 
					
						
							|  |  |  |           depends_on formula: "two" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let(:cask_multiple_deps) do | 
					
						
							|  |  |  |       Cask::CaskLoader.load(+<<-RUBY)
 | 
					
						
							|  |  |  |         cask "blue" do | 
					
						
							|  |  |  |           depends_on formula: "zero" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |     let(:first_cask_no_deps) do | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |       Cask::CaskLoader.load(+<<-RUBY)
 | 
					
						
							|  |  |  |         cask "green" do | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |     let(:second_cask_no_deps) do | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |       Cask::CaskLoader.load(+<<-RUBY)
 | 
					
						
							|  |  |  |         cask "purple" do | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |     let(:casks_no_deps) { [first_cask_no_deps, second_cask_no_deps] } | 
					
						
							|  |  |  |     let(:casks_one_dep) { [first_cask_no_deps, second_cask_no_deps, cask_one_dep] } | 
					
						
							|  |  |  |     let(:casks_multiple_deps) { [first_cask_no_deps, second_cask_no_deps, cask_multiple_deps] } | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       allow(Formula).to receive("[]").with("zero").and_return(formula_with_deps) | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |       allow(Formula).to receive("[]").with("one").and_return(first_formula_dep) | 
					
						
							|  |  |  |       allow(Formula).to receive("[]").with("two").and_return(second_formula_dep) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::formulae_with_cask_dependents" do | 
					
						
							|  |  |  |     include_context "with formulae and casks for dependency testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     specify "no dependents" do | 
					
						
							|  |  |  |       expect(described_class.send(:formulae_with_cask_dependents, casks_no_deps)) | 
					
						
							|  |  |  |         .to eq([]) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     specify "one dependent" do | 
					
						
							|  |  |  |       expect(described_class.send(:formulae_with_cask_dependents, casks_one_dep)) | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |         .to eq([second_formula_dep]) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     specify "multiple dependents" do | 
					
						
							|  |  |  |       expect(described_class.send(:formulae_with_cask_dependents, casks_multiple_deps)) | 
					
						
							| 
									
										
										
										
											2023-04-20 12:14:45 +00:00
										 |  |  |         .to contain_exactly(formula_with_deps, first_formula_dep, second_formula_dep) | 
					
						
							| 
									
										
										
										
											2022-09-13 23:23:48 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |