Module: Omnitest::Skeptic::TestTransitions

Included in:
Scenario
Defined in:
lib/omnitest/skeptic/test_transitions.rb

Defined Under Namespace

Classes: FSM

Instance Method Summary collapse

Instance Method Details

#action(what, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/omnitest/skeptic/test_transitions.rb', line 73

def action(what, &block)
  evidence.last_attempted_action = what.to_s
  elapsed = Benchmark.measure do
    block.call(@state)
  end
  evidence.last_completed_action = what.to_s
  elapsed
rescue FeatureNotImplementedError => e
  raise e
rescue ActionFailed => e
  log_failure(what, e)
  raise(ScenarioFailure, failure_message(what) +
    "  Please see .omnitest/logs/#{name}.log for more details",
        e.backtrace)
rescue Exception => e # rubocop:disable RescueException
  log_failure(what, e)
  raise ActionFailed,
        "Failed to complete ##{what} action: [#{e.message}]", e.backtrace
ensure
  save unless what == :clear
end

#clearObject



34
35
36
37
38
# File 'lib/omnitest/skeptic/test_transitions.rb', line 34

def clear
  # Transitioning would try to load the data we're clearing... let's just
  # jump straight to the action.
  clear_action
end

#clear_actionObject



40
41
42
43
44
# File 'lib/omnitest/skeptic/test_transitions.rb', line 40

def clear_action
  perform_action(:clear, 'Clearing') do
    clear!
  end
end

#detectObject



4
5
6
# File 'lib/omnitest/skeptic/test_transitions.rb', line 4

def detect
  transition_to :detect
end

#detect_actionObject



8
9
10
11
12
# File 'lib/omnitest/skeptic/test_transitions.rb', line 8

def detect_action
  perform_action(:detect, 'Detecting code sample') do
    detect!
  end
end

#execObject



14
15
16
# File 'lib/omnitest/skeptic/test_transitions.rb', line 14

def exec
  transition_to :exec
end

#exec_actionObject



18
19
20
21
22
# File 'lib/omnitest/skeptic/test_transitions.rb', line 18

def exec_action
  perform_action(:exec, 'Executing') do
    exec!
  end
end

#failure_message(what) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a string explaining what action failed, at a high level. Used for displaying to end user.

Parameters:

  • what (String)

    an action

Returns:

  • (String)

    a failure message



126
127
128
# File 'lib/omnitest/skeptic/test_transitions.rb', line 126

def failure_message(what)
  "#{what.capitalize} failed for test #{slug}."
end

#log_failure(what, e) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/omnitest/skeptic/test_transitions.rb', line 112

def log_failure(what, e)
  return unless logger.respond_to? :logdev
  return if logger.logdev.nil?

  logger.logdev.error(failure_message(what))
  Error.formatted_trace(e).each { |line| logger.logdev.error(line) }
end

#perform_action(verb, output_verb) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/omnitest/skeptic/test_transitions.rb', line 63

def perform_action(verb, output_verb)
  banner "#{output_verb} #{slug}..."
  elapsed = action(verb) { yield }
  # elapsed = action(verb) { |state| driver.public_send(verb, state) }
  info("Finished #{output_verb.downcase} #{slug}" \
    " #{Core::Util.duration(elapsed.real)}.")
  # yield if block_given?
  self
end

#test(_clear_mode = :passing) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omnitest/skeptic/test_transitions.rb', line 46

def test(_clear_mode = :passing)
  elapsed = Benchmark.measure do
    banner "Cleaning up any prior instances of #{slug}"
    clear
    banner "Testing #{slug}"
    verify
    # clear if clear_mode == :passing
  end
  info "Finished testing #{slug} #{Core::Util.duration(elapsed.real)}."
  evidence.duration = elapsed.real
  save
  evidence = nil # it's saved, free up memory...
  self
  # ensure
  # clear if clear_mode == :always
end

#transition_to(desired) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/omnitest/skeptic/test_transitions.rb', line 95

def transition_to(desired)
  transition_result = nil
  begin
    FSM.actions(last_completed_action, desired).each do |transition|
      transition_result = send("#{transition}_action")
    end
  rescue FeatureNotImplementedError
    warn("#{slug} is not implemented")
  rescue ActionFailed => e
    # Need to use with_friendly_errors again somewhere, since errors don't bubble up
    # without fast-fail?
    Omnitest.handle_error(e)
    raise(ScenarioFailure, e.message, e.backtrace)
  end
  transition_result
end

#verifyObject



24
25
26
# File 'lib/omnitest/skeptic/test_transitions.rb', line 24

def verify
  transition_to :verify
end

#verify_actionObject



28
29
30
31
32
# File 'lib/omnitest/skeptic/test_transitions.rb', line 28

def verify_action
  perform_action(:verify, 'Verifying') do
    verify!
  end
end