Class: CLITest

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

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(binary_path) ⇒ CLITest

Returns a new instance of CLITest.



11
12
13
# File 'lib/cli_test.rb', line 11

def initialize(binary_path)
  @binary_path = File.expand_path(binary_path)
end

Instance Method Details

#cli_arguments_to_argv(arguments_string_or_array) ⇒ Object



56
57
58
59
# File 'lib/cli_test.rb', line 56

def cli_arguments_to_argv(arguments_string_or_array)
  return arguments_string_or_array if arguments_string_or_array.is_a?(Array)
  arguments_string_or_array.split
end

#in_fork(args) ⇒ Object

Run the binary under test with passed options, and return [exit_code, stdout_content, stderr_content] There is a limitation however: your app should be using $stdout and $stdeer and NOT their constant equivalents.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cli_test.rb', line 64

def in_fork(args)
  args.unshift(@binary_path)
  
  if ruby19?
    Open3.popen3(ENV, args.join(' ')) do |stdin, stdout, stderr, wait_thr|
      # Process::Status object returned in Ruby 1.9
      [make_signed(wait_thr.value.exitstatus), stdout.read, stderr.read]
    end
  else
    Open3.popen3(args.join(' ')) do |stdin, stdout, stderr|
      [make_signed($?.to_i), stdout.read, stderr.read]
    end
  end
end

#in_process(commandline_arguments) ⇒ Object

Run the binary under test with passed options, and return [exit_code, stdout_content, stderr_content] There is a limitation however: your app should be using $stdout and $stdeer and NOT their constant equivalents.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cli_test.rb', line 32

def in_process(commandline_arguments)
  old_stdout, old_stderr, old_argv = $stdout, $stderr, ARGV.dup
  os, es = StringIO.new, StringIO.new
  begin
    $stdout, $stderr, verbosity = os, es, $VERBOSE
    ARGV.replace(commandline_arguments)
    $VERBOSE = false
    
    load(@binary_path)
    return [0, os.string, es.string]
    
  rescue SystemExit => boom # The binary uses exit(), we use that to preserve the output code
    return [boom.status, os.string, es.string]
  rescue Exception => e
    es.puts(e.class)
    es.puts(e.exception)
    return [1, os.string, es.string]
  ensure
    $VERBOSE = verbosity
    ARGV.replace(old_argv)
    $stdout, $stderr = old_stdout, old_stderr
  end
end

#make_signed(retcode) ⇒ Object

yuck!



80
81
82
83
# File 'lib/cli_test.rb', line 80

def make_signed(retcode)
  ((retcode > 128)) && ((retcode = retcode - 256))
  retcode
end

#not_unix?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/cli_test.rb', line 20

def not_unix?
  RUBY_PLATFORM =~ /mswin|jruby/
end

#ruby19?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cli_test.rb', line 25

def ruby19?
  !(RUBY_VERSION < "1.9")
end

#run(commandline_arguments) ⇒ Object



15
16
17
18
# File 'lib/cli_test.rb', line 15

def run(commandline_arguments)
  arguments_as_argv = cli_arguments_to_argv(commandline_arguments)
  (not_unix? || !ruby19?) ? in_process(arguments_as_argv) : in_fork(arguments_as_argv)
end