CYCAMORE
generate_test_macros.py
Go to the documentation of this file.
1 """generate_test_macros.py
2 
3 A simple module and default main execution to generate a listing of
4 ADD_TEST CMake macros for all non-disabled tests in a
5 google-test-based executable.
6 
7 The default main function writes a list of macros to the given output
8 file.
9 """
10 from __future__ import print_function
11 
12 import os
13 import subprocess
14 import sys
15 
16 try:
17  import argparse as ap
18 except ImportError:
19  import pyne._argparse as ap
20 
21 def parse_tests(test_lines):
22  """Return a list of google test names.
23 
24  Arguments:
25 
26  test_lines -- a list of the output of a google test exectuable
27  using the --gtest_list_tests flag. If the output is in a file,
28  test_lines are the result of file.readlines().
29  """
30  tests = []
31  current_test = None
32  for test_line in test_lines:
33  line = test_line.decode().strip()
34  if line[-1] == ".":
35  current_test = line
36  else:
37  assert current_test is not None
38  if str(line).lower().find("disabled") == -1:
39  tests.append(current_test + line)
40  return tests
41 
42 def write_macros_to_output(tests, executable, reg_dir, output=None):
43  """writes a list of test names as ADD_TEST cmake macros to an
44  output file
45 
46  Arguments
47  tests -- a list of all test names to be added as ADD_TEST macros
48  to the output file
49  exectuable -- the name of the test executable
50  output -- the output file to write to, if output is not
51  specified, the list of ADD_TEST macros will be written to stdout
52  """
53  lines = []
54  # add unit tests
55  for test in tests:
56  lines.append("ADD_TEST(" + test + " " + \
57  executable + " " + "--gtest_filter=" + test + ")")
58  # add regression tests
59  lines.append("ADD_TEST(RegressionTests nosetests -v -w " + \
60  reg_dir + ")")
61 
62  if output is None:
63  for line in lines:
64  print(line)
65  else:
66  with open(output, 'a') as f:
67  for line in lines:
68  f.write(line + '\n')
69 
70 def main():
71  description = "A simple script to add CTest ADD_TEST macros to a "+\
72  "file for every test in a google-test executable."
73  parser = ap.ArgumentParser(description=description)
74 
75  executable = 'the path to the test exectuable to call'
76  parser.add_argument('--executable', help=executable, required=True)
77 
78  reg_dir = "the regression tests directory"
79  parser.add_argument('--reg_dir', help=reg_dir, required=True)
80 
81  output = "the file to write the ADD_TEST macros to "+\
82  "(nominally CTestTestfile.cmake)"
83  parser.add_argument('--output', help=output, required=True)
84 
85  args = parser.parse_args()
86 
87  assert os.path.exists(args.executable)
88  assert os.path.exists(args.output)
89 
90  rtn = subprocess.Popen([args.executable, "--gtest_list_tests"],
91  stdout=subprocess.PIPE, shell=(os.name=='nt'))
92  rtn.wait()
93  if rtn.returncode != 0:
94  raise OSError('Could not generate test list, return code: '
95  + str(rtn.returncode) + '.')
96 
97  tests = parse_tests(rtn.stdout.readlines())
98  rtn.stdout.close()
99 
100  write_macros_to_output(tests, args.executable, args.reg_dir, args.output)
101 
102  return 0
103 
104 if __name__ == "__main__":
105  sys.exit(main())
def write_macros_to_output(tests, executable, reg_dir, output=None)
def parse_tests(test_lines)