• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Verifies that Google Test correctly determines whether to use colors."""
33
34__author__ = 'wan@google.com (Zhanyong Wan)'
35
36import gtest_test_utils
37import os
38import sys
39import unittest
40
41
42COLOR_ENV_VAR = 'GTEST_COLOR'
43COLOR_FLAG = 'gtest_color'
44COMMAND = os.path.join(gtest_test_utils.GetBuildDir(),
45                       'gtest_color_test_')
46
47
48def SetEnvVar(env_var, value):
49  """Sets the env variable to 'value'; unsets it when 'value' is None."""
50
51  if value is not None:
52    os.environ[env_var] = value
53  elif env_var in os.environ:
54    del os.environ[env_var]
55
56
57def UsesColor(term, color_env_var, color_flag):
58  """Runs gtest_color_test_ and returns its exit code."""
59
60  SetEnvVar('TERM', term)
61  SetEnvVar(COLOR_ENV_VAR, color_env_var)
62  cmd = COMMAND
63  if color_flag is not None:
64    cmd += ' --%s=%s' % (COLOR_FLAG, color_flag)
65  return gtest_test_utils.GetExitStatus(os.system(cmd))
66
67
68class GTestColorTest(unittest.TestCase):
69  def testNoEnvVarNoFlag(self):
70    """Tests the case when there's neither GTEST_COLOR nor --gtest_color."""
71
72    self.assert_(not UsesColor('dumb', None, None))
73    self.assert_(not UsesColor('emacs', None, None))
74    self.assert_(not UsesColor('xterm-mono', None, None))
75    self.assert_(not UsesColor('unknown', None, None))
76    self.assert_(not UsesColor(None, None, None))
77    self.assert_(UsesColor('cygwin', None, None))
78    self.assert_(UsesColor('xterm', None, None))
79    self.assert_(UsesColor('xterm-color', None, None))
80
81  def testFlagOnly(self):
82    """Tests the case when there's --gtest_color but not GTEST_COLOR."""
83
84    self.assert_(not UsesColor('dumb', None, 'no'))
85    self.assert_(not UsesColor('xterm-color', None, 'no'))
86    self.assert_(not UsesColor('emacs', None, 'auto'))
87    self.assert_(UsesColor('xterm', None, 'auto'))
88    self.assert_(UsesColor('dumb', None, 'yes'))
89    self.assert_(UsesColor('xterm', None, 'yes'))
90
91  def testEnvVarOnly(self):
92    """Tests the case when there's GTEST_COLOR but not --gtest_color."""
93
94    self.assert_(not UsesColor('dumb', 'no', None))
95    self.assert_(not UsesColor('xterm-color', 'no', None))
96    self.assert_(not UsesColor('dumb', 'auto', None))
97    self.assert_(UsesColor('xterm-color', 'auto', None))
98    self.assert_(UsesColor('dumb', 'yes', None))
99    self.assert_(UsesColor('xterm-color', 'yes', None))
100
101  def testEnvVarAndFlag(self):
102    """Tests the case when there are both GTEST_COLOR and --gtest_color."""
103
104    self.assert_(not UsesColor('xterm-color', 'no', 'no'))
105    self.assert_(UsesColor('dumb', 'no', 'yes'))
106    self.assert_(UsesColor('xterm-color', 'no', 'auto'))
107
108  def testAliasesOfYesAndNo(self):
109    """Tests using aliases in specifying --gtest_color."""
110
111    self.assert_(UsesColor('dumb', None, 'true'))
112    self.assert_(UsesColor('dumb', None, 'YES'))
113    self.assert_(UsesColor('dumb', None, 'T'))
114    self.assert_(UsesColor('dumb', None, '1'))
115
116    self.assert_(not UsesColor('xterm', None, 'f'))
117    self.assert_(not UsesColor('xterm', None, 'false'))
118    self.assert_(not UsesColor('xterm', None, '0'))
119    self.assert_(not UsesColor('xterm', None, 'unknown'))
120
121
122if __name__ == '__main__':
123  gtest_test_utils.Main()
124