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
34import os
35from googletest.test import gtest_test_utils
36
37IS_WINDOWS = os.name == 'nt'
38
39COLOR_ENV_VAR = 'GTEST_COLOR'
40COLOR_FLAG = 'gtest_color'
41COMMAND = gtest_test_utils.GetTestExecutablePath('googletest-color-test_')
42
43
44def SetEnvVar(env_var, value):
45  """Sets the env variable to 'value'; unsets it when 'value' is None."""
46
47  if value is not None:
48    os.environ[env_var] = value
49  elif env_var in os.environ:
50    del os.environ[env_var]
51
52
53def UsesColor(term, color_env_var, color_flag):
54  """Runs googletest-color-test_ and returns its exit code."""
55
56  SetEnvVar('TERM', term)
57  SetEnvVar(COLOR_ENV_VAR, color_env_var)
58
59  if color_flag is None:
60    args = []
61  else:
62    args = ['--%s=%s' % (COLOR_FLAG, color_flag)]
63  p = gtest_test_utils.Subprocess([COMMAND] + args)
64  return not p.exited or p.exit_code
65
66
67class GTestColorTest(gtest_test_utils.TestCase):
68
69  def testNoEnvVarNoFlag(self):
70    """Tests the case when there's neither GTEST_COLOR nor --gtest_color."""
71
72    if not IS_WINDOWS:
73      self.assertTrue(not UsesColor('dumb', None, None))
74      self.assertTrue(not UsesColor('emacs', None, None))
75      self.assertTrue(not UsesColor('xterm-mono', None, None))
76      self.assertTrue(not UsesColor('unknown', None, None))
77      self.assertTrue(not UsesColor(None, None, None))
78    self.assertTrue(UsesColor('linux', None, None))
79    self.assertTrue(UsesColor('cygwin', None, None))
80    self.assertTrue(UsesColor('xterm', None, None))
81    self.assertTrue(UsesColor('xterm-color', None, None))
82    self.assertTrue(UsesColor('xterm-kitty', None, None))
83    self.assertTrue(UsesColor('xterm-256color', None, None))
84
85  def testFlagOnly(self):
86    """Tests the case when there's --gtest_color but not GTEST_COLOR."""
87
88    self.assertTrue(not UsesColor('dumb', None, 'no'))
89    self.assertTrue(not UsesColor('xterm-color', None, 'no'))
90    if not IS_WINDOWS:
91      self.assertTrue(not UsesColor('emacs', None, 'auto'))
92    self.assertTrue(UsesColor('xterm', None, 'auto'))
93    self.assertTrue(UsesColor('dumb', None, 'yes'))
94    self.assertTrue(UsesColor('xterm', None, 'yes'))
95
96  def testEnvVarOnly(self):
97    """Tests the case when there's GTEST_COLOR but not --gtest_color."""
98
99    self.assertTrue(not UsesColor('dumb', 'no', None))
100    self.assertTrue(not UsesColor('xterm-color', 'no', None))
101    if not IS_WINDOWS:
102      self.assertTrue(not UsesColor('dumb', 'auto', None))
103    self.assertTrue(UsesColor('xterm-color', 'auto', None))
104    self.assertTrue(UsesColor('dumb', 'yes', None))
105    self.assertTrue(UsesColor('xterm-color', 'yes', None))
106
107  def testEnvVarAndFlag(self):
108    """Tests the case when there are both GTEST_COLOR and --gtest_color."""
109
110    self.assertTrue(not UsesColor('xterm-color', 'no', 'no'))
111    self.assertTrue(UsesColor('dumb', 'no', 'yes'))
112    self.assertTrue(UsesColor('xterm-color', 'no', 'auto'))
113
114  def testAliasesOfYesAndNo(self):
115    """Tests using aliases in specifying --gtest_color."""
116
117    self.assertTrue(UsesColor('dumb', None, 'true'))
118    self.assertTrue(UsesColor('dumb', None, 'YES'))
119    self.assertTrue(UsesColor('dumb', None, 'T'))
120    self.assertTrue(UsesColor('dumb', None, '1'))
121
122    self.assertTrue(not UsesColor('xterm', None, 'f'))
123    self.assertTrue(not UsesColor('xterm', None, 'false'))
124    self.assertTrue(not UsesColor('xterm', None, '0'))
125    self.assertTrue(not UsesColor('xterm', None, 'unknown'))
126
127
128if __name__ == '__main__':
129  gtest_test_utils.Main()
130