Class: Test::Cmd
- Inherits:
-
Object
- Object
- Test::Cmd
- Defined in:
- lib/test/cmd.rb
Overview
test-cmd.rb provides an object oriented interface for spawning a command
Defined Under Namespace
Classes: Pipe
IO collapse
-
#stderr ⇒ String
Returns the contents of stderr.
-
#stdout ⇒ String
Returns the contents of stdout.
Predicates collapse
-
#alive? ⇒ Boolean
(also: #running?)
Returns true when a command is running.
-
#command_not_found? ⇒ Boolean
(also: #not_found?)
Returns true when a command can't be found.
-
#kill! ⇒ void
Sends SIGKILL to a running command.
-
#spawned? ⇒ Boolean
Returns true when a command has been spawned.
-
#success? ⇒ Boolean
Returns true when a command exited successfully.
Callbacks collapse
Instance Method Summary collapse
- #argv(*argv) ⇒ Test::Cmd
-
#exit_status ⇒ Integer
(also: #exitstatus)
Returns the exit status of a process.
- #initialize(cmd, *argv) ⇒ Test::Cmd constructor
-
#pid ⇒ Integer
Returns the process ID of a spawned command.
-
#spawn ⇒ Test::Cmd
Spawns a command.
-
#status ⇒ Process::Status
Returns the status of a process.
Constructor Details
#initialize(cmd, *argv) ⇒ Test::Cmd
26 27 28 29 30 31 32 33 34 |
# File 'lib/test/cmd.rb', line 26 def initialize(cmd, *argv) @cmd = cmd @argv = argv.dup @status = nil @spawned = false @stdout = "" @stderr = "" @enoent = false end |
Instance Method Details
#alive? ⇒ Boolean Also known as: running?
Returns true when a command is running
122 123 124 |
# File 'lib/test/cmd.rb', line 122 def alive? @producer&.alive? end |
#argv(*argv) ⇒ Test::Cmd
40 41 42 |
# File 'lib/test/cmd.rb', line 40 def argv(*argv) tap { @argv.concat(argv) } end |
#command_not_found? ⇒ Boolean Also known as: not_found?
Returns true when a command can't be found
138 139 140 141 142 |
# File 'lib/test/cmd.rb', line 138 def command_not_found? spawn consume(@producer, @out, @err) @enoent end |
#exit_status ⇒ Integer Also known as: exitstatus
Returns the exit status of a process
75 76 77 |
# File 'lib/test/cmd.rb', line 75 def exit_status status.exitstatus end |
#failure {|cmd| ... } ⇒ Test::Cmd
173 174 175 176 177 178 179 |
# File 'lib/test/cmd.rb', line 173 def failure tap do spawn consume(@producer, @out, @err) status.success? ? nil : yield(self) end end |
#kill! ⇒ void
This method returns an undefined value.
Sends SIGKILL to a running command
130 131 132 133 |
# File 'lib/test/cmd.rb', line 130 def kill! return unless alive? Process.kill("SIGKILL", @pid) end |
#pid ⇒ Integer
Returns the process ID of a spawned command
68 69 70 |
# File 'lib/test/cmd.rb', line 68 def pid status.pid end |
#spawn ⇒ Test::Cmd
Spawns a command
47 48 49 50 51 52 53 54 |
# File 'lib/test/cmd.rb', line 47 def spawn return self if @spawned tap do @spawned = true @out, @err = Pipe.pair, Pipe.pair produce(@out, @err) end end |
#spawned? ⇒ Boolean
Returns true when a command has been spawned
115 116 117 |
# File 'lib/test/cmd.rb', line 115 def spawned? @spawned end |
#status ⇒ Process::Status
Returns the status of a process
59 60 61 62 63 |
# File 'lib/test/cmd.rb', line 59 def status spawn consume(@producer, @out, @err) @status end |
#stderr ⇒ String
Returns the contents of stderr
95 96 97 98 99 |
# File 'lib/test/cmd.rb', line 95 def stderr spawn consume(@producer, @out, @err) @stderr end |
#stdout ⇒ String
Returns the contents of stdout
86 87 88 89 90 |
# File 'lib/test/cmd.rb', line 86 def stdout spawn consume(@producer, @out, @err) @stdout end |
#success {|cmd| ... } ⇒ Test::Cmd
157 158 159 160 161 162 163 |
# File 'lib/test/cmd.rb', line 157 def success tap do spawn consume(@producer, @out, @err) status.success? ? yield(self) : nil end end |
#success? ⇒ Boolean
Returns true when a command exited successfully
108 109 110 |
# File 'lib/test/cmd.rb', line 108 def success? status.success? end |