Class: Test::Command

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::Command

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
  @env = {}
  @status = nil
  @spawned = false
  @stdout = ""
  @stderr = ""
  @enoent = false
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



171
172
173
# File 'lib/test/cmd.rb', line 171

def alive?
  @producer&.alive?
end

#argv(*argv) ⇒ Test::Command

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



187
188
189
190
191
# File 'lib/test/cmd.rb', line 187

def command_not_found?
  spawn
  consume
  @enoent
end

#env(env) ⇒ Test::Command

Parameters:

  • env (Hash{String => String})

    Environment variables to set for the spawned command

Returns:



49
50
51
# File 'lib/test/cmd.rb', line 49

def env(env)
  tap { @env.merge!(env) }
end

#exit_statusInteger Also known as: exitstatus

Returns the exit status of a process

Returns:

  • (Integer)

    Returns the exit status of a process



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

def exit_status
  status.exitstatus
end

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

Examples:

Test::Command.new("ruby", "-e", "exit 1").failure do
  print "fail pid #{_1.pid}", "\n"
end

Yield Parameters:

Returns:



222
223
224
225
226
227
228
# File 'lib/test/cmd.rb', line 222

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

#kill!void

This method returns an undefined value.

Sends SIGKILL to a running command



179
180
181
182
# File 'lib/test/cmd.rb', line 179

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



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

def pid
  status.pid
end

#spawnTest::Command

Spawns a command

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/test/cmd.rb', line 71

def spawn
  return self if @spawned
  @in_r = input_pipe
  tap do
    @spawned = true
    @out, @err = Pipe.pair, Pipe.pair
    @pid = Process.spawn(
      @env,
      @cmd, *@argv,
      {in: @in_r, out: @out.w, err: @err.w}
    )
    @in_r.close unless @in_r.equal?(IO::NULL)
    @out.w.close
    @err.w.close
    @producer = Thread.new do
      @stdout = @out.r.read
      @stderr = @err.r.read
      Process.wait
      @status = $?
    ensure
      @out.r.close
      @err.r.close
    end
  end
rescue Errno::ENOENT => ex
  @stderr = ex.message
  @enoent = true
  ##
  # Close the read end of any input pipe so a pending
  # writer thread does not block forever.
  @in_r.close unless @in_r.equal?(IO::NULL)
  @status = Process.waitpid2(Process.spawn("false")).last
end

#spawned?Boolean

Returns true when a command has been spawned

Returns:

  • (Boolean)

    Returns true when a command has been spawned



164
165
166
# File 'lib/test/cmd.rb', line 164

def spawned?
  @spawned
end

#statusProcess::Status

Returns the status of a process

Returns:

  • (Process::Status)

    Returns the status of a process



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

def status
  spawn
  consume
  @status
end

#stderrString

Returns the contents of stderr

Returns:

  • (String)

    Returns the contents of stderr



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

def stderr
  spawn
  consume
  @stderr
end

#stdin(data) ⇒ Test::Command

Presets the standard input that will be sent to the spawned process. Pass a String, or another Test::Command whose standard output will be used as the standard input.

Examples:

cmd = Test::Command.new("cat").stdin("hello world")
puts cmd.stdout

Parameters:

  • data (String, Test::Command)

    The standard input of the spawned process

Returns:



64
65
66
# File 'lib/test/cmd.rb', line 64

def stdin(data)
  tap { @stdin = data }
end

#stdoutString

Returns the contents of stdout

Returns:

  • (String)

    Returns the contents of stdout



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

def stdout
  spawn
  consume
  @stdout
end

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

Examples:

Test::Command.new("ruby", "-e", "exit 0").success do
  print "ok pid #{_1.pid}", "\n"
end

Yield Parameters:

Returns:



206
207
208
209
210
211
212
# File 'lib/test/cmd.rb', line 206

def success
  tap do
    spawn
    consume
    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



157
158
159
# File 'lib/test/cmd.rb', line 157

def success?
  status.success?
end