| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2023-02-17 15:26:18 +00:00
										 |  |  |   before do | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |     described_class.clear_cache | 
					
						
							| 
									
										
										
										
											2023-02-17 15:26:18 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |   describe "::default_tags_influx" do | 
					
						
							| 
									
										
										
										
											2023-02-15 16:34:50 +00:00
										 |  |  |     let(:ci) { ", CI" if ENV["CI"] } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns OS_VERSION and prefix when HOMEBREW_PREFIX is a custom prefix on intel" do | 
					
						
							| 
									
										
										
										
											2023-02-17 15:26:18 +00:00
										 |  |  |       expect(Homebrew).to receive(:default_prefix?).and_return(false).at_least(:once) | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       expect(described_class.default_tags_influx).to have_key(:prefix) | 
					
						
							|  |  |  |       expect(described_class.default_tags_influx[:prefix]).to eq "custom-prefix" | 
					
						
							| 
									
										
										
										
											2023-02-15 16:34:50 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns OS_VERSION, ARM and prefix when HOMEBREW_PREFIX is a custom prefix on arm" do | 
					
						
							| 
									
										
										
										
											2023-02-17 15:26:18 +00:00
										 |  |  |       expect(Homebrew).to receive(:default_prefix?).and_return(false).at_least(:once) | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       expect(described_class.default_tags_influx).to have_key(:arch) | 
					
						
							|  |  |  |       expect(described_class.default_tags_influx[:arch]).to eq HOMEBREW_PHYSICAL_PROCESSOR | 
					
						
							|  |  |  |       expect(described_class.default_tags_influx).to have_key(:prefix) | 
					
						
							|  |  |  |       expect(described_class.default_tags_influx[:prefix]).to eq "custom-prefix" | 
					
						
							| 
									
										
										
										
											2023-02-15 16:34:50 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns OS_VERSION, Rosetta and prefix when HOMEBREW_PREFIX is a custom prefix on Rosetta", :needs_macos do | 
					
						
							| 
									
										
										
										
											2023-02-17 15:26:18 +00:00
										 |  |  |       expect(Homebrew).to receive(:default_prefix?).and_return(false).at_least(:once) | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       expect(described_class.default_tags_influx).to have_key(:prefix) | 
					
						
							|  |  |  |       expect(described_class.default_tags_influx[:prefix]).to eq "custom-prefix" | 
					
						
							| 
									
										
										
										
											2023-02-15 16:34:50 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "does not include prefix when HOMEBREW_PREFIX is the default prefix" do | 
					
						
							| 
									
										
										
										
											2023-02-17 15:26:18 +00:00
										 |  |  |       expect(Homebrew).to receive(:default_prefix?).and_return(true).at_least(:once) | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       expect(described_class.default_tags_influx).to have_key(:prefix) | 
					
						
							|  |  |  |       expect(described_class.default_tags_influx[:prefix]).to eq HOMEBREW_PREFIX.to_s | 
					
						
							| 
									
										
										
										
											2023-02-15 16:34:50 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "includes CI when ENV['CI'] is set" do | 
					
						
							|  |  |  |       ENV["CI"] = "1" | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       expect(described_class.default_tags_influx).to have_key(:ci) | 
					
						
							| 
									
										
										
										
											2023-02-15 16:34:50 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "includes developer when ENV['HOMEBREW_DEVELOPER'] is set" do | 
					
						
							| 
									
										
										
										
											2023-02-17 15:26:18 +00:00
										 |  |  |       expect(Homebrew::EnvConfig).to receive(:developer?).and_return(true) | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       expect(described_class.default_tags_influx).to have_key(:developer) | 
					
						
							| 
									
										
										
										
											2017-07-27 04:41:49 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |   describe "::report_event" do | 
					
						
							|  |  |  |     let(:f) { formula { url "foo-1.0" } } | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |     let(:package_name)  { f.name } | 
					
						
							|  |  |  |     let(:tap_name) { f.tap.name } | 
					
						
							|  |  |  |     let(:on_request) { false } | 
					
						
							|  |  |  |     let(:options) { "--HEAD" } | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     context "when ENV vars is set" do | 
					
						
							|  |  |  |       it "returns nil when HOMEBREW_NO_ANALYTICS is true" do | 
					
						
							|  |  |  |         ENV["HOMEBREW_NO_ANALYTICS"] = "true" | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |         expect(described_class).not_to receive(:report_influx) | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |         described_class.report_event(:install, package_name: package_name, tap_name: tap_name, | 
					
						
							|  |  |  |           on_request: on_request, options: options) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "returns nil when HOMEBREW_NO_ANALYTICS_THIS_RUN is true" do | 
					
						
							|  |  |  |         ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "true" | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |         expect(described_class).not_to receive(:report_influx) | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |         described_class.report_event(:install, package_name: package_name, tap_name: tap_name, | 
					
						
							|  |  |  |           on_request: on_request, options: options) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |       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" | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |         expect(described_class).to receive(:report_influx) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |         described_class.report_event(:install, package_name: package_name, tap_name: tap_name, | 
					
						
							|  |  |  |           on_request: on_request, options: options) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:47:28 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-16 10:33:15 +01:00
										 |  |  |     it "passes to the influxdb method" do | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |       ENV.delete("HOMEBREW_NO_ANALYTICS_THIS_RUN") | 
					
						
							|  |  |  |       ENV.delete("HOMEBREW_NO_ANALYTICS") | 
					
						
							|  |  |  |       ENV["HOMEBREW_ANALYTICS_DEBUG"] = "true" | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       expect(described_class).to receive(:report_influx).with(:install, hash_including(package_name: package_name, | 
					
						
							|  |  |  |                                                                                        on_request:   on_request)).once | 
					
						
							|  |  |  |       described_class.report_event(:install, package_name: package_name, tap_name: tap_name, | 
					
						
							|  |  |  |           on_request: on_request, options: options) | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::report_influx" do | 
					
						
							|  |  |  |     let(:f) { formula { url "foo-1.0" } } | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |     let(:package_name)  { f.name } | 
					
						
							|  |  |  |     let(:tap_name) { f.tap.name } | 
					
						
							|  |  |  |     let(:on_request) { false } | 
					
						
							|  |  |  |     let(:options) { "--HEAD" } | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it "outputs in debug mode" do | 
					
						
							|  |  |  |       ENV.delete("HOMEBREW_NO_ANALYTICS_THIS_RUN") | 
					
						
							|  |  |  |       ENV.delete("HOMEBREW_NO_ANALYTICS") | 
					
						
							|  |  |  |       ENV["HOMEBREW_ANALYTICS_DEBUG"] = "true" | 
					
						
							| 
									
										
										
										
											2023-02-06 16:56:25 +01:00
										 |  |  |       expect(described_class).to receive(:deferred_curl).once | 
					
						
							| 
									
										
										
										
											2023-03-20 15:26:47 +00:00
										 |  |  |       described_class.report_influx(:install, package_name: package_name, tap_name: tap_name, on_request: on_request, | 
					
						
							|  |  |  | options: options) | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |         allow_any_instance_of(Tap).to receive(:private?).and_return(true) | 
					
						
							|  |  |  |         expect(described_class).not_to receive(:report_event) | 
					
						
							|  |  |  |         described_class.report_build_error(err) | 
					
						
							| 
									
										
										
										
											2017-08-15 21:05:52 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when formula does not have a tap" do | 
					
						
							|  |  |  |       let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) } | 
					
						
							| 
									
										
										
										
											2023-01-22 17:08:50 -08:00
										 |  |  |       let(:f) { instance_double(Formula, name: "foo", path: "blah", tap: nil) } | 
					
						
							| 
									
										
										
										
											2017-08-15 21:05:52 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       it "does not report event if BuildError is raised" do | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |         expect(described_class).not_to receive(:report_event) | 
					
						
							|  |  |  |         described_class.report_build_error(err) | 
					
						
							| 
									
										
										
										
											2017-08-15 21:05:52 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when tap for a formula is not installed" do | 
					
						
							|  |  |  |       let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) } | 
					
						
							| 
									
										
										
										
											2023-01-22 17:08:50 -08:00
										 |  |  |       let(:f) { instance_double(Formula, name: "foo", path: "blah", tap: CoreTap.instance) } | 
					
						
							| 
									
										
										
										
											2017-08-15 21:05:52 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       it "does not report event if BuildError is raised" do | 
					
						
							|  |  |  |         allow_any_instance_of(Pathname).to receive(:directory?).and_return(false) | 
					
						
							| 
									
										
										
										
											2022-05-31 18:03:48 +02:00
										 |  |  |         expect(described_class).not_to receive(:report_event) | 
					
						
							|  |  |  |         described_class.report_build_error(err) | 
					
						
							| 
									
										
										
										
											2017-08-15 21:05:52 +05:30
										 |  |  |       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 |