| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 04:41:49 +05:30
										 |  |  | require "utils/analytics" | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  | require "formula_installer" | 
					
						
							| 
									
										
										
										
											2017-07-27 04:41:49 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | describe Utils::Analytics do | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |   describe "::os_arch_prefix_ci" do | 
					
						
							|  |  |  |     context "when os_arch_prefix_ci is not set" do | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |       before do | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |         described_class.clear_os_arch_prefix_ci | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 16:58:17 -07:00
										 |  |  |       it "returns OS_VERSION and prefix when HOMEBREW_PREFIX is a custom prefix" do | 
					
						
							| 
									
										
										
										
											2021-06-15 20:35:21 -07:00
										 |  |  |         allow(Hardware::CPU).to receive(:type).and_return(:intel) | 
					
						
							| 
									
										
										
										
											2019-01-21 14:33:56 +00:00
										 |  |  |         allow(Homebrew).to receive(:default_prefix?).and_return(false) | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |         expect(described_class.os_arch_prefix_ci).to include("#{OS_VERSION}, #{described_class.custom_prefix_label}") | 
					
						
							| 
									
										
										
										
											2017-07-27 04:41:49 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 14:33:56 +00:00
										 |  |  |       it "does not include prefix when HOMEBREW_PREFIX is the default prefix" do | 
					
						
							| 
									
										
										
										
											2021-01-29 09:11:35 +00:00
										 |  |  |         allow(Homebrew).to receive(:default_prefix?).and_return(true) | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |         expect(described_class.os_arch_prefix_ci).not_to include(described_class.custom_prefix_label) | 
					
						
							| 
									
										
										
										
											2019-01-21 14:33:56 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 04:41:49 +05:30
										 |  |  |       it "includes CI when ENV['CI'] is set" do | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |         ENV["CI"] = "true" | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |         expect(described_class.os_arch_prefix_ci).to include("CI") | 
					
						
							| 
									
										
										
										
											2017-07-27 04:41:49 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |   describe "::report_event" do | 
					
						
							|  |  |  |     let(:f) { formula { url "foo-1.0" } } | 
					
						
							|  |  |  |     let(:options) { FormulaInstaller.new(f).display_options(f) } | 
					
						
							|  |  |  |     let(:action)  { "#{f.full_name} #{options}".strip } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when ENV vars is set" do | 
					
						
							|  |  |  |       it "returns nil when HOMEBREW_NO_ANALYTICS is true" do | 
					
						
							|  |  |  |         ENV["HOMEBREW_NO_ANALYTICS"] = "true" | 
					
						
							|  |  |  |         expect(described_class.report_event("install", action)).to be_nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "returns nil when HOMEBREW_NO_ANALYTICS_THIS_RUN is true" do | 
					
						
							|  |  |  |         ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "true" | 
					
						
							|  |  |  |         expect(described_class.report_event("install", action)).to be_nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "returns nil when HOMEBREW_ANALYTICS_DEBUG is true" do | 
					
						
							| 
									
										
										
										
											2017-08-20 22:04:09 +05:30
										 |  |  |         ENV.delete("HOMEBREW_NO_ANALYTICS_THIS_RUN") | 
					
						
							|  |  |  |         ENV.delete("HOMEBREW_NO_ANALYTICS") | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |         ENV["HOMEBREW_ANALYTICS_DEBUG"] = "true" | 
					
						
							|  |  |  |         expect(described_class.report_event("install", action)).to be_nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-07-27 04:41:49 +05:30
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-08-15 21:05:52 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "::report_build_error" do | 
					
						
							|  |  |  |     context "when tap is installed" do | 
					
						
							|  |  |  |       let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) } | 
					
						
							|  |  |  |       let(:f) { formula { url "foo-1.0" } } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "reports event if BuildError raised for a formula with a public remote repository" do | 
					
						
							|  |  |  |         allow_any_instance_of(Tap).to receive(:custom_remote?).and_return(false) | 
					
						
							|  |  |  |         expect(described_class).to respond_to(:report_event) | 
					
						
							|  |  |  |         described_class.report_build_error(err) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not report event if BuildError raised for a formula with a private remote repository" do | 
					
						
							|  |  |  |         expect(described_class.report_build_error(err)).to be_nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when formula does not have a tap" do | 
					
						
							|  |  |  |       let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) } | 
					
						
							|  |  |  |       let(:f) { double(Formula, name: "foo", path: "blah", tap: nil) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not report event if BuildError is raised" do | 
					
						
							|  |  |  |         expect(described_class.report_build_error(err)).to be_nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when tap for a formula is not installed" do | 
					
						
							|  |  |  |       let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) } | 
					
						
							|  |  |  |       let(:f) { double(Formula, name: "foo", path: "blah", tap: CoreTap.instance) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not report event if BuildError is raised" do | 
					
						
							|  |  |  |         allow_any_instance_of(Pathname).to receive(:directory?).and_return(false) | 
					
						
							|  |  |  |         expect(described_class.report_build_error(err)).to be_nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   specify "::table_output" do | 
					
						
							|  |  |  |     results = { ack: 10, wget: 100 } | 
					
						
							|  |  |  |     expect { described_class.table_output("install", "30", results) } | 
					
						
							|  |  |  |       .to output(/110 |  100.00%/).to_stdout | 
					
						
							|  |  |  |       .and not_to_output.to_stderr | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  | end |