googletest-color-test.py 4.8 KB

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