Class: Qor::Test::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/qor_test/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
# File 'lib/qor_test/cli.rb', line 9

def initialize(options={})
  self.options = options
  self.scripts = []
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/qor_test/cli.rb', line 7

def options
  @options
end

#scriptsObject

Returns the value of attribute scripts.



7
8
9
# File 'lib/qor_test/cli.rb', line 7

def scripts
  @scripts
end

Class Method Details

.copy_sample_configurationObject



73
74
75
76
77
# File 'lib/qor_test/cli.rb', line 73

def self.copy_sample_configuration
  FileUtils.mkdir_p(File.dirname(Qor::Test::Configuration.config_path))
  FileUtils.cp(Qor::Test::Configuration.sample_file, Qor::Test::Configuration.config_path)
  puts("Copied sample configuration to #{Qor::Test::Configuration.config_path}!")
end

.temp_directoryObject



68
69
70
71
# File 'lib/qor_test/cli.rb', line 68

def self.temp_directory
  tempfile = Tempfile.new('fake')
  File.dirname(tempfile.path)
end

Instance Method Details

#gemfileObject



14
15
16
# File 'lib/qor_test/cli.rb', line 14

def gemfile
  @gemfile ||= Qor::Test::Gemfile.new(options)
end

#gemfilesObject



18
19
20
# File 'lib/qor_test/cli.rb', line 18

def gemfiles
  @gemfiles ||= gemfile.generate_gemfiles.reverse
end

#rubiesObject



22
23
24
# File 'lib/qor_test/cli.rb', line 22

def rubies
  gemfile.ruby_versions
end

#runObject



26
27
28
29
30
31
32
33
# File 'lib/qor_test/cli.rb', line 26

def run
  rubies.map do |version|
    scripts << Qor::Test::Rubies.switch_ruby_version(version)
    gemfiles.map { |file| run_with_gemfile(file) }
  end
rescue Qor::Dsl::ConfigurationNotFound
  puts "ConfigurationNotFound, please run `qor_test --init` in project's root to get a sample configuration"
end

#run_with_gemfile(file) ⇒ Object



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
# File 'lib/qor_test/cli.rb', line 35

def run_with_gemfile(file)
  $case_num += 1
  scripts << "echo -n '\n\e[01;31m[ENV #{gemfile.group_name}] \e[0m'" unless gemfile.group_name.nil?
  scripts << "echo -n '\e[31mRunning case #{$case_num} with '$(ruby -v)', '$[$total_cases_num-#{$case_num}]' cases left\e[0m\n'"

  lock_file = "#{file}.lock"
  temp_file = "QorTest_" + File.basename(file)
  temp_lock = "#{temp_file}.lock"

  # Copy Gemfile and Gemfile.lock if exist
  scripts << "cp '#{file}' '#{temp_file}'"
  scripts << "[ -f '#{lock_file}' ] && cp '#{lock_file}' '#{temp_lock}'"

  # Export Gemfile
  scripts << "echo '>> BUNDLE_GEMFILE=#{file}'"
  scripts << "export BUNDLE_GEMFILE=#{temp_file}"

  # Test commands
  [
    "bundle install --quiet",
    "#{options[:command].sub(/^(bundle\s+exec\s+)?/, 'bundle exec ')} && pass_cases_num=$(($pass_cases_num+1)) || failed_cases=(\"${failed_cases[@]}\" \"RUBY_VERSION:$(ruby -v | sed 's/ /-/g') #{file}\")"
  ].map do |command|
    scripts << "echo '>> #{command.sub(/ && .*$/,'')}'"
    scripts << command unless options[:pretend]
  end

  # Backup & Cleanup
  scripts << "[ -f '#{temp_lock}' ] && cp '#{temp_lock}' '#{lock_file}'"
  [temp_file, temp_lock].map do |file|
    scripts << "[ -f '#{file}' ] && rm '#{file}'"
  end
end