32 """fuse_gtest_files.py v0.2.0 33 Fuses Google Test source code into a .h file and a .cc file. 36 fuse_gtest_files.py [GTEST_ROOT_DIR] OUTPUT_DIR 38 Scans GTEST_ROOT_DIR for Google Test source code, and generates 39 two files: OUTPUT_DIR/gtest/gtest.h and OUTPUT_DIR/gtest/gtest-all.cc. 40 Then you can build your tests by adding OUTPUT_DIR to the include 41 search path and linking with OUTPUT_DIR/gtest/gtest-all.cc. These 42 two files contain everything you need to use Google Test. Hence 43 you can "install" Google Test by copying them to wherever you want. 45 GTEST_ROOT_DIR can be omitted and defaults to the parent 46 directory of the directory holding this script. 49 ./fuse_gtest_files.py fused_gtest 50 ./fuse_gtest_files.py path/to/unpacked/gtest fused_gtest 52 This tool is experimental. In particular, it assumes that there is no 53 conditional inclusion of Google Test headers. Please report any 54 problems to googletestframework@googlegroups.com. You can read 55 http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for 59 __author__ =
'wan@google.com (Zhanyong Wan)' 67 DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__),
'..')
70 INCLUDE_GTEST_FILE_REGEX = re.compile(
r'^\s*#\s*include\s*"(gtest/.+)"')
73 INCLUDE_SRC_FILE_REGEX = re.compile(
r'^\s*#\s*include\s*"(src/.+)"')
76 GTEST_H_SEED =
'include/gtest/gtest.h' 77 GTEST_SPI_H_SEED =
'include/gtest/gtest-spi.h' 78 GTEST_ALL_CC_SEED =
'src/gtest-all.cc' 81 GTEST_H_OUTPUT =
'gtest/gtest.h' 82 GTEST_ALL_CC_OUTPUT =
'gtest/gtest-all.cc' 86 """Verifies that the given file exists; aborts on failure. 88 relative_path is the file path relative to the given directory. 91 if not os.path.isfile(os.path.join(directory, relative_path)):
92 print((
'ERROR: Cannot find %s in directory %s.' % (relative_path,
94 print (
'Please either specify a valid project root directory ' 95 'or omit it on the command line.')
100 """Makes sure gtest_root points to a valid gtest root directory. 102 The function aborts the program on failure. 110 """Verifies that the given output file path is valid. 112 relative_path is relative to the output_dir directory. 116 output_file = os.path.join(output_dir, relative_path)
117 if os.path.exists(output_file):
121 print((
'%s already exists in directory %s - overwrite it? (y/N) ' %
122 (relative_path, output_dir)))
123 answer = sys.stdin.readline().strip()
124 if answer
not in [
'y',
'Y']:
130 parent_directory = os.path.dirname(output_file)
131 if not os.path.isdir(parent_directory):
132 os.makedirs(parent_directory)
136 """Makes sure output_dir points to a valid output directory. 138 The function aborts the program on failure. 146 """Scans folder gtest_root to generate gtest/gtest.h in output_dir.""" 148 output_file = open(os.path.join(output_dir, GTEST_H_OUTPUT),
'w')
149 processed_files =
set()
151 def ProcessFile(gtest_header_path):
152 """Processes the given gtest header file.""" 155 if gtest_header_path
in processed_files:
158 processed_files.add(gtest_header_path)
161 for line
in open(os.path.join(gtest_root, gtest_header_path),
'r'): 162 m = INCLUDE_GTEST_FILE_REGEX.match(line) 165 ProcessFile(
'include/' + m.group(1))
168 output_file.write(line)
170 ProcessFile(GTEST_H_SEED)
175 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file.""" 177 processed_files =
set()
179 def ProcessFile(gtest_source_file):
180 """Processes the given gtest source file.""" 183 if gtest_source_file
in processed_files:
186 processed_files.add(gtest_source_file)
189 for line
in open(os.path.join(gtest_root, gtest_source_file),
'r'): 190 m = INCLUDE_GTEST_FILE_REGEX.match(line) 192 if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
195 ProcessFile(GTEST_SPI_H_SEED)
203 if not GTEST_H_SEED
in processed_files:
204 processed_files.add(GTEST_H_SEED)
205 output_file.write(
'#include "%s"\n' % (GTEST_H_OUTPUT,))
207 m = INCLUDE_SRC_FILE_REGEX.match(line)
210 ProcessFile(m.group(1))
212 output_file.write(line)
214 ProcessFile(GTEST_ALL_CC_SEED)
218 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir.""" 220 output_file = open(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT),
'w')
226 """Fuses gtest.h and gtest-all.cc.""" 239 FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
248 if __name__ ==
'__main__':
def VerifyOutputFile(output_dir, relative_path)
def FuseGTestH(gtest_root, output_dir)
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
def VerifyFileExists(directory, relative_path)
def FuseGTestAllCc(gtest_root, output_dir)
def FuseGTest(gtest_root, output_dir)
def ValidateOutputDir(output_dir)
def FuseGTestAllCcToFile(gtest_root, output_file)
def ValidateGTestRootDir(gtest_root)