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.
Instance Method Summary collapse
- #argv(*argv) ⇒ Test::Cmd
-
#exit_status ⇒ Integer
Returns the exit status of a process.
-
#failure ⇒ Test::Cmd
Yields an instance of Test::Cmd.
- #initialize(cmd, *argv) ⇒ Test::Cmd constructor
-
#pid ⇒ Integer
Returns the process ID of a spawned command.
-
#spawn ⇒ Test::Cmd
Spawns a command.
-
#spawned? ⇒ Boolean
Returns true when a command has been spawned.
-
#status ⇒ Process::Status
Returns the status of a process.
-
#stderr ⇒ String
Returns the contents of stderr.
-
#stdout ⇒ String
Returns the contents of stdout.
-
#success ⇒ Test::Cmd
Yields an instance of Test::Cmd.
-
#success? ⇒ Boolean
Returns true when a command exited successfully.
Constructor Details
#initialize(cmd, *argv) ⇒ Test::Cmd
17 18 19 20 21 22 |
# File 'lib/test/cmd.rb', line 17 def initialize(cmd, *argv) @cmd = cmd @argv = argv.dup @status = nil @spawned = false end |
Instance Method Details
#argv(*argv) ⇒ Test::Cmd
28 29 30 |
# File 'lib/test/cmd.rb', line 28 def argv(*argv) tap { @argv.concat(argv) } end |
#exit_status ⇒ Integer
Returns the exit status of a process
91 92 93 |
# File 'lib/test/cmd.rb', line 91 def exit_status status.exitstatus end |
#failure ⇒ Test::Cmd
Yields an instance of Test::Cmd.
127 128 129 130 131 132 |
# File 'lib/test/cmd.rb', line 127 def failure tap do spawn status.success? ? nil : yield(self) end 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 |
#spawn ⇒ Test::Cmd
Spawns a command
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/test/cmd.rb', line 35 def spawn return self if @spawned tap do @spawned = true @out_io, @err_io = spawn_io Process.spawn(@cmd, *@argv, {out: @out_io, err: @err_io}) Process.wait @status = $? ensure [stdout, stderr] end end |
#spawned? ⇒ Boolean
Returns true when a command has been spawned
137 138 139 |
# File 'lib/test/cmd.rb', line 137 def spawned? @spawned 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 |
#stderr ⇒ String
Returns the contents of stderr
64 65 66 67 68 69 70 71 |
# File 'lib/test/cmd.rb', line 64 def stderr @stderr ||= begin spawn err_io.tap(&:rewind).read.tap { err_io.close } rescue IOError @stderr end end |
#stdout ⇒ String
Returns the contents of stdout
52 53 54 55 56 57 58 59 |
# File 'lib/test/cmd.rb', line 52 def stdout @stdout ||= begin spawn out_io.tap(&:rewind).read.tap { out_io.close } rescue IOError @stdout end end |
#success ⇒ Test::Cmd
Yields an instance of Test::Cmd.
111 112 113 114 115 116 |
# File 'lib/test/cmd.rb', line 111 def success tap do spawn status.success? ? yield(self) : nil end end |
#success? ⇒ Boolean
Returns true when a command exited successfully
98 99 100 |
# File 'lib/test/cmd.rb', line 98 def success? status.success? end |