OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2006, Google Inc. | 3 # Copyright 2006, Google Inc. |
4 # All rights reserved. | 4 # All rights reserved. |
5 # | 5 # |
6 # Redistribution and use in source and binary forms, with or without | 6 # Redistribution and use in source and binary forms, with or without |
7 # modification, are permitted provided that the following conditions are | 7 # modification, are permitted provided that the following conditions are |
8 # met: | 8 # met: |
9 # | 9 # |
10 # * Redistributions of source code must retain the above copyright | 10 # * Redistributions of source code must retain the above copyright |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test') | 44 gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test') |
45 if os.path.isdir(gtest_tests_util_dir): | 45 if os.path.isdir(gtest_tests_util_dir): |
46 GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir | 46 GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir |
47 else: | 47 else: |
48 GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test') | 48 GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test') |
49 | 49 |
50 sys.path.append(GTEST_TESTS_UTIL_DIR) | 50 sys.path.append(GTEST_TESTS_UTIL_DIR) |
51 import gtest_test_utils # pylint: disable-msg=C6204 | 51 import gtest_test_utils # pylint: disable-msg=C6204 |
52 | 52 |
53 | 53 |
54 # Initially maps a flag to its default value. After | |
55 # _ParseAndStripGMockFlags() is called, maps a flag to its actual | |
56 # value. | |
57 _flag_map = {'gmock_source_dir': os.path.dirname(sys.argv[0]), | |
58 'gmock_build_dir': os.path.dirname(sys.argv[0])} | |
59 _gmock_flags_are_parsed = False | |
60 | |
61 | |
62 def _ParseAndStripGMockFlags(argv): | |
63 """Parses and strips Google Test flags from argv. This is idempotent.""" | |
64 | |
65 global _gmock_flags_are_parsed | |
66 if _gmock_flags_are_parsed: | |
67 return | |
68 | |
69 _gmock_flags_are_parsed = True | |
70 for flag in _flag_map: | |
71 # The environment variable overrides the default value. | |
72 if flag.upper() in os.environ: | |
73 _flag_map[flag] = os.environ[flag.upper()] | |
74 | |
75 # The command line flag overrides the environment variable. | |
76 i = 1 # Skips the program name. | |
77 while i < len(argv): | |
78 prefix = '--' + flag + '=' | |
79 if argv[i].startswith(prefix): | |
80 _flag_map[flag] = argv[i][len(prefix):] | |
81 del argv[i] | |
82 break | |
83 else: | |
84 # We don't increment i in case we just found a --gmock_* flag | |
85 # and removed it from argv. | |
86 i += 1 | |
87 | |
88 | |
89 def GetFlag(flag): | |
90 """Returns the value of the given flag.""" | |
91 | |
92 # In case GetFlag() is called before Main(), we always call | |
93 # _ParseAndStripGMockFlags() here to make sure the --gmock_* flags | |
94 # are parsed. | |
95 _ParseAndStripGMockFlags(sys.argv) | |
96 | |
97 return _flag_map[flag] | |
98 | |
99 | |
100 def GetSourceDir(): | 54 def GetSourceDir(): |
101 """Returns the absolute path of the directory where the .py files are.""" | 55 """Returns the absolute path of the directory where the .py files are.""" |
102 | 56 |
103 return os.path.abspath(GetFlag('gmock_source_dir')) | 57 return gtest_test_utils.GetSourceDir() |
104 | |
105 | |
106 def GetBuildDir(): | |
107 """Returns the absolute path of the directory where the test binaries are.""" | |
108 | |
109 return os.path.abspath(GetFlag('gmock_build_dir')) | |
110 | 58 |
111 | 59 |
112 def GetTestExecutablePath(executable_name): | 60 def GetTestExecutablePath(executable_name): |
113 """Returns the absolute path of the test binary given its name. | 61 """Returns the absolute path of the test binary given its name. |
114 | 62 |
115 The function will print a message and abort the program if the resulting file | 63 The function will print a message and abort the program if the resulting file |
116 doesn't exist. | 64 doesn't exist. |
117 | 65 |
118 Args: | 66 Args: |
119 executable_name: name of the test binary that the test script runs. | 67 executable_name: name of the test binary that the test script runs. |
120 | 68 |
121 Returns: | 69 Returns: |
122 The absolute path of the test binary. | 70 The absolute path of the test binary. |
123 """ | 71 """ |
124 | 72 |
125 return gtest_test_utils.GetTestExecutablePath(executable_name, GetBuildDir()) | 73 return gtest_test_utils.GetTestExecutablePath(executable_name) |
126 | 74 |
127 | 75 |
128 def GetExitStatus(exit_code): | 76 def GetExitStatus(exit_code): |
129 """Returns the argument to exit(), or -1 if exit() wasn't called. | 77 """Returns the argument to exit(), or -1 if exit() wasn't called. |
130 | 78 |
131 Args: | 79 Args: |
132 exit_code: the result value of os.system(command). | 80 exit_code: the result value of os.system(command). |
133 """ | 81 """ |
134 | 82 |
135 if os.name == 'nt': | 83 if os.name == 'nt': |
(...skipping 17 matching lines...) Expand all Loading... |
153 | 101 |
154 # Exposes TestCase from gtest_test_utils. | 102 # Exposes TestCase from gtest_test_utils. |
155 TestCase = gtest_test_utils.TestCase | 103 TestCase = gtest_test_utils.TestCase |
156 | 104 |
157 # pylint: enable-msg=C6409 | 105 # pylint: enable-msg=C6409 |
158 | 106 |
159 | 107 |
160 def Main(): | 108 def Main(): |
161 """Runs the unit test.""" | 109 """Runs the unit test.""" |
162 | 110 |
163 # We must call _ParseAndStripGMockFlags() before calling | |
164 # gtest_test_utils.Main(). Otherwise unittest.main it calls will be | |
165 # confused by the --gmock_* flags. | |
166 _ParseAndStripGMockFlags(sys.argv) | |
167 gtest_test_utils.Main() | 111 gtest_test_utils.Main() |
OLD | NEW |