Last active
August 29, 2015 13:55
-
-
Save jimmythompson/8773052 to your computer and use it in GitHub Desktop.
Test suite for Software Architectures compiler project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse as ap | |
| import difflib as diff | |
| import os | |
| import os.path as path | |
| import subprocess as sp | |
| import sys | |
| class Settings(object): | |
| CONFIG_FILE_PATH = ".testconfig" | |
| ACTIONS = { "scan": "scanner", "parse": "parser", "inter": "semantics", "assembly": "codegen" } | |
| COMPILER_PATH = path.join( | |
| path.dirname(path.realpath(__file__)), "dist", "Compiler.jar") | |
| SOURCE_PATH = "src" | |
| def compare_text(first, second): | |
| if first != second: | |
| print file_name | |
| differences = list(diff.Differ().compare( | |
| to_line_list(second), to_line_list(first))) | |
| print "\n".join(differences) | |
| def get_text_from_file(file_name): | |
| output = "" | |
| with open(file_name, 'r') as file_contents: | |
| output = "".join(file_contents.readlines()) | |
| return output | |
| def get_file_names(root_directory): | |
| file_list = [] | |
| for root, _, file_names in os.walk(root_directory): | |
| file_list = file_list + [path.join(root, file_name) for file_name in file_names] | |
| return file_list | |
| def touch_file(file_name): | |
| with file(file_name, 'a'): | |
| os.utime(file_name, None) | |
| return file_name | |
| def to_line_list(s): | |
| return s.split("\n") | |
| def get_path_from_user(): | |
| while(True): | |
| test_path = raw_input("Where is the folder with the test files inside?: ") | |
| if path.isdir(test_path): | |
| return test_path | |
| def get_test_path(): | |
| if path.isfile(Settings.CONFIG_FILE_PATH): | |
| with open(Settings.CONFIG_FILE_PATH, 'r') as config_file: | |
| test_path = config_file.readline() | |
| if path.isdir(test_path): | |
| return test_path | |
| test_path = get_path_from_user() | |
| with open(Settings.CONFIG_FILE_PATH, 'w+') as config_file: | |
| config_file.write(test_path) | |
| return test_path | |
| def is_source_file(file_name): | |
| return path.isfile(file_name) and (file_name.endswith(".java") or file_name.endswith(".g")) | |
| def get_command(action, file_name, debug): | |
| if debug: | |
| return ["java", "-jar", Settings.COMPILER_PATH, "-debug", "-target", action, file_name] | |
| return ["java", "-jar", Settings.COMPILER_PATH, "-target", action, file_name] | |
| def get_output(action, file_name, debug): | |
| command = get_command(action, file_name, debug) | |
| executable_name, _ = path.splitext(file_name) | |
| assembly_name = "{0}.s".format(executable_name) | |
| try: | |
| if action == "assembly": | |
| sp.check_call(command) | |
| if not path.isfile(assembly_name): | |
| return "{0} not found.".format(assembly_name) | |
| sp.check_call(["gcc", assembly_name, "-o", executable_name]) | |
| output = sp.check_output(["./{0}".format(executable_name)], stderr=sp.STDOUT) | |
| return output | |
| return sp.check_output(command, stderr=sp.STDOUT) | |
| except sp.CalledProcessError, error: | |
| return error.output | |
| finally: | |
| pass | |
| #if path.isfile(executable_name): | |
| # os.remove(executable_name) | |
| #if path.isfile(assembly_name): | |
| # os.remove(assembly_name) | |
| parser = ap.ArgumentParser(description='Testing suite for the Software Architectures compiler project') | |
| parser.add_argument('-d', '--debug', dest='is_debug', action='store_true', help='run the compiler with the debug flag enabled') | |
| parser.add_argument('-v', '--verbose', dest='is_verbose', action='store_true', help='display detailed output information') | |
| parser.add_argument('action', choices=Settings.ACTIONS.keys()) | |
| parser.add_argument('test_files', nargs='*') | |
| options = parser.parse_args() | |
| test_path = path.join(get_test_path(), Settings.ACTIONS[options.action]) | |
| test_files = options.test_files | |
| if len(options.test_files) == 0: | |
| test_files = filter(lambda x: ".DS" not in x, [file_name for file_name in os.listdir( | |
| test_path) if path.isfile(path.join(test_path, file_name))]) | |
| source_files = filter(is_source_file, get_file_names(Settings.SOURCE_PATH)) | |
| map(touch_file, source_files) | |
| try: | |
| script = sp.check_output(["ant"], stderr=sp.STDOUT) | |
| except sp.CalledProcessError, error: | |
| raise Exception(error.output) | |
| if "BUILD SUCCESSFUL" not in script: | |
| raise Exception(script) | |
| os.chdir(test_path) | |
| outputs = {file_name: get_output(options.action, file_name, options.is_debug) for file_name in test_files} | |
| for file_name, output in outputs.iteritems(): | |
| if options.action == "scan": | |
| test_output_name = "output/{0}.out".format(file_name) | |
| compare_text(output, get_text_from_file(test_output_name)) | |
| elif options.action == "parse" or options.action == "inter": | |
| if "illegal" in file_name: | |
| if not output or options.is_verbose: | |
| print file_name | |
| if options.is_verbose: | |
| print output | |
| elif output: | |
| print file_name | |
| if options.is_verbose: | |
| print output | |
| elif options.action == "assembly": | |
| name, _ = path.splitext(file_name) | |
| test_output_name = "output/{0}.out".format(name) | |
| compare_text(output, get_text_from_file(test_output_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment