Class: Cosmos::Group

Inherits:
Object show all
Defined in:
lib/cosmos/script/suite.rb

Overview

Base class for a group. All COSMOS Script Runner scripts should inherit Group and then implement scripts methods starting with ‘script_’, ‘test_’, or ‘op_’ e.g. script_mech_open, test_mech_open, op_mech_open.

Direct Known Subclasses

Test

Constant Summary collapse

@@abort_on_exception =
false
@@current_result =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGroup

Returns a new instance of Group.



269
270
271
272
273
# File 'lib/cosmos/script/suite.rb', line 269

def initialize
  @output_io = StringIO.new('', 'r+')
  $stdout = Stdout.instance
  $stderr = Stderr.instance
end

Class Method Details

.abort_on_exceptionObject



275
276
277
# File 'lib/cosmos/script/suite.rb', line 275

def self.abort_on_exception
  @@abort_on_exception
end

.abort_on_exception=(value) ⇒ Object



279
280
281
# File 'lib/cosmos/script/suite.rb', line 279

def self.abort_on_exception=(value)
  @@abort_on_exception = value
end

.current_groupObject



464
465
466
467
468
469
470
# File 'lib/cosmos/script/suite.rb', line 464

def self.current_group
  if @@current_result
    @@current_result.group
  else
    nil
  end
end

.current_scriptObject



472
473
474
475
476
477
478
# File 'lib/cosmos/script/suite.rb', line 472

def self.current_script
  if @@current_result
    @@current_result.script
  else
    nil
  end
end

.current_suiteObject



456
457
458
459
460
461
462
# File 'lib/cosmos/script/suite.rb', line 456

def self.current_suite
  if @@current_result
    @@current_result.suite
  else
    nil
  end
end

.get_num_scriptsObject



439
440
441
442
443
444
445
# File 'lib/cosmos/script/suite.rb', line 439

def self.get_num_scripts
  num_scripts = 0
  num_scripts += 1 if self.method_defined?(:setup)
  num_scripts += 1 if self.method_defined?(:teardown)
  num_scripts += self.scripts.length
  num_scripts
end

.puts(string) ⇒ Object



447
448
449
450
451
452
453
454
# File 'lib/cosmos/script/suite.rb', line 447

def self.puts(string)
  $stdout.puts string
  if @@current_result
    @@current_result.message ||= ''
    @@current_result.message << string.chomp
    @@current_result.message << "\n"
  end
end

.scriptsObject



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/cosmos/script/suite.rb', line 283

def self.scripts
  # Find all the script methods
  methods = []
  self.instance_methods.each do |method_name|
    if /^test|^script|op_/.match?(method_name.to_s)
      methods << method_name.to_s
    end
  end
  # Sort by name for all found methods
  methods.sort!
  methods
end

Instance Method Details

#nameObject

Name of the script group



297
298
299
300
301
302
303
# File 'lib/cosmos/script/suite.rb', line 297

def name
  if self.class != Group
    self.class.to_s.split('::')[-1]
  else
    'UnnamedGroup'
  end
end

#runObject

Run all the scripts



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/cosmos/script/suite.rb', line 306

def run
  results = []

  # Setup the script group
  result = run_setup()
  if result
    results << result
    yield result if block_given?
    raise StopScript if (results[-1].exceptions and @@abort_on_exception) or results[-1].stopped
  end

  # Run all the scripts
  self.class.scripts.each do |method_name|
    results << run_script(method_name)
    yield results[-1] if block_given?
    raise StopScript if (results[-1].exceptions and @@abort_on_exception) or results[-1].stopped
  end

  # Teardown the script group
  result = run_teardown()
  if result
    results << result
    yield result if block_given?
    raise StopScript if (results[-1].exceptions and @@abort_on_exception) or results[-1].stopped
  end

  results
end

#run_method(object, method_name) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/cosmos/script/suite.rb', line 341

def run_method(object, method_name)
  # Convert to a symbol to use as a method_name
  method_name = method_name.to_s.intern unless method_name.class == Symbol

  result = ScriptResult.new
  @@current_result = result

  # Verify script method exists
  if object.class.method_defined?(method_name)
    # Capture STDOUT and STDERR
    $stdout.add_stream(@output_io)
    $stderr.add_stream(@output_io)

    result.group = object.class.to_s.split('::')[-1]
    result.script = method_name.to_s
    begin
      object.public_send(method_name)
      result.result = :PASS

      if RunningScript.instance and RunningScript.instance.exceptions
        result.exceptions = RunningScript.instance.exceptions
        result.result     = :FAIL
        RunningScript.instance.exceptions = nil
      end
    rescue StandardError, SyntaxError => error
      # Check that the error belongs to the StopScript inheritance chain
      if error.class <= StopScript
        result.stopped = true
        result.result  = :STOP
      end
      # Check that the error belongs to the SkipScript inheritance chain
      if error.class <= SkipScript
        result.result  = :SKIP
        result.message ||= ''
        result.message << error.message + "\n"
      else
        if error.class != StopScript and
           (not RunningScript.instance or
             not RunningScript.instance.exceptions or
             not RunningScript.instance.exceptions.include? error)
          result.exceptions ||= []
          result.exceptions << error
          puts "*** Exception in Control Statement:"
          error.formatted.each_line do |line|
            puts '  ' + line
          end
        end
        if RunningScript.instance and RunningScript.instance.exceptions
          result.exceptions ||= []
          result.exceptions.concat(RunningScript.instance.exceptions)
          RunningScript.instance.exceptions = nil
        end
      end

      result.result = :FAIL if result.exceptions
    ensure
      result.output     = @output_io.string
      @output_io.string = ''
      $stdout.remove_stream(@output_io)
      $stderr.remove_stream(@output_io)

      case result.result
      when :FAIL
        ScriptStatus.instance.fail_count += 1
      when :SKIP
        ScriptStatus.instance.skip_count += 1
      when :PASS
        ScriptStatus.instance.pass_count += 1
      end
    end

  else
    @@current_result = nil
    raise "Unknown method #{method_name} for #{object.class}"
  end

  @@current_result = nil
  result
end

#run_script(method_name) ⇒ Object

Run a specific script method



336
337
338
339
# File 'lib/cosmos/script/suite.rb', line 336

def run_script(method_name)
  ScriptStatus.instance.status = "#{self.class} : #{method_name}"
  run_method(self, method_name)
end

#run_setupObject



421
422
423
424
425
426
427
428
# File 'lib/cosmos/script/suite.rb', line 421

def run_setup
  result = nil
  if self.class.method_defined?(:setup)
    ScriptStatus.instance.status = "#{self.class} : setup"
    result = run_script(:setup)
  end
  result
end

#run_teardownObject



430
431
432
433
434
435
436
437
# File 'lib/cosmos/script/suite.rb', line 430

def run_teardown
  result = nil
  if self.class.method_defined?(:teardown)
    ScriptStatus.instance.status = "#{self.class} : teardown"
    result = run_script(:teardown)
  end
  result
end