Module: Appraisal::Matrix::AppraiseFileWithMatrix

Defined in:
lib/appraisal/matrix/extensions/appraisal_file.rb

Defined Under Namespace

Classes: VersionArray

Instance Method Summary collapse

Instance Method Details

#appraisal_matrix(**kwargs, &block) ⇒ Object

Define a matrix of appraisals to test against Expected usage:

appraisal_matrix(rails: "6.0")
appraisal_matrix(rails: "> 6.0.3")
appraisal_matrix(rails: [">= 6.0", "< 7.1"])
appraisal_matrix(rails: { versions: [">= 6.0", "< 7.1"], step: "major" })
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


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/appraisal/matrix/extensions/appraisal_file.rb', line 43

def appraisal_matrix(**kwargs, &block)
  # names_and_versions_to_test
  # [
  #   [[rails, 6.0], [rails, 6.1], [rails, 7.0], [rails, 7.1]],
  #   [[sidekiq, 1.0], [sidekiq, 2.0]],
  #   [[a, x], [a, y], [a, z]]
  # ]
  names_and_versions_to_test =
    kwargs.map do |gem_name, version_options|
      version_array =
        case version_options
        when String
          parsed_options = version_options.include?(" ") ? [version_options] : [">= #{version_options}"]
          VersionArray.new(gem_name: gem_name, versions: parsed_options)
        when Integer, Float
          VersionArray.new(gem_name: gem_name, versions: [">= #{version_options}"])
        when Array
          VersionArray.new(gem_name: gem_name, versions: version_options)
        when Hash
          VersionArray.new(gem_name: gem_name, **version_options)
        end

      version_array.versions.map do |version|
        [gem_name, version]
      end
    end

  # matrix
  # [
  #   [[rails, 6.0], [sidekiq, 1.0], ...[a, x]]
  #   [[rails, 6.0], [sidekiq, 1.0], ...[a, y]]
  #   [[rails, 6.0], [sidekiq, 1.0], ...[a, z]]
  #   ...
  #   [[rails, 7.1], [sidekiq, 2.0], ...[a, z]]
  # ]
  matrix = names_and_versions_to_test[0].product(*names_and_versions_to_test[1..])

  # Iterate over versions to test and create appraisal per
  matrix.each do |appraisal_spec|
    appraise appraisal_file_name(appraisal_spec) do
      appraisal_spec.each do |gem_name, version|
        gem gem_name, "~> #{version}.0"
      end
      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