Port Homebrew::DevCmd::Tests

This commit is contained in:
Douglas Eichelberger 2024-03-21 21:57:08 -07:00
parent 827e943803
commit 445d81db2e
2 changed files with 253 additions and 241 deletions

View File

@ -1,16 +1,17 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "cli/parser" require "cli/parser"
require "fileutils" require "fileutils"
require "system_command" require "system_command"
module Homebrew module Homebrew
extend SystemCommand::Mixin module DevCmd
class Tests < AbstractCommand
include SystemCommand::Mixin
sig { returns(CLI::Parser) } cmd_args do
def self.tests_args
Homebrew::CLI::Parser.new do
description <<~EOS description <<~EOS
Run Homebrew's unit and integration tests. Run Homebrew's unit and integration tests.
EOS EOS
@ -39,70 +40,22 @@ module Homebrew
named_args :none named_args :none
end end
end
def self.use_buildpulse?
return @use_buildpulse if defined?(@use_buildpulse)
@use_buildpulse = ENV["HOMEBREW_BUILDPULSE_ACCESS_KEY_ID"].present? &&
ENV["HOMEBREW_BUILDPULSE_SECRET_ACCESS_KEY"].present? &&
ENV["HOMEBREW_BUILDPULSE_ACCOUNT_ID"].present? &&
ENV["HOMEBREW_BUILDPULSE_REPOSITORY_ID"].present?
end
def self.run_buildpulse
require "formula"
with_env(HOMEBREW_NO_AUTO_UPDATE: "1", HOMEBREW_NO_BOOTSNAP: "1") do
ensure_formula_installed!("buildpulse-test-reporter",
reason: "reporting test flakiness")
end
ENV["BUILDPULSE_ACCESS_KEY_ID"] = ENV.fetch("HOMEBREW_BUILDPULSE_ACCESS_KEY_ID")
ENV["BUILDPULSE_SECRET_ACCESS_KEY"] = ENV.fetch("HOMEBREW_BUILDPULSE_SECRET_ACCESS_KEY")
ohai "Sending test results to BuildPulse"
system_command Formula["buildpulse-test-reporter"].opt_bin/"buildpulse-test-reporter",
args: [
"submit", "#{HOMEBREW_LIBRARY_PATH}/test/junit",
"--account-id", ENV.fetch("HOMEBREW_BUILDPULSE_ACCOUNT_ID"),
"--repository-id", ENV.fetch("HOMEBREW_BUILDPULSE_REPOSITORY_ID")
]
end
def self.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?)
end
def self.tests
args = tests_args.parse
sig { override.void }
def run
# Given we might be testing various commands, we probably want everything (except sorbet-static) # 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.install_bundler_gems!(groups: Homebrew.valid_gem_groups - ["sorbet"])
require "byebug" if args.byebug? require "byebug" if args.byebug?
HOMEBREW_LIBRARY_PATH.cd do HOMEBREW_LIBRARY_PATH.cd do
setup_environment!(args) setup_environment!
parallel = true parallel = true
files = if args.only files = if args.only
test_name, line = args.only.split(":", 2) # FIXME: This is safe once args are namespaced by command
test_name, line = T.unsafe(args.only).split(":", 2)
if line.nil? if line.nil?
Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb") Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
@ -207,7 +160,56 @@ module Homebrew
end end
end end
def self.setup_environment!(args) private
def use_buildpulse?
return @use_buildpulse if defined?(@use_buildpulse)
@use_buildpulse = ENV["HOMEBREW_BUILDPULSE_ACCESS_KEY_ID"].present? &&
ENV["HOMEBREW_BUILDPULSE_SECRET_ACCESS_KEY"].present? &&
ENV["HOMEBREW_BUILDPULSE_ACCOUNT_ID"].present? &&
ENV["HOMEBREW_BUILDPULSE_REPOSITORY_ID"].present?
end
def run_buildpulse
require "formula"
with_env(HOMEBREW_NO_AUTO_UPDATE: "1", HOMEBREW_NO_BOOTSNAP: "1") do
ensure_formula_installed!("buildpulse-test-reporter",
reason: "reporting test flakiness")
end
ENV["BUILDPULSE_ACCESS_KEY_ID"] = ENV.fetch("HOMEBREW_BUILDPULSE_ACCESS_KEY_ID")
ENV["BUILDPULSE_SECRET_ACCESS_KEY"] = ENV.fetch("HOMEBREW_BUILDPULSE_SECRET_ACCESS_KEY")
ohai "Sending test results to BuildPulse"
system_command Formula["buildpulse-test-reporter"].opt_bin/"buildpulse-test-reporter",
args: [
"submit", "#{HOMEBREW_LIBRARY_PATH}/test/junit",
"--account-id", ENV.fetch("HOMEBREW_BUILDPULSE_ACCOUNT_ID"),
"--repository-id", ENV.fetch("HOMEBREW_BUILDPULSE_REPOSITORY_ID")
]
end
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?)
end
def setup_environment!
# Cleanup any unwanted user configuration. # Cleanup any unwanted user configuration.
allowed_test_env = %w[ allowed_test_env = %w[
HOMEBREW_GITHUB_API_TOKEN HOMEBREW_GITHUB_API_TOKEN
@ -265,3 +267,5 @@ module Homebrew
end end
end end
end end
end
end

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/tests"
RSpec.describe Homebrew::DevCmd::Tests do
it_behaves_like "parseable arguments"
end