brew/Library/Homebrew/test/spec_helper.rb

315 lines
9.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-02-10 21:41:15 +01:00
if ENV["HOMEBREW_TESTS_COVERAGE"]
require "simplecov"
2021-04-01 20:29:27 -04:00
require "simplecov-cobertura"
2021-04-01 18:59:46 -04:00
formatters = [
SimpleCov::Formatter::HTMLFormatter,
2021-04-01 21:19:56 -04:00
SimpleCov::Formatter::CoberturaFormatter,
2021-04-01 18:59:46 -04:00
]
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/github"
require "rspec/retry"
2020-11-17 04:49:54 +01:00
require "rspec/sorbet"
require "rubocop/rspec/support"
require "find"
2020-07-22 00:38:27 +02:00
require "timeout"
$LOAD_PATH.unshift(File.expand_path("#{ENV.fetch("HOMEBREW_LIBRARY")}/Homebrew/test/support/lib"))
2017-02-10 21:41:15 +01:00
require_relative "support/extend/cachable"
2018-07-17 14:23:33 +02:00
require_relative "../global"
2017-02-10 21:41:15 +01:00
require "debug" if ENV["HOMEBREW_DEBUG"]
require "test/support/quiet_progress_formatter"
2020-08-14 13:48:03 -04:00
require "test/support/helper/cask"
2024-02-19 08:28:12 -08:00
require "test/support/helper/files"
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-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!
RSpec.configure do |config|
2017-02-10 21:41:15 +01:00
config.order = :random
2018-05-25 23:19:13 +02:00
config.raise_errors_for_deprecations!
2024-02-18 15:03:00 -08:00
config.warnings = true
2024-02-18 15:17:25 -08:00
config.disable_monkey_patching!
2018-05-25 23:19:13 +02:00
config.filter_run_when_matching :focus
config.silence_filter_announcements = true if ENV["TEST_ENV_NUMBER"]
# Improve backtrace formatting
config.filter_gems_from_backtrace "rspec-retry", "sorbet-runtime"
config.backtrace_exclusion_patterns << %r{test/spec_helper\.rb}
config.expect_with :rspec do |c|
c.max_formatted_output_length = 200
end
# Use rspec-retry to handle flaky tests.
config.default_sleep_interval = 1
# Don't want the nicer default retry behaviour when using BuildPulse to
# identify flaky tests.
config.default_retry_count = 2 unless ENV["BUILDPULSE"]
2024-02-18 15:08:05 -08:00
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
# Increase timeouts for integration tests (as we expect them to take longer).
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
# Don't want the nicer default retry behaviour when using BuildPulse to
# identify flaky tests.
example.metadata[:retry] ||= 4 unless ENV["BUILDPULSE"]
example.metadata[:retry_wait] ||= 2
example.metadata[:exponential_backoff] ||= true
example.run
end
# Never truncate output objects.
RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil
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)
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_ci) do
skip "Not running on CI." unless ENV["CI"]
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
skip "Python is not installed." if !which("python3") && !which("python")
end
config.before(:each, :needs_network) do
skip "Requires network connection." unless ENV["HOMEBREW_TEST_ONLINE"]
end
config.before do |example|
next if example.metadata.key?(:needs_network)
next if example.metadata.key?(:needs_utils_curl)
allow(Utils::Curl).to receive(:curl_executable).and_raise(<<~ERROR)
Unexpected call to Utils::Curl.curl_executable without setting :needs_network or :needs_utils_curl.
ERROR
end
config.before(:each, :no_api) do
ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1"
end
config.before(:each, :needs_svn) do
svn_shim = HOMEBREW_SHIMS_PATH/"shared/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.fetch("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.fetch("PATH"))
.append(svn.dirname)
.append(svnadmin.dirname)
end
config.before(:each, :needs_homebrew_curl) do
ENV["HOMEBREW_CURL"] = HOMEBREW_BREWED_CURL_PATH
skip "A `curl` with TLS 1.3 support is required." unless Utils::Curl.curl_supports_tls13?
2021-10-23 15:09:34 -04:00
rescue FormulaUnavailableError
skip "No `curl` formula is available."
end
config.before(:each, :needs_unzip) do
skip "Unzip is not installed." unless which("unzip")
end
config.around do |example|
Homebrew.raise_deprecation_exceptions = true
Tap.installed.each(&:clear_cache)
Cachable::Registry.clear_all_caches
FormulaInstaller.clear_attempted
FormulaInstaller.clear_installed
FormulaInstaller.clear_fetched
Utils::Curl.clear_path_cache
2018-07-16 10:55:22 +02:00
TEST_DIRECTORIES.each(&:mkpath)
2017-02-10 21:41:15 +01:00
@__homebrew_failed = Homebrew.failed?
2024-02-19 08:28:12 -08:00
@__files_before_test = Test::Helper::Files.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
@__stdin = $stdin.clone
2020-04-07 08:32:30 +02:00
begin
if example.metadata.keys.exclude?(:focus) && !ENV.key?("HOMEBREW_VERBOSE_TESTS")
2017-07-29 19:55:05 +02:00
$stdout.reopen(File::NULL)
$stderr.reopen(File::NULL)
Set up the `debug` gem for test debugging - This processed that we'd requested a debugger, but didn't drop us into a debugging console until I [stopped the stdin disablement](https://github.com/Homebrew/brew/issues/16708#issuecomment-1953483970). Usage: ``` ❯ git diff diff --git a/Library/Homebrew/test/migrator_spec.rb b/Library/Homebrew/test/migrator_spec.rb index 87fadd5e95..db4700810a 100644 --- a/Library/Homebrew/test/migrator_spec.rb +++ b/Library/Homebrew/test/migrator_spec.rb @@ -69,6 +69,8 @@ RSpec.describe Migrator do tab.source["tap"] = "homebrew/core" tab.write + binding.break + expect do described_class.new(new_formula, "oldname") end.to raise_error(Migrator::MigratorDifferentTapsError) issyl0 at pictor in /opt/homebrew on bye-byebug ❯ brew tests --only=migrator --debug Randomized with seed 59158 1 process for 1 spec, ~ 1 spec per process .==> Relinking newname .==> Unlinking oldname ...==> Moving oldname versions to /private/tmp/homebrew-tests-20240403-85464-3uogqr/cellar/newname ....==> Migrating formula oldname to newname ==> Unlinking oldname ==> Moving oldname versions to /private/tmp/homebrew-tests-20240403-85464-3uogqr/cellar/newname ==> Relinking newname ....[67, 76] in ~/migrator_spec.rb 67| tab = Tab.empty 68| tab.tabfile = HOMEBREW_CELLAR/"oldname/0.1/INSTALL_RECEIPT.json" 69| tab.source["tap"] = "homebrew/core" 70| tab.write 71| => 72| binding.break 73| 74| expect do 75| described_class.new(new_formula, "oldname") 76| end.to raise_error(Migrator::MigratorDifferentTapsError) =>#0 block in <top (required)> (3 levels) at ~/migrator_spec.rb:72 #1 [C] BasicObject#instance_exec at /opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.13.0/lib/rspec/core/example.rb:263 # and 68 frames (use `bt' command for all frames) (rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p tab (rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p # command(rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p # command t # command ta # command tab # command(rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p tab # command => #<Tab:0x0000000107156be0 @aliases=[], @arch=nil, @built_as_bottle=false, @built_on={"os"=>"Macintosh", "os_version"=>"macOS 14", "cpu_family"=>"arm_firestorm_icestorm"}, @compiler=:clang, @homebrew_version="4.2.16-55-gc8f60ec-dirty", @installed_as_dependency=false, @installed_on_request=false, @loaded_from_api=false, @poured_from_bottle=false, @runtime_dependencies=nil, @source={"path"=>nil, "tap"=>"homebrew/core", "tap_git_head"=>nil, "spec"=>"stable", "versions"=>{"stable"=>nil, "head"=>nil, "version_scheme"=>0}}, @source_modified_time=0, @stdlib=nil, @tabfile=#<Pathname:/private/tmp/homebrew-tests-20240403-85464-3uogqr/cellar/oldname/0.1/INSTALL_RECEIPT.json>, @time=nil, @unused_options=[], @used_options=[]> (rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) ```
2024-04-02 23:43:46 +01:00
$stdin.reopen(File::NULL)
else
Set up the `debug` gem for test debugging - This processed that we'd requested a debugger, but didn't drop us into a debugging console until I [stopped the stdin disablement](https://github.com/Homebrew/brew/issues/16708#issuecomment-1953483970). Usage: ``` ❯ git diff diff --git a/Library/Homebrew/test/migrator_spec.rb b/Library/Homebrew/test/migrator_spec.rb index 87fadd5e95..db4700810a 100644 --- a/Library/Homebrew/test/migrator_spec.rb +++ b/Library/Homebrew/test/migrator_spec.rb @@ -69,6 +69,8 @@ RSpec.describe Migrator do tab.source["tap"] = "homebrew/core" tab.write + binding.break + expect do described_class.new(new_formula, "oldname") end.to raise_error(Migrator::MigratorDifferentTapsError) issyl0 at pictor in /opt/homebrew on bye-byebug ❯ brew tests --only=migrator --debug Randomized with seed 59158 1 process for 1 spec, ~ 1 spec per process .==> Relinking newname .==> Unlinking oldname ...==> Moving oldname versions to /private/tmp/homebrew-tests-20240403-85464-3uogqr/cellar/newname ....==> Migrating formula oldname to newname ==> Unlinking oldname ==> Moving oldname versions to /private/tmp/homebrew-tests-20240403-85464-3uogqr/cellar/newname ==> Relinking newname ....[67, 76] in ~/migrator_spec.rb 67| tab = Tab.empty 68| tab.tabfile = HOMEBREW_CELLAR/"oldname/0.1/INSTALL_RECEIPT.json" 69| tab.source["tap"] = "homebrew/core" 70| tab.write 71| => 72| binding.break 73| 74| expect do 75| described_class.new(new_formula, "oldname") 76| end.to raise_error(Migrator::MigratorDifferentTapsError) =>#0 block in <top (required)> (3 levels) at ~/migrator_spec.rb:72 #1 [C] BasicObject#instance_exec at /opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.13.0/lib/rspec/core/example.rb:263 # and 68 frames (use `bt' command for all frames) (rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p tab (rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p # command(rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p # command t # command ta # command tab # command(rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) p tab # command => #<Tab:0x0000000107156be0 @aliases=[], @arch=nil, @built_as_bottle=false, @built_on={"os"=>"Macintosh", "os_version"=>"macOS 14", "cpu_family"=>"arm_firestorm_icestorm"}, @compiler=:clang, @homebrew_version="4.2.16-55-gc8f60ec-dirty", @installed_as_dependency=false, @installed_on_request=false, @loaded_from_api=false, @poured_from_bottle=false, @runtime_dependencies=nil, @source={"path"=>nil, "tap"=>"homebrew/core", "tap_git_head"=>nil, "spec"=>"stable", "versions"=>{"stable"=>nil, "head"=>nil, "version_scheme"=>0}}, @source_modified_time=0, @stdlib=nil, @tabfile=#<Pathname:/private/tmp/homebrew-tests-20240403-85464-3uogqr/cellar/oldname/0.1/INSTALL_RECEIPT.json>, @time=nil, @unused_options=[], @used_options=[]> (rdbg@/opt/homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/rspec#85464) ```
2024-04-02 23:43:46 +01:00
# don't retry when focusing
config.default_retry_count = 0
2017-07-29 19:55:05 +02:00
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)
2023-10-10 03:39:42 +02:00
Context.current = Context::ContextStruct.new
2017-02-10 21:41:15 +01:00
2020-04-07 08:32:30 +02:00
$stdout.reopen(@__stdout)
$stderr.reopen(@__stderr)
$stdin.reopen(@__stdin)
2020-04-07 08:32:30 +02:00
@__stdout.close
@__stderr.close
@__stdin.close
2017-07-29 19:55:05 +02:00
Tap.all.each(&:clear_cache)
Cachable::Registry.clear_all_caches
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
]
2024-02-19 08:28:12 -08:00
files_after_test = Test::Helper::Files.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
# 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