File: spec_suite_configuration.rb

package info (click to toggle)
ruby-rr 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,424 kB
  • sloc: ruby: 11,405; makefile: 7
file content (126 lines) | stat: -rw-r--r-- 3,016 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require File.expand_path('../spec_suite_runner', __FILE__)

require 'yaml'
require 'erb'
require 'forwardable'
require 'ostruct'

class SpecSuiteConfiguration
  SUITES_CONFIG_FILE = File.expand_path('../suites.yml', __FILE__)
  TRAVIS_CONFIG_FILE = File.expand_path('../../.travis.yml', __FILE__)

  class Suite < Struct.new(:name, :desc, :rvm_version, :ruby_id, :env, :travis)
    attr_reader :runner

    def initialize(name, desc, rvm_version, ruby_id, env, travis)
      super
      @runner = SpecSuiteRunner.new(self)
    end

    def appraisal_name
      "ruby_#{ruby_id}_#{name}"
    end

    def run
      runner.call
    end

    def current_ruby_id
      '19'
    end

    def matching_current_ruby_version?
      ruby_id == current_ruby_id
    end
  end

  attr_reader :ruby_groups, :runners

  def self.build
    new(parse)
  end

  def self.parse
    suites = []
    yaml = ERB.new(File.read(SUITES_CONFIG_FILE)).result(binding)
    ruby_groups = YAML.load(yaml)
    ruby_groups.each do |ruby_id, ruby_group|
      base_env = ruby_group.fetch('env', {})
      ruby_group['suites'].each do |suite|
        ruby_group['rvm_versions'].each do |rvm_version|
          suite = suite.dup
          suite['ruby_id'] = ruby_id.to_s
          suite['rvm_version'] = rvm_version['name']
          suite['env'] = base_env.merge(rvm_version.fetch('env', {}))
          suite['travis'] = ruby_group.fetch('travis', {})
          suite['travis']['env'] = base_env.merge(suite['travis'].fetch('env', {}))
          suites << suite
        end
      end
    end
    suites
  end

  def self.root_dir
    File.expand_path('../..', __FILE__)
  end

  attr_reader :suites

  def initialize(suites)
    @suites = suites.map do |suite|
      Suite.new(
        suite['name'],
        suite['desc'],
        suite['rvm_version'],
        suite['ruby_id'],
        suite['env'],
        suite['travis']
      )
    end
  end

  def each_matching_suite(&block)
    suites.
      select { |suite| suite.matching_current_ruby_version? }.
      each(&block)
  end

  def to_travis_config
    inclusions = []
    allowed_failures = []

    sorted_suites = suites.sort_by { |suite| [suite.rvm_version, suite.name] }

    sorted_suites.each do |suite|
      env = suite.travis['env'].merge('SUITE' => suite.name)
      row = {
        'rvm' => suite.rvm_version,
        'env' => env.map { |key, val| "#{key}=#{val.inspect}" }
      }
      inclusions << row
      if suite.rvm_version == 'jruby-19mode'
        allowed_failures << row
      end
    end
    {
      'language' => 'ruby',
      'script' => 'rake spec:${SUITE}',
      'rvm' => '1.9.3',
      'matrix' => {
        # exclude default row
        'exclude' => [
          {'rvm' => '1.9.3'},
        ],
        'include' => inclusions,
        'allow_failures' => allowed_failures
      }
    }
  end

  def generate_travis_config
    config = to_travis_config
    File.open(TRAVIS_CONFIG_FILE, 'w') {|f| YAML.dump(config, f) }
    puts "Updated .travis.yml."
  end
end