diff --git a/Library/Homebrew/Gemfile b/Library/Homebrew/Gemfile index 3d4f7e5298..2f231e18a7 100644 --- a/Library/Homebrew/Gemfile +++ b/Library/Homebrew/Gemfile @@ -15,6 +15,7 @@ end gem "bootsnap", require: false gem "byebug", require: false gem "json_schemer", require: false +gem "method_source", require: false gem "minitest", require: false gem "parallel_tests", require: false gem "ronn", require: false diff --git a/Library/Homebrew/Gemfile.lock b/Library/Homebrew/Gemfile.lock index 98c9c1838b..fbd80d6e2f 100644 --- a/Library/Homebrew/Gemfile.lock +++ b/Library/Homebrew/Gemfile.lock @@ -233,6 +233,7 @@ DEPENDENCIES did_you_mean json_schemer mechanize + method_source minitest parallel_tests parlour diff --git a/Library/Homebrew/cask/artifact/abstract_flight_block.rb b/Library/Homebrew/cask/artifact/abstract_flight_block.rb index eccdb10cab..64e4d727bf 100644 --- a/Library/Homebrew/cask/artifact/abstract_flight_block.rb +++ b/Library/Homebrew/cask/artifact/abstract_flight_block.rb @@ -36,6 +36,12 @@ module Cask directives.keys.map(&:to_s).join(", ") end + def to_h + require "method_source" + + directives.transform_values(&:source) + end + private def class_for_dsl_key(dsl_key) diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index b50acc9223..2e00a20a70 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -288,13 +288,9 @@ module Cask def artifacts_list artifacts.map do |artifact| - key, value = if artifact.is_a? Artifact::AbstractFlightBlock - artifact.summarize - else - [artifact.class.dsl_key, to_h_gsubs(artifact.to_args)] - end + next artifact.to_h if artifact.is_a? Artifact::AbstractFlightBlock - { key => value } + { artifact.class.dsl_key => to_h_gsubs(artifact.to_args) } end end diff --git a/Library/Homebrew/test/cask/cask_spec.rb b/Library/Homebrew/test/cask/cask_spec.rb index a6c563d012..e11a3e4c9b 100644 --- a/Library/Homebrew/test/cask/cask_spec.rb +++ b/Library/Homebrew/test/cask/cask_spec.rb @@ -265,6 +265,46 @@ describe Cask::Cask, :cask do } JSON } + let(:expected_flight_variations) { + <<~JSON + { + "arm64_big_sur": { + "artifacts": [ + { + "preflight": " preflight do\\n puts \\"preflight on Big Sur\\"\\n end\\n" + }, + { + "uninstall_postflight": " uninstall_postflight do\\n puts \\"uninstall_postflight on Big Sur\\"\\n end\\n" + } + ] + }, + "big_sur": { + "artifacts": [ + { + "preflight": " preflight do\\n puts \\"preflight on Big Sur\\"\\n end\\n" + }, + { + "uninstall_postflight": " uninstall_postflight do\\n puts \\"uninstall_postflight on Big Sur\\"\\n end\\n" + } + ] + }, + "catalina": { + "artifacts": [ + { + "preflight": " preflight do\\n puts \\"preflight on Catalina or older\\"\\n end\\n" + } + ] + }, + "mojave": { + "artifacts": [ + { + "preflight": " preflight do\\n puts \\"preflight on Catalina or older\\"\\n end\\n" + } + ] + } + } + JSON + } before do # Use a more limited symbols list to shorten the variations hash @@ -300,5 +340,13 @@ describe Cask::Cask, :cask do expect(h).to be_a(Hash) expect(JSON.pretty_generate(h["variations"])).to eq expected_sha256_variations.strip end + + it "returns the correct variations hash for a cask with conditional flight blocks" do + c = Cask::CaskLoader.load("conditional-flight") + h = c.to_hash_with_variations + + expect(h).to be_a(Hash) + expect(JSON.pretty_generate(h["variations"])).to eq expected_flight_variations.strip + end end end diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/conditional-flight.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/conditional-flight.rb new file mode 100644 index 0000000000..ea49b82c11 --- /dev/null +++ b/Library/Homebrew/test/support/fixtures/cask/Casks/conditional-flight.rb @@ -0,0 +1,21 @@ +cask "conditional-flight" do + version "1.2.3" + sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" + + url "file://#{TEST_FIXTURE_DIR}/cask/caffeine/#{platform}/#{version}/#{arch}.zip" + homepage "https://brew.sh/" + + on_big_sur do + preflight do + puts "preflight on Big Sur" + end + uninstall_postflight do + puts "uninstall_postflight on Big Sur" + end + end + on_catalina :or_older do + preflight do + puts "preflight on Catalina or older" + end + end +end