149 lines
4.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2019-04-17 18:25:08 +09:00
require "cli/parser"
require "fileutils"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
def tests_args
Homebrew::CLI::Parser.new do
2018-09-28 21:39:52 +05:30
usage_banner <<~EOS
`tests` [<options>]
2018-09-28 21:39:52 +05:30
Run Homebrew's unit and integration tests.
2018-09-28 21:39:52 +05:30
EOS
switch "--coverage",
2019-04-30 08:44:35 +01:00
description: "Generate code coverage reports."
2018-09-28 21:39:52 +05:30
switch "--generic",
2019-04-30 08:44:35 +01:00
description: "Run only OS-agnostic tests."
2018-09-28 21:39:52 +05:30
switch "--no-compat",
2019-04-30 08:44:35 +01:00
description: "Do not load the compatibility layer when running tests."
2018-09-28 21:39:52 +05:30
switch "--online",
2019-04-30 08:44:35 +01:00
description: "Include tests that use the GitHub API and tests that use any of the taps for "\
"official external commands."
2020-05-18 16:28:43 +05:30
switch "--byebug",
description: "Enable debugging using byebug."
2018-09-28 21:39:52 +05:30
flag "--only=",
2019-04-30 08:44:35 +01:00
description: "Run only <test_script>`_spec.rb`. Appending `:`<line_number> will start at a "\
"specific line."
2018-09-28 21:39:52 +05:30
flag "--seed=",
description: "Randomise tests with the specified <value> instead of a random seed."
switch :verbose
2018-09-28 21:39:52 +05:30
switch :debug
max_named 0
end
end
2018-09-08 22:21:04 +05:30
def tests
tests_args.parse
2018-03-25 13:18:23 +05:30
Homebrew.install_bundler_gems!
gem_user_dir = Gem.user_dir
2020-05-18 16:28:43 +05:30
require "byebug" if args.byebug?
2016-10-21 08:57:39 +02:00
HOMEBREW_LIBRARY_PATH.cd do
2018-08-02 07:20:40 +02:00
ENV.delete("HOMEBREW_COLOR")
ENV.delete("HOMEBREW_NO_COLOR")
2017-02-28 14:33:39 +01:00
ENV.delete("HOMEBREW_VERBOSE")
ENV.delete("HOMEBREW_DEBUG")
2017-02-28 14:33:39 +01:00
ENV.delete("VERBOSE")
2017-02-10 21:21:57 +01:00
ENV.delete("HOMEBREW_CASK_OPTS")
ENV.delete("HOMEBREW_TEMP")
ENV.delete("HOMEBREW_NO_GITHUB_API")
ENV.delete("HOMEBREW_NO_EMOJI")
ENV.delete("HOMEBREW_DEVELOPER")
ENV.delete("HOMEBREW_PRY")
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "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?
ENV["HOMEBREW_TEST_ONLINE"] = "1" if args.online?
2017-02-28 14:33:39 +01:00
ENV["USER"] ||= system_command!("id", args: ["-nu"]).stdout.chomp
2019-04-08 12:47:15 -04:00
# Avoid local configuration messing with tests, e.g. git being configured
# to use GPG to sign by default
ENV["HOME"] = "#{HOMEBREW_LIBRARY_PATH}/test"
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
# 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
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")
end
parallel_args = 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-06-06 23:34:19 -04:00
seed = args.seed || rand(0xFFFF).to_i
bundle_args = ["-I", HOMEBREW_LIBRARY_PATH/"test"]
bundle_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?
2018-08-09 15:00:19 +02:00
bundle_args << "--tag" << "~needs_macos" << "--tag" << "~cask"
2017-02-28 14:33:39 +01:00
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?
bundle_args << "--tag" << "~needs_linux"
2017-03-05 20:45:48 -08:00
files = files.reject { |p| p =~ %r{^test/os/linux(/.*|_spec\.rb)$} }
end
puts "Randomized with seed #{seed}"
# Let `bundle` in PATH find its gem.
ENV["GEM_PATH"] = "#{ENV["GEM_PATH"]}:#{gem_user_dir}"
if parallel
system "bundle", "exec", "parallel_rspec", *parallel_args, "--", *bundle_args, "--", *files
else
system "bundle", "exec", "rspec", *bundle_args, "--", *files
2017-02-28 14:33:39 +01:00
end
return if $CHILD_STATUS.success?
2018-09-17 02:45:00 +02:00
2017-02-28 14:33:39 +01:00
Homebrew.failed = true
end
end
end