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

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
# 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

#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



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

def command_not_found?
  spawn
  @enoent
end

#argv(*argv) ⇒ Test::Cmd

Parameters:

  • argv (Array<String, #to_s>)

    Command-line arguments

Returns:



40
41
42
# File 'lib/test/cmd.rb', line 40

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

#spawnTest::Cmd

Spawns a command

Returns:



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.message
      @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

#stdoutString

Returns the contents of stdout

Returns:

  • (String)

    Returns the contents of stdout



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

def stdout
  spawn
  @stdout
end

#stderrString

Returns the contents of stderr

Returns:

  • (String)

    Returns the contents of stderr



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

def stderr
  spawn
  @stderr
end

#statusProcess::Status

Returns the status of a process

Returns:

  • (Process::Status)

    Returns the status of a process



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

def status
  spawn
  @status
end

#pidInteger

Returns the process ID of a spawned command

Returns:

  • (Integer)

    Returns the process ID of a spawned command



100
101
102
# File 'lib/test/cmd.rb', line 100

def pid
  status.pid
end

#exit_statusInteger Also known as: exitstatus

Returns the exit status of a process

Returns:

  • (Integer)

    Returns the exit status of a process



107
108
109
# File 'lib/test/cmd.rb', line 107

def exit_status
  status.exitstatus
end

#success?Boolean

Returns true when a command exited successfully

Returns:

  • (Boolean)

    Returns true when a command exited successfully



115
116
117
# File 'lib/test/cmd.rb', line 115

def success?
  status.success?
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:



128
129
130
131
132
133
# File 'lib/test/cmd.rb', line 128

def success
  tap do
    spawn
    status.success? ? yield(self) : nil
  end
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:



144
145
146
147
148
149
# File 'lib/test/cmd.rb', line 144

def failure
  tap do
    spawn
    status.success? ? nil : yield(self)
  end
end

#spawned?Boolean

Returns true when a command has been spawned

Returns:

  • (Boolean)

    Returns true when a command has been spawned



154
155
156
# File 'lib/test/cmd.rb', line 154

def spawned?
  @spawned
end