Class: Cosmos::ResultsWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/tools/test_runner/results_writer.rb

Constant Summary collapse

TIME_TOLERANCE =
5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultsWriter

Returns a new instance of ResultsWriter.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 26

def initialize
  @filename = nil
  @file = nil
  @context = nil
  @path = System.paths['LOGS']
  @start_time = nil
  @stop_time = nil
  @results = nil
  @settings = nil
  @data_package = false
  @canceled = false
  @metadata = nil
  @auto_cycle_logs = false
end

Instance Attribute Details

#auto_cycle_logsObject

Returns the value of attribute auto_cycle_logs.



22
23
24
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 22

def auto_cycle_logs
  @auto_cycle_logs
end

#data_packageObject

Returns the value of attribute data_package.



20
21
22
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 20

def data_package
  @data_package
end

#filenameObject (readonly)

Returns the value of attribute filename.



19
20
21
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 19

def filename
  @filename
end

#metadataObject

Returns the value of attribute metadata.



21
22
23
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 21

def 
  @metadata
end

Instance Method Details

#cancel_callback(progress_dialog = nil) ⇒ Object

Parameters:

  • progress_dialog (ProgressDialog) (defaults to: nil)

    The dialog that was cancelled



201
202
203
204
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 201

def cancel_callback(progress_dialog = nil)
  @canceled = true
  return true, false
end

#collect_metadata(parent = nil) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 255

def (parent = nil)
  success = true
  if @metadata
    Qt.execute_in_main_thread(true) do
      begin
        success = SetTlmDialog.execute(parent, 'Enter Test Metadata', 'Start Test', 'Cancel Test', @metadata[0], @metadata[1])
      rescue DRb::DRbConnError
        success = false
        Qt::MessageBox.critical(parent, 'Error', 'Error Connecting to Command and Telemetry Server')
      rescue Exception => err
        Cosmos.handle_fatal_exception(err)
      end
    end
  end
  success
end

#completeObject



113
114
115
116
117
118
119
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 113

def complete
  @stop_time = Time.now
  cycle_logs() if @auto_cycle_logs
  footer()
ensure
  @file.close if @file and not @file.closed?
end

#create_data_package(progress_dialog = nil) ⇒ Object



150
151
152
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
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 150

def create_data_package(progress_dialog = nil)
  if @data_package
    progress_dialog.cancel_callback = method(:cancel_callback) if progress_dialog
    progress_dialog.enable_cancel_button if progress_dialog

    begin
      Cosmos.set_working_dir do
        file_list = data_package_files()
        Zip::File.open(@data_package_filename, Zip::File::CREATE) do|zf|
          count = 0
          file_list.each do |file|
            break if @canceled
            count += 1
            if File.exist?(file) and File.size(file) > 0
              zf.add(File.basename(file), file)
              progress_dialog.set_overall_progress(count / (file_list.length).to_f) if progress_dialog
            end
          end
        end
      end
      progress_dialog.close_done if progress_dialog
    rescue => error
      progress_dialog.append_text("Error creating data package:\n#{error.formatted}\n") if progress_dialog
    ensure
      progress_dialog.set_overall_progress(1.0) if progress_dialog and !@canceled
      progress_dialog.complete if progress_dialog
    end
  end
end

#cycle_logsObject



272
273
274
275
276
277
278
279
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 272

def cycle_logs
  begin
    start_new_server_message_log()
    start_logging()
  rescue DRb::DRbConnError
    # Oh well - Probably running a test that does not use realtime data
  end
end

#data_package_filesObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 180

def data_package_files
  # Grab data files that were created during test
  file_list = []
  Cosmos.set_working_dir do
    if File.exist?(@path)
      dir = Dir.new(@path)
      dir.each do |file|
        path = File.join(@path, file)
        unless FileTest.directory?(path)
          file_time = File.ctime(path)
          if file_time >= (@start_time - TIME_TOLERANCE) and file_time <= (@stop_time + TIME_TOLERANCE)
            file_list << path
          end
        end
      end
    end
  end
  file_list
end


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
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 206

