Class: BlueShell::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/blue-shell/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/blue-shell/runner.rb', line 10

def initialize(*args)
  @stdout, slave = PTY.open
  system('stty raw', :in => slave)
  read, @stdin = IO.pipe

  @pid = spawn(*(args.push(:in => read, :out => slave, :err => slave)))

  @expector = BufferedReaderExpector.new(@stdout, ENV['DEBUG_BACON'])

  if block_given?
    yield self
  else
    wait_for_exit
  end
end

Instance Method Details

#debugObject



94
95
96
# File 'lib/blue-shell/runner.rb', line 94

def debug
  @expector.debug
end

#debug=(x) ⇒ Object



98
99
100
# File 'lib/blue-shell/runner.rb', line 98

def debug=(x)
  @expector.debug = x
end

#exit_codeObject Also known as: wait_for_exit



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/blue-shell/runner.rb', line 59

def exit_code
  return @code if @code

  code = nil
  begin
    Timeout.timeout(BlueShell.timeout) do
      _, code = Process.waitpid2(@pid)
    end
  rescue Timeout::Error
    raise ::Timeout::Error.new("execution expired, output was:\n#{@expector.read_to_end}")
  end

  @code = numeric_exit_code(code)
end

#exited?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/blue-shell/runner.rb', line 76

def exited?
  !running?
end

#expect(matcher) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/blue-shell/runner.rb', line 30

def expect(matcher)
  case matcher
  when Hash
    expect_branches(matcher)
  else
    @expector.expect(matcher)
  end
end

#outputObject



90
91
92
# File 'lib/blue-shell/runner.rb', line 90

def output
  @expector.output
end

#running?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/blue-shell/runner.rb', line 86

def running?
  !!Process.getpgid(@pid)
end

#send_backspaceObject



47
48
49
# File 'lib/blue-shell/runner.rb', line 47

def send_backspace
  @stdin.print("\b \b")
end

#send_keys(text_to_send) ⇒ Object



51
52
53
# File 'lib/blue-shell/runner.rb', line 51

def send_keys(text_to_send)
  @stdin.puts(text_to_send)
end

#send_returnObject



55
56
57
# File 'lib/blue-shell/runner.rb', line 55

def send_return
  @stdin.puts
end

#send_right_arrowObject



43
44
45
# File 'lib/blue-shell/runner.rb', line 43

def send_right_arrow
  @stdin.print("\e[C")
end

#send_up_arrowObject



39
40
41
# File 'lib/blue-shell/runner.rb', line 39

def send_up_arrow
  @stdin.print("\e[A")
end

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


80
81
82
# File 'lib/blue-shell/runner.rb', line 80

def success?
  @code.zero?
end

#with_timeout(timeout, &block) ⇒ Object



26
27
28
# File 'lib/blue-shell/runner.rb', line 26

def with_timeout(timeout, &block)
  BlueShell.with_timeout(timeout, &block)
end