test/gensuitemaker.py
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 18 Nov 2019 15:34:21 +0100
changeset 4205 f75e9274551f
parent 2941 23726372aea0
child 3249 03119f76ea35
permissions -rw-r--r--
Ugh. Old API.
     1 # This file is under GNU General Public License 3.0
     2 # see LICENSE.txt
     3 import argparse
     4 from os import listdir, getcwd
     5 from os.path import isfile, join
     6 from re import sub
     7 import datetime
     8 
     9 srcpath = join(getcwd(), "src/engine_tests")
    10 
    11 ls = listdir(srcpath)
    12 
    13 suites = []
    14 
    15 indent = "    "
    16 
    17 parser = argparse.ArgumentParser()
    18 parser.add_argument("--exclude", "-x", help="Comma-separated list of tests to exclude from test suite build")
    19 
    20 args = parser.parse_args()
    21 
    22 exclude_arg = args.exclude
    23 exclude_list = []
    24 if exclude_arg:
    25     exclude_list = exclude_arg.split(",")
    26     for x in exclude_list:
    27         x.replace(" ","")
    28 for f in ls:
    29     if isfile(join(srcpath, f)):
    30         if (f.endswith(".cc")):
    31             suite = sub('\.cc$', '', f)
    32             if not suite in exclude_list:
    33                 suites.append(suite)
    34 
    35 license = (''
    36 '// This file is under GNU General Public License 3.0\n'
    37 '// see LICENSE.txt\n\n')
    38 
    39 
    40 header = license + "//\n// src/SuiteMaker.cc generated by gensuitemaker.py - changes may be overwritten. You've been warned!\n//\n\n"
    41 
    42 header += (''
    43 '#include <cpptest.h>\n'
    44 '#include <cpptest-suite.h>\n'
    45 '#include <memory>\n'
    46 '#include <vector>\n'
    47 '#include "SuiteMaker.h"\n')
    48 
    49 suiteincludes = ""
    50 
    51 for s in suites:
    52     suiteincludes += '#include "' + s + '.h"\n'
    53 
    54 output = header + "\n// Begin where we generate stuff\n" + suiteincludes + "\n"
    55 
    56 output += ("\n"
    57 "const char* SuiteMaker::all_suites[] = {\n")
    58 
    59 first = True
    60 
    61 for s in suites:
    62     output += indent + '"' + s + '",\n'
    63 
    64 output += "};\n\n"
    65 
    66 output += (
    67 '// This file is generated, so magic constants are ok.\n'
    68 'int SuiteMaker::num_suites = ')
    69 
    70 output += str(len(suites)) + ";\n\n"
    71     
    72 output += "void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {\n"
    73 
    74 first = True
    75 
    76 for s in suites:
    77     output += indent
    78     
    79     if not first:
    80         output += "else "
    81     else:
    82         first = False
    83     
    84     output += ('if (strcmp(test_class_name, "' + s + '") == 0)\n' + 
    85                indent + indent + '*test_suite = new ' + s + '(test_class_name, test_home);\n')
    86         
    87 output += "}\n\n"
    88 
    89 output += (
    90 'void SuiteMaker::suitemaker_buildlist(const char** test_class_names, int num_to_run, const char* test_home, std::vector<Test::Suite*>& test_suites) {\n'
    91 '    for (int i = 0; i < num_to_run; i++) {\n'
    92 '        Test::Suite* suite = NULL;\n'
    93 '        SuiteMaker::suitemaker_build(test_class_names[i], test_home, &suite);\n'
    94 '        if (!suite)\n'
    95 '            throw std::runtime_error("Could not create a test suite instance."); // FIXME, better error, cleanup, obviously\n'
    96 '        test_suites.push_back(suite);\n'
    97 '    }\n'    
    98 '}'
    99 '\n'
   100 'void SuiteMaker::suitemaker_buildall(const char* test_home, std::vector<Test::Suite*>& test_suites) {\n'
   101 '    SuiteMaker::suitemaker_buildlist(SuiteMaker::all_suites, SuiteMaker::num_suites, test_home, test_suites);\n'
   102 '}\n\n')
   103 
   104 suitemaker = open("src/SuiteMaker.cc", 'w') 
   105 suitemaker.write(output)
   106 suitemaker.close()
   107 
   108 generated_at = ("src/SuiteMaker.cc" + " generated at " + datetime.datetime.now().strftime('%c'))
   109 print(generated_at)