1# 2# Copyright (C) 2016 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17import logging 18import re 19import uuid 20 21from vts.runners.host import utils 22from vts.testcases.template.binary_test import binary_test_case 23from vts.utils.python.os import path_utils 24 25 26class GtestTestCase(binary_test_case.BinaryTestCase): 27 '''A class to represent a gtest test case. 28 29 Attributes: 30 test_suite: string, test suite name 31 test_name: string, test case name which does not include test suite 32 path: string, absolute test binary path on device 33 tag: string, test tag 34 put_tag_func: function that takes a name and tag to output a combination 35 output_file_path: string, gtest output xml path on device 36 ''' 37 38 # @Override 39 def GetRunCommand(self, 40 output_file_path=None, 41 test_name=None, 42 raw_command=False): 43 '''Get the command to run the test. 44 45 Args: 46 output_file_path: file to store the gtest results. 47 test_name: name of the gtest test case. 48 raw_command: whether to return raw command (without gtest_filter 49 and gtest_output). 50 51 Returns: 52 List of strings 53 ''' 54 if raw_command: 55 return super(GtestTestCase, self).GetRunCommand() 56 57 if output_file_path: 58 self.output_file_path = output_file_path 59 if not test_name: 60 test_name = self.full_name 61 62 gtest_filter_flag = ('--gtest_filter={test}').format(test=test_name) 63 if self.filter_file: 64 gtest_filter_flag='--gtest_flagfile=%s' % self.filter_file 65 66 return [('{cmd} {filter_flag} ' 67 '--gtest_output=xml:{output_file_path}').format( 68 cmd=super(GtestTestCase, self).GetRunCommand(), 69 filter_flag = gtest_filter_flag, 70 output_file_path=self.output_file_path), 71 'cat {output} && rm -rf {output}'.format( 72 output=self.output_file_path)] 73 74 @property 75 def output_file_path(self): 76 """Get output_file_path""" 77 if not hasattr(self, 78 '_output_file_path') or self._output_file_path is None: 79 self.output_file_path = '{directory}/gtest_output_{name}.xml'.format( 80 directory=path_utils.TargetDirName(self.path), 81 name=re.sub(r'\W+', '_', str(self))) 82 return self._output_file_path 83 84 @output_file_path.setter 85 def output_file_path(self, output_file_path): 86 """Set output_file_path. 87 88 Lengths of both file name and path will be checked. If longer than 89 maximum allowance, file name will be set to a random name, and 90 directory will be set to relative directory. 91 92 Args: 93 output_file_path: string, intended path of output xml file 94 """ 95 output_file_path = path_utils.TargetNormPath(output_file_path.strip()) 96 output_base_name = path_utils.TargetBaseName(output_file_path) 97 output_dir_name = path_utils.TargetDirName(output_file_path) 98 99 if len(output_base_name) > utils.MAX_FILENAME_LEN: 100 logging.warn( 101 'File name of output file "{}" is longer than {}.'.format( 102 output_file_path, utils.MAX_FILENAME_LEN)) 103 output_base_name = '{}.xml'.format(uuid.uuid4()) 104 output_file_path = path_utils.JoinTargetPath( 105 output_dir_name, output_base_name) 106 logging.debug('Output file path is set as "%s".', output_file_path) 107 108 if len(output_file_path) > utils.MAX_PATH_LEN: 109 logging.warn( 110 'File path of output file "{}" is longer than {}.'.format( 111 output_file_path, utils.MAX_PATH_LEN)) 112 output_file_path = output_base_name 113 logging.debug('Output file path is set as "%s".', output_file_path) 114 115 self._output_file_path = output_file_path 116