Class: Patir::CommandSequenceStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/patir/command.rb

Overview

CommandSequenceStatus represents the status of a CommandSequence, including the status of all the steps for this sequence.

In order to extract the status from steps, classes should quack to the rythm of Command. CommandSequenceStatus does this, so you can nest Stati

The status of an action sequence is :not_executed, :running, :success, :warning or :error and represents the overall status :not_executed is set when all steps are :not_executed :running is set while the sequence is running. Upon completion or interruption one of :success, :error or :warning will be set. :success is set when all steps are succesfull. :warning is set when at least one step generates warnings and there are no failures. :error is set when after execution at least one step has the :error status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequence_name, steps = nil) ⇒ CommandSequenceStatus

You can pass an array of Commands to initialize CommandSequenceStatus



341
342
343
344
345
346
347
348
349
350
351
# File 'lib/patir/command.rb', line 341

def initialize sequence_name,steps=nil
  @sequence_name=sequence_name
  @sequence_runner=""
  @sequence_id=nil
  @step_states||=Hash.new
  #not run yet
  @status=:not_executed
  #translate the array of steps as we need it in number=>state form
  steps.each{|step| self.step=step } if steps
  @start_time=Time.now
end

Instance Attribute Details

#sequence_idObject

Returns the value of attribute sequence_id.



339
340
341
# File 'lib/patir/command.rb', line 339

def sequence_id
  @sequence_id
end

#sequence_nameObject

Returns the value of attribute sequence_name.



339
340
341
# File 'lib/patir/command.rb', line 339

def sequence_name
  @sequence_name
end

#sequence_runnerObject

Returns the value of attribute sequence_runner.



339
340
341
# File 'lib/patir/command.rb', line 339

def sequence_runner
  @sequence_runner
end

#start_timeObject

Returns the value of attribute start_time.



339
340
341
# File 'lib/patir/command.rb', line 339

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



339
340
341
# File 'lib/patir/command.rb', line 339

def status
  @status
end

#step_statesObject

Returns the value of attribute step_states.



339
340
341
# File 'lib/patir/command.rb', line 339

def step_states
  @step_states
end

#stop_timeObject

Returns the value of attribute stop_time.



339
340
341
# File 'lib/patir/command.rb', line 339

def stop_time
  @stop_time
end

#strategyObject

Returns the value of attribute strategy.



339
340
341
# File 'lib/patir/command.rb', line 339

def strategy
  @strategy
end

Instance Method Details

#completed?Boolean

A sequence is considered completed when:

a step has errors and the :fail_on_error strategy is used

a step has warnings and the :fail_on_warning strategy is used

in all other cases if none of the steps has a :not_executed or :running status

Returns:

  • (Boolean)


369
370
371
372
373
374
375
376
377
378
# File 'lib/patir/command.rb', line 369

def completed?
  #this saves us iterating once+1 when no execution took place
  return false if !self.executed?
  @step_states.each do |state|
    return true if state[1][:status]==:error && state[1][:strategy]==:fail_on_error
    return true if state[1][:status]==:warning && state[1][:strategy]==:fail_on_warning
  end
  @step_states.each{|state| return false if state[1][:status]==:not_executed || state[1][:status]==:running }
  return true
end

#errorObject



447
448
449
# File 'lib/patir/command.rb', line 447

def error
  return ""
end

#exec_timeObject



434
435
436
437
# File 'lib/patir/command.rb', line 434

def exec_time
  return @stop_time-@start_time if @stop_time
  return 0
end

#executed?Boolean

Returns:

  • (Boolean)


450
451
452
453
# File 'lib/patir/command.rb', line 450

def executed?
  return true unless @status==:not_executed
  return false
end

#nameObject



438
439
440
# File 'lib/patir/command.rb', line 438

def name
  return @sequence_name
end

#numberObject



441
442
443
# File 'lib/patir/command.rb', line 441

def number
  return @sequence_id
end

#outputObject



444
445
446
# File 'lib/patir/command.rb', line 444

def output
  return self.summary
end

#running?Boolean

Returns:

  • (Boolean)


352
353
354
355
# File 'lib/patir/command.rb', line 352

def running?
  return true if :running==@status
  return false
end

#step=(step) ⇒ Object

Adds a step to the state. The step state is inferred from the Command instance __step__



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
# File 'lib/patir/command.rb', line 385

def step=step
  @step_states[step.number]={:name=>step.name,
    :status=>step.status,
    :output=>step.output,
    :duration=>step.exec_time,
    :error=>step.error,
    :strategy=>step.strategy
  }
  #this way we don't have to compare all the step states we always get the worst last stable state
  #:not_executed<:success<:warning<:success
  unless @status==:running
    @previous_status=@status 
    case step.status
    when :running
      @status=:running
    when :warning
      @status=:warning unless @status==:error
      @status=:error if @previous_status==:error
    when :error
      @status=:error
    when :success
      @status=:success unless @status==:error || @status==:warning
      @status=:warning if @previous_status==:warning
      @status=:error if @previous_status==:error
    when :not_executed
      @status=@previous_status
    end
  end#unless running
end

#step_state(number) ⇒ Object

A nil means there is no step with that number



380
381
382
383
# File 'lib/patir/command.rb', line 380

def step_state number
  s=@step_states[number] if @step_states[number]
  return s
end

#success?Boolean

true is returned when all steps were succesfull.

Returns:

  • (Boolean)


357
358
359
360
# File 'lib/patir/command.rb', line 357

def success?
  return true if :success==@status
  return false
end

#summaryObject

produces a brief text summary for this status



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/patir/command.rb', line 415

def summary
  sum=""
  sum<<"#{@sequence_id}:" if @sequence_id
  sum<<"#{@sequence_name}. " unless @sequence_name.empty?
  sum<<"Status - #{@status}" 
  if !@step_states.empty? && @status!=:not_executed
    sum<<". States #{@step_states.size}\nStep status summary:"
    sorter=Hash.new
    @step_states.each do |number,state|
      #sort them by number
      sorter[number]="\n\t#{number}:'#{state[:name]}' - #{state[:status]}"
    end
    1.upto(sorter.size) {|i| sum<<sorter[i] if sorter[i]}
  end 
  return sum
end

#to_sObject



431
432
433
# File 'lib/patir/command.rb', line 431

def to_s
  "'#{sequence_id}':'#{@sequence_name}' on '#{@sequence_runner}' started at #{@start_time}.#{@step_states.size} steps"
end