xref: /freebsd-src/contrib/googletest/googletest/test/googletest-json-outfiles-test.py (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
1b89a7cc2SEnji Cooper#!/usr/bin/env python
2b89a7cc2SEnji Cooper# Copyright 2018, Google Inc.
3b89a7cc2SEnji Cooper# All rights reserved.
4b89a7cc2SEnji Cooper#
5b89a7cc2SEnji Cooper# Redistribution and use in source and binary forms, with or without
6b89a7cc2SEnji Cooper# modification, are permitted provided that the following conditions are
7b89a7cc2SEnji Cooper# met:
8b89a7cc2SEnji Cooper#
9b89a7cc2SEnji Cooper#     * Redistributions of source code must retain the above copyright
10b89a7cc2SEnji Cooper# notice, this list of conditions and the following disclaimer.
11b89a7cc2SEnji Cooper#     * Redistributions in binary form must reproduce the above
12b89a7cc2SEnji Cooper# copyright notice, this list of conditions and the following disclaimer
13b89a7cc2SEnji Cooper# in the documentation and/or other materials provided with the
14b89a7cc2SEnji Cooper# distribution.
15b89a7cc2SEnji Cooper#     * Neither the name of Google Inc. nor the names of its
16b89a7cc2SEnji Cooper# contributors may be used to endorse or promote products derived from
17b89a7cc2SEnji Cooper# this software without specific prior written permission.
18b89a7cc2SEnji Cooper#
19b89a7cc2SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20b89a7cc2SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21b89a7cc2SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22b89a7cc2SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23b89a7cc2SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24b89a7cc2SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25b89a7cc2SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26b89a7cc2SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27b89a7cc2SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28b89a7cc2SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29b89a7cc2SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30b89a7cc2SEnji Cooper
31b89a7cc2SEnji Cooper"""Unit test for the gtest_json_output module."""
32b89a7cc2SEnji Cooper
33b89a7cc2SEnji Cooperimport json
34b89a7cc2SEnji Cooperimport os
35*28f6c2f2SEnji Cooperfrom googletest.test import gtest_json_test_utils
36*28f6c2f2SEnji Cooperfrom googletest.test import gtest_test_utils
37b89a7cc2SEnji Cooper
38b89a7cc2SEnji CooperGTEST_OUTPUT_SUBDIR = 'json_outfiles'
39b89a7cc2SEnji CooperGTEST_OUTPUT_1_TEST = 'gtest_xml_outfile1_test_'
40b89a7cc2SEnji CooperGTEST_OUTPUT_2_TEST = 'gtest_xml_outfile2_test_'
41b89a7cc2SEnji Cooper
42b89a7cc2SEnji CooperEXPECTED_1 = {
43*28f6c2f2SEnji Cooper    'tests': 1,
44*28f6c2f2SEnji Cooper    'failures': 0,
45*28f6c2f2SEnji Cooper    'disabled': 0,
46*28f6c2f2SEnji Cooper    'errors': 0,
47*28f6c2f2SEnji Cooper    'time': '*',
48*28f6c2f2SEnji Cooper    'timestamp': '*',
49*28f6c2f2SEnji Cooper    'name': 'AllTests',
50*28f6c2f2SEnji Cooper    'testsuites': [{
51*28f6c2f2SEnji Cooper        'name': 'PropertyOne',
52*28f6c2f2SEnji Cooper        'tests': 1,
53*28f6c2f2SEnji Cooper        'failures': 0,
54*28f6c2f2SEnji Cooper        'disabled': 0,
55*28f6c2f2SEnji Cooper        'errors': 0,
56*28f6c2f2SEnji Cooper        'time': '*',
57*28f6c2f2SEnji Cooper        'timestamp': '*',
58*28f6c2f2SEnji Cooper        'testsuite': [{
59*28f6c2f2SEnji Cooper            'name': 'TestSomeProperties',
60*28f6c2f2SEnji Cooper            'file': 'gtest_xml_outfile1_test_.cc',
61*28f6c2f2SEnji Cooper            'line': 41,
62*28f6c2f2SEnji Cooper            'status': 'RUN',
63*28f6c2f2SEnji Cooper            'result': 'COMPLETED',
64*28f6c2f2SEnji Cooper            'time': '*',
65*28f6c2f2SEnji Cooper            'timestamp': '*',
66*28f6c2f2SEnji Cooper            'classname': 'PropertyOne',
67*28f6c2f2SEnji Cooper            'SetUpProp': '1',
68*28f6c2f2SEnji Cooper            'TestSomeProperty': '1',
69*28f6c2f2SEnji Cooper            'TearDownProp': '1',
70b89a7cc2SEnji Cooper        }],
71b89a7cc2SEnji Cooper    }],
72b89a7cc2SEnji Cooper}
73b89a7cc2SEnji Cooper
74b89a7cc2SEnji CooperEXPECTED_2 = {
75*28f6c2f2SEnji Cooper    'tests': 1,
76*28f6c2f2SEnji Cooper    'failures': 0,
77*28f6c2f2SEnji Cooper    'disabled': 0,
78*28f6c2f2SEnji Cooper    'errors': 0,
79*28f6c2f2SEnji Cooper    'time': '*',
80*28f6c2f2SEnji Cooper    'timestamp': '*',
81*28f6c2f2SEnji Cooper    'name': 'AllTests',
82*28f6c2f2SEnji Cooper    'testsuites': [{
83*28f6c2f2SEnji Cooper        'name': 'PropertyTwo',
84*28f6c2f2SEnji Cooper        'tests': 1,
85*28f6c2f2SEnji Cooper        'failures': 0,
86*28f6c2f2SEnji Cooper        'disabled': 0,
87*28f6c2f2SEnji Cooper        'errors': 0,
88*28f6c2f2SEnji Cooper        'time': '*',
89*28f6c2f2SEnji Cooper        'timestamp': '*',
90*28f6c2f2SEnji Cooper        'testsuite': [{
91*28f6c2f2SEnji Cooper            'name': 'TestInt64ConvertibleProperties',
92*28f6c2f2SEnji Cooper            'file': 'gtest_xml_outfile2_test_.cc',
93*28f6c2f2SEnji Cooper            'line': 43,
94*28f6c2f2SEnji Cooper            'status': 'RUN',
95*28f6c2f2SEnji Cooper            'result': 'COMPLETED',
96*28f6c2f2SEnji Cooper            'timestamp': '*',
97*28f6c2f2SEnji Cooper            'time': '*',
98*28f6c2f2SEnji Cooper            'classname': 'PropertyTwo',
99*28f6c2f2SEnji Cooper            'SetUpProp': '2',
100*28f6c2f2SEnji Cooper            'TestFloatProperty': '3.25',
101*28f6c2f2SEnji Cooper            'TestDoubleProperty': '4.75',
102*28f6c2f2SEnji Cooper            'TestSizetProperty': '5',
103*28f6c2f2SEnji Cooper            'TestBoolProperty': 'true',
104*28f6c2f2SEnji Cooper            'TestCharProperty': 'A',
105*28f6c2f2SEnji Cooper            'TestInt16Property': '6',
106*28f6c2f2SEnji Cooper            'TestInt32Property': '7',
107*28f6c2f2SEnji Cooper            'TestInt64Property': '8',
108*28f6c2f2SEnji Cooper            'TestEnumProperty': '9',
109*28f6c2f2SEnji Cooper            'TestAtomicIntProperty': '10',
110*28f6c2f2SEnji Cooper            'TearDownProp': '2',
111b89a7cc2SEnji Cooper        }],
112b89a7cc2SEnji Cooper    }],
113b89a7cc2SEnji Cooper}
114b89a7cc2SEnji Cooper
115b89a7cc2SEnji Cooper
116b89a7cc2SEnji Cooperclass GTestJsonOutFilesTest(gtest_test_utils.TestCase):
117b89a7cc2SEnji Cooper  """Unit test for Google Test's JSON output functionality."""
118b89a7cc2SEnji Cooper
119b89a7cc2SEnji Cooper  def setUp(self):
120b89a7cc2SEnji Cooper    # We want the trailing '/' that the last "" provides in os.path.join, for
121b89a7cc2SEnji Cooper    # telling Google Test to create an output directory instead of a single file
122b89a7cc2SEnji Cooper    # for xml output.
123*28f6c2f2SEnji Cooper    self.output_dir_ = os.path.join(
124*28f6c2f2SEnji Cooper        gtest_test_utils.GetTempDir(), GTEST_OUTPUT_SUBDIR, ''
125*28f6c2f2SEnji Cooper    )
126b89a7cc2SEnji Cooper    self.DeleteFilesAndDir()
127b89a7cc2SEnji Cooper
128b89a7cc2SEnji Cooper  def tearDown(self):
129b89a7cc2SEnji Cooper    self.DeleteFilesAndDir()
130b89a7cc2SEnji Cooper
131b89a7cc2SEnji Cooper  def DeleteFilesAndDir(self):
132b89a7cc2SEnji Cooper    try:
133b89a7cc2SEnji Cooper      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + '.json'))
134b89a7cc2SEnji Cooper    except os.error:
135b89a7cc2SEnji Cooper      pass
136b89a7cc2SEnji Cooper    try:
137b89a7cc2SEnji Cooper      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + '.json'))
138b89a7cc2SEnji Cooper    except os.error:
139b89a7cc2SEnji Cooper      pass
140b89a7cc2SEnji Cooper    try:
141b89a7cc2SEnji Cooper      os.rmdir(self.output_dir_)
142b89a7cc2SEnji Cooper    except os.error:
143b89a7cc2SEnji Cooper      pass
144b89a7cc2SEnji Cooper
145b89a7cc2SEnji Cooper  def testOutfile1(self):
146b89a7cc2SEnji Cooper    self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_1)
147b89a7cc2SEnji Cooper
148b89a7cc2SEnji Cooper  def testOutfile2(self):
149b89a7cc2SEnji Cooper    self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_2)
150b89a7cc2SEnji Cooper
151b89a7cc2SEnji Cooper  def _TestOutFile(self, test_name, expected):
152b89a7cc2SEnji Cooper    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
153b89a7cc2SEnji Cooper    command = [gtest_prog_path, '--gtest_output=json:%s' % self.output_dir_]
154*28f6c2f2SEnji Cooper    p = gtest_test_utils.Subprocess(
155*28f6c2f2SEnji Cooper        command, working_dir=gtest_test_utils.GetTempDir()
156*28f6c2f2SEnji Cooper    )
157*28f6c2f2SEnji Cooper    self.assertTrue(p.exited)
158*28f6c2f2SEnji Cooper    self.assertEqual(0, p.exit_code)
159b89a7cc2SEnji Cooper
160b89a7cc2SEnji Cooper    output_file_name1 = test_name + '.json'
161b89a7cc2SEnji Cooper    output_file1 = os.path.join(self.output_dir_, output_file_name1)
162b89a7cc2SEnji Cooper    output_file_name2 = 'lt-' + output_file_name1
163b89a7cc2SEnji Cooper    output_file2 = os.path.join(self.output_dir_, output_file_name2)
164*28f6c2f2SEnji Cooper    self.assertTrue(
165*28f6c2f2SEnji Cooper        os.path.isfile(output_file1) or os.path.isfile(output_file2),
166*28f6c2f2SEnji Cooper        output_file1,
167*28f6c2f2SEnji Cooper    )
168b89a7cc2SEnji Cooper
169b89a7cc2SEnji Cooper    if os.path.isfile(output_file1):
170b89a7cc2SEnji Cooper      with open(output_file1) as f:
171b89a7cc2SEnji Cooper        actual = json.load(f)
172b89a7cc2SEnji Cooper    else:
173b89a7cc2SEnji Cooper      with open(output_file2) as f:
174b89a7cc2SEnji Cooper        actual = json.load(f)
175b89a7cc2SEnji Cooper    self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
176b89a7cc2SEnji Cooper
177b89a7cc2SEnji Cooper
178b89a7cc2SEnji Cooperif __name__ == '__main__':
179b89a7cc2SEnji Cooper  os.environ['GTEST_STACK_TRACE_DEPTH'] = '0'
180b89a7cc2SEnji Cooper  gtest_test_utils.Main()
181