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 parses environment variables.""" 33 34__author__ = 'wan@google.com (Zhanyong Wan)' 35 36import gtest_test_utils 37import os 38import sys 39import unittest 40 41IS_WINDOWS = os.name == 'nt' 42IS_LINUX = os.name == 'posix' 43 44if IS_WINDOWS: 45 BUILD_DIRS = [ 46 'build.dbg\\', 47 'build.opt\\', 48 'build.dbg8\\', 49 'build.opt8\\', 50 ] 51 COMMAND = 'gtest_env_var_test_.exe' 52 53if IS_LINUX: 54 COMMAND = os.path.join(gtest_test_utils.GetBuildDir(), 55 'gtest_env_var_test_') 56 57 58def AssertEq(expected, actual): 59 if expected != actual: 60 print 'Expected: %s' % (expected,) 61 print ' Actual: %s' % (actual,) 62 raise AssertionError 63 64 65def SetEnvVar(env_var, value): 66 """Sets the env variable to 'value'; unsets it when 'value' is None.""" 67 68 if value is not None: 69 os.environ[env_var] = value 70 elif env_var in os.environ: 71 del os.environ[env_var] 72 73 74def GetFlag(command, flag): 75 """Runs gtest_env_var_test_ and returns its output.""" 76 77 cmd = command 78 if flag is not None: 79 cmd += ' %s' % (flag,) 80 stdin, stdout = os.popen2(cmd, 'b') 81 stdin.close() 82 line = stdout.readline() 83 stdout.close() 84 return line 85 86 87def TestFlag(command, flag, test_val, default_val): 88 """Verifies that the given flag is affected by the corresponding env var.""" 89 90 env_var = 'GTEST_' + flag.upper() 91 SetEnvVar(env_var, test_val) 92 AssertEq(test_val, GetFlag(command, flag)) 93 SetEnvVar(env_var, None) 94 AssertEq(default_val, GetFlag(command, flag)) 95 96 97def TestEnvVarAffectsFlag(command): 98 """An environment variable should affect the corresponding flag.""" 99 100 TestFlag(command, 'break_on_failure', '1', '0') 101 TestFlag(command, 'color', 'yes', 'auto') 102 TestFlag(command, 'filter', 'FooTest.Bar', '*') 103 TestFlag(command, 'output', 'tmp/foo.xml', '') 104 TestFlag(command, 'print_time', '1', '0') 105 TestFlag(command, 'repeat', '999', '1') 106 TestFlag(command, 'throw_on_failure', '1', '0') 107 108 if IS_WINDOWS: 109 TestFlag(command, 'catch_exceptions', '1', '0') 110 if IS_LINUX: 111 TestFlag(command, 'stack_trace_depth', '0', '100') 112 TestFlag(command, 'death_test_style', 'thread-safe', 'fast') 113 TestFlag(command, 'death_test_use_fork', '1', '0') 114 115 116if IS_WINDOWS: 117 118 def main(): 119 for build_dir in BUILD_DIRS: 120 command = build_dir + COMMAND 121 print 'Testing with %s . . .' % (command,) 122 TestEnvVarAffectsFlag(command) 123 return 0 124 125 if __name__ == '__main__': 126 main() 127 128 129if IS_LINUX: 130 131 class GTestEnvVarTest(unittest.TestCase): 132 def testEnvVarAffectsFlag(self): 133 TestEnvVarAffectsFlag(COMMAND) 134 135 136 if __name__ == '__main__': 137 gtest_test_utils.Main() 138