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

Defined Under Namespace

Classes: Pipe

IO collapse

Predicates collapse

Callbacks collapse

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



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
  @status = nil
  @spawned = false
  @stdout = ""
  @stderr = ""
  @enoent = false
  @waiter = Queue.new
end

Instance Method Details

#alive?Boolean Also known as: running?

Returns true when a command is running

Returns:

  • (Boolean)

    Returns true when a command is running



124
125
126
# File 'lib/test/cmd.rb', line 124

def alive?
  @producer&.alive?
end

#argv(*argv) ⇒ Test::Cmd

Parameters:

  • argv (Array<String, #to_s>)

    Command-line arguments

Returns:



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

Returns:

  • (Boolean)

    Returns true when a command can't be found



140
141
142
143
144
# File 'lib/test/cmd.rb', line 140

def command_not_found?
  spawn
  consume(@producer, @out, @err)
  @enoent
end

#exit_statusInteger Also known as: exitstatus

Returns the exit status of a process

Returns:

  • (Integer)

    Returns the exit status of a process



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

def exit_status
  status.exitstatus
end

#failure {|cmd| ... } ⇒ Test::Cmd

Examples:

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

Yield Parameters:

Returns:



175
176
177
178
179
180
181
# File 'lib/test/cmd.rb', line 175

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



132
133
134
135
# File 'lib/test/cmd.rb', line 132

def kill!
  return unless alive?
  Process.kill("SIGKILL", @pid)
end

#pidInteger

Returns the process ID of a spawned command

Returns:

  • (Integer)

    Returns the process ID of a spawned command



70
71
72
# File 'lib/test/cmd.rb', line 70

def pid
  status.pid
end

#spawnTest::Cmd

Spawns a command

Returns:



48
49
50
51
52
53
54
55
56
# File 'lib/test/cmd.rb', line 48

def spawn
  return self if @spawned
  tap do
    @spawned = true
    @out, @err = Pipe.pair, Pipe.pair
    produce(@out, @err)
    @waiter.pop
  end
end

#spawned?Boolean

Returns true when a command has been spawned

Returns:

  • (Boolean)

    Returns true when a command has been spawned



117
118
119
# File 'lib/test/cmd.rb', line 117

def spawned?
  @spawned
end

#statusProcess::Status

Returns the status of a process

Returns:

  • (Process::Status)

    Returns the status of a process



61
62
63
64
65
# File 'lib/test/cmd.rb', line 61

def status
  spawn
  consume(@producer, @out, @err)
  @status
end

#stderrString

Returns the contents of stderr

Returns:

  • (String)

    Returns the contents of stderr



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

def stderr
  spawn
  consume(@producer, @out, @err)
  @stderr
end

#stdoutString

Returns the contents of stdout

Returns:

  • (String)

    Returns the contents of stdout



88
89
90
91
92
# File 'lib/test/cmd.rb', line 88

def stdout
  spawn
  consume(@producer, @out, @err)
  @stdout
end

#success {|cmd| ... } ⇒ Test::Cmd

Examples:

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

Yield Parameters:

Returns:



159
160
161
162
163
164
165
# File 'lib/test/cmd.rb', line 159

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

Returns:

  • (Boolean)

    Returns true when a command exited successfully



110
111
112
# File 'lib/test/cmd.rb', line 110

def success?
  status.success?
end