|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +import shutil |
| 5 | + |
| 6 | +script_path = os.path.abspath(__file__).replace("run.py", "") |
| 7 | +examples = [example for example in os.listdir(script_path) if os.path.isdir(os.path.join(script_path, example))] |
| 8 | + |
| 9 | +# Get input project name from user from args |
| 10 | +example_name = str(sys.argv[1]).strip().lower() |
| 11 | + |
| 12 | +# Check if project exists |
| 13 | +if example_name not in examples: |
| 14 | + print(f"Example {example_name} not found") |
| 15 | + print(f"Available examples: {examples}") |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | +# Get path to repo root |
| 19 | +project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
| 20 | + |
| 21 | +cli_path = f"{project_root}/src/CLI/CSynth.CLI.csproj" |
| 22 | +example_path = f"{project_root}/examples/{example_name}" |
| 23 | +example_dll_path = f"{example_path}/bin/Release/net8.0/{example_name}.dll" |
| 24 | +output_path = f"{example_path}/{example_name}.lua" |
| 25 | + |
| 26 | +# Build example and wait for it to finish |
| 27 | +os.system(f"dotnet build -c Release {example_path}") |
| 28 | + |
| 29 | +# Run example and capture stdout |
| 30 | +result = subprocess.run(["dotnet", "run", "--project", cli_path, "--", example_dll_path], capture_output=True, text=True) |
| 31 | + |
| 32 | +if result.returncode != 0: |
| 33 | + print(result.stderr) |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | +# Write to example output |
| 37 | +with open(output_path, 'w') as f: |
| 38 | + f.write(result.stdout) |
| 39 | + |
| 40 | +print(f"Output written to {output_path}") |
| 41 | + |
| 42 | +# if there's a output.txt file, try compare lua output with it |
| 43 | +output_txt_path = f"{example_path}/output.txt" |
| 44 | + |
| 45 | +if os.path.exists(output_txt_path): |
| 46 | + with open(output_txt_path, 'r') as f: |
| 47 | + expected_output = f.read() |
| 48 | + |
| 49 | + # run example.lua and capture stdout |
| 50 | + lune_executable = shutil.which("lune") |
| 51 | + |
| 52 | + if lune_executable: |
| 53 | + args = [lune_executable, "run", output_path] |
| 54 | + result = subprocess.run(args, capture_output=True, text=True) |
| 55 | + else: |
| 56 | + print("No lune executable found in path") |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | + if result.returncode != 0: |
| 60 | + print(f"Error running {example_name}.lua!") |
| 61 | + print(result.stderr) |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + if result.stdout.strip() == expected_output.strip(): |
| 65 | + print(f"{example_name}.lua output matches expected output") |
| 66 | + else: |
| 67 | + print(f"{example_name}.lua output does not match expected output") |
| 68 | + print("Expected:") |
| 69 | + print(expected_output) |
| 70 | + print("Actual:") |
| 71 | + print(result.stdout) |
| 72 | +else: |
| 73 | + print(f"No output.txt file found for {example_name}, won't compare output") |
| 74 | + |
0 commit comments