|
| 1 | +r"""Generates test files for the conversion tests. |
| 2 | +
|
| 3 | +In each directory, only `input.js` is manually written. The other files are |
| 4 | +generated by running this tool. |
| 5 | +
|
| 6 | +Usage: |
| 7 | +
|
| 8 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 9 | + --passes source2ast,ast2hir,constprop,hir2ast,ast2source \ |
| 10 | + --path maldoca/js/ir/transforms/constant_propagation/tests |
| 11 | +
|
| 12 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 13 | + --passes source2ast,extract_prelude,erase_comments,ast2hir,dynconstprop,hir2ast,ast2source \ |
| 14 | + --path maldoca/js/ir/transforms/dynamic_constant_propagation/tests |
| 15 | +
|
| 16 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 17 | + --passes source2ast,erase_comments,ast2hir,movenamedfuncs,hir2ast,ast2source \ |
| 18 | + --path maldoca/js/ir/transforms/move_named_functions/tests |
| 19 | +
|
| 20 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 21 | + --passes source2ast,erase_comments,ast2hir,normalize_member_expressions,hir2ast,ast2source \ |
| 22 | + --path maldoca/js/ir/transforms/normalize_member_expressions/tests |
| 23 | +
|
| 24 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 25 | + --passes source2ast,erase_comments,ast2hir,constprop,normalize_member_expressions,hir2ast,ast2source \ |
| 26 | + --path maldoca/js/ir/transforms/normalize_member_expressions/tests/with_constant_propagation |
| 27 | +
|
| 28 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 29 | + --passes source2ast,ast2hir,normalizeobjprops,hir2ast,ast2source \ |
| 30 | + --path maldoca/js/ir/transforms/normalize_object_properties/tests |
| 31 | +
|
| 32 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 33 | + --passes source2ast,erase_comments,ast2hir,peelparens,hir2ast,ast2source \ |
| 34 | + --path maldoca/js/ir/transforms/peel_parentheses/tests |
| 35 | +
|
| 36 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 37 | + --passes source2ast,ast2hir,split_declaration_statements,hir2ast,ast2source \ |
| 38 | + --path maldoca/js/ir/transforms/split_declaration_statements/tests |
| 39 | +
|
| 40 | +python3 maldoca/js/ir/transforms/generate_tests.py \ |
| 41 | + --passes source2ast,ast2hir,split_sequence_expressions,hir2ast,ast2source \ |
| 42 | + --path maldoca/js/ir/transforms/split_sequence_expressions/tests |
| 43 | +""" |
| 44 | + |
| 45 | +import argparse |
| 46 | +import os |
| 47 | +import shutil |
| 48 | +import subprocess |
| 49 | +import sys |
| 50 | + |
| 51 | + |
| 52 | +def run_command(cmd: str) -> bytes: |
| 53 | + """Run a shell command and return its output.""" |
| 54 | + |
| 55 | + print("Running:") |
| 56 | + print(" ", cmd) |
| 57 | + |
| 58 | + try: |
| 59 | + return subprocess.check_output(cmd, shell=True, stderr=subprocess.PIPE) |
| 60 | + except subprocess.CalledProcessError as e: |
| 61 | + print("Error running command, output:", e.output) |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + |
| 65 | +def generate_test_file( |
| 66 | + input_path: str, |
| 67 | + output_path: str, |
| 68 | + passes: str, |
| 69 | + prefix: str, |
| 70 | +): |
| 71 | + """Generates a test file. |
| 72 | +
|
| 73 | + Args: |
| 74 | + input_path: Test case name. |
| 75 | + output_path: The output file will be <name>/<output_name>. |
| 76 | + passes: Comma-separated list of JSIR passes. |
| 77 | + prefix: FileCheck prefix. |
| 78 | + """ |
| 79 | + input_abs_path: str = os.path.abspath(input_path) |
| 80 | + |
| 81 | + command: str = ( |
| 82 | + "bazel run //maldoca/js/ir:jsir_gen --" |
| 83 | + f" --input_file={input_abs_path}" |
| 84 | + f" --passes='{passes}'" |
| 85 | + ) |
| 86 | + |
| 87 | + try: |
| 88 | + output = run_command(command).decode("utf-8") |
| 89 | + except UnicodeDecodeError: |
| 90 | + output = run_command(command).decode("latin-1") |
| 91 | + |
| 92 | + output = output.strip() |
| 93 | + |
| 94 | + # Prepend prefix |
| 95 | + lines = output.split("\n") |
| 96 | + out_lines = [] |
| 97 | + for i, line in enumerate(lines): |
| 98 | + if not line: |
| 99 | + line = f"// {prefix}-EMPTY:" |
| 100 | + elif i == 0: |
| 101 | + line = f"// {prefix}: " + line |
| 102 | + else: |
| 103 | + line = f"// {prefix}-NEXT: " + line |
| 104 | + out_lines.append(line) |
| 105 | + |
| 106 | + output = "\n".join(out_lines) + "\n" |
| 107 | + |
| 108 | + with open(output_path, "w") as f: |
| 109 | + f.write(output) |
| 110 | + |
| 111 | + |
| 112 | +README_TEMPLATE = r"""To run manually: |
| 113 | +
|
| 114 | +```shell |
| 115 | +bazel run //maldoca/js/ir:jsir_gen -- \ |
| 116 | + --input_file $(pwd)/{test_path}/input.js \ |
| 117 | + --passes "{passes}" |
| 118 | +``` |
| 119 | +""" |
| 120 | + |
| 121 | +LIT_TEMPLATE = r"""// RUN: CURRENT_FILE_BASENAME=$(basename %s .lit) && \ |
| 122 | +// RUN: jsir_gen --input_file "$(dirname %s)"/input.js \ |
| 123 | +// RUN: --passes "{passes}" \ |
| 124 | +// RUN: | FileCheck --check-prefix SOURCE "$(dirname %s)"/output.generated.txt |
| 125 | +""" |
| 126 | + |
| 127 | + |
| 128 | +def generate_readme_file(test_path: str, passes: str): |
| 129 | + with open(os.path.join(test_path, "README.generated.md"), "w") as f: |
| 130 | + f.write(README_TEMPLATE.format(test_path=test_path, passes=passes)) |
| 131 | + |
| 132 | + |
| 133 | +def generate_lit_file(test_path: str, passes: str): |
| 134 | + with open(os.path.join(test_path, "run.generated.lit"), "w") as f: |
| 135 | + f.write(LIT_TEMPLATE.format(test_path=test_path, passes=passes)) |
| 136 | + |
| 137 | + |
| 138 | +def main(): |
| 139 | + parser = argparse.ArgumentParser() |
| 140 | + parser.add_argument("--passes", required=True) |
| 141 | + parser.add_argument("--path", required=True) |
| 142 | + |
| 143 | + args = parser.parse_args() |
| 144 | + passes: str = args.passes |
| 145 | + root_path: str = args.path |
| 146 | + print("Passes:", passes) |
| 147 | + print("Path:", root_path) |
| 148 | + |
| 149 | + for test_path, _, file_names_in_dir in os.walk(root_path): |
| 150 | + |
| 151 | + if "input.js" not in file_names_in_dir: |
| 152 | + continue |
| 153 | + |
| 154 | + test_name = os.path.basename(test_path) |
| 155 | + print("Generating test:", test_name) |
| 156 | + |
| 157 | + generate_test_file( |
| 158 | + input_path=os.path.join(test_path, "input.js"), |
| 159 | + output_path=os.path.join(test_path, "output.generated.txt"), |
| 160 | + passes=passes, |
| 161 | + prefix="SOURCE", |
| 162 | + ) |
| 163 | + |
| 164 | + generate_readme_file( |
| 165 | + test_path=test_path, |
| 166 | + passes=passes, |
| 167 | + ) |
| 168 | + |
| 169 | + generate_lit_file( |
| 170 | + test_path, |
| 171 | + passes=passes, |
| 172 | + ) |
| 173 | + |
| 174 | + shutil.copyfile( |
| 175 | + os.path.join(root_path, "BUILD.template"), |
| 176 | + os.path.join(test_path, "BUILD"), |
| 177 | + ) |
| 178 | + |
| 179 | + |
| 180 | +if __name__ == "__main__": |
| 181 | + main() |
0 commit comments