1#!/usr/bin/env python
2#
3# Copyright 2008, Google Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10#     * Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12#     * Redistributions in binary form must reproduce the above
13# copyright notice, this list of conditions and the following disclaimer
14# in the documentation and/or other materials provided with the
15# distribution.
16#     * Neither the name of Google Inc. nor the names of its
17# contributors may be used to endorse or promote products derived from
18# this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32"""Unit test for the gtest_xml_output module."""
33
34import os
35from xml.dom import minidom, Node
36from googletest.test import gtest_test_utils
37from googletest.test import gtest_xml_test_utils
38
39GTEST_OUTPUT_SUBDIR = "xml_outfiles"
40GTEST_OUTPUT_1_TEST = "gtest_xml_outfile1_test_"
41GTEST_OUTPUT_2_TEST = "gtest_xml_outfile2_test_"
42
43EXPECTED_XML_1 = """<?xml version="1.0" encoding="UTF-8"?>
44<testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
45  <testsuite name="PropertyOne" tests="1" failures="0" skipped="0" disabled="0" errors="0" time="*" timestamp="*">
46    <testcase name="TestSomeProperties" file="gtest_xml_outfile1_test_.cc" line="41" status="run" result="completed" time="*" timestamp="*" classname="PropertyOne">
47      <properties>
48        <property name="SetUpProp" value="1"/>
49        <property name="TestSomeProperty" value="1"/>
50        <property name="TearDownProp" value="1"/>
51      </properties>
52    </testcase>
53  </testsuite>
54</testsuites>
55"""
56
57EXPECTED_XML_2 = """<?xml version="1.0" encoding="UTF-8"?>
58<testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
59  <testsuite name="PropertyTwo" tests="1" failures="0" skipped="0" disabled="0" errors="0" time="*" timestamp="*">
60    <testcase name="TestInt64ConvertibleProperties" file="gtest_xml_outfile2_test_.cc" line="43" status="run" result="completed" time="*" timestamp="*" classname="PropertyTwo">
61      <properties>
62        <property name="SetUpProp" value="2"/>
63        <property name="TestFloatProperty" value="3.25"/>
64        <property name="TestDoubleProperty" value="4.75"/>
65        <property name="TestSizetProperty" value="5"/>
66        <property name="TestBoolProperty" value="true"/>
67        <property name="TestCharProperty" value="A"/>
68        <property name="TestInt16Property" value="6"/>
69        <property name="TestInt32Property" value="7"/>
70        <property name="TestInt64Property" value="8"/>
71        <property name="TestEnumProperty" value="9"/>
72        <property name="TestAtomicIntProperty" value="10"/>
73        <property name="TearDownProp" value="2"/>
74      </properties>
75    </testcase>
76  </testsuite>
77</testsuites>
78"""
79
80
81class GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase):
82  """Unit test for Google Test's XML output functionality."""
83
84  def setUp(self):
85    # We want the trailing '/' that the last "" provides in os.path.join, for
86    # telling Google Test to create an output directory instead of a single file
87    # for xml output.
88    self.output_dir_ = os.path.join(
89        gtest_test_utils.GetTempDir(), GTEST_OUTPUT_SUBDIR, ""
90    )
91    self.DeleteFilesAndDir()
92
93  def tearDown(self):
94    self.DeleteFilesAndDir()
95
96  def DeleteFilesAndDir(self):
97    try:
98      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + ".xml"))
99    except os.error:
100      pass
101    try:
102      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + ".xml"))
103    except os.error:
104      pass
105    try:
106      os.rmdir(self.output_dir_)
107    except os.error:
108      pass
109
110  def testOutfile1(self):
111    self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_XML_1)
112
113  def testOutfile2(self):
114    self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_XML_2)
115
116  def _TestOutFile(self, test_name, expected_xml):
117    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
118    command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_]
119    p = gtest_test_utils.Subprocess(
120        command, working_dir=gtest_test_utils.GetTempDir()
121    )
122    self.assertTrue(p.exited)
123    self.assertEqual(0, p.exit_code)
124
125    output_file_name1 = test_name + ".xml"
126    output_file1 = os.path.join(self.output_dir_, output_file_name1)
127    output_file_name2 = "lt-" + output_file_name1
128    output_file2 = os.path.join(self.output_dir_, output_file_name2)
129    self.assertTrue(
130        os.path.isfile(output_file1) or os.path.isfile(output_file2),
131        output_file1,
132    )
133
134    expected = minidom.parseString(expected_xml)
135    if os.path.isfile(output_file1):
136      actual = minidom.parse(output_file1)
137    else:
138      actual = minidom.parse(output_file2)
139    self.NormalizeXml(actual.documentElement)
140    self.AssertEquivalentNodes(expected.documentElement, actual.documentElement)
141    expected.unlink()
142    actual.unlink()
143
144
145if __name__ == "__main__":
146  os.environ["GTEST_STACK_TRACE_DEPTH"] = "0"
147  gtest_test_utils.Main()
148