mirror of
https://github.com/pocketpy/pocketpy
synced 2026-02-04 14:40:16 +00:00
- Use sys.executable in Python scripts (amalgamate.py, cmake_build.py) instead of hardcoded 'python' - Update shell scripts to prefer python3 over python with fallback - Fix scripts/run_tests.py to detect python3 for CPython benchmarking This fixes the issue where the amalgamation script fails on Linux systems that only have python3 available and no 'python' symlink.
92 lines
2.5 KiB
Python
92 lines
2.5 KiB
Python
import os
|
|
import sys
|
|
import time
|
|
import subprocess
|
|
import shutil
|
|
|
|
|
|
def test_file(filepath, cpython=False):
|
|
if cpython:
|
|
# Use python3 if available, otherwise fall back to python
|
|
python_cmd = shutil.which("python3") or shutil.which("python") or "python"
|
|
return os.system(f"{python_cmd} {filepath}") == 0
|
|
if sys.platform == 'win32':
|
|
code = os.system("main.exe " + filepath)
|
|
else:
|
|
code = os.system("./main " + filepath)
|
|
if code != 0:
|
|
print('Return code:', code)
|
|
return code == 0
|
|
|
|
def test_dir(path):
|
|
print("Testing directory:", path)
|
|
for filename in sorted(os.listdir(path)):
|
|
if not filename.endswith('.py'):
|
|
continue
|
|
filepath = os.path.join(path, filename)
|
|
print("> " + filepath, flush=True)
|
|
|
|
if path == 'benchmarks/':
|
|
_0 = time.time()
|
|
if not test_file(filepath, cpython=True):
|
|
print('cpython run failed')
|
|
continue
|
|
_1 = time.time()
|
|
if not test_file(filepath): exit(1)
|
|
_2 = time.time()
|
|
print(f' cpython: {_1 - _0:.6f}s (100%)')
|
|
print(f' pocketpy: {_2 - _1:.6f}s ({(_2 - _1) / (_1 - _0) * 100:.2f}%)')
|
|
else:
|
|
if not test_file(filepath):
|
|
print('-' * 50)
|
|
print("TEST FAILED!")
|
|
exit(1)
|
|
# print("TEST FAILED! Press any key to continue...")
|
|
# input()
|
|
|
|
print('CPython:', str(sys.version).replace('\n', ''))
|
|
print('System:', '64-bit' if sys.maxsize > 2**32 else '32-bit')
|
|
|
|
def test_repl():
|
|
print("[REPL Test Enabled]")
|
|
if sys.platform in ['linux', 'darwin']:
|
|
cmd = './main'
|
|
else:
|
|
cmd = None
|
|
|
|
if cmd is not None:
|
|
res = subprocess.run([cmd], encoding='utf-8', input=r'''
|
|
def add(a, b):
|
|
return a + b
|
|
|
|
class A:
|
|
def __init__(self, x):
|
|
self.x = x
|
|
def get(self):
|
|
return self.x
|
|
|
|
print('ans_1:', add(1, 2))
|
|
print('ans_2:', A('abc').get())
|
|
exit()
|
|
''', capture_output=True, check=True)
|
|
res.check_returncode()
|
|
# assert 'ans_1: 3' in res.stdout, res.stdout
|
|
if 'ans_1: 3' not in res.stdout:
|
|
print(res.stdout)
|
|
exit(1)
|
|
# assert 'ans_2: abc' in res.stdout, res.stdout
|
|
if 'ans_2: abc' not in res.stdout:
|
|
print(res.stdout)
|
|
exit(1)
|
|
|
|
|
|
if len(sys.argv) == 2:
|
|
assert 'benchmark' in sys.argv[1]
|
|
test_dir('benchmarks/')
|
|
else:
|
|
test_dir('tests/')
|
|
test_repl()
|
|
|
|
|
|
print("ALL TESTS PASSED")
|