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.



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

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

  @reader, @writer = IO.pipe

  with_env({})
  with_options({})
  and_output(nil)
  and_succeed
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#exitstatusObject (readonly)

Returns the value of attribute exitstatus.



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

def exitstatus
  @exitstatus
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#side_effectObject (readonly)

Returns the value of attribute side_effect.



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

def side_effect
  @side_effect
end

Instance Method Details

#and_exit(exitstatus) ⇒ Object



43
44
45
46
47
# File 'lib/shell_mock/command_stub.rb', line 43

def and_exit(exitstatus)
  @exitstatus = exitstatus

  self
end

#and_failObject



53
54
55
# File 'lib/shell_mock/command_stub.rb', line 53

def and_fail
  and_exit(1)
end

#and_output(output) ⇒ Object



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

def and_output(output)
  @output = output

  self
end

#and_return(output) ⇒ Object



37
38
39
40
41
# File 'lib/shell_mock/command_stub.rb', line 37

def and_return(output)
  self.
    and_output(output).
    and_exit(0)
end

#and_succeedObject



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

def and_succeed
  and_exit(0)
end

#ranObject



72
73
74
# File 'lib/shell_mock/command_stub.rb', line 72

def ran
  writer.write("R")
end

#runsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shell_mock/command_stub.rb', line 57

def runs
  @runs ||= 0

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

  @runs
end

#to_onelinerObject



76
77
78
79
80
81
82
# File 'lib/shell_mock/command_stub.rb', line 76

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

#with_env(env) ⇒ Object



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

def with_env(env)
  @env = env

  self
end

#with_options(options) ⇒ Object



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

def with_options(options)
  @options = options

  self
end