1# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14require 'pathname' 15require_relative 'string_logger' 16require_relative 'base_test_reporter' 17 18module TestRunner 19 # JtrTestReporter is reporter to separate file of each test. 20 class JtrTestReporter < BaseTestReporter 21 def initialize(root_dir, pa_file, report_dir) 22 @root = Pathname.new(root_dir) 23 file = Pathname.new(pa_file) # path to file 24 actual_file = file.relative_path_from(@root) 25 report_dir = Pathname.new(report_dir) 26 actual_file = @root.basename if @root.file? 27 @pa_file = actual_file.to_s 28 report_file = report_dir + Pathname.new(@pa_file.gsub(/.pa$/, '.jtr').to_s) 29 report_file.dirname.mkpath 30 FileUtils.rm report_file if report_file.exist? 31 @logger = Reporters::SeparateFileLogger.new(report_file) 32 @status = 'Failed.' 33 @output = '' 34 end 35 36 def jtr_time 37 # on farm timezone %Z got 'Europe' which is rejected by uploader 38 # so MSK set as a workaround 39 Time.now.strftime('%a %b %d %H:%M:%S MSK %Y') 40 end 41 42 def prologue 43 @logger.log 1, '#Test Results (version 2)' 44 @logger.log 1, '#-----testdescription-----' 45 @logger.log 1, "$file=#{@root}/CTS/conformance/tests/#{@pa_file}" 46 @logger.log 1, "$root=#{@root}" 47 @logger.log 1, '' 48 @logger.log 1, '#-----testresult-----' 49 @logger.log 1, "test=CTS/conformance/tests/#{@pa_file}" 50 @logger.log 1, 'suitename=CTS' 51 @logger.log 1, "start=#{jtr_time}" 52 end 53 54 def epilogue 55 @logger.log 1, "end=#{jtr_time}" 56 @logger.log 1, "execStatus=#{@status}" 57 # add output section in case of failure 58 if !(@status =~ /Passed\.|Not run\./) and !@output.empty? 59 @logger.log 1, 'sections=output' 60 @logger.log 1, '' 61 @logger.log 1, '#section:output' 62 lines = @output.split("\n").map(&:strip).reject { |l| l.match(/^$/) } 63 @output = lines.join("\n") 64 @logger.log 1, "----------messages:(#{lines.length}/#{@output.length + 1})----------" 65 @logger.log 1, @output 66 @logger.log 1, 'result: Failed.' 67 end 68 @logger.close 69 end 70 71 def log_exclusion 72 @status = 'Not run. Excluded by tag' 73 end 74 75 def log_skip_include 76 @status = 'Not run. Not included by tag' 77 end 78 79 def log_skip_bugid 80 @status = 'Not run. Not mtched bugid' 81 end 82 83 def log_skip_ignore 84 @status = 'Not run. runner_option is ignore' 85 end 86 87 def log_skip_only_ignore 88 @status = 'Not run. Running only ignored' 89 end 90 91 def log_ignore_ignored 92 @output << "\nRun all requested" 93 end 94 95 def log_start_command(cmd) 96 @output << "\ncommand = #{cmd}" 97 end 98 99 def log_failed_compilation(output) 100 @status = 'Failed. Failed to compile' 101 @output << "\n" << output 102 end 103 104 def log_negative_passed_compilation(output) 105 @status = 'Failed. Test is compiled, but should be rejected.' 106 @output << "\n" << output 107 end 108 109 def log_failed_negative_compilation(output) 110 @status = 'Passed. Test failed to compile, as expected.' 111 @output << "\n" << output 112 end 113 114 def log_compilation_passed(output) 115 @status = 'Passed. Compilation-only test.' 116 @output << "\n" << output 117 end 118 119 def log_run_negative_failure(output, status) 120 @status = "Failed. Exit code: #{status}, but expected failure." 121 @output << "\n" << output 122 end 123 124 def log_verifier_negative_failure(output, status) 125 @status = "Failed. Verifier exit code: #{status}, but expected failure." 126 @output << "\n" << output 127 end 128 129 def log_run_failure(output, status, core) 130 @status = "Failed. Exit code: #{status}." 131 @output << "\nCore dump was created." if core 132 @output << "\n" << output 133 end 134 135 def log_verifier_failure(output, status, core) 136 @status = "Failed. Verifier exit code: #{status}." 137 @output << "\nCore dump was created." if core 138 @output << "\n" << output 139 end 140 141 def log_passed(output, status) 142 @status = "Passed. Exit code: #{status}." 143 @output << "\n" << output 144 end 145 146 def log_excluded 147 @status = 'Not run.' 148 end 149 150 def verbose_log(status) 151 @output << "\n" << status 152 end 153 154 def log_bugids(bugids) end 155 156 def log_repro_commands(cmds) end 157 end 158end 159