Class: RakeTasks::Tests

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_tasks/tests.rb

Overview

This class assists in setting up test tasks.

Constant Summary collapse

ROOTS =

Returns an array of potential root folder names.

[
  'test',
  'tests',
  'spec',
]
PATTERNS =

The patterns that indicate that a file contains tests.

[
  '*_test.rb',
  'test_*.rb',
]

Class Method Summary collapse

Class Method Details

.exist?Boolean

Indicates that tests exist.

Returns:

  • (Boolean)


52
53
54
# File 'lib/rake_tasks/tests.rb', line 52

def exist?
  !file_list.empty?
end

.file_list(group = :all) ⇒ Object

Returns an array of test files for the specified group.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rake_tasks/tests.rb', line 57

def file_list(group = :all)
  list = []

  PATTERNS.each do |pattern|
    paths(group).each do |path|
      files = RakeTasks::System.dir_glob(File.join(path, pattern))
      list << files
    end
  end

  return list.flatten
end

.get_types(path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/rake_tasks/tests.rb', line 101

def get_types(path)
  types = []
  PATTERNS.each do |pattern|
    next if types.include?(File.basename(path))
    unless RakeTasks::System.dir_glob(File.join(path, pattern)).empty?
      types << File.basename(path)
    end
  end
  types
end

.init_rubies(configs) ⇒ Object

Initialize gemsets for rubies.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rake_tasks/tests.rb', line 167

def init_rubies(configs)
  # Loop through the test configurations to initialize gemsets.
  gem_rubies = []
  configs.each do |config|
    next if gem_rubies.include?(config[:ruby])
    gem_rubies << config[:ruby]

    cmd = ['bash', RakeTasks::SCRIPTS[:gemsets]]
    cmd << config[:ruby].split('@')

    pid = Process.spawn(*cmd.flatten)
    Process.wait pid
  end
end

.paths(group = :all) ⇒ Object

Paths to check for test files. Only paths for a specified type will be returned, if specified.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rake_tasks/tests.rb', line 198

def paths(group = :all)
  group = group.to_sym
  paths = []

  paths << root if group == :all && root

  types.each do |type|
    if group == type.to_sym || group == :all
      paths << File.join(root, type)
    end
  end

  return paths
end

.rootObject

The root test folder.



214
215
216
217
218
219
# File 'lib/rake_tasks/tests.rb', line 214

def root
  ROOTS.each do |r|
    return r if RakeTasks::System.directory?(r)
  end
  return
end

.rubies_shell_scriptObject

Outputs commands to run all tests.



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rake_tasks/tests.rb', line 152

def rubies_shell_script
  configs = test_configs

  # Loop through the test configurations.
  commands = setup_commands(configs)

  run_commands(configs).each do |command|
    commands << command
  end

  RakeTasks::System.write_file 'rubies.sh',
    commands.map { |c| c.join(' ') }
end

.rubies_yamlObject

Returns the location of the rubies yaml file.



222
223
224
225
# File 'lib/rake_tasks/tests.rb', line 222

def rubies_yaml
  return unless root
  File.join '.', root, 'rubies.yml'
end

.run_rubies?Boolean

Indicates whether tests can be run against multiple rubies.

Returns:

  • (Boolean)


113
114
115
# File 'lib/rake_tasks/tests.rb', line 113

def run_rubies?
  RakeTasks::System.file? rubies_yaml
end

.run_ruby_testsObject

Runs tests against specified ruby/gemset/rake configurations.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rake_tasks/tests.rb', line 118

def run_ruby_tests
  parser = Parser.new

  configs = test_configs

  init_rubies configs

  # Loop through the test configurations.
  configs.each do |config|
    puts '*' * 80

    if config[:rake]
      puts "#{config[:ruby]} - #{config[:rake]}"
    end

    cmd = ['bash', RakeTasks::SCRIPTS[:rubies], 'test:all']
    cmd << config[:ruby]
    cmd << config[:rake] if config[:rake]

    # Run the tests.
    pid = Process.spawn(*cmd, :out => 'out.log', :err => 'err.log')
    Process.wait pid

    parse_log parser
  end

  RakeTasks::System.rm 'out.log'
  RakeTasks::System.rm 'err.log'

  puts '*' * 80
  parser.summarize
end

.task_name(file_path) ⇒ Object

Convert a path to a file into an appropriate task name. This is done by removing the pattern that is used to indicate it is a test file.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rake_tasks/tests.rb', line 73

def task_name(file_path)
  file = File.basename(file_path, '.rb')

  PATTERNS.each do |pattern|
    pattern = pattern.sub(/\.rb$/, '').sub(/\*/, '.+?')

    if file =~ /#{pattern}/
      pattern = pattern.sub(/\.\+\?/, '')
      return file_task(file, pattern)

    end
  end
end

.test_configsObject

Returns an array of hashes containing all testable rubies/gemsets.

Output

Array

The configurations that will be tested.



185
186
187
188
189
190
191
192
193
194
# File 'lib/rake_tasks/tests.rb', line 185

def test_configs
  configs = RakeTasks::System.load_yaml(rubies_yaml)
  return [] unless configs.is_a?(Array)

  configs.select! { |c| c['ruby'] || c['gemset'] }

  set_configs configs

  configs
end

.typesObject

Return an array containing the types of tests that are included.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rake_tasks/tests.rb', line 88

def types
  return [] unless root

  types = []

  RakeTasks::System.dir_glob(File.join(root, '**')).each do |path|
    next unless RakeTasks::System.directory?(path)
    types << get_types(path)
  end

  return types.flatten
end