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
-
#stdout ⇒ String
Returns the contents of stdout.
-
#stderr ⇒ String
Returns the contents of stderr.
Predicates collapse
-
#success? ⇒ Boolean
Returns true when a command exited successfully.
-
#spawned? ⇒ Boolean
Returns true when a command has been spawned.
-
#command_not_found? ⇒ Boolean
(also: #not_found?)
Returns true when a command can’t be found.
Callbacks collapse
Instance Method Summary collapse
-
#argv(*argv) ⇒ Test::Cmd
-
#spawn ⇒ Test::Cmd
Spawns a command.
-
#status ⇒ Process::Status
Returns the status of a process.
-
#pid ⇒ Integer
Returns the process ID of a spawned command.
-
#exit_status ⇒ Integer
(also: #exitstatus)
Returns the exit status of a process.
-
#initialize(cmd, *argv) ⇒ Test::Cmd
constructor
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
#failure {|cmd| ... } ⇒ Test::Cmd
171 172 173 174 175 176 |
# File 'lib/test/cmd.rb', line 171 def failure tap do spawn status.success? ? nil : yield(self) end end |
#argv(*argv) ⇒ Test::Cmd
40 41 42 |
# File 'lib/test/cmd.rb', line 40 def argv(*argv) tap { @argv.concat(argv) } end |
#spawn ⇒ Test::Cmd
Spawns a command
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/test/cmd.rb', line 47 def spawn return self if @spawned tap do out, err = Pipe.pair, Pipe.pair @spawned = true t = Thread.new do Process.spawn(@cmd, *@argv, {out: out.w, err: err.w}) Process.wait @status = $? rescue Errno::ENOENT => ex @cmd, @argv, @stderr = "false", [], ex. @enoent = true retry end loop do io, _ = IO.select([out.r, err.r], nil, nil, 0.01) io&.include?(out.r) ? @stdout << out.r.read(1) : nil io&.include?(err.r) ? @stderr << err.r.read(1) : nil break unless t.alive? || IO.select([out.r, err.r], nil, nil, 0.01) end ensure [out, err].each(&:close) end end |
#status ⇒ Process::Status
Returns the status of a process
76 77 78 79 |
# File 'lib/test/cmd.rb', line 76 def status spawn @status end |
#pid ⇒ Integer
Returns the process ID of a spawned command
84 85 86 |
# File 'lib/test/cmd.rb', line 84 def pid status.pid end |
#exit_status ⇒ Integer Also known as: exitstatus
Returns the exit status of a process
91 92 93 |
# File 'lib/test/cmd.rb', line 91 def exit_status status.exitstatus end |
#stdout ⇒ String
Returns the contents of stdout
102 103 104 105 |
# File 'lib/test/cmd.rb', line 102 def stdout spawn @stdout end |
#stderr ⇒ String
Returns the contents of stderr
110 111 112 113 |
# File 'lib/test/cmd.rb', line 110 def stderr spawn @stderr end |
#success? ⇒ Boolean
Returns true when a command exited successfully
122 123 124 |
# File 'lib/test/cmd.rb', line 122 def success? status.success? end |
#spawned? ⇒ Boolean
Returns true when a command has been spawned
129 130 131 |
# File 'lib/test/cmd.rb', line 129 def spawned? @spawned end |
#command_not_found? ⇒ Boolean Also known as: not_found?
Returns true when a command can’t be found
136 137 138 139 |
# File 'lib/test/cmd.rb', line 136 def command_not_found? spawn @enoent end |
#success {|cmd| ... } ⇒ Test::Cmd
155 156 157 158 159 160 |
# File 'lib/test/cmd.rb', line 155 def success tap do spawn status.success? ? yield(self) : nil end end |