11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#!/usr/bin/env python
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Copyright 2008, Google Inc.
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# All rights reserved.
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Redistribution and use in source and binary forms, with or without
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# modification, are permitted provided that the following conditions are
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# met:
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions of source code must retain the above copyright
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# notice, this list of conditions and the following disclaimer.
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions in binary form must reproduce the above
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# copyright notice, this list of conditions and the following disclaimer
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# in the documentation and/or other materials provided with the
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# distribution.
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Neither the name of Google Inc. nor the names of its
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# contributors may be used to endorse or promote products derived from
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# this software without specific prior written permission.
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""Unit test for the gtest_xml_output module."""
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania__author__ = "keith.ray@gmail.com (Keith Ray)"
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport os
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniafrom xml.dom import minidom, Node
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotimport gtest_test_utils
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport gtest_xml_test_utils
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_OUTPUT_SUBDIR = "xml_outfiles"
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_OUTPUT_1_TEST = "gtest_xml_outfile1_test_"
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_OUTPUT_2_TEST = "gtest_xml_outfile2_test_"
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaEXPECTED_XML_1 = """<?xml version="1.0" encoding="UTF-8"?>
48fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes<testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  <testsuite name="PropertyOne" tests="1" failures="0" disabled="0" errors="0" time="*">
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="TestSomeProperties" status="run" time="*" classname="PropertyOne" SetUpProp="1" TestSomeProperty="1" TearDownProp="1" />
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot</testsuites>
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaEXPECTED_XML_2 = """<?xml version="1.0" encoding="UTF-8"?>
56fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes<testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  <testsuite name="PropertyTwo" tests="1" failures="0" disabled="0" errors="0" time="*">
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="TestSomeProperties" status="run" time="*" classname="PropertyTwo" SetUpProp="2" TestSomeProperty="2" TearDownProp="2" />
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
6041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot</testsuites>
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase):
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Unit test for Google Test's XML output functionality."""
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def setUp(self):
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # We want the trailing '/' that the last "" provides in os.path.join, for
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # telling Google Test to create an output directory instead of a single file
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # for xml output.
7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
7241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                    GTEST_OUTPUT_SUBDIR, "")
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.DeleteFilesAndDir()
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def tearDown(self):
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.DeleteFilesAndDir()
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def DeleteFilesAndDir(self):
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    try:
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + ".xml"))
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    except os.error:
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      pass
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    try:
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + ".xml"))
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    except os.error:
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      pass
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    try:
8841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      os.rmdir(self.output_dir_)
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    except os.error:
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      pass
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testOutfile1(self):
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_XML_1)
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testOutfile2(self):
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_XML_2)
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def _TestOutFile(self, test_name, expected_xml):
9941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_]
10141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    p = gtest_test_utils.Subprocess(command,
10241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                    working_dir=gtest_test_utils.GetTempDir())
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.assert_(p.exited)
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.assertEquals(0, p.exit_code)
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # TODO(wan@google.com): libtool causes the built test binary to be
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    #   named lt-gtest_xml_outfiles_test_ instead of
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    #   gtest_xml_outfiles_test_.  To account for this possibillity, we
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    #   allow both names in the following code.  We should remove this
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    #   hack when Chandler Carruth's libtool replacement tool is ready.
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    output_file_name1 = test_name + ".xml"
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    output_file1 = os.path.join(self.output_dir_, output_file_name1)
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    output_file_name2 = 'lt-' + output_file_name1
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    output_file2 = os.path.join(self.output_dir_, output_file_name2)
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                 output_file1)
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    expected = minidom.parseString(expected_xml)
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if os.path.isfile(output_file1):
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      actual = minidom.parse(output_file1)
1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else:
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      actual = minidom.parse(output_file2)
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.NormalizeXml(actual.documentElement)
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.AssertEquivalentNodes(expected.documentElement,
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               actual.documentElement)
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    expected.unlink()
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    actual.unlink()
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif __name__ == "__main__":
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  os.environ["GTEST_STACK_TRACE_DEPTH"] = "0"
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  gtest_test_utils.Main()
133