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 return [('{cmd} --gtest_filter={test} ' 62 '--gtest_output=xml:{output_file_path}').format( 63 cmd=super(GtestTestCase, self).GetRunCommand(), 64 test = test_name, 65 output_file_path=self.output_file_path), 66 'cat {output} && rm -rf {output}'.format( 67 output=self.output_file_path)] 68 69 @property 70 def output_file_path(self): 71 """Get output_file_path""" 72 if not hasattr(self, 73 '_output_file_path') or self._output_file_path is None: 74 self.output_file_path = '{directory}/gtest_output_{name}.xml'.format( 75 directory=path_utils.TargetDirName(self.path), 76 name=re.sub(r'\W+', '_', str(self))) 77 return self._output_file_path 78 79 @output_file_path.setter 80 def output_file_path(self, output_file_path): 81 """Set output_file_path. 82 83 Lengths of both file name and path will be checked. If longer than 84 maximum allowance, file name will be set to a random name, and 85 directory will be set to relative directory. 86 87 Args: 88 output_file_path: string, intended path of output xml file 89 """ 90 output_file_path = path_utils.TargetNormPath(output_file_path.strip()) 91 output_base_name = path_utils.TargetBaseName(output_file_path) 92 output_dir_name = path_utils.TargetDirName(output_file_path) 93 94 if len(output_base_name) > utils.MAX_FILENAME_LEN: 95 logging.warn( 96 'File name of output file "{}" is longer than {}.'.format( 97 output_file_path, utils.MAX_FILENAME_LEN)) 98 output_base_name = '{}.xml'.format(uuid.uuid4()) 99 output_file_path = path_utils.JoinTargetPath( 100 output_dir_name, output_base_name) 101 logging.debug('Output file path is set as "%s".', output_file_path) 102 103 if len(output_file_path) > utils.MAX_PATH_LEN: 104 logging.warn( 105 'File path of output file "{}" is longer than {}.'.format( 106 output_file_path, utils.MAX_PATH_LEN)) 107 output_file_path = output_base_name 108 logging.debug('Output file path is set as "%s".', output_file_path) 109 110 self._output_file_path = output_file_path 111