|
| 1 | +require 'yaml' |
| 2 | + |
| 3 | +RSpec.describe "GitHub Actions workflow Ruby version consistency" do |
| 4 | + let(:workflows_dir) { File.expand_path('../../.github/workflows', __dir__) } |
| 5 | + |
| 6 | + let(:latest_matrix_version) do |
| 7 | + pr_workflow = YAML.safe_load( |
| 8 | + File.read(File.join(workflows_dir, 'pull_request.yml')), |
| 9 | + permitted_classes: [Symbol] |
| 10 | + ) |
| 11 | + versions = pr_workflow.dig('jobs', 'build', 'strategy', 'matrix', 'ruby') |
| 12 | + expect(versions).not_to be_nil, "No ruby matrix found in pull_request.yml" |
| 13 | + versions.map(&:to_s).max_by { |v| Gem::Version.new(v) } |
| 14 | + end |
| 15 | + |
| 16 | + let(:hardcoded_ruby_versions) do |
| 17 | + versions = {} |
| 18 | + |
| 19 | + Dir[File.join(workflows_dir, '*.yml')].each do |file| |
| 20 | + workflow = YAML.safe_load(File.read(file), permitted_classes: [Symbol]) |
| 21 | + basename = File.basename(file) |
| 22 | + |
| 23 | + next unless workflow['jobs'] |
| 24 | + |
| 25 | + workflow['jobs'].each do |job_name, job| |
| 26 | + steps = job['steps'] || [] |
| 27 | + steps.each do |step| |
| 28 | + next unless step.is_a?(Hash) && step.dig('with', 'ruby-version') |
| 29 | + |
| 30 | + ruby_version = step['with']['ruby-version'].to_s |
| 31 | + next if ruby_version.include?('${{') |
| 32 | + |
| 33 | + versions["#{basename} -> #{job_name}"] = ruby_version |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + versions |
| 39 | + end |
| 40 | + |
| 41 | + it "uses the same Ruby version as the latest in pull_request.yml matrix" do |
| 42 | + expect(hardcoded_ruby_versions).not_to be_empty, |
| 43 | + "No hardcoded ruby-version found in workflows" |
| 44 | + |
| 45 | + mismatched = hardcoded_ruby_versions.reject { |_, v| v == latest_matrix_version } |
| 46 | + expect(mismatched).to be_empty, |
| 47 | + "Expected all hardcoded ruby-version to be '#{latest_matrix_version}' " \ |
| 48 | + "(latest in pull_request.yml matrix), but found:\n" \ |
| 49 | + "#{mismatched.map { |k, v| " #{k}: #{v}" }.join("\n")}" |
| 50 | + end |
| 51 | +end |
0 commit comments