Class: Test::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/test/cmd.rb

Overview

test-cmd.rb provides an object oriented interface for spawning a command.

Instance Method Summary collapse

Constructor Details

#initialize(cmd, *argv) ⇒ Test::Cmd

Parameters:

  • cmd (String)

    A command to spawn

  • argv (Array<String>)

    Zero or more command-line arguments



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

Parameters:

  • argv (Array<String, #to_s>)

    Command-line arguments

Returns:



28
29
30
# File 'lib/test/cmd.rb', line 28

def argv(*argv)
  tap { @argv.concat(argv) }
end

#exit_statusInteger

Returns the exit status of a process

Returns:

  • (Integer)

    Returns the exit status of a process



91
92
93
# File 'lib/test/cmd.rb', line 91

def exit_status
  status.exitstatus
end

#failureTest::Cmd

Yields an instance of Test::Cmd.

Examples:

cmd("ruby", "-e", "exit 1")
  .success { }
  .failure { print "Command [#{_1.pid}] exited unsuccessfully", "\n" }

Returns:



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

#pidInteger

Returns the process ID of a spawned command

Returns:

  • (Integer)

    Returns the process ID of a spawned command



84
85
86
# File 'lib/test/cmd.rb', line 84

def pid
  status.pid
end

#spawnTest::Cmd

Spawns a command

Returns:



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

Returns:

  • (Boolean)

    Returns true when a command has been spawned



137
138
139
# File 'lib/test/cmd.rb', line 137

def spawned?
  @spawned
end

#statusProcess::Status

Returns the status of a process

Returns:

  • (Process::Status)

    Returns the status of a process



76
77
78
79
# File 'lib/test/cmd.rb', line 76

def status
  spawn
  @status
end

#stderrString

Returns the contents of stderr

Returns:

  • (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

#stdoutString

Returns the contents of stdout

Returns:

  • (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

#successTest::Cmd

Yields an instance of Test::Cmd.

Examples:

cmd("ruby", "-e", "exit 0")
  .success { print "Command [#{_1.pid}] exited successfully", "\n" }
  .failure { }

Returns:



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

Returns:

  • (Boolean)

    Returns true when a command exited successfully



98
99
100
# File 'lib/test/cmd.rb', line 98

def success?
  status.success?
end