Class: OpenC3::SuiteRunner

Inherits:
Object show all
Defined in:
lib/openc3/script/suite_runner.rb

Constant Summary collapse

DEFAULT_METHOD =

Default suite_runner options shared by all callers (e.g. openc3cli) so the values stay consistent in one place.

'start'
DEFAULT_OPTIONS =
['continueAfterError'].freeze
@@suites =
[]
@@settings =
{}
@@suite_results =
nil

Class Method Summary collapse

Class Method Details

.build_options(suite:, group: nil, script: nil, method: nil, options: nil) ⇒ Object

Build the canonical, validated suite_runner hash from raw values, applying defaults. Shared by openc3cli (constructing from CLI flags) and the running script (normalizing a received hash before dispatch) so the hash shape, defaults, and validation live in one place.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openc3/script/suite_runner.rb', line 62

def self.build_options(suite:, group: nil, script: nil, method: nil, options: nil)
  validate_options(group: group, script: script)
  suite_runner = { 'suite' => suite }
  if group
    suite_runner['group'] = group
    suite_runner['script'] = script if script
  end
  # A script always runs via start, matching the GUI
  suite_runner['method'] = script ? DEFAULT_METHOD : (method || DEFAULT_METHOD)
  suite_runner['options'] = options || DEFAULT_OPTIONS.dup
  suite_runner
end

.build_suitesObject

Build list of Suites and Groups



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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/openc3/script/suite_runner.rb', line 153

def self.build_suites
  @@suites = []
  suites = {}
  groups = []
  ObjectSpace.each_object(Class) do |object|
    # If we inherit from Suite but aren't the deprecated TestSuite
    if object < Suite && object != TestSuite
      # Ensure they didn't override name for some reason
      if object.instance_methods(false).include?(:name)
        raise FatalError.new("#{object} redefined the 'name' method. Delete the 'name' method and try again.")
      end

      # ObjectSpace.each_object appears to yield objects in the reverse
      # order that they were parsed by the interpreter so push each
      # Suite object to the front of the array to order as encountered
      @@suites.unshift(object.new)
    end
    # If we inherit from Group but aren't the deprecated Test
    if object < Group && object != Test
      # Ensure they didn't override self.name for some reason
      if object.methods(false).include?(:name)
        raise FatalError.new("#{object} redefined the 'self.name' method. Delete the 'self.name' method and try again.")
      end
      groups << object
    end
  end
  # Raise error if no suites or groups
  if @@suites.empty? || groups.empty?
    return "No Suite or no Group classes found"
  end

  # Remove assigned Groups from the array of groups
  @@suites.each do |suite|
    next if suite.class == UnassignedSuite
    groups_to_delete = []
    groups.each { |group| groups_to_delete << group if suite.scripts[group] }
    groups_to_delete.each { |group| groups.delete(group) }
  end
  if groups.empty?
    # If there are no unassigned group we simply remove the UnassignedSuite
    @@suites = @@suites.select { |suite| suite.class != UnassignedSuite }
  else
    # unassigned groups should be added to the UnassignedSuite
    unassigned_suite = @@suites.select { |suite| suite.class == UnassignedSuite }[0]
    groups.each { |group| unassigned_suite.add_group(group) }
  end

  @@suites.each do |suite|
    cur_suite = OpenStruct.new(:setup => false, :teardown => false, :groups => {})
    cur_suite.setup = true if suite.class.method_defined?(:setup)
    cur_suite.teardown = true if suite.class.method_defined?(:teardown)

    suite.plans.each do |type, group_class, script|
      case type
      when :GROUP
        cur_suite.groups[group_class.name] ||=
          OpenStruct.new(:setup => false, :teardown => false, :scripts => [])
        cur_suite.groups[group_class.name].scripts.concat(group_class.scripts)
        cur_suite.groups[group_class.name].scripts.uniq!
        cur_suite.groups[group_class.name].setup = true if group_class.method_defined?(:setup)
        cur_suite.groups[group_class.name].teardown = true if group_class.method_defined?(:teardown)
      when :SCRIPT
        cur_suite.groups[group_class.name] ||=
          OpenStruct.new(:setup => false, :teardown => false, :scripts => [])
        # Explicitly check for this method and raise an error if it does not exist
        if group_class.method_defined?(script.intern)
          cur_suite.groups[group_class.name].scripts << script
          cur_suite.groups[group_class.name].scripts.uniq!
        else
          raise "#{group_class} does not have a #{script} method defined."
        end
        cur_suite.groups[group_class.name].setup = true if group_class.method_defined?(:setup)
        cur_suite.groups[group_class.name].teardown = true if group_class.method_defined?(:teardown)
      when :GROUP_SETUP
        cur_suite.groups[group_class.name] ||=
          OpenStruct.new(:setup => false, :teardown => false, :scripts => [])
        # Explicitly check for the setup method and raise an error if it does not exist
        if group_class.method_defined?(:setup)
          cur_suite.groups[group_class.name].setup = true
        else
          raise "#{group_class} does not have a setup method defined."
        end
      when :GROUP_TEARDOWN
        cur_suite.groups[group_class.name] ||=
          OpenStruct.new(:setup => false, :teardown => false, :scripts => [])
        # Explicitly check for the teardown method and raise an error if it does not exist
        if group_class.method_defined?(:teardown)
          cur_suite.groups[group_class.name].teardown = true
        else
          raise "#{group_class} does not have a teardown method defined."
        end
      end
    end
    suites[suite.name.split('::')[-1]] = open_struct_to_hash(cur_suite) unless suite.name == 'CustomSuite'
  end
  return suites
end

.execute(result_string, suite_class, group_class = nil, script = nil) ⇒ Object

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openc3/script/suite_runner.rb', line 75

def self.execute(result_string, suite_class, group_class = nil, script = nil)
  @@suite_results = SuiteResults.new
  # Surface invalid suite / group / option combinations rather than
  # silently running nothing or failing with an opaque nil error. An
  # invalid script method raises later in Group#run_method.
  validate_options(group: group_class, script: script)
  suite = @@suites.find { |s| s.class == suite_class }
  raise ArgumentError, "Suite #{suite_class} not found" unless suite
  if group_class and not suite.scripts.key?(group_class)
    raise ArgumentError, "Group #{group_class} not found in Suite #{suite_class}"
  end
  @@suite_results.start(result_string, suite_class, group_class, script, @@settings)
  loop do
    yield(suite)
    break if not @@settings['Loop'] or (ScriptStatus.instance.fail_count > 0 and @@settings['Break Loop On Error'])
  end
end

.open_struct_to_hash(object) ⇒ Object

Convert the OpenStruct structure to a simple hash TODO: Maybe just use hashes right from the beginning?



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/openc3/script/suite_runner.rb', line 138

def self.open_struct_to_hash(object)
  hash = object.to_h
  hash.each do |key1, val1|
    if val1.is_a?(Hash)
      val1.each do |key2, val2|
        if val2.is_a?(OpenStruct)
          hash[key1][key2] = val2.to_h
        end
      end
    end
  end
  hash
end

.settingsObject



38
39
40
# File 'lib/openc3/script/suite_runner.rb', line 38

def self.settings
  @@settings
end

.settings=(settings) ⇒ Object



42
43
44
# File 'lib/openc3/script/suite_runner.rb', line 42

def self.settings=(settings)
  @@settings = settings
end

.setup(suite_class, group_class = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/openc3/script/suite_runner.rb', line 108

def self.setup(suite_class, group_class = nil)
  execute('Manual Setup', suite_class, group_class) do |suite|
    if group_class
      result = suite.run_group_setup(group_class)
    else
      result = suite.run_setup
    end
    if result
      @@suite_results.process_result(result)
      raise StopScript if result.stopped
    end
  end
end

.start(suite_class, group_class = nil, script = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/openc3/script/suite_runner.rb', line 93

def self.start(suite_class, group_class = nil, script = nil)
  result = []
  execute('', suite_class, group_class, script) do |suite|
    if script
      result = suite.run_script(group_class, script)
      @@suite_results.process_result(result)
      raise StopScript if (result.exceptions and Group.abort_on_exception) or result.stopped
    elsif group_class
      suite.run_group(group_class) { |current_result| @@suite_results.process_result(current_result); raise StopScript if current_result.stopped }
    else
      suite.run { |current_result| @@suite_results.process_result(current_result); raise StopScript if current_result.stopped }
    end
  end
end

.suite_resultsObject



46
47
48
# File 'lib/openc3/script/suite_runner.rb', line 46

def self.suite_results
  @@suite_results
end

.teardown(suite_class, group_class = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/openc3/script/suite_runner.rb', line 122

def self.teardown(suite_class, group_class = nil)
  execute('Manual Teardown', suite_class, group_class) do |suite|
    if group_class
      result = suite.run_group_teardown(group_class)
    else
      result = suite.run_teardown
    end
    if result
      @@suite_results.process_result(result)
      raise StopScript if result.stopped
    end
  end
end

.validate_options(group: nil, script: nil) ⇒ Object

Validate the suite_runner option combinations that don't require the built suites. Shared by all callers (the running script via execute and openc3cli when constructing options) so the contract lives in one place. Suite/Group existence is checked in execute since it needs @@suites.

Raises:

  • (ArgumentError)


54
55
56
# File 'lib/openc3/script/suite_runner.rb', line 54

def self.validate_options(group: nil, script: nil)
  raise ArgumentError, "Script #{script} requires a Group" if script and not group
end