Class: ShellMock::CommandStub

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ CommandStub

Returns a new instance of CommandStub.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shell_mock/command_stub.rb', line 8

def initialize(command)
  @command     = command
  @side_effect = proc {}

  @reader, @writer = IO.pipe

  with_env({})
  with_options({})
  to_output(nil)
  to_succeed
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/shell_mock/command_stub.rb', line 6

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/shell_mock/command_stub.rb', line 6

def env
  @env
end

#exitstatusObject (readonly)

Returns the value of attribute exitstatus.



6
7
8
# File 'lib/shell_mock/command_stub.rb', line 6

def exitstatus
  @exitstatus
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/shell_mock/command_stub.rb', line 6

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/shell_mock/command_stub.rb', line 6

def output
  @output
end

#side_effectObject (readonly)

Returns the value of attribute side_effect.



6
7
8
# File 'lib/shell_mock/command_stub.rb', line 6

def side_effect
  @side_effect
end

Instance Method Details

#ranObject



83
84
85
# File 'lib/shell_mock/command_stub.rb', line 83

def ran
  writer.write("R")
end

#runsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shell_mock/command_stub.rb', line 68

def runs
  @runs ||= 0

  loop do
    begin
      reader.read_nonblock(1)
      @runs += 1
    rescue IO::WaitReadable
      break
    end
  end

  @runs
end

#to_exit(exitstatus) ⇒ Object Also known as: and_exit



48
49
50
51
52
# File 'lib/shell_mock/command_stub.rb', line 48

def to_exit(exitstatus)
  @exitstatus = exitstatus

  self
end

#to_failObject Also known as: and_fail



62
63
64
# File 'lib/shell_mock/command_stub.rb', line 62

def to_fail
  to_exit(1)
end

#to_onelinerObject



87
88
89
90
91
92
93
# File 'lib/shell_mock/command_stub.rb', line 87

def to_oneliner
  if output
    "echo '#{output}' && exit #{exitstatus}"
  else
    "exit #{exitstatus}"
  end
end

#to_output(output) ⇒ Object Also known as: and_output



32
33
34
35
36
# File 'lib/shell_mock/command_stub.rb', line 32

def to_output(output)
  @output = output

  self
end

#to_return(output) ⇒ Object Also known as: and_return



40
41
42
43
44
# File 'lib/shell_mock/command_stub.rb', line 40

def to_return(output)
  self.
    to_output(output).
    to_exit(0)
end

#to_succeedObject Also known as: and_succeed



56
57
58
# File 'lib/shell_mock/command_stub.rb', line 56

def to_succeed
  to_exit(0)
end

#with_env(env) ⇒ Object



20
21
22
23
24
# File 'lib/shell_mock/command_stub.rb', line 20

def with_env(env)
  @env = env

  self
end

#with_options(options) ⇒ Object



26
27
28
29
30
# File 'lib/shell_mock/command_stub.rb', line 26

def with_options(options)
  @options = options

  self
end