class Swat::UI::RSpecCommands::CommandsBuilder

Constants

ENV_VARS

Attributes

config[R]
revision_name[R]
time[R]

Public Class Methods

current_branch() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 94
def current_branch
  call_command(current_branch_command) rescue 'undefined'
end
current_revision_name() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 78
def current_revision_name
  env[ENV_VARS.revision_name]
end
current_revision_time() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 62
def current_revision_time
  env[ENV_VARS.revision_time] ? env[ENV_VARS.revision_time].to_i : now.utc.to_i
end
current_scenarios() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 82
def current_scenarios
  new(Swat::UI.config.options, current_revision_time, current_revision_name).thread_scenarios
end
current_thread_id() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 74
def current_thread_id
  env[ENV_VARS.thread_id].to_i
end
current_thread_name() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 70
def current_thread_name
  env[ENV_VARS.thread_name]
end
current_threads_count() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 66
def current_threads_count
  env[ENV_VARS.threads_count] || 1
end
current_user() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 98
def current_user
  (env[ENV_VARS.user] || call_command(current_user_command)) rescue 'undefined'
end
debug_mode?() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 90
def debug_mode?
  !!env[ENV_VARS.debug_mode]
end
env() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 102
def env
  ENV
end
new(conf_options, time, name=nil) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 25
def initialize(conf_options, time, name=nil)
  @config = conf_options
  unless @config[:threads]
    @config[:threads] = [{ name: 'Full' }]
  end
  @time = time
  @name = name || @time
end
now() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 86
def now
  (Time.respond_to?(:now_without_mock_time) ? Time.now_without_mock_time : Time.now)
end

Private Class Methods

call_command(c) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 116
def call_command(c)
  `#{c}`.gsub("\n",'')
end
current_branch_command() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 108
def current_branch_command
  "git branch | grep '\''*'\'' | awk '\''{print $2}'\''"
end
current_user_command() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 112
def current_user_command
  'whoami'
end

Public Instance Methods

env() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 122
def env
  self.class.env
end
thread_name(thread_opts, index) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 57
def thread_name(thread_opts, index)
  (thread_opts[:name] || "Thread##{index+1}")
end
thread_scenario(thread_opts, index) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 46
def thread_scenario(thread_opts, index)
  {
      index: index,
      name: @name,
      thread: thread_name(thread_opts, index),
      clean: clean_command(thread_opts, index),
      prepare: prepare_command(thread_opts, index),
      run: run_command(thread_opts, index),
  }
end
thread_scenarios() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 38
def thread_scenarios
  res = []
  @config[:threads].each_with_index  do |th, index|
    res << thread_scenario(th, index)
  end
  res
end
threads_count() click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 34
def threads_count
  @config[:threads].count
end

Private Instance Methods

build_params(params) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 159
def build_params(params)
  params.to_a.map do |k, v|
    [ k, "'#{v}'" ]*?=
  end.join(' ')
end
clean_command(thread_opts, index) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 150
def clean_command(thread_opts, index)
  env_params = build_params({
      ENV_VARS.rails_env => 'test',
      ENV_VARS.db_env_number => index,
  })

  [ env_params, rake_command('db:drop') ].join(' ')
end
file_pattern_opts(pattern) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 180
def file_pattern_opts(pattern)
  pattern
end
prepare_command(thread_opts, index) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 141
def prepare_command(thread_opts, index)
  env_params = build_params({
      ENV_VARS.rails_env => 'test',
      ENV_VARS.db_env_number => index,
  })

  [ env_params, rake_command('db:create'), '&&', env_params, rake_command('db:migrate') ].join(' ')
end
rake_command(task) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 184
def rake_command(task)
  "rake #{task}"
end
rspec_command(thread_opts) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 165
def rspec_command(thread_opts)
  [
      'rspec',
      tag_opts(thread_opts[:tags]),
      file_pattern_opts(thread_opts[:file_pattern]),
  ].compact.join ' '
end
run_command(thread_opts, index) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 128
def run_command(thread_opts, index)
  env_params = build_params({
    ENV_VARS.db_env_number => index,
    ENV_VARS.revision_name => @name,
    ENV_VARS.revision_time => @time,
    ENV_VARS.threads_count => threads_count,
    ENV_VARS.thread_id => index,
    ENV_VARS.thread_name => thread_name(thread_opts, index),
  })

  [ env_params, rspec_command(thread_opts) ].join(' ')
end
tag_opts(tags) click to toggle source
# File lib/swat/ui/rspec_commands.rb, line 173
def tag_opts(tags)
  return nil unless tags
  ([tags].flatten).map do |tag_name|
    [ '--tag', tag_name ]*' '
  end*' '
end