mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Port Homebrew::DevCmd::Tests
This commit is contained in:
parent
827e943803
commit
445d81db2e
@ -1,16 +1,17 @@
|
||||
# typed: true
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_command"
|
||||
require "cli/parser"
|
||||
require "fileutils"
|
||||
require "system_command"
|
||||
|
||||
module Homebrew
|
||||
extend SystemCommand::Mixin
|
||||
module DevCmd
|
||||
class Tests < AbstractCommand
|
||||
include SystemCommand::Mixin
|
||||
|
||||
sig { returns(CLI::Parser) }
|
||||
def self.tests_args
|
||||
Homebrew::CLI::Parser.new do
|
||||
cmd_args do
|
||||
description <<~EOS
|
||||
Run Homebrew's unit and integration tests.
|
||||
EOS
|
||||
@ -39,70 +40,22 @@ module Homebrew
|
||||
|
||||
named_args :none
|
||||
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)
|
||||
Homebrew.install_bundler_gems!(groups: Homebrew.valid_gem_groups - ["sorbet"])
|
||||
|
||||
require "byebug" if args.byebug?
|
||||
|
||||
HOMEBREW_LIBRARY_PATH.cd do
|
||||
setup_environment!(args)
|
||||
setup_environment!
|
||||
|
||||
parallel = true
|
||||
|
||||
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?
|
||||
Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
|
||||
@ -207,7 +160,56 @@ module Homebrew
|
||||
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.
|
||||
allowed_test_env = %w[
|
||||
HOMEBREW_GITHUB_API_TOKEN
|
||||
@ -264,4 +266,6 @@ module Homebrew
|
||||
ENV["GIT_#{role}_DATE"] = "Sun Jan 22 19:59:13 2017 +0000"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
8
Library/Homebrew/test/dev-cmd/tests_spec.rb
Normal file
8
Library/Homebrew/test/dev-cmd/tests_spec.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user