107 lines
3.1 KiB
Ruby
Raw Normal View History

#: * `test` [`--devel`|`--HEAD`] [`--debug`] [`--keep-tmp`] <formula>:
2016-09-22 17:12:13 -04:00
#: Most formulae provide a test method. `brew test` <formula> runs this
2016-04-08 16:28:43 +02:00
#: test method. There is no standard output or return code, but it should
#: generally indicate to the user if something is wrong with the installed
#: formula.
#:
#: To test the development or head version of a formula, use `--devel` or
#: `--HEAD`.
#:
2017-04-02 10:14:21 +01:00
#: If `--debug` (or `-d`) is passed and the test fails, an interactive debugger will be
2016-04-08 16:28:43 +02:00
#: launched with access to IRB or a shell inside the temporary test directory.
#:
#: If `--keep-tmp` is passed, the temporary files created for the test are
#: not deleted.
#:
2016-04-08 16:28:43 +02:00
#: Example: `brew install jruby && brew test jruby`
2014-06-19 17:57:36 -05:00
require "extend/ENV"
require "formula_assertions"
2015-04-13 18:05:41 +08:00
require "sandbox"
require "timeout"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2011-03-10 21:28:49 -08:00
def test
raise FormulaUnspecifiedError if ARGV.named.empty?
2015-05-17 21:13:09 +08:00
ARGV.resolved_formulae.each do |f|
2011-03-10 21:28:49 -08:00
# Cannot test uninstalled formulae
unless f.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
# Don't test unlinked formulae
if !ARGV.force? && !f.keg_only? && !f.linked?
ofail "#{f.full_name} is not linked"
next
end
# Don't test formulae missing test dependencies
missing_test_deps = f.recursive_dependencies do |_, dependency|
Dependency.prune if dependency.installed?
next if dependency.test?
2018-09-17 02:45:00 +02:00
Dependency.prune if dependency.optional?
Dependency.prune if dependency.build?
end.map(&:to_s)
unless missing_test_deps.empty?
ofail "#{f.full_name} is missing test dependencies: #{missing_test_deps.join(" ")}"
next
end
2015-05-27 21:14:35 +08:00
puts "Testing #{f.full_name}"
env = ENV.to_hash
2011-03-10 21:28:49 -08:00
begin
2015-04-13 18:05:41 +08:00
args = %W[
#{RUBY_PATH}
-W0
2018-07-17 14:23:33 +02:00
-I #{$LOAD_PATH.join(File::PATH_SEPARATOR)}
2015-04-13 18:05:41 +08:00
--
#{HOMEBREW_LIBRARY_PATH}/test.rb
#{f.path}
].concat(ARGV.options_only)
if f.head?
args << "--HEAD"
elsif f.devel?
args << "--devel"
end
2015-04-13 18:05:41 +08:00
Utils.safe_fork do
2016-08-14 17:33:17 +01:00
if Sandbox.test?
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)
sandbox.allow_write_xcode
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/cache")
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/homebrew/locks")
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/log")
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/run")
2015-04-13 18:05:41 +08:00
sandbox.exec(*args)
else
exec(*args)
end
2013-06-04 20:34:34 +01:00
end
rescue Exception => e # rubocop:disable Lint/RescueException
ofail "#{f.full_name}: failed"
puts e, e.backtrace
ensure
ENV.replace(env)
2011-03-10 21:28:49 -08:00
end
end
end
end