run_tests.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import sys
  3. import time
  4. import subprocess
  5. class WorkDir:
  6. def __init__(self, next):
  7. self.prev = os.getcwd()
  8. self.next = next
  9. def __enter__(self):
  10. os.chdir(self.next)
  11. def __exit__(self, *args, **kwargs):
  12. os.chdir(self.prev)
  13. def test_file(filepath, cpython=False):
  14. if cpython:
  15. x, y = os.path.split(filepath)
  16. with WorkDir(x):
  17. return os.system("python3 " + y) == 0
  18. if sys.platform == 'win32':
  19. return os.system("main.exe " + filepath) == 0
  20. else:
  21. return os.system("./main " + filepath) == 0
  22. def test_dir(path):
  23. print("Testing directory:", path)
  24. for filename in sorted(os.listdir(path)):
  25. if not filename.endswith('.py'):
  26. continue
  27. filepath = os.path.join(path, filename)
  28. print("> " + filepath, flush=True)
  29. if path == 'benchmarks/':
  30. _0 = time.time()
  31. if not test_file(filepath, cpython=True):
  32. print('cpython run failed')
  33. continue
  34. _1 = time.time()
  35. if not test_file(filepath): exit(1)
  36. _2 = time.time()
  37. print(f' cpython: {_1 - _0:.6f}s (100%)')
  38. print(f' pocketpy: {_2 - _1:.6f}s ({(_2 - _1) / (_1 - _0) * 100:.2f}%)')
  39. else:
  40. if not test_file(filepath):
  41. print('-' * 50)
  42. print("TEST FAILED! Press any key to continue...")
  43. input()
  44. print('CPython:', str(sys.version).replace('\n', ''))
  45. print('System:', '64-bit' if sys.maxsize > 2**32 else '32-bit')
  46. if len(sys.argv) == 2:
  47. assert 'benchmark' in sys.argv[1]
  48. test_dir('benchmarks/')
  49. else:
  50. test_dir('tests/')
  51. # test interactive mode
  52. print("[REPL Test Enabled]")
  53. if sys.platform in ['linux', 'darwin']:
  54. cmd = './main'
  55. elif sys.platform == 'win32':
  56. cmd = 'main.exe'
  57. else:
  58. cmd = None
  59. if cmd is not None:
  60. res = subprocess.run([cmd], encoding='utf-8', input=r'''
  61. def add(a, b):
  62. return a + b
  63. class A:
  64. def __init__(self, x):
  65. self.x = x
  66. def get(self):
  67. return self.x
  68. print(add(1, 2))
  69. print(A('abc').get())
  70. ''', capture_output=True, check=True)
  71. res.check_returncode()
  72. assert res.stdout.endswith('>>> 3\n>>> abc\n>>> ')
  73. print("ALL TESTS PASSED")