From 3233f4a247558206acc917240592d4920741e085 Mon Sep 17 00:00:00 2001 From: Tristan Starck Date: Mon, 15 Jul 2024 16:46:14 -0700 Subject: [PATCH] pass current version to block as kwargs --- .gitignore | 2 + CHANGELOG.md | 4 + Gemfile.lock | 2 +- README.md | 12 +++ .../matrix/extensions/appraisal_file.rb | 12 ++- lib/appraisal/matrix/version.rb | 2 +- .../matrix/extensions/appraisal_file_spec.rb | 90 +++++++++++++++++-- 7 files changed, 116 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 2297067..8c44e5a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,9 +6,11 @@ /pkg/ /spec/reports/ /tmp/ +.idea/ # rspec failure tracking .rspec_status # appraisal gemfiles/*.lock +.rubocop-https* diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cb64c3..a2b897c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.3.0 - Unreleased +### Changed +- Changed the block passed to `appraisal_matrix` to be called with the versions of gems for the current matrix entry. + ## 0.2.0 - 2024-07-09 ### Added - Support special request options for `appraisal_matrix`: diff --git a/Gemfile.lock b/Gemfile.lock index 29c4817..ece43ac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - appraisal-matrix (0.2.0) + appraisal-matrix (0.3.0) appraisal (~> 2.2) GEM diff --git a/README.md b/README.md index 8a4715e..c6c535f 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,18 @@ appraisal_matrix(activesupport: "6.1") do end ``` +#### Block arguments +If you would like to setup conditional logic based off of the versions of the gems in the matrix, you can pass a block with arguments to `appraisal_matrix`. + +```ruby +appraisal_matrix(activesupport: "6.1") do |activesupport:, sidekiq:| + # activesupport + if activesupport < "7" + remove_gem 'test_after_commit' + end +end +``` + ### Appraising more than one Gem You can also pass more than one gem to `appraisal_matrix` to create a matrix of all combinations of the specified gems. diff --git a/lib/appraisal/matrix/extensions/appraisal_file.rb b/lib/appraisal/matrix/extensions/appraisal_file.rb index f4f7127..25ca092 100644 --- a/lib/appraisal/matrix/extensions/appraisal_file.rb +++ b/lib/appraisal/matrix/extensions/appraisal_file.rb @@ -33,6 +33,13 @@ def versions # appraisal_matrix(rails: "6.0") do # gem "sqlite3", "~> 2.5" # end + # appraisal_matrix(rails: "6.0", sidekiq: "5.0") do |rails:, sidekiq:| + # if rails < "7" + # # do something... + # else + # # do something else... + # end + # end def appraisal_matrix(**kwargs, &block) # names_and_versions_to_test # [ @@ -76,7 +83,10 @@ def appraisal_matrix(**kwargs, &block) appraisal_spec.each do |gem_name, version| gem gem_name, "~> #{version}.0" end - instance_eval(&block) if block + if block + block_args = appraisal_spec.to_h { |gem_name, version| [gem_name.to_sym, Gem::Version.new(version)] } + instance_exec(**block_args, &block) + end end end end diff --git a/lib/appraisal/matrix/version.rb b/lib/appraisal/matrix/version.rb index 6b0e693..62405dc 100644 --- a/lib/appraisal/matrix/version.rb +++ b/lib/appraisal/matrix/version.rb @@ -2,6 +2,6 @@ module Appraisal module Matrix - VERSION = "0.2.0" + VERSION = "0.3.0" end end diff --git a/spec/appraisal/matrix/extensions/appraisal_file_spec.rb b/spec/appraisal/matrix/extensions/appraisal_file_spec.rb index 2e698ec..f4e565a 100644 --- a/spec/appraisal/matrix/extensions/appraisal_file_spec.rb +++ b/spec/appraisal/matrix/extensions/appraisal_file_spec.rb @@ -92,23 +92,57 @@ let(:desired_gems) { { rails: "6.1" } } it_behaves_like "a matrix of appraisals" + end + + context "with a block to pass into each appraisal" do + subject(:run_matrix) do + appraisal_matrix(**desired_gems) { gem "sqlite3", "~> 2.5" } + end + let(:desired_gems) { { rails: "6.1" } } + + it "yields the block to each appraisal" do + expect(self).to receive(:appraise).with("rails-6_1").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 6.1.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + expect(self).to receive(:appraise).with("rails-7_0").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 7.0.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + expect(self).to receive(:appraise).with("rails-7_1").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 7.1.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + + run_matrix + end - context "with a block to pass into each appraisal" do - it "yields the block to each appraisal" do + context "with a block with arguments" do + subject(:run_matrix) do + appraisal_matrix(**desired_gems) do |rails:| + if rails < "7" + gem "sqlite3", "< 2" + else + gem "sqlite3", "~> 2.5" + end + end + end + + it "yields the block with the versions to each appraisal" do expect(self).to receive(:appraise).with("rails-6_1").and_yield do |block_scope| expect(block_scope).to receive(:gem).with(:rails, "~> 6.1.0") - expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + expect(block_scope).to receive(:gem).with("sqlite3", "< 2") end expect(self).to receive(:appraise).with("rails-7_0").and_yield do |block_scope| expect(block_scope).to receive(:gem).with(:rails, "~> 7.0.0") expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") end - expect(self).to receive(:appraise).with("rails-7_1").and_yield do |block_scope| + expect(self).to receive(:appraise).with("rails-7_1").and_yield do |block_scope, versions| expect(block_scope).to receive(:gem).with(:rails, "~> 7.1.0") expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") end - appraisal_matrix(**desired_gems) { gem "sqlite3", "~> 2.5" } + run_matrix end end end @@ -168,6 +202,52 @@ end subject end + + context "with a block" do + subject(:run_matrix) do + appraisal_matrix(**desired_gems) do |rails:, sidekiq:| + if rails < "7" && sidekiq < "6" + gem "sqlite3", "< 2" + else + gem "sqlite3", "~> 2.5" + end + end + end + + it "yields the block with the versions to each appraisal" do + expect(self).to receive(:appraise).with("rails-6_1-sidekiq-5_0").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 6.1.0") + expect(block_scope).to receive(:gem).with(:sidekiq, "~> 5.0.0") + expect(block_scope).to receive(:gem).with("sqlite3", "< 2") + end + expect(self).to receive(:appraise).with("rails-6_1-sidekiq-6_0").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 6.1.0") + expect(block_scope).to receive(:gem).with(:sidekiq, "~> 6.0.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + expect(self).to receive(:appraise).with("rails-7_0-sidekiq-5_0").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 7.0.0") + expect(block_scope).to receive(:gem).with(:sidekiq, "~> 5.0.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + expect(self).to receive(:appraise).with("rails-7_0-sidekiq-6_0").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 7.0.0") + expect(block_scope).to receive(:gem).with(:sidekiq, "~> 6.0.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + expect(self).to receive(:appraise).with("rails-7_1-sidekiq-5_0").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 7.1.0") + expect(block_scope).to receive(:gem).with(:sidekiq, "~> 5.0.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + expect(self).to receive(:appraise).with("rails-7_1-sidekiq-6_0").and_yield do |block_scope| + expect(block_scope).to receive(:gem).with(:rails, "~> 7.1.0") + expect(block_scope).to receive(:gem).with(:sidekiq, "~> 6.0.0") + expect(block_scope).to receive(:gem).with("sqlite3", "~> 2.5") + end + run_matrix + end + end end end end