Class: Test::Command
- Inherits:
-
Object
- Object
- Test::Command
- 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::Command
- #env(env) ⇒ Test::Command
-
#exit_status ⇒ Integer
(also: #exitstatus)
Returns the exit status of a process.
- #initialize(cmd, *argv) ⇒ Test::Command constructor
-
#pid ⇒ Integer
Returns the process ID of a spawned command.
-
#spawn ⇒ Test::Command
Spawns a command.
-
#status ⇒ Process::Status
Returns the status of a process.
-
#stdin(data) ⇒ Test::Command
Presets the standard input that will be sent to the spawned process.
Constructor Details
#initialize(cmd, *argv) ⇒ Test::Command
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/test/cmd.rb', line 26 def initialize(cmd, *argv) @cmd = cmd @argv = argv.dup @env = {} @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
171 172 173 |
# File 'lib/test/cmd.rb', line 171 def alive? @producer&.alive? end |
#argv(*argv) ⇒ Test::Command
41 42 43 |
# File 'lib/test/cmd.rb', line 41 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
187 188 189 190 191 |
# File 'lib/test/cmd.rb', line 187 def command_not_found? spawn consume @enoent end |
#env(env) ⇒ Test::Command
49 50 51 |
# File 'lib/test/cmd.rb', line 49 def env(env) tap { @env.merge!(env) } end |
#exit_status ⇒ Integer Also known as: exitstatus
Returns the exit status of a process
124 125 126 |
# File 'lib/test/cmd.rb', line 124 def exit_status status.exitstatus end |
#failure {|cmd| ... } ⇒ Test::Command
222 223 224 225 226 227 228 |
# File 'lib/test/cmd.rb', line 222 def failure tap do spawn consume status.success? ? nil : yield(self) end end |
#kill! ⇒ void
This method returns an undefined value.
Sends SIGKILL to a running command
179 180 181 182 |
# File 'lib/test/cmd.rb', line 179 def kill! return unless alive? Process.kill("SIGKILL", @pid) end |
#pid ⇒ Integer
Returns the process ID of a spawned command
117 118 119 |
# File 'lib/test/cmd.rb', line 117 def pid status.pid end |
#spawn ⇒ Test::Command
Spawns a command
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/test/cmd.rb', line 71 def spawn return self if @spawned @in_r = input_pipe tap do @spawned = true @out, @err = Pipe.pair, Pipe.pair @pid = Process.spawn( @env, @cmd, *@argv, {in: @in_r, out: @out.w, err: @err.w} ) @in_r.close unless @in_r.equal?(IO::NULL) @out.w.close @err.w.close @producer = Thread.new do @stdout = @out.r.read @stderr = @err.r.read Process.wait @status = $? ensure @out.r.close @err.r.close end end rescue Errno::ENOENT => ex @stderr = ex. @enoent = true ## # Close the read end of any input pipe so a pending # writer thread does not block forever. @in_r.close unless @in_r.equal?(IO::NULL) @status = Process.waitpid2(Process.spawn("false")).last end |
#spawned? ⇒ Boolean
Returns true when a command has been spawned
164 165 166 |
# File 'lib/test/cmd.rb', line 164 def spawned? @spawned end |
#status ⇒ Process::Status
Returns the status of a process
108 109 110 111 112 |
# File 'lib/test/cmd.rb', line 108 def status spawn consume @status end |
#stderr ⇒ String
Returns the contents of stderr
144 145 146 147 148 |
# File 'lib/test/cmd.rb', line 144 def stderr spawn consume @stderr end |
#stdin(data) ⇒ Test::Command
Presets the standard input that will be sent to the spawned process. Pass a String, or another Test::Command whose standard output will be used as the standard input.
64 65 66 |
# File 'lib/test/cmd.rb', line 64 def stdin(data) tap { @stdin = data } end |
#stdout ⇒ String
Returns the contents of stdout
135 136 137 138 139 |
# File 'lib/test/cmd.rb', line 135 def stdout spawn consume @stdout end |
#success {|cmd| ... } ⇒ Test::Command
206 207 208 209 210 211 212 |
# File 'lib/test/cmd.rb', line 206 def success tap do spawn consume status.success? ? yield(self) : nil end end |
#success? ⇒ Boolean
Returns true when a command exited successfully
157 158 159 |
# File 'lib/test/cmd.rb', line 157 def success? status.success? end |