def footer
  self.puts "Completed #{@context}"

  @file.puts ''
  @file.puts "--- Test Summary ---"
  @file.puts ''

  pass_count = 0
  skip_count = 0
  fail_count = 0
  stopped    = false
  @results.each do |result|
    if result.result == :PASS
      pass_count += 1
    elsif result.result == :SKIP
      skip_count += 1
    elsif result.result == :FAIL
      fail_count += 1
    end

    if result.stopped
      stopped = true
    end
  end
  run_time = Time.format_seconds(@stop_time - @start_time)
  run_time << " (#{@stop_time - @start_time} seconds)" if @stop_time-@start_time > 60
  @file.puts("Run Time : #{run_time}")
  @file.puts("Total Tests : #{@results.length}")
  @file.puts("Pass : #{pass_count}")
  @file.puts("Skip : #{skip_count}")
  @file.puts("Fail : #{fail_count}")
  @file.puts('')
  if stopped
    @file.puts '*** Test was stopped prematurely ***'
    @file.puts ''
  end
  @file.flush
end

#headerObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 121

def header
  @file.puts "--- Test Report ---"
  @file.puts ''
  @file.puts "Files:"
  @file.puts "Report Filename: #{@filename}"
  @file.puts "Detailed Test Output Logged to: #{get_scriptrunner_message_log_filename()}"
  if @metadata
    begin
      items = get_tlm_packet(@metadata[0], @metadata[1])
      @file.puts ''
      @file.puts "Metadata:"
      items.each do |item_name, item_value, _|
        next if SetTlmDialog::IGNORED_ITEMS.include?(item_name)
        @file.puts "#{item_name} = #{item_value}"
      end
    rescue DRb::DRbConnError
      # Oh well
    end
  end
  if @settings
    @file.puts ''
    @file.puts "Settings:"
    @settings.each { |setting_name, setting_value| @file.puts "#{setting_name} = #{setting_value}" }
  end
  @file.puts ''
  @file.puts "Results:"
  self.puts "Executing #{@context}"
end

#process_result(results) ⇒ Object

process_result can handle an array of CosmosTestResult objects or a single CosmosTestResult object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 70

def process_result(results)
  cosmos_lib = Regexp.new(File.join(Cosmos::PATH, 'lib'))
  # If we were passed an array we concat it to the results global
  if results.is_a? Array
    @results.concat(results)
  # A single result is appended and then turned into an array
  else
    @results << results
    results = [results]
  end
  # Process all the results (may be just one)
  results.each do |result|
    self.puts("#{result.test}:#{result.test_case}:#{result.result}")
    if result.message
      result.message.each_line do |line|
        if line =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F-\xFF]/
          line.chomp!
          line = line.inspect.remove_quotes
        end
        @file.puts '  ' + line
      end
      @file.flush
    end
    if result.exceptions
      @file.puts "  Exceptions:"
      result.exceptions.each_with_index do |error, index|
        error.formatted(true).each_line do |line|
          break if line =~ /test_runner\/test.rb/
          next  if line =~ cosmos_lib
          if line =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F-\xFF]/
            line.chomp!
            line = line.inspect.remove_quotes
            line << "\n"
          end
          @file.print '    ' + line
        end
        @file.puts '' if index != (result.exceptions.length - 1)
      end
      @file.flush
    end
  end
end

#puts(string) ⇒ Object



250
251
252
253
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 250

def puts(string)
  @file.puts(Time.now.formatted + ': ' + string)
  @file.flush
end

#start(test_type, test_suite_class, test_class = nil, test_case = nil, settings = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 41

def start(test_type, test_suite_class, test_class = nil, test_case = nil, settings = nil)
  @results = []
  @start_time = Time.now
  @filename = File.join(@path, File.build_timestamped_filename(['testrunner', 'results']))
  @data_package_filename = @filename[0..-5] + '.zip'
  Cosmos.set_working_dir do
    @file = File.new(@filename, 'w')
  end
  @settings = settings
  @canceled = false

  if test_case
    # Executing a single test case
    @context = "#{test_suite_class.name}:#{test_class.name}:#{test_case} #{test_type}"
  elsif test_class
    # Executing an entire test
    @context = "#{test_suite_class.name}:#{test_class.name} #{test_type}"
  else
    # Executing a test suite
    @context = "#{test_suite_class.name} #{test_type}"
  end

  cycle_logs() if @auto_cycle_logs

  header()
end

#write(string) ⇒ Object



245
246
247
248
# File 'lib/cosmos/tools/test_runner/results_writer.rb', line 245

def write(string)
  @file.write(Time.now.formatted + ': ' + string)
  @file.flush
end