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
36import gtest_test_utils
37import 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" disabled="0" errors="0" time="*">
46    <testcase name="TestSomeProperties" status="run" time="*" 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" disabled="0" errors="0" time="*">
60    <testcase name="TestSomeProperties" status="run" time="*" classname="PropertyTwo">
61      <properties>
62        <property name="SetUpProp" value="2"/>
63        <property name="TestSomeProperty" value="2"/>
64        <property name="TearDownProp" value="2"/>
65      </properties>
66    </testcase>
67  </testsuite>
68</testsuites>
69"""
70
71
72class GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase):
73  """Unit test for Google Test's XML output functionality."""
74
75  def setUp(self):
76    # We want the trailing '/' that the last "" provides in os.path.join, for
77    # telling Google Test to create an output directory instead of a single file
78    # for xml output.
79    self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
80                                    GTEST_OUTPUT_SUBDIR, "")
81    self.DeleteFilesAndDir()
82
83  def tearDown(self):
84    self.DeleteFilesAndDir()
85
86  def DeleteFilesAndDir(self):
87    try:
88      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + ".xml"))
89    except os.error:
90      pass
91    try:
92      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + ".xml"))
93    except os.error:
94      pass
95    try:
96      os.rmdir(self.output_dir_)
97    except os.error:
98      pass
99
100  def testOutfile1(self):
101    self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_XML_1)
102
103  def testOutfile2(self):
104    self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_XML_2)
105
106  def _TestOutFile(self, test_name, expected_xml):
107    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
108    command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_]
109    p = gtest_test_utils.Subprocess(command,
110                                    working_dir=gtest_test_utils.GetTempDir())
111    self.assert_(p.exited)
112    self.assertEquals(0, p.exit_code)
113
114    # FIXME: libtool causes the built test binary to be
115    #   named lt-gtest_xml_outfiles_test_ instead of
116    #   gtest_xml_outfiles_test_.  To account for this possibility, we
117    #   allow both names in the following code.  We should remove this
118    #   when libtool replacement tool is ready.
119    output_file_name1 = test_name + ".xml"
120    output_file1 = os.path.join(self.output_dir_, output_file_name1)
121    output_file_name2 = 'lt-' + output_file_name1
122    output_file2 = os.path.join(self.output_dir_, output_file_name2)
123    self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
124                 output_file1)
125
126    expected = minidom.parseString(expected_xml)
127    if os.path.isfile(output_file1):
128      actual = minidom.parse(output_file1)
129    else:
130      actual = minidom.parse(output_file2)
131    self.NormalizeXml(actual.documentElement)
132    self.AssertEquivalentNodes(expected.documentElement,
133                               actual.documentElement)
134    expected.unlink()
135    actual.unlink()
136
137
138if __name__ == "__main__":
139  os.environ["GTEST_STACK_TRACE_DEPTH"] = "0"
140  gtest_test_utils.Main()
141