| 
									
										
										
										
											2021-09-11 01:00:23 +01:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-19 17:57:36 -05:00
										 |  |  | require "extend/ENV" | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  | require "sandbox" | 
					
						
							| 
									
										
										
										
											2015-04-21 16:59:23 +08:00
										 |  |  | require "timeout" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2012-03-12 21:21:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |   def test_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |         Run the test method provided by an installed formula. | 
					
						
							|  |  |  |         There is no standard output or return code, but generally it should notify the | 
					
						
							|  |  |  |         user if something is wrong with the installed formula. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         *Example:* `brew install jruby && brew test jruby` | 
					
						
							|  |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2021-03-18 14:46:48 +00:00
										 |  |  |       switch "-f", "--force", | 
					
						
							|  |  |  |              description: "Test formulae even if they are unlinked." | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |       switch "--HEAD", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Test the head version of a formula." | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |       switch "--keep-tmp", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Retain the temporary files created for the test." | 
					
						
							| 
									
										
										
										
											2020-07-29 12:00:45 +01:00
										 |  |  |       switch "--retry", | 
					
						
							|  |  |  |              description: "Retry if a testing fails." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       named_args :installed_formula, min: 1
 | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-10 21:28:49 -08:00
										 |  |  |   def test | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = test_args.parse | 
					
						
							| 
									
										
										
										
											2019-03-27 11:49:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 04:46:39 +00:00
										 |  |  |     Homebrew.install_bundler_gems!(setup_path: false) | 
					
						
							| 
									
										
										
										
											2021-02-24 18:01:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-27 11:49:56 +00:00
										 |  |  |     require "formula_assertions" | 
					
						
							| 
									
										
										
										
											2020-03-27 14:29:50 +00:00
										 |  |  |     require "formula_free_port" | 
					
						
							| 
									
										
										
										
											2019-03-27 11:49:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 10:34:48 -04:00
										 |  |  |     args.named.to_resolved_formulae.each do |f| | 
					
						
							| 
									
										
										
										
											2011-03-10 21:28:49 -08:00
										 |  |  |       # Cannot test uninstalled formulae | 
					
						
							| 
									
										
										
										
											2019-12-03 11:42:09 +00:00
										 |  |  |       unless f.latest_version_installed? | 
					
						
							| 
									
										
										
										
											2015-05-27 21:14:35 +08:00
										 |  |  |         ofail "Testing requires the latest version of #{f.full_name}" | 
					
						
							| 
									
										
										
										
											2011-03-10 21:28:49 -08:00
										 |  |  |         next | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Cannot test formulae without a test method | 
					
						
							| 
									
										
										
										
											2013-01-07 17:34:56 -06:00
										 |  |  |       unless f.test_defined? | 
					
						
							| 
									
										
										
										
											2015-05-27 21:14:35 +08:00
										 |  |  |         ofail "#{f.full_name} defines no test" | 
					
						
							| 
									
										
										
										
											2011-03-10 21:28:49 -08:00
										 |  |  |         next | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 17:42:09 +01:00
										 |  |  |       # Don't test unlinked formulae | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:24 +00:00
										 |  |  |       if !args.force? && !f.keg_only? && !f.linked? | 
					
						
							| 
									
										
										
										
											2017-06-23 17:42:09 +01:00
										 |  |  |         ofail "#{f.full_name} is not linked" | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-05 10:36:39 +00:00
										 |  |  |       # Don't test formulae missing test dependencies | 
					
						
							|  |  |  |       missing_test_deps = f.recursive_dependencies do |_, dependency| | 
					
						
							| 
									
										
										
										
											2018-03-08 08:56:31 +00:00
										 |  |  |         Dependency.prune if dependency.installed? | 
					
						
							|  |  |  |         next if dependency.test? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 08:56:31 +00:00
										 |  |  |         Dependency.prune if dependency.optional? | 
					
						
							|  |  |  |         Dependency.prune if dependency.build? | 
					
						
							| 
									
										
										
										
											2018-03-05 10:36:39 +00:00
										 |  |  |       end.map(&:to_s) | 
					
						
							|  |  |  |       unless missing_test_deps.empty? | 
					
						
							|  |  |  |         ofail "#{f.full_name} is missing test dependencies: #{missing_test_deps.join(" ")}" | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 12:00:45 +01:00
										 |  |  |       oh1 "Testing #{f.full_name}" | 
					
						
							| 
									
										
										
										
											2014-06-18 23:17:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 12:19:56 -05:00
										 |  |  |       env = ENV.to_hash | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-10 21:28:49 -08:00
										 |  |  |       begin | 
					
						
							| 
									
										
										
										
											2021-02-25 16:29:05 +00:00
										 |  |  |         exec_args = HOMEBREW_RUBY_EXEC_ARGS + %W[
 | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  |           -- | 
					
						
							|  |  |  |           #{HOMEBREW_LIBRARY_PATH}/test.rb | 
					
						
							|  |  |  |           #{f.path} | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:24 +00:00
										 |  |  |         ].concat(args.options_only) | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 10:34:22 +01:00
										 |  |  |         exec_args << "--HEAD" if f.head? | 
					
						
							| 
									
										
										
										
											2015-10-09 20:26:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  |         Utils.safe_fork do | 
					
						
							| 
									
										
										
										
											2020-05-02 13:45:04 +01:00
										 |  |  |           if Sandbox.available? | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  |             sandbox = Sandbox.new | 
					
						
							| 
									
										
										
										
											2015-04-26 21:59:47 -04:00
										 |  |  |             f.logs.mkpath | 
					
						
							| 
									
										
										
										
											2016-05-27 01:53:08 -04:00
										 |  |  |             sandbox.record_log(f.logs/"test.sandbox.log") | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  |             sandbox.allow_write_temp_and_cache | 
					
						
							|  |  |  |             sandbox.allow_write_log(f) | 
					
						
							| 
									
										
										
										
											2015-08-25 17:34:52 +01:00
										 |  |  |             sandbox.allow_write_xcode | 
					
						
							| 
									
										
										
										
											2015-10-09 17:42:21 +08:00
										 |  |  |             sandbox.allow_write_path(HOMEBREW_PREFIX/"var/cache") | 
					
						
							| 
									
										
										
										
											2018-10-17 14:23:02 +02:00
										 |  |  |             sandbox.allow_write_path(HOMEBREW_PREFIX/"var/homebrew/locks") | 
					
						
							| 
									
										
										
										
											2015-10-09 17:42:21 +08:00
										 |  |  |             sandbox.allow_write_path(HOMEBREW_PREFIX/"var/log") | 
					
						
							|  |  |  |             sandbox.allow_write_path(HOMEBREW_PREFIX/"var/run") | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:24 +00:00
										 |  |  |             sandbox.exec(*exec_args) | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:24 +00:00
										 |  |  |             exec(*exec_args) | 
					
						
							| 
									
										
										
										
											2015-04-13 18:05:41 +08:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2013-06-04 20:34:34 +01:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-08-18 08:20:13 +01:00
										 |  |  |       rescue Exception => e # rubocop:disable Lint/RescueException | 
					
						
							| 
									
										
										
										
											2020-07-31 19:45:18 +02:00
										 |  |  |         retry if retry_test?(f, args: args) | 
					
						
							| 
									
										
										
										
											2018-08-18 08:20:13 +01:00
										 |  |  |         ofail "#{f.full_name}: failed" | 
					
						
							| 
									
										
										
										
											2021-02-26 05:09:14 +00:00
										 |  |  |         $stderr.puts e, e.backtrace | 
					
						
							| 
									
										
										
										
											2015-01-13 12:19:56 -05:00
										 |  |  |       ensure | 
					
						
							|  |  |  |         ENV.replace(env) | 
					
						
							| 
									
										
										
										
											2011-03-10 21:28:49 -08:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2020-07-29 12:00:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 19:45:18 +02:00
										 |  |  |   def retry_test?(f, args:) | 
					
						
							| 
									
										
										
										
											2020-07-29 12:00:45 +01:00
										 |  |  |     @test_failed ||= Set.new | 
					
						
							|  |  |  |     if args.retry? && @test_failed.add?(f) | 
					
						
							|  |  |  |       oh1 "Testing #{f.full_name} (again)" | 
					
						
							|  |  |  |       f.clear_cache | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       Homebrew.failed = true | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-03-10 21:28:49 -08:00
										 |  |  | end |