Skip to content

Commit

Permalink
Merge pull request #389 from Shopify/version-stuff
Browse files Browse the repository at this point in the history
Declare `required_ruby_version` 2.7 and run CI against Ruby 2.7 - 3.1, and minimum Rubocop version
  • Loading branch information
rafaelfranca committed May 13, 2022
2 parents 519f3b7 + 670e838 commit 0ad0cdb
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/ruby.yml
Expand Up @@ -4,6 +4,15 @@ on: [push, pull_request]

jobs:
build:
name: Ruby ${{matrix.ruby}} / ${{matrix.gemfile}}

strategy:
matrix:
gemfile: [Gemfile, gemfiles/minimum_rubocop.gemfile]
ruby: ["2.7", "3.0", "3.1"]

env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}

runs-on: ubuntu-latest

Expand All @@ -12,6 +21,10 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Rubocop Version
if: ${{ matrix.gemfile == 'gemfiles/minimum_rubocop.gemfile' }}
run: bundle info rubocop | head -1
- name: RuboCop and Tests
run: bundle exec rake
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@ _site
node_modules
css/
pkg/

gemfiles/*.gemfile.lock
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
rubocop-shopify (2.5.0)
rubocop (~> 1.28)
rubocop (~> 1.29)

GEM
remote: https://rubygems.org/
Expand Down
10 changes: 10 additions & 0 deletions gemfiles/minimum_rubocop.gemfile
@@ -0,0 +1,10 @@
# frozen_string_literal: true

eval_gemfile "../Gemfile"

# This naively extracts the minimum rubocop version from our gemspec,
# allowing us to ensure that it remains compatible.
minimum_rubocop_version = File.read("rubocop-shopify.gemspec")[/(?<=add_dependency\("rubocop", "~> ).*(?="\)$)/]
raise "Failed to extract minimum rubocop version from gemspec" if minimum_rubocop_version.nil?

gem "rubocop", minimum_rubocop_version
4 changes: 3 additions & 1 deletion rubocop-shopify.gemspec
Expand Up @@ -21,5 +21,7 @@ Gem::Specification.new do |s|
"allowed_push_host" => "https://rubygems.org",
}

s.add_dependency("rubocop", "~> 1.28")
s.required_ruby_version = ">= 2.7.0"

s.add_dependency("rubocop", "~> 1.29")
end
50 changes: 50 additions & 0 deletions test/ruby_versions_test.rb
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require "test_helper"
require "pry"
require "yaml"

class RubyVersionsTest < Minitest::Test
def test_ci_matrix_includes_minimum_ruby_version
minimum_ruby_version = normalize(extract_minimum_ruby_version_from_gemspec)
ruby_versions = extract_ruby_versions_from_ci_matrix.map { |version| normalize(version) }

assert_includes(
ruby_versions,
minimum_ruby_version,
"CI must be configured to test as far back as gem's required_ruby_version",
)
end

private

def extract_minimum_ruby_version_from_gemspec
# This naively extracts the minimum Ruby version compatible with out gemspec
# allowing us to ensure that it is included in the CI matrix.
minimum_ruby_version = File.read("rubocop-shopify.gemspec")[/(?<=required_ruby_version = ">= ).*(?="$)/]

return minimum_ruby_version unless minimum_ruby_version.nil?

flunk("Failed to extract required_ruby_version from gemspec")
end

def extract_ruby_versions_from_ci_matrix
YAML
.load_file(".github/workflows/ruby.yml")
.fetch("jobs")
.fetch("build")
.fetch("strategy")
.fetch("matrix")
.fetch("ruby")
end

# Remove the trailing .0 for versions of the form X.Y.0
# Otherwise, return the version unchanged
def normalize(version)
if version.match?(/^\d+\.\d+\.0$/)
version.chomp(".0")
else
version
end
end
end

0 comments on commit 0ad0cdb

Please sign in to comment.