class DeltaTest::CLI::CommandBase

Constants

DEFAULT_OPTIONS

Public Class Methods

new(args) click to toggle source
# File lib/delta_test/cli/command_base.rb, line 22
def initialize(args)
  @args    = args.dup
  @options = parse_options!(@args)

  DeltaTest.verbose = !!@options['verbose']
end

Public Instance Methods

bundler_enabled?() click to toggle source

Check bundler existance

@return {Boolean}

# File lib/delta_test/cli/command_base.rb, line 111
def bundler_enabled?
  Object.const_defined?(:Bundler) || !!Utils.find_file_upward('Gemfile')
end
create_error_file() click to toggle source

Create an error file

# File lib/delta_test/cli/command_base.rb, line 164
def create_error_file
  FileUtils.mkdir_p(File.dirname(error_file))
  File.new(error_file, 'w')
end
current_process_status_success?() click to toggle source

Check exit status of the current process

@return {Boolean}

# File lib/delta_test/cli/command_base.rb, line 139
def current_process_status_success?
  $!.nil? || $!.is_a?(SystemExit) && $!.success?
end
error_file() click to toggle source

Path for an error file

@return {Pathname}

# File lib/delta_test/cli/command_base.rb, line 157
def error_file
  @error_file ||= DeltaTest.config.tmp_table_file.parent.join('error.txt')
end
error_recorded?() click to toggle source

Check if any error is recorded

@return {Boolean}

# File lib/delta_test/cli/command_base.rb, line 148
def error_recorded?
  File.exists?(error_file)
end
exec_with_data(args, ary, status = nil) click to toggle source

Exec command with data passed as stdin

@params {String} args @params {Array} ary @params {Integer|Nil} status

# File lib/delta_test/cli/command_base.rb, line 90
def exec_with_data(args, ary, status = nil)
  $stdout.sync = true

  Open3.popen3(args) do |i, o, e, w|
    i.write(ary.join("\n")) if ary
    i.close

    threads = []
    threads << Thread.new { o.each { |l| puts l } }
    threads << Thread.new { e.each { |l| $stderr.puts l } }

    ThreadsWait.all_waits(*threads)
    exit(status.nil? ? w.value.exitstatus : status)
  end
end
exit_with_message(status, *args) click to toggle source

Print message and exit with a status

@params {Integer} status - exit code @params {Object} *args

# File lib/delta_test/cli/command_base.rb, line 73
def exit_with_message(status, *args)
  if status.zero?
    puts(*args)
  else
    $stderr.puts(*args)
  end

  exit status
end
hook_create_error_file() click to toggle source

Hook on exit and record errors

# File lib/delta_test/cli/command_base.rb, line 128
def hook_create_error_file
  at_exit do
    create_error_file unless current_process_status_success?
  end
end
invoke() click to toggle source
# File lib/delta_test/cli/command_base.rb, line 33
def invoke
  begin
    invoke!
  rescue => e
    if DeltaTest.verbose?
      raise e
    else
      exit_with_message(1, '[%s] %s' % [e.class.name, e.message])
    end
  end
end
invoke!() click to toggle source
# File lib/delta_test/cli/command_base.rb, line 29
def invoke!
  raise 'Not implemented'
end
parse_options!(args) click to toggle source

Parse option arguments

@return {Hash<String, Boolean|String>}

# File lib/delta_test/cli/command_base.rb, line 50
def parse_options!(args)
  options = {}

  args.reject! do |arg|
    case arg
    when /^-([a-z0-9])$/i, /^--([a-z0-9][a-z0-9-]*)$/i
      options[$1] = true
    when /^--([a-z0-9][a-z0-9-]*)=(.+)$/i
      options[$1] = $2
    else
      break
    end
  end

  DEFAULT_OPTIONS.merge(options)
end
record_error() { || ... } click to toggle source

Wrapper of hook_create_error_file

@block

# File lib/delta_test/cli/command_base.rb, line 120
def record_error
  hook_create_error_file
  yield if block_given?
end