brew/Library/Homebrew/test/spec_helper.rb

287 lines
7.8 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2017-02-10 21:41:15 +01:00
if ENV["HOMEBREW_TESTS_COVERAGE"]
require "simplecov"
2021-04-01 18:59:46 -04:00
require 'simplecov-cobertura'
2021-04-01 18:59:46 -04:00
formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::CoberturaFormater,
]
2021-02-04 12:35:55 -05:00
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new(formatters)
2018-12-30 20:06:13 +00:00
2021-02-04 12:35:55 -05:00
if RUBY_PLATFORM[/darwin/] && ENV["TEST_ENV_NUMBER"]
SimpleCov.at_exit do
result = SimpleCov.result
result.format! if ParallelTests.number_of_running_processes <= 1
end
end
2017-02-10 21:41:15 +01:00
end
2021-02-14 11:56:32 -05:00
require_relative "../warnings"
Warnings.ignore :parser_syntax do
require "rubocop"
end
require "rspec/its"
require "rspec/github"
require "rspec/wait"
require "rspec/retry"
2020-11-17 04:49:54 +01:00
require "rspec/sorbet"
require "rubocop/rspec/support"
require "find"
2020-05-18 16:28:43 +05:30
require "byebug"
2020-07-22 00:38:27 +02:00
require "timeout"
2018-07-17 14:23:33 +02:00
$LOAD_PATH.push(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/test/support/lib"))
2017-02-10 21:41:15 +01:00
2018-07-17 14:23:33 +02:00
require_relative "../global"
2017-02-10 21:41:15 +01:00
require "test/support/no_seed_progress_formatter"
2020-08-14 13:48:03 -04:00
require "test/support/helper/cask"
2017-02-21 04:52:53 +01:00
require "test/support/helper/fixtures"
2017-02-25 13:26:50 +01:00
require "test/support/helper/formula"
2017-02-28 14:50:46 +01:00
require "test/support/helper/mktmpdir"
2017-07-08 00:57:08 +02:00
require "test/support/helper/output_as_tty"
2017-03-05 06:31:36 +01:00
require "test/support/helper/spec/shared_context/homebrew_cask" if OS.mac?
require "test/support/helper/spec/shared_context/integration_test"
require "test/support/helper/spec/shared_examples/formulae_exist"
2017-02-10 21:41:15 +01:00
TEST_DIRECTORIES = [
CoreTap.instance.path/"Formula",
HOMEBREW_CACHE,
HOMEBREW_CACHE_FORMULA,
HOMEBREW_CELLAR,
HOMEBREW_LOCKS,
2017-02-10 21:41:15 +01:00
HOMEBREW_LOGS,
HOMEBREW_TEMP,
].freeze
2020-11-17 04:49:54 +01:00
# Make `instance_double` and `class_double`
# work when type-checking is active.
RSpec::Sorbet.allow_doubles!
2017-02-10 21:41:15 +01:00
RSpec.configure do |config|
config.order = :random
2018-05-25 23:19:13 +02:00
config.raise_errors_for_deprecations!
config.filter_run_when_matching :focus
config.silence_filter_announcements = true if ENV["TEST_ENV_NUMBER"]
config.expect_with :rspec do |c|
c.max_formatted_output_length = 200
end
# Use rspec-retry in CI.
if ENV["CI"]
config.verbose_retry = true
config.display_try_failure_messages = true
config.default_retry_count = 2
config.default_sleep_interval = 1
config.around(:each, :integration_test) do |example|
example.metadata[:timeout] ||= 120
example.run
end
config.around(:each, :needs_network) do |example|
example.metadata[:timeout] ||= 120
example.metadata[:retry] ||= 4
example.metadata[:retry_wait] ||= 2
example.metadata[:exponential_backoff] ||= true
example.run
end
end
# Never truncate output objects.
RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil
2017-10-21 19:43:14 +02:00
config.include(FileUtils)
2017-10-21 03:12:50 +02:00
config.include(RuboCop::RSpec::ExpectOffense)
2020-08-14 13:48:03 -04:00
config.include(Test::Helper::Cask)
2017-02-21 04:52:53 +01:00
config.include(Test::Helper::Fixtures)
2017-02-25 13:26:50 +01:00
config.include(Test::Helper::Formula)
2017-02-28 14:50:46 +01:00
config.include(Test::Helper::MkTmpDir)
2017-07-08 00:57:08 +02:00
config.include(Test::Helper::OutputAsTTY)
2017-02-28 13:42:52 +01:00
config.before(:each, :needs_compat) do
2020-09-08 19:23:32 +02:00
skip "Requires the compatibility layer." if ENV["HOMEBREW_NO_COMPAT"]
2017-02-28 13:42:52 +01:00
end
config.before(:each, :needs_linux) do
2020-09-08 19:23:32 +02:00
skip "Not running on Linux." unless OS.linux?
end
config.before(:each, :needs_macos) do
2020-09-08 19:23:32 +02:00
skip "Not running on macOS." unless OS.mac?
end
config.before(:each, :needs_java) do
2021-01-25 09:18:10 +00:00
skip "Java is not installed." unless which("java")
end
config.before(:each, :needs_python) do
2020-09-08 19:23:32 +02:00
skip "Python is not installed." unless which("python")
end
config.before(:each, :needs_network) do
skip "Requires network connection." unless ENV["HOMEBREW_TEST_ONLINE"]
end
config.before(:each, :needs_svn) do
svn_shim = HOMEBREW_SHIMS_PATH/"scm/svn"
skip "Subversion is not installed." unless quiet_system svn_shim, "--version"
svn_shim_path = Pathname(Utils.popen_read(svn_shim, "--homebrew=print-path").chomp.presence)
svn_paths = PATH.new(ENV["PATH"])
svn_paths.prepend(svn_shim_path.dirname)
if OS.mac?
xcrun_svn = Utils.popen_read("xcrun", "-f", "svn")
svn_paths.append(File.dirname(xcrun_svn)) if $CHILD_STATUS.success? && xcrun_svn.present?
end
svn = which("svn", svn_paths)
skip "svn is not installed." unless svn
svnadmin = which("svnadmin", svn_paths)
skip "svnadmin is not installed." unless svnadmin
ENV["PATH"] = PATH.new(ENV["PATH"])
.append(svn.dirname)
.append(svnadmin.dirname)
end
config.before(:each, :needs_unzip) do
skip "Unzip is not installed." unless which("unzip")
end
config.around do |example|
def find_files
return [] unless File.exist?(TEST_TMPDIR)
Find.find(TEST_TMPDIR)
.reject { |f| File.basename(f) == ".DS_Store" }
.reject { |f| TEST_DIRECTORIES.include?(Pathname(f)) }
.map { |f| f.sub(TEST_TMPDIR, "") }
end
Homebrew.raise_deprecation_exceptions = true
Formulary.clear_cache
Tap.clear_cache
DependencyCollector.clear_cache
Formula.clear_cache
Keg.clear_cache
Tab.clear_cache
FormulaInstaller.clear_attempted
FormulaInstaller.clear_installed
2018-07-16 10:55:22 +02:00
TEST_DIRECTORIES.each(&:mkpath)
2017-02-10 21:41:15 +01:00
@__homebrew_failed = Homebrew.failed?
@__files_before_test = find_files
2017-02-10 21:41:15 +01:00
@__env = ENV.to_hash # dup doesn't work on ENV
2017-02-10 21:41:15 +01:00
@__stdout = $stdout.clone
@__stderr = $stderr.clone
2020-04-07 08:32:30 +02:00
begin
if (example.metadata.keys & [:focus, :byebug]).empty? && !ENV.key?("HOMEBREW_VERBOSE_TESTS")
2017-07-29 19:55:05 +02:00
$stdout.reopen(File::NULL)
$stderr.reopen(File::NULL)
end
2020-07-22 00:38:27 +02:00
begin
timeout = example.metadata.fetch(:timeout, 60)
2020-07-22 00:38:27 +02:00
Timeout.timeout(timeout) do
example.run
end
rescue Timeout::Error => e
example.example.set_exception(e)
2020-07-22 00:38:27 +02:00
end
2020-04-07 08:32:30 +02:00
rescue SystemExit => e
example.example.set_exception(e)
2017-02-10 21:41:15 +01:00
ensure
ENV.replace(@__env)
2020-04-07 08:32:30 +02:00
$stdout.reopen(@__stdout)
$stderr.reopen(@__stderr)
@__stdout.close
@__stderr.close
2017-07-29 19:55:05 +02:00
Formulary.clear_cache
Tap.clear_cache
DependencyCollector.clear_cache
Formula.clear_cache
2019-11-05 20:33:32 +00:00
Keg.clear_cache
Tab.clear_cache
2017-02-10 21:41:15 +01:00
FileUtils.rm_rf [
*TEST_DIRECTORIES,
*Keg::MUST_EXIST_SUBDIRECTORIES,
2017-02-10 21:41:15 +01:00
HOMEBREW_LINKED_KEGS,
HOMEBREW_PINNED_KEGS,
HOMEBREW_PREFIX/"var",
2017-02-10 21:41:15 +01:00
HOMEBREW_PREFIX/"Caskroom",
2019-10-21 12:27:39 -07:00
HOMEBREW_PREFIX/"Frameworks",
2018-05-25 17:28:43 +02:00
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-cask",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-bar",
2017-02-10 21:41:15 +01:00
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-bundle",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-foo",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-services",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-shallow",
2018-04-13 16:40:08 +02:00
HOMEBREW_LIBRARY/"PinnedTaps",
2017-02-10 21:41:15 +01:00
HOMEBREW_REPOSITORY/".git",
CoreTap.instance.path/".git",
CoreTap.instance.alias_dir,
CoreTap.instance.path/"formula_renames.json",
CoreTap.instance.path/"tap_migrations.json",
CoreTap.instance.path/"audit_exceptions",
CoreTap.instance.path/"style_exceptions",
CoreTap.instance.path/"pypi_formula_mappings.json",
*Pathname.glob("#{HOMEBREW_CELLAR}/*/"),
2017-02-10 21:41:15 +01:00
]
files_after_test = find_files
2017-02-10 21:41:15 +01:00
diff = Set.new(@__files_before_test) ^ Set.new(files_after_test)
2017-10-15 02:28:32 +02:00
expect(diff).to be_empty, <<~EOS
2017-02-10 21:41:15 +01:00
file leak detected:
#{diff.map { |f| " #{f}" }.join("\n")}
EOS
Homebrew.failed = @__homebrew_failed
2017-02-10 21:41:15 +01:00
end
end
end
RSpec::Matchers.define_negated_matcher :not_to_output, :output
RSpec::Matchers.alias_matcher :have_failed, :be_failed
RSpec::Matchers.alias_matcher :a_string_containing, :include
RSpec::Matchers.define :a_json_string do
match do |actual|
JSON.parse(actual)
true
rescue JSON::ParserError
false
end
end
# Match consecutive elements in an array.
RSpec::Matchers.define :array_including_cons do |*cons|
match do |actual|
expect(actual.each_cons(cons.size)).to include(cons)
end
end