2024-06-30 20:41:02 +01:00
|
|
|
# typed: strict
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
require "abstract_command"
|
2016-01-18 15:33:59 +01:00
|
|
|
require "fileutils"
|
2025-01-09 15:42:27 -05:00
|
|
|
require "hardware"
|
2024-01-31 14:54:56 -08:00
|
|
|
require "system_command"
|
2016-01-18 15:33:59 +01:00
|
|
|
|
2014-06-18 22:41:47 -05:00
|
|
|
module Homebrew
|
2024-03-21 21:57:08 -07:00
|
|
|
module DevCmd
|
|
|
|
class Tests < AbstractCommand
|
|
|
|
include SystemCommand::Mixin
|
|
|
|
|
|
|
|
cmd_args do
|
|
|
|
description <<~EOS
|
|
|
|
Run Homebrew's unit and integration tests.
|
|
|
|
EOS
|
|
|
|
switch "--coverage",
|
|
|
|
description: "Generate code coverage reports."
|
|
|
|
switch "--generic",
|
|
|
|
description: "Run only OS-agnostic tests."
|
|
|
|
switch "--online",
|
|
|
|
description: "Include tests that use the GitHub API and tests that use any of the taps for " \
|
|
|
|
"official external commands."
|
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
|
|
|
switch "--debug",
|
2024-04-30 11:10:23 +02:00
|
|
|
description: "Enable debugging using `ruby/debug`, or surface the standard `odebug` output."
|
2024-03-21 21:57:08 -07:00
|
|
|
switch "--changed",
|
|
|
|
description: "Only runs tests on files that were changed from the master branch."
|
|
|
|
switch "--fail-fast",
|
|
|
|
description: "Exit early on the first failing test."
|
|
|
|
flag "--only=",
|
2024-04-30 11:10:23 +02:00
|
|
|
description: "Run only `<test_script>_spec.rb`. Appending `:<line_number>` will start at a " \
|
2024-03-21 21:57:08 -07:00
|
|
|
"specific line."
|
|
|
|
flag "--profile=",
|
|
|
|
description: "Run the test suite serially to find the <n> slowest tests."
|
|
|
|
flag "--seed=",
|
|
|
|
description: "Randomise tests with the specified <value> instead of a random seed."
|
|
|
|
|
|
|
|
conflicts "--changed", "--only"
|
|
|
|
|
|
|
|
named_args :none
|
2022-05-03 00:16:33 -07:00
|
|
|
end
|
2019-02-26 22:13:00 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
sig { override.void }
|
|
|
|
def run
|
|
|
|
# Given we might be testing various commands, we probably want everything (except sorbet-static)
|
|
|
|
Homebrew.install_bundler_gems!(groups: Homebrew.valid_gem_groups - ["sorbet"])
|
|
|
|
|
|
|
|
HOMEBREW_LIBRARY_PATH.cd do
|
|
|
|
setup_environment!
|
|
|
|
|
|
|
|
parallel = true
|
|
|
|
|
2024-08-18 16:28:17 -07:00
|
|
|
only = args.only
|
|
|
|
files = if only
|
|
|
|
test_name, line = only.split(":", 2)
|
2024-03-21 21:57:08 -07:00
|
|
|
|
|
|
|
if line.nil?
|
|
|
|
Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
|
|
|
|
else
|
|
|
|
parallel = false
|
|
|
|
["test/#{test_name}_spec.rb:#{line}"]
|
|
|
|
end
|
|
|
|
elsif args.changed?
|
|
|
|
changed_test_files
|
|
|
|
else
|
|
|
|
Dir.glob("test/**/*_spec.rb")
|
|
|
|
end
|
|
|
|
|
|
|
|
if files.blank?
|
2024-08-18 16:28:17 -07:00
|
|
|
raise UsageError, "The `--only` argument requires a valid file or folder name!" if only
|
2024-03-21 21:57:08 -07:00
|
|
|
|
|
|
|
if args.changed?
|
|
|
|
opoo "No tests are directly associated with the changed files!"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-01-09 15:42:27 -05:00
|
|
|
# We use `ParallelTests.last_process?` in `test/spec_helper.rb` to
|
|
|
|
# handle SimpleCov output but, due to how the method is implemented,
|
|
|
|
# it doesn't work as expected if the number of processes is greater
|
|
|
|
# than one but lower than the number of CPU cores in the execution
|
|
|
|
# environment. Coverage information isn't saved in that scenario,
|
|
|
|
# so we disable parallel testing as a workaround in this case.
|
|
|
|
parallel = false if args.profile || (args.coverage? && files.length < Hardware::CPU.cores)
|
2024-03-21 21:57:08 -07:00
|
|
|
|
|
|
|
parallel_rspec_log_name = "parallel_runtime_rspec"
|
|
|
|
parallel_rspec_log_name = "#{parallel_rspec_log_name}.generic" if args.generic?
|
|
|
|
parallel_rspec_log_name = "#{parallel_rspec_log_name}.online" if args.online?
|
|
|
|
parallel_rspec_log_name = "#{parallel_rspec_log_name}.log"
|
|
|
|
|
|
|
|
parallel_rspec_log_path = if ENV["CI"]
|
|
|
|
"tests/#{parallel_rspec_log_name}"
|
|
|
|
else
|
|
|
|
"#{HOMEBREW_CACHE}/#{parallel_rspec_log_name}"
|
|
|
|
end
|
|
|
|
ENV["PARALLEL_RSPEC_LOG_PATH"] = parallel_rspec_log_path
|
|
|
|
|
|
|
|
parallel_args = if ENV["CI"]
|
|
|
|
%W[
|
|
|
|
--combine-stderr
|
|
|
|
--serialize-stdout
|
|
|
|
--runtime-log #{parallel_rspec_log_path}
|
|
|
|
]
|
|
|
|
else
|
|
|
|
%w[
|
|
|
|
--nice
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate seed ourselves and output later to avoid multiple different
|
|
|
|
# seeds being output when running parallel tests.
|
|
|
|
seed = args.seed || rand(0xFFFF).to_i
|
|
|
|
|
|
|
|
bundle_args = ["-I", HOMEBREW_LIBRARY_PATH/"test"]
|
|
|
|
bundle_args += %W[
|
|
|
|
--seed #{seed}
|
|
|
|
--color
|
|
|
|
--require spec_helper
|
|
|
|
]
|
|
|
|
bundle_args << "--fail-fast" if args.fail_fast?
|
|
|
|
bundle_args << "--profile" << args.profile if args.profile
|
|
|
|
|
|
|
|
# TODO: Refactor and move to extend/os
|
|
|
|
# rubocop:disable Homebrew/MoveToExtendOS
|
|
|
|
unless OS.mac?
|
|
|
|
bundle_args << "--tag" << "~needs_macos" << "--tag" << "~cask"
|
|
|
|
files = files.grep_v(%r{^test/(os/mac|cask)(/.*|_spec\.rb)$})
|
|
|
|
end
|
|
|
|
|
|
|
|
unless OS.linux?
|
|
|
|
bundle_args << "--tag" << "~needs_linux"
|
|
|
|
files = files.grep_v(%r{^test/os/linux(/.*|_spec\.rb)$})
|
|
|
|
end
|
|
|
|
# rubocop:enable Homebrew/MoveToExtendOS
|
|
|
|
|
2025-04-04 09:07:15 -04:00
|
|
|
bundle_args << "--tag" << "~needs_arm" unless Hardware::CPU.arm?
|
|
|
|
|
|
|
|
bundle_args << "--tag" << "~needs_intel" unless Hardware::CPU.intel?
|
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
bundle_args << "--tag" << "~needs_network" unless args.online?
|
|
|
|
unless ENV["CI"]
|
|
|
|
bundle_args << "--tag" << "~needs_ci" \
|
|
|
|
<< "--tag" << "~needs_svn"
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Randomized with seed #{seed}"
|
|
|
|
|
2024-04-17 22:17:47 +01:00
|
|
|
ENV["HOMEBREW_DEBUG"] = "1" if args.debug? # Used in spec_helper.rb to require the "debug" gem.
|
|
|
|
|
2024-04-11 17:26:38 +01:00
|
|
|
# Workaround for:
|
2024-04-30 11:10:23 +02:00
|
|
|
#
|
|
|
|
# ```
|
2024-04-11 17:26:38 +01:00
|
|
|
# ruby: no -r allowed while running setuid (SecurityError)
|
2024-04-30 11:10:23 +02:00
|
|
|
# ```
|
2024-04-11 17:26:38 +01:00
|
|
|
Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid
|
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
if parallel
|
|
|
|
system "bundle", "exec", "parallel_rspec", *parallel_args, "--", *bundle_args, "--", *files
|
|
|
|
else
|
|
|
|
system "bundle", "exec", "rspec", *bundle_args, "--", *files
|
|
|
|
end
|
|
|
|
success = $CHILD_STATUS.success?
|
|
|
|
|
|
|
|
return if success
|
|
|
|
|
|
|
|
Homebrew.failed = true
|
2017-02-28 14:33:39 +01:00
|
|
|
end
|
2016-01-20 07:06:09 +01:00
|
|
|
end
|
2016-09-20 14:57:08 +01:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
private
|
2022-04-14 20:33:39 -07:00
|
|
|
|
2024-06-30 20:41:02 +01:00
|
|
|
sig { returns(T::Array[String]) }
|
2024-03-21 21:57:08 -07:00
|
|
|
def changed_test_files
|
|
|
|
changed_files = Utils.popen_read("git", "diff", "--name-only", "master")
|
|
|
|
|
|
|
|
raise UsageError, "No files have been changed from the master branch!" if changed_files.blank?
|
|
|
|
|
|
|
|
filestub_regex = %r{Library/Homebrew/([\w/-]+).rb}
|
|
|
|
changed_files.scan(filestub_regex).map(&:last).filter_map do |filestub|
|
|
|
|
if filestub.start_with?("test/")
|
|
|
|
# Only run tests on *_spec.rb files in test/ folder
|
|
|
|
filestub.end_with?("_spec") ? Pathname("#{filestub}.rb") : nil
|
|
|
|
else
|
|
|
|
# For all other changed .rb files guess the associated test file name
|
|
|
|
Pathname("test/#{filestub}_spec.rb")
|
|
|
|
end
|
|
|
|
end.select(&:exist?)
|
2017-02-28 14:33:39 +01:00
|
|
|
end
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2024-06-30 20:41:02 +01:00
|
|
|
sig { returns(T::Array[String]) }
|
2024-03-21 21:57:08 -07:00
|
|
|
def setup_environment!
|
|
|
|
# Cleanup any unwanted user configuration.
|
|
|
|
allowed_test_env = %w[
|
|
|
|
HOMEBREW_GITHUB_API_TOKEN
|
|
|
|
HOMEBREW_CACHE
|
|
|
|
HOMEBREW_LOGS
|
|
|
|
HOMEBREW_TEMP
|
|
|
|
]
|
|
|
|
allowed_test_env << "HOMEBREW_USE_RUBY_FROM_PATH" if Homebrew::EnvConfig.developer?
|
|
|
|
Homebrew::EnvConfig::ENVS.keys.map(&:to_s).each do |env|
|
|
|
|
next if allowed_test_env.include?(env)
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
ENV.delete(env)
|
|
|
|
end
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
# Codespaces HOMEBREW_PREFIX and /tmp are mounted 755 which makes Ruby warn constantly.
|
|
|
|
if (ENV["HOMEBREW_CODESPACES"] == "true") && (HOMEBREW_TEMP.to_s == "/tmp")
|
|
|
|
# Need to keep this fairly short to avoid socket paths being too long in tests.
|
|
|
|
homebrew_prefix_tmp = "/home/linuxbrew/tmp"
|
|
|
|
ENV["HOMEBREW_TEMP"] = homebrew_prefix_tmp
|
|
|
|
FileUtils.mkdir_p homebrew_prefix_tmp
|
|
|
|
system "chmod", "-R", "g-w,o-w", HOMEBREW_PREFIX, homebrew_prefix_tmp
|
|
|
|
end
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
ENV["HOMEBREW_TESTS"] = "1"
|
|
|
|
ENV["HOMEBREW_NO_AUTO_UPDATE"] = "1"
|
|
|
|
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
|
|
|
|
ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if args.generic?
|
|
|
|
ENV["HOMEBREW_TEST_ONLINE"] = "1" if args.online?
|
|
|
|
ENV["HOMEBREW_SORBET_RUNTIME"] = "1"
|
2025-01-23 16:06:23 +00:00
|
|
|
ENV["HOMEBREW_NO_FORCE_BREW_WRAPPER"] = "1"
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
# TODO: remove this and fix tests when possible.
|
|
|
|
ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1"
|
2023-02-07 19:25:40 +01:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
ENV["USER"] ||= system_command!("id", args: ["-nu"]).stdout.chomp
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07: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"
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
# Print verbose output when requesting debug or verbose output.
|
|
|
|
ENV["HOMEBREW_VERBOSE_TESTS"] = "1" if args.debug? || args.verbose?
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
if args.coverage?
|
|
|
|
ENV["HOMEBREW_TESTS_COVERAGE"] = "1"
|
|
|
|
FileUtils.rm_f "test/coverage/.resultset.json"
|
|
|
|
end
|
2022-11-09 14:45:43 +00:00
|
|
|
|
2024-03-21 21:57:08 -07:00
|
|
|
# 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
|
|
|
|
end
|
2022-11-09 14:45:43 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-25 00:49:18 +11:00
|
|
|
end
|