class RakeTasks::Tests

This class assists in setting up test tasks.

Constants

PATTERNS

The patterns that indicate that a file contains tests.

ROOTS

Returns an array of potential root folder names.

Public Class Methods

exist?() click to toggle source

Indicates that tests exist.

# File lib/rake_tasks/tests.rb, line 52
def exist?
  !file_list.empty?
end
file_list(group = :all) click to toggle source

Returns an array of test files for the specified group.

# 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) click to toggle source
# 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) click to toggle source

Initialize gemsets for rubies.

# 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) click to toggle source

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

# 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
root() click to toggle source

The root test folder.

# 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_script() click to toggle source

Outputs commands to run all tests.

# 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_yaml() click to toggle source

Returns the location of the rubies yaml file.

# File lib/rake_tasks/tests.rb, line 222
def rubies_yaml
  return unless root
  File.join '.', root, 'rubies.yml'
end
run_rubies?() click to toggle source

Indicates whether tests can be run against multiple rubies.

# File lib/rake_tasks/tests.rb, line 113
def run_rubies?
  RakeTasks::System.file? rubies_yaml
end
run_ruby_tests() click to toggle source

Runs tests against specified ruby/gemset/rake configurations.

# 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) click to toggle source

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.

# 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_configs() click to toggle source

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

Output

Array

The configurations that will be tested.

# 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
types() click to toggle source

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

# 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

Private Class Methods

file_task(file, pattern) click to toggle source
# File lib/rake_tasks/tests.rb, line 231
def file_task(file, pattern)
  if pattern.index('_') == 0
    return file.sub(/#{pattern}$/, '')
  else
    return file.sub(/^#{pattern}/, '')
  end
end
gemset_create_commands(configs) click to toggle source
# File lib/rake_tasks/tests.rb, line 297
def gemset_create_commands(configs)
  cmds = []
  cmds << ['set', '-e']
  configs.uniq { |c| c[:ruby] }.each do |config|
    ruby = config[:ruby].sub(/@.*/, '')
    gemset = config[:ruby].sub(/.*@/, '')
    cmds << ['rvm', ruby, 'do', 'rvm', 'gemset', 'create', gemset]
  end
  cmds
end
parse_log(parser) click to toggle source
# File lib/rake_tasks/tests.rb, line 239
def parse_log(parser)
  RakeTasks::System.open_file('out.log', 'r') do |file|
    while line = file.gets
      parser.parse line
    end
  end
end
run_commands(configs) click to toggle source
# File lib/rake_tasks/tests.rb, line 279
def run_commands(configs)
  cmds = []
  if configs.any? { |c| c[:rake] }
    configs.each do |config|
      if config[:rake]
        cmds << ['echo',
          "ruby: #{config[:ruby]} / rake: #{config[:rake]}"]
        cmds << ['rvm', config[:ruby], 'do', 'rake', "_#{config[:rake]}_"]
      else
        cmds << ['rvm', config[:ruby], 'do', 'bundle', 'exec', 'rake']
      end
    end
  else
    cmds << ['rvm', rvm_rubies(configs), 'do', 'bundle', 'exec', 'rake']
  end
  cmds
end
rvm_rubies(configs) click to toggle source
# File lib/rake_tasks/tests.rb, line 308
def rvm_rubies(configs)
  configs.uniq { |c| c[:ruby] }.map do |config|
    config[:ruby]
  end.join(',')
end
set_configs(configs) click to toggle source
# File lib/rake_tasks/tests.rb, line 247
def set_configs(configs)
  for i in 0..configs.length - 1 do
    config = configs[i]

    config.keys.each do |key|
      config[key.to_sym] = config[key]
      config.delete key
    end

    if config[:gemset]
      config[:ruby] = "#{config[:ruby]}@#{config[:gemset]}"
    end

    config.delete(:gemset)
  end
end
setup_commands(configs) click to toggle source
# File lib/rake_tasks/tests.rb, line 264
def setup_commands(configs)
  cmds = gemset_create_commands(configs)
  cmds << ['rvm', rvm_rubies(configs), 'do', 'gem', 'install',
    'bundler', '--no-rdoc', '--no-ri']
  cmds << ['rvm', rvm_rubies(configs), 'do', 'bundle', 'install']
  cmds << ['rvm', rvm_rubies(configs), 'do', 'bundle', 'clean', '--force']
  configs.each do |config|
    if config[:rake]
      cmds << ['rvm', config[:ruby], 'do', 'gem', 'install',
        'rake', '-v', config[:rake], '--no-rdoc', '--no-ri']
    end
  end
  cmds
end