• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ntpath
19import os
20import re
21import uuid
22
23from vts.runners.host import utils
24from vts.testcases.template.binary_test import binary_test_case
25
26
27class GtestTestCase(binary_test_case.BinaryTestCase):
28    '''A class to represent a gtest test case.
29
30    Attributes:
31        test_suite: string, test suite name
32        test_name: string, test case name which does not include test suite
33        path: string, absolute test binary path on device
34        tag: string, test tag
35        put_tag_func: function that takes a name and tag to output a combination
36        output_file_path: string, gtest output xml file name
37    '''
38
39    # @Override
40    def GetRunCommand(self, output_file_path=None, test_name=None):
41        '''Get the command to run the test.
42
43        Args:
44            output_file_path: file to store the gtest results.
45            test_name: name of the gtest test case.
46        Returns:
47            List of strings
48        '''
49        if output_file_path:
50            self.output_file_path = output_file_path
51        if not test_name:
52            test_name = self.GetFullName()
53        return [('{cmd} --gtest_filter={test} '
54                 '--gtest_output=xml:{output_file_path}').format(
55                     cmd=super(GtestTestCase, self).GetRunCommand(),
56                     test=test_name,
57                     output_file_path=self.output_file_path),
58                'cat {output} && rm -rf {output}'.format(
59                    output=self.output_file_path)]
60
61    @property
62    def output_file_path(self):
63        """Get output_file_path"""
64        if not hasattr(self,
65                       '_output_file_path') or self._output_file_path is None:
66            self.output_file_path = '{directory}/gtest_output_{name}.xml'.format(
67                directory=ntpath.dirname(self.path),
68                name=re.sub(r'\W+', '_', str(self)))
69        return self._output_file_path
70
71    @output_file_path.setter
72    def output_file_path(self, output_file_path):
73        """Set output_file_path.
74
75        Lengths of both file name and path will be checked. If longer than
76        maximum allowance, file name will be set to a random name, and
77        directory will be set to relative directory.
78
79        Args:
80            output_file_path: string, intended path of output xml file
81        """
82        output_file_path = os.path.normpath(output_file_path.strip())
83
84        if len(ntpath.basename(output_file_path)) > utils.MAX_FILENAME_LEN:
85            logging.error(
86                'File name of output file "{}" is longer than {}.'.format(
87                    output_file_path), utils.MAX_FILENAME_LEN)
88            output_file_path = os.path.join(
89                ntpath.dirname(output_file_path),
90                '{}.xml'.format(uuid.uuid4()))
91            logging.info('Output file path is set as "%s".', output_file_path)
92
93        if len(output_file_path) > utils.MAX_PATH_LEN:
94            logging.error(
95                'File path of output file "{}" is longer than {}.'.format(
96                    output_file_path), utils.MAX_PATH_LEN)
97            output_file_path = ntpath.basename(output_file_path)
98            logging.info('Output file path is set as "%s".',
99                         os.path.abspath(output_file_path))
100
101        self._output_file_path = output_file_path
102