131 lines
4.0 KiB
Ruby
Raw Normal View History

2018-03-25 13:18:23 +05:30
#: * `tests` [`--verbose`] [`--coverage`] [`--generic`] [`--no-compat`] [`--only=`<test_script>[`:`<line_number>]] [`--seed=`<seed>] [`--online`] [`--official-cmd-taps`]:
#: Run Homebrew's unit and integration tests. If provided,
#: `--only=`<test_script> runs only <test_script>_spec.rb, and `--seed`
#: randomizes tests with the provided value instead of a random seed.
#:
2017-04-02 10:14:21 +01:00
#: If `--verbose` (or `-v`) is passed, print the command that runs the tests.
#:
#: If `--coverage` is passed, also generate code coverage reports.
#:
#: If `--generic` is passed, only run OS-agnostic tests.
#:
#: If `--no-compat` is passed, do not load the compatibility layer when
#: running tests.
#:
#: If `--online` is passed, include tests that use the GitHub API and tests
#: that use any of the taps for official external commands.
2018-03-25 13:18:23 +05:30
require "cli_parser"
require "fileutils"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
def tests
args = Homebrew::CLI::Parser.parse do
2018-03-25 13:18:23 +05:30
switch "--no-compat"
switch "--generic"
switch "-v", "--verbose"
switch "--coverage"
switch "--online"
flag "--only", required: true
flag "--seed", required: true
end
2018-03-25 13:18:23 +05:30
2016-10-21 08:57:39 +02:00
HOMEBREW_LIBRARY_PATH.cd do
2017-02-28 14:33:39 +01:00
ENV.delete("HOMEBREW_VERBOSE")
ENV.delete("VERBOSE")
2017-02-10 21:21:57 +01:00
ENV.delete("HOMEBREW_CASK_OPTS")
ENV.delete("HOMEBREW_TEMP")
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
ENV["HOMEBREW_DEVELOPER"] = "1"
2018-03-25 13:18:23 +05:30
ENV["HOMEBREW_NO_COMPAT"] = "1" if args.no_compat?
ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if args.generic?
2018-03-25 13:18:23 +05:30
if args.online?
ENV["HOMEBREW_TEST_ONLINE"] = "1"
else
ENV["HOMEBREW_NO_GITHUB_API"] = "1"
end
2017-02-28 14:33:39 +01:00
2018-03-25 13:18:23 +05:30
if args.coverage?
ENV["HOMEBREW_TESTS_COVERAGE"] = "1"
2016-09-19 23:00:58 +01:00
FileUtils.rm_f "test/coverage/.resultset.json"
end
ENV["BUNDLE_GEMFILE"] = "#{HOMEBREW_LIBRARY_PATH}/test/Gemfile"
# Override author/committer as global settings might be invalid and thus
# will cause silent failure during the setup of dummy Git repositories.
%w[AUTHOR COMMITTER].each do |role|
ENV["GIT_#{role}_NAME"] = "brew tests"
ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost"
ENV["GIT_#{role}_DATE"] = "Sun Jan 22 19:59:13 2017 +0000"
end
Homebrew.install_gem_setup_path! "bundler"
2017-05-29 18:24:52 +01:00
system "bundle", "install" unless quiet_system("bundle", "check")
2017-02-28 14:33:39 +01:00
parallel = true
2018-03-25 13:18:23 +05:30
files = if args.only
test_name, line = args.only.split(":", 2)
2017-02-28 14:33:39 +01:00
if line.nil?
Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
else
parallel = false
["test/#{test_name}_spec.rb:#{line}"]
end
else
Dir.glob("test/**/*_spec.rb").reject { |p| p =~ %r{^test/vendor/bundle/} }
end
opts = if ENV["CI"]
%w[
--combine-stderr
--serialize-stdout
]
else
%w[
--nice
]
2017-02-28 14:33:39 +01:00
end
2017-02-10 21:21:57 +01:00
# Generate seed ourselves and output later to avoid multiple different
# seeds being output when running parallel tests.
2018-03-25 13:18:23 +05:30
seed = args.seed ? args.seed : rand(0xFFFF).to_i
args = ["-I", HOMEBREW_LIBRARY_PATH/"test"]
args += %W[
--seed #{seed}
--color
--require spec_helper
--format NoSeedProgressFormatter
--format ParallelTests::RSpec::RuntimeLogger
--out #{HOMEBREW_CACHE}/tests/parallel_runtime_rspec.log
2017-02-10 21:21:57 +01:00
]
2017-02-28 14:33:39 +01:00
unless OS.mac?
args << "--tag" << "~needs_macos"
files = files.reject { |p| p =~ %r{^test/(os/mac|cask)(/.*|_spec\.rb)$} }
end
2017-02-28 14:33:39 +01:00
2017-03-05 20:45:48 -08:00
unless OS.linux?
files = files.reject { |p| p =~ %r{^test/os/linux(/.*|_spec\.rb)$} }
end
puts "Randomized with seed #{seed}"
if parallel
system "bundle", "exec", "parallel_rspec", *opts, "--", *args, "--", *files
else
system "bundle", "exec", "rspec", *args, "--", *files
2017-02-28 14:33:39 +01:00
end
return if $CHILD_STATUS.success?
2017-02-28 14:33:39 +01:00
Homebrew.failed = true
end
end
end