Module: Aruba::Api::Commands

Included in:
Aruba::Api
Defined in:
lib/aruba/api/command.rb

Overview

Command module

Instance Method Summary collapse

Instance Method Details

#all_commandsArray

Return all commands

Returns:

  • (Array)

    List of commands



58
59
60
# File 'lib/aruba/api/command.rb', line 58

def all_commands
  aruba.command_monitor.registered_commands
end

#close_inputObject

Close stdin



302
303
304
# File 'lib/aruba/api/command.rb', line 302

def close_input
  last_command_started.close_io(:stdin)
end

#find_command(commandline) ⇒ Object

Find a started command

Parameters:



110
111
112
# File 'lib/aruba/api/command.rb', line 110

def find_command(commandline)
  aruba.command_monitor.find(commandline)
end

#last_command_startedObject

Last command started



63
64
65
# File 'lib/aruba/api/command.rb', line 63

def last_command_started
  aruba.command_monitor.last_command_started
end

#last_command_stoppedObject

Last command stopped



68
69
70
# File 'lib/aruba/api/command.rb', line 68

def last_command_stopped
  aruba.command_monitor.last_command_stopped
end

#pipe_in_file(file_name) ⇒ Object

Pipe data in file

Parameters:

  • file_name (String)

    The file which should be used to pipe in data



46
47
48
49
50
51
52
# File 'lib/aruba/api/command.rb', line 46

def pipe_in_file(file_name)
  file_name = expand_path(file_name)

  File.open(file_name, 'r').each_line do |line|
    last_command_started.write(line)
  end
end

#run(*args) {|SpawnProcess| ... } ⇒ Object

Run given command and stop it if timeout is reached

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity

Parameters:

  • cmd (String)

    The command which should be executed

  • opts (Hash)

    Options

  • [Integer] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

Yields:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/aruba/api/command.rb', line 139

def run(*args)
  fail ArgumentError, 'Please pass at least a command as first argument.' if args.size < 1

  cmd = args.shift

  if args.last.is_a? Hash
    opts = args.pop

    exit_timeout      = opts[:exit_timeout].nil? ? aruba.config.exit_timeout : opts[:exit_timeout]
    io_wait_timeout   = opts[:io_wait_timeout].nil? ? aruba.config.io_wait_timeout : opts[:io_wait_timeout]
    stop_signal       = opts[:stop_signal].nil? ? aruba.config.stop_signal : opts[:stop_signal]
    startup_wait_time = opts[:startup_wait_time].nil? ? aruba.config.startup_wait_time : opts[:startup_wait_time]
  else
    if args.size > 1
      Aruba.platform.deprecated("Please pass options to `#run` as named parameters/hash and don\'t use the old style, e.g. `#run('cmd', :exit_timeout => 5)`.")
    end

    exit_timeout      = args[0].nil? ? aruba.config.exit_timeout : args[0]
    io_wait_timeout   = args[1].nil? ? aruba.config.io_wait_timeout : args[1]
    stop_signal       = args[2].nil? ? aruba.config.stop_signal : args[2]
    startup_wait_time = args[3].nil? ? aruba.config.startup_wait_time : args[3]
  end

  cmd = replace_variables(cmd)

  @commands ||= []
  @commands << cmd

  environment       = aruba.environment
  working_directory = expand_path('.')
  event_bus         = aruba.event_bus

  cmd = Aruba.platform.detect_ruby(cmd)

  mode = if Aruba.process
           # rubocop:disable Metrics/LineLength
           Aruba.platform.deprecated('The use of "Aruba.process = <process>" and "Aruba.process.main_class" is deprecated. Use "Aruba.configure { |config| config.command_launcher = :in_process|:debug|:spawn }" and "Aruba.configure { |config| config.main_class = <klass> }" instead.')
           # rubocop:enable Metrics/LineLength
           Aruba.process
         else
           aruba.config.command_launcher
         end

  main_class = if Aruba.process.respond_to?(:main_class) && Aruba.process.main_class
                 # rubocop:disable Metrics/LineLength
                 Aruba.platform.deprecated('The use of "Aruba.process = <process>" and "Aruba.process.main_class" is deprecated. Use "Aruba.configure { |config| config.command_launcher = :in_process|:debug|:spawn }" and "Aruba.configure { |config| config.main_class = <klass> }" instead.')
                 # rubocop:enable Metrics/LineLength
                 Aruba.process.main_class
               else
                 aruba.config.main_class
               end

  command = Command.new(
    cmd,
    :mode              => mode,
    :exit_timeout      => exit_timeout,
    :io_wait_timeout   => io_wait_timeout,
    :working_directory => working_directory,
    :environment       => environment.to_hash,
    :main_class        => main_class,
    :stop_signal       => stop_signal,
    :startup_wait_time => startup_wait_time,
    :event_bus         => event_bus
  )

  if aruba.config.before? :cmd
    # rubocop:disable Metrics/LineLength
    Aruba.platform.deprecated('The use of "before"-hook" ":cmd" is deprecated. Use ":command" instead. Please be aware that this hook gets the command passed in not the cmdline itself. To get the commandline use "#cmd.commandline"')
    # rubocop:enable Metrics/LineLength
    aruba.config.before(:cmd, self, cmd)
  end

  aruba.config.before(:command, self, command)

  command.start

  aruba.announcer.announce(:stop_signal, command.pid, stop_signal) if stop_signal

  aruba.config.after(:command, self, command)

  block_given? ? yield(command) : command
end

#run_simple(*args) ⇒ Object

Run a command with aruba

Checks for error during command execution and checks the output to detect an timeout error.

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength

Parameters:

  • cmd (String)

    The command to be executed

  • options (Hash)

    Options for aruba

  • [TrueClass,FalseClass] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/aruba/api/command.rb', line 246

def run_simple(*args)
  fail ArgumentError, 'Please pass at least a command as first argument.' if args.size < 1

  cmd = args.shift

  if args.last.is_a? Hash
    opts = args.pop
    fail_on_error = opts.delete(:fail_on_error) == true ? true : false
  else
    if args.size > 1
      # rubocop:disable Metrics/LineLength
      Aruba.platform.deprecated("Please pass options to `#run_simple` as named parameters/hash and don\'t use the old style with positional parameters, NEW: e.g. `#run_simple('cmd', :exit_timeout => 5)`.")
      # rubocop:enable Metrics/LineLength
    end

    fail_on_error = args[0] == false ? false : true

    opts = {
      :exit_timeout      => (args[1] || aruba.config.exit_timeout),
      :io_wait_timeout   => (args[2] || aruba.config.io_wait_timeout),
      :stop_signal       => (args[3] || aruba.config.stop_signal),
      :startup_wait_time => (args[4] || aruba.config.startup_wait_time)
    }
  end

  command = run(cmd, opts)
  command.stop

  if Aruba::VERSION < '1'
    @last_exit_status = command.exit_status
    @timed_out = command.timed_out?
  end

  if fail_on_error
    begin
      expect(command).to have_finished_in_time
      expect(command).to be_successfully_executed
    rescue RSpec::Expectations::ExpectationNotMetError => e
      aruba.announcer.activate(aruba.config.activate_announcer_on_command_failure)
      raise e
    end
  end
end

#stop_all_commands {|Command| ... } ⇒ Object

Stop all commands

Yields:

  • (Command)

    If block is given use it to filter the commands which should be stoppend.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/aruba/api/command.rb', line 77

def stop_all_commands(&block)
  cmds = if block_given?
           all_commands.select(&block)
         else
           all_commands
         end

  cmds.each(&:stop)

  self
end

#terminate_all_commands {|Command| ... } ⇒ Object

Terminate all commands

Yields:

  • (Command)

    If block is given use it to filter the commands which should be terminated.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/aruba/api/command.rb', line 94

def terminate_all_commands(&block)
  cmds = if block_given?
           all_commands.select(&block)
         else
           all_commands
         end

  cmds.each(&:terminate)

  self
end

#type(input) ⇒ Object

Provide data to command via stdin

Parameters:

  • input (String)

    The input for the command



296
297
298
299
# File 'lib/aruba/api/command.rb', line 296

def type(input)
  return close_input if "" == input
  last_command_started.write(input << "\n")
end

#which(program, path = nil) ⇒ Object

Resolve path for command using the PATH-environment variable

Parameters:

  • program (#to_s)

    The name of the program which should be resolved

  • path (String) (defaults to: nil)

    The PATH, a string concatenated with ":", e.g. /usr/bin/:/bin on a UNIX-system



33
34
35
36
37
38
39
40
# File 'lib/aruba/api/command.rb', line 33

def which(program, path = nil)
  with_environment do
    # ENV is set within this block
    path = ENV['PATH'] if path.nil?

    Aruba.platform.which(program, path)
  end
end