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
ALLOWED_METHODS =

The only SuiteRunner entry points a client may request

['start', 'setup', 'teardown'].freeze
CLASS_NAME_REGEX =

Suite and Group are Ruby constant (class) names, optionally namespaced

/\A[A-Z]\w*(::[A-Z]\w*)*\z/
METHOD_NAME_REGEX =

Script is a Ruby method name on the Group

/\A[a-z_]\w*[?!]?\z/
@@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.



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openc3/script/suite_runner.rb', line 87

def self.build_options(suite:, group: nil, script: nil, method: nil, options: nil)
  validate_options(group: group, script: script)
  validate_identifiers(suite: suite, group: group, script: script, method: method)
  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



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
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
# File 'lib/openc3/script/suite_runner.rb', line 179

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)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/openc3/script/suite_runner.rb', line 101

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?



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/openc3/script/suite_runner.rb', line 164

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



44
45
46
# File 'lib/openc3/script/suite_runner.rb', line 44

def self.settings
  @@settings
end

.settings=(settings) ⇒ Object



48
49
50
# File 'lib/openc3/script/suite_runner.rb', line 48

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

.setup(suite_class, group_class = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/openc3/script/suite_runner.rb', line 134

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



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

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



52
53
54
# File 'lib/openc3/script/suite_runner.rb', line 52

def self.suite_results
  @@suite_results
end

.teardown(suite_class, group_class = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/openc3/script/suite_runner.rb', line 148

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_identifiers(suite:, group: nil, script: nil, method: nil) ⇒ Object

Validate that the raw (client supplied) suite_runner values are plain identifiers. These values are interpolated into the Ruby snippet the running script evaluates, so anything other than a bare class / method name must be rejected before it reaches the interpreter.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/openc3/script/suite_runner.rb', line 68

def self.validate_identifiers(suite:, group: nil, script: nil, method: nil)
  unless suite.is_a?(String) and CLASS_NAME_REGEX.match?(suite)
    raise ArgumentError, "Invalid Suite name: #{suite.inspect}"
  end
  if group && !(group.is_a?(String) and CLASS_NAME_REGEX.match?(group))
    raise ArgumentError, "Invalid Group name: #{group.inspect}"
  end
  if script && !(script.is_a?(String) and METHOD_NAME_REGEX.match?(script))
    raise ArgumentError, "Invalid Script name: #{script.inspect}"
  end
  if method && !ALLOWED_METHODS.include?(method.to_s)
    raise ArgumentError, "Invalid method: #{method.inspect}"
  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)


60
61
62
# File 'lib/openc3/script/suite_runner.rb', line 60

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