00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 """Verifies that Google Test correctly determines whether to use colors."""
00033
00034 __author__ = 'wan@google.com (Zhanyong Wan)'
00035
00036 import os
00037 import gtest_test_utils
00038
00039
00040 IS_WINDOWS = os.name = 'nt'
00041
00042 COLOR_ENV_VAR = 'GTEST_COLOR'
00043 COLOR_FLAG = 'gtest_color'
00044 COMMAND = gtest_test_utils.GetTestExecutablePath('gtest_color_test_')
00045
00046
00047 def SetEnvVar(env_var, value):
00048 """Sets the env variable to 'value'; unsets it when 'value' is None."""
00049
00050 if value is not None:
00051 os.environ[env_var] = value
00052 elif env_var in os.environ:
00053 del os.environ[env_var]
00054
00055
00056 def UsesColor(term, color_env_var, color_flag):
00057 """Runs gtest_color_test_ and returns its exit code."""
00058
00059 SetEnvVar('TERM', term)
00060 SetEnvVar(COLOR_ENV_VAR, color_env_var)
00061
00062 if color_flag is None:
00063 args = []
00064 else:
00065 args = ['--%s=%s' % (COLOR_FLAG, color_flag)]
00066 p = gtest_test_utils.Subprocess([COMMAND] + args)
00067 return not p.exited or p.exit_code
00068
00069
00070 class GTestColorTest(gtest_test_utils.TestCase):
00071 def testNoEnvVarNoFlag(self):
00072 """Tests the case when there's neither GTEST_COLOR nor --gtest_color."""
00073
00074 if not IS_WINDOWS:
00075 self.assert_(not UsesColor('dumb', None, None))
00076 self.assert_(not UsesColor('emacs', None, None))
00077 self.assert_(not UsesColor('xterm-mono', None, None))
00078 self.assert_(not UsesColor('unknown', None, None))
00079 self.assert_(not UsesColor(None, None, None))
00080 self.assert_(UsesColor('linux', None, None))
00081 self.assert_(UsesColor('cygwin', None, None))
00082 self.assert_(UsesColor('xterm', None, None))
00083 self.assert_(UsesColor('xterm-color', None, None))
00084 self.assert_(UsesColor('xterm-256color', None, None))
00085
00086 def testFlagOnly(self):
00087 """Tests the case when there's --gtest_color but not GTEST_COLOR."""
00088
00089 self.assert_(not UsesColor('dumb', None, 'no'))
00090 self.assert_(not UsesColor('xterm-color', None, 'no'))
00091 if not IS_WINDOWS:
00092 self.assert_(not UsesColor('emacs', None, 'auto'))
00093 self.assert_(UsesColor('xterm', None, 'auto'))
00094 self.assert_(UsesColor('dumb', None, 'yes'))
00095 self.assert_(UsesColor('xterm', None, 'yes'))
00096
00097 def testEnvVarOnly(self):
00098 """Tests the case when there's GTEST_COLOR but not --gtest_color."""
00099
00100 self.assert_(not UsesColor('dumb', 'no', None))
00101 self.assert_(not UsesColor('xterm-color', 'no', None))
00102 if not IS_WINDOWS:
00103 self.assert_(not UsesColor('dumb', 'auto', None))
00104 self.assert_(UsesColor('xterm-color', 'auto', None))
00105 self.assert_(UsesColor('dumb', 'yes', None))
00106 self.assert_(UsesColor('xterm-color', 'yes', None))
00107
00108 def testEnvVarAndFlag(self):
00109 """Tests the case when there are both GTEST_COLOR and --gtest_color."""
00110
00111 self.assert_(not UsesColor('xterm-color', 'no', 'no'))
00112 self.assert_(UsesColor('dumb', 'no', 'yes'))
00113 self.assert_(UsesColor('xterm-color', 'no', 'auto'))
00114
00115 def testAliasesOfYesAndNo(self):
00116 """Tests using aliases in specifying --gtest_color."""
00117
00118 self.assert_(UsesColor('dumb', None, 'true'))
00119 self.assert_(UsesColor('dumb', None, 'YES'))
00120 self.assert_(UsesColor('dumb', None, 'T'))
00121 self.assert_(UsesColor('dumb', None, '1'))
00122
00123 self.assert_(not UsesColor('xterm', None, 'f'))
00124 self.assert_(not UsesColor('xterm', None, 'false'))
00125 self.assert_(not UsesColor('xterm', None, '0'))
00126 self.assert_(not UsesColor('xterm', None, 'unknown'))
00127
00128
00129 if __name__ == '__main__':
00130 gtest_test_utils.Main()