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"""Verifies that Google Test warns the user when not initialized properly."""
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania__author__ = 'wan@google.com (Zhanyong Wan)'
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport gtest_test_utils
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport os
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport sys
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport unittest
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaIS_WINDOWS = os.name == 'nt'
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaIS_LINUX = os.name == 'posix'
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif IS_WINDOWS:
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  BUILD_DIRS = [
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      'build.dbg\\',
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      'build.opt\\',
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      'build.dbg8\\',
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      'build.opt8\\',
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ]
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  COMMAND = 'gtest_uninitialized_test_.exe'
521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif IS_LINUX:
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  COMMAND = os.path.join(gtest_test_utils.GetBuildDir(),
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                         'gtest_uninitialized_test_')
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef Assert(condition):
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if not condition:
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    raise AssertionError
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef AssertEq(expected, actual):
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if expected != actual:
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    print 'Expected: %s' % (expected,)
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    print '  Actual: %s' % (actual,)
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    raise AssertionError
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef TestExitCodeAndOutput(command):
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Runs the given command and verifies its exit code and output."""
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  # Verifies that 'command' exits with code 1.
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  p = gtest_test_utils.Subprocess(command)
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Assert(p.exited)
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  AssertEq(1, p.exit_code)
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Assert('InitGoogleTest' in p.output)
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif IS_WINDOWS:
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def main():
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for build_dir in BUILD_DIRS:
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      command = build_dir + COMMAND
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      print 'Testing with %s . . .' % (command,)
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      TestExitCodeAndOutput(command)
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return 0
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if __name__ == '__main__':
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    main()
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif IS_LINUX:
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  class GTestUninitializedTest(unittest.TestCase):
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    def testExitCodeAndOutput(self):
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      TestExitCodeAndOutput(COMMAND)
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if __name__ == '__main__':
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    gtest_test_utils.Main()
102