Skip to content

Commit

Permalink
feat: detect all package changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Jan 31, 2024
1 parent 21c18df commit e47f98c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
2 changes: 2 additions & 0 deletions cmd/test-bot.rb
Expand Up @@ -73,6 +73,8 @@ def test_bot_args
description: "Only run the formulae steps."
switch "--only-formulae-detect",
description: "Only run the formulae detection steps."
switch "--only-packages-detect",
description: "Only run the packages detection steps."
switch "--only-formulae-dependents",
description: "Only run the formulae dependents steps."
switch "--only-bottles-fetch",
Expand Down
10 changes: 6 additions & 4 deletions lib/test_runner.rb
Expand Up @@ -6,7 +6,7 @@
require_relative "test_formulae"
require_relative "tests/cleanup_after"
require_relative "tests/cleanup_before"
require_relative "tests/formulae_detect"
require_relative "tests/packages_detect"
require_relative "tests/formulae_dependents"
require_relative "tests/bottles_fetch"
require_relative "tests/formulae"
Expand Down Expand Up @@ -111,6 +111,7 @@ def no_only_args?(args)
args.only_tap_syntax? ||
args.only_formulae? ||
args.only_formulae_detect? ||
args.only_packages_detect? ||
args.only_formulae_dependents? ||
args.only_bottles_fetch? ||
args.only_cleanup_after?
Expand Down Expand Up @@ -139,8 +140,9 @@ def build_tests(argument, tap:, git:, output_paths:, skip_setup:,
no_formulae_flags = args.testing_formulae.nil? &&
args.added_formulae.nil? &&
args.deleted_formulae.nil?
if no_formulae_flags && (no_only_args || args.only_formulae? || args.only_formulae_detect?)
tests[:formulae_detect] = Tests::FormulaeDetect.new(argument, tap: tap,
if no_formulae_flags &&
(no_only_args || args.only_formulae? || args.only_formulae_detect? || args.only_packages_detect?)
tests[:packages_detect] = Tests::PackagesDetect.new(argument, tap: tap,
git: git,
dry_run: args.dry_run?,
fail_fast: args.fail_fast?,
Expand Down Expand Up @@ -199,7 +201,7 @@ def run_tests(tests, args:)
tests[:setup]&.run!(args: args)
tests[:tap_syntax]&.run!(args: args)

testing_formulae, added_formulae, deleted_formulae = if (detect_test = tests[:formulae_detect])
testing_formulae, added_formulae, deleted_formulae = if (detect_test = tests[:packages_detect])
detect_test.run!(args: args)

[
Expand Down
69 changes: 54 additions & 15 deletions lib/tests/formulae_detect.rb → lib/tests/packages_detect.rb
Expand Up @@ -2,20 +2,23 @@

module Homebrew
module Tests
class FormulaeDetect < Test
attr_reader :testing_formulae, :added_formulae, :deleted_formulae
class PackagesDetect < Test
attr_reader :testing_formulae, :added_formulae, :deleted_formulae, :testing_packages, :added_packages,
:deleted_packages

def initialize(argument, tap:, git:, dry_run:, fail_fast:, verbose:)
super(tap: tap, git: git, dry_run: dry_run, fail_fast: fail_fast, verbose: verbose)

@argument = argument
@added_formulae = []
@added_packages = []
@deleted_formulae = []
@deleted_packages = []
@formulae_to_fetch = []
end

def run!(args:)
detect_formulae!(args: args)
detect_packages!(args: args)

return unless ENV["GITHUB_ACTIONS"]

Expand All @@ -24,13 +27,17 @@ def run!(args:)
f.puts "added_formulae=#{@added_formulae.join(",")}"
f.puts "deleted_formulae=#{@deleted_formulae.join(",")}"
f.puts "formulae_to_fetch=#{@formulae_to_fetch.join(",")}"

f.puts "testing_packages=#{@testing_packages.join(",")}"
f.puts "added_packages=#{@added_packages.join(",")}"
f.puts "deleted_packages=#{@deleted_packages.join(",")}"
end
end

private

def detect_formulae!(args:)
test_header(:FormulaeDetect, method: :detect_formulae!)
def detect_packages!(args:)
test_header(:PackagesDetect, method: :detect_packages!)

url = nil
origin_ref = "origin/master"
Expand All @@ -40,6 +47,7 @@ def detect_formulae!(args:)

if @argument == "HEAD"
@testing_formulae = []
@testing_packages = []
# Use GitHub Actions variables for pull request jobs.
if github_ref.present? && github_repository.present? &&
%r{refs/pull/(?<pr>\d+)/merge} =~ github_ref
Expand Down Expand Up @@ -101,7 +109,7 @@ def detect_formulae!(args:)
diff_start_sha1 = current_sha1 if diff_start_sha1.blank?
diff_end_sha1 = current_sha1 if diff_end_sha1.blank?

diff_start_sha1 = diff_end_sha1 if @testing_formulae.present?
diff_start_sha1 = diff_end_sha1 if @testing_formulae.present? || @testing_packages.present?

if tap
tap_origin_ref_revision_args =
Expand All @@ -127,34 +135,50 @@ def detect_formulae!(args:)
EOS

modified_formulae = []
modified_packages = []

if tap && diff_start_sha1 != diff_end_sha1
formula_path = tap.formula_dir.to_s
@added_formulae +=
diff_formulae(diff_start_sha1, diff_end_sha1, formula_path, "A")
diff_packages(diff_start_sha1, diff_end_sha1, formula_path, "A")
modified_formulae +=
diff_formulae(diff_start_sha1, diff_end_sha1, formula_path, "M")
diff_packages(diff_start_sha1, diff_end_sha1, formula_path, "M")
@deleted_formulae +=
diff_formulae(diff_start_sha1, diff_end_sha1, formula_path, "D")
diff_packages(diff_start_sha1, diff_end_sha1, formula_path, "D")

cask_path = tap.cask_dir.to_s
@added_packages += @added_formulae +
diff_packages(diff_start_sha1, diff_end_sha1, cask_path, "A")
modified_packages += modified_formulae +
diff_packages(diff_start_sha1, diff_end_sha1, cask_path, "M")
@deleted_packages += @deleted_formulae +
diff_packages(diff_start_sha1, diff_end_sha1, cask_path, "D")
end

# If a formula is both added and deleted: it's actually modified.
# If a package is both added and deleted: it's actually modified.
added_and_deleted_formulae = @added_formulae & @deleted_formulae
@added_formulae -= added_and_deleted_formulae
@deleted_formulae -= added_and_deleted_formulae
modified_formulae += added_and_deleted_formulae

added_and_deleted_packages = @added_packages & @deleted_packages
@added_packages -= added_and_deleted_packages
@deleted_packages -= added_and_deleted_packages
modified_packages += added_and_deleted_packages

if args.test_default_formula?
# Build the default test formula.
modified_formulae << "homebrew/test-bot/testbottest"
modified_packages << "homebrew/test-bot/testbottest"
end

@testing_formulae += @added_formulae + modified_formulae
@testing_packages += @added_packages + modified_packages

# TODO: Remove `GITHUB_EVENT_NAME` check when formulae detection
# is fixed for branch jobs.
if @testing_formulae.blank? &&
@deleted_formulae.blank? &&
if @testing_packages.blank? &&
@deleted_packages.blank? &&
diff_start_sha1 == diff_end_sha1 &&
(ENV["GITHUB_EVENT_NAME"] != "push")
raise UsageError, "Did not find any formulae or commits to test!"
Expand All @@ -165,6 +189,10 @@ def detect_formulae!(args:)
@added_formulae.uniq!
modified_formulae.uniq!
@deleted_formulae.uniq!
@testing_packages.uniq!
@added_packages.uniq!
modified_packages.uniq!
@deleted_packages.uniq!

# We only need to do a fetch test on formulae that have had a change in the pkg version or bottle block.
# These fetch tests only happen in merge queues.
Expand Down Expand Up @@ -193,6 +221,11 @@ def detect_formulae!(args:)
modified_formulae #{modified_formulae.join(" ").presence || "(none)"}
deleted_formulae #{@deleted_formulae.join(" ").presence || "(none)"}
formulae_to_fetch #{@formulae_to_fetch.join(" ").presence || "(none)"}
testing_packages #{@testing_packages.join(" ").presence || "(none)"}
added_packages #{@added_packages.join(" ").presence || "(none)"}
modified_packages #{modified_packages.join(" ").presence || "(none)"}
deleted_packages #{@deleted_packages.join(" ").presence || "(none)"}
EOS
end

Expand Down Expand Up @@ -221,7 +254,7 @@ def current_sha1
rev_parse("HEAD")
end

def diff_formulae(start_revision, end_revision, path, filter)
def diff_packages(start_revision, end_revision, path, filter)
return unless tap

Utils.safe_popen_read(
Expand All @@ -230,9 +263,15 @@ def diff_formulae(start_revision, end_revision, path, filter)
start_revision, end_revision, "--", path
).lines.map do |line|
file = Pathname.new line.chomp
next unless tap.formula_file?(file)

tap.formula_file_to_name(file)
name = nil
if tap.formula_file?(file)
name = tap.formula_file_to_name(file)
elsif tap.cask_file?(file)
name = file.basename(".rb").to_s
end

name
end.compact
end
end
Expand Down

0 comments on commit e47f98c

Please sign in to comment.