1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#!/usr/bin/env python
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# Copyright 2009, Google Inc.
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# All rights reserved.
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# Redistribution and use in source and binary forms, with or without
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# modification, are permitted provided that the following conditions are
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# met:
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#     * Redistributions of source code must retain the above copyright
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# notice, this list of conditions and the following disclaimer.
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#     * Redistributions in binary form must reproduce the above
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# copyright notice, this list of conditions and the following disclaimer
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# in the documentation and/or other materials provided with the
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# distribution.
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#     * Neither the name of Google Inc. nor the names of its
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# contributors may be used to endorse or promote products derived from
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# this software without specific prior written permission.
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville"""Tests the --help flag of Google C++ Testing Framework.
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleSYNOPSIS
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville       gtest_help_test.py --gtest_build_dir=BUILD/DIR
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville         # where BUILD/DIR contains the built gtest_help_test_ file.
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville       gtest_help_test.py
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville"""
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville__author__ = 'wan@google.com (Zhanyong Wan)'
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleimport os
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleimport re
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleimport gtest_test_utils
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleIS_WINDOWS = os.name == 'nt'
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavillePROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleFLAG_PREFIX = '--gtest_'
51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleCATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions'
52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleDEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville# The help message must match this regex.
55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleHELP_REGEX = re.compile(
56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'list_tests.*' +
57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'filter=.*' +
58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'also_run_disabled_tests.*' +
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'repeat=.*' +
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'shuffle.*' +
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'random_seed=.*' +
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'color=.*' +
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'print_time.*' +
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'output=.*' +
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'break_on_failure.*' +
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FLAG_PREFIX + r'throw_on_failure.*',
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    re.DOTALL)
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilledef RunWithFlag(flag):
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  """Runs gtest_help_test_ with the given flag.
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  Returns:
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    the exit code and the text output as a tuple.
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  Args:
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    flag: the command-line flag to pass to gtest_help_test_, or None.
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  """
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if flag is None:
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    command = [PROGRAM_PATH]
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  else:
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    command = [PROGRAM_PATH, flag]
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  child = gtest_test_utils.Subprocess(command)
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return child.exit_code, child.output
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass GTestHelpTest(gtest_test_utils.TestCase):
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  """Tests the --help flag and its equivalent forms."""
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  def TestHelpFlag(self, flag):
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    """Verifies that the right message is printed and the tests are
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    skipped when the given flag is specified."""
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    exit_code, output = RunWithFlag(flag)
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.assertEquals(0, exit_code)
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.assert_(HELP_REGEX.search(output), output)
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if IS_WINDOWS:
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      self.assert_(CATCH_EXCEPTIONS_FLAG in output, output)
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      self.assert_(DEATH_TEST_STYLE_FLAG not in output, output)
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    else:
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      self.assert_(CATCH_EXCEPTIONS_FLAG not in output, output)
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  def testPrintsHelpWithFullFlag(self):
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.TestHelpFlag('--help')
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  def testPrintsHelpWithShortFlag(self):
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.TestHelpFlag('-h')
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  def testPrintsHelpWithQuestionFlag(self):
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.TestHelpFlag('-?')
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  def testPrintsHelpWithWindowsStyleQuestionFlag(self):
114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.TestHelpFlag('/?')
115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  def testRunsTestsWithoutHelpFlag(self):
117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    """Verifies that when no help flag is specified, the tests are run
118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    and the help message is not printed."""
119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    exit_code, output = RunWithFlag(None)
121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.assert_(exit_code != 0)
122fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    self.assert_(not HELP_REGEX.search(output), output)
123fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
124fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
125fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleif __name__ == '__main__':
126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  gtest_test_utils.Main()
127