You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.6KB

  1. #!/usr/bin/env python3
  2. import argparse
  3. from pathlib import Path
  4. from typing import List, NamedTuple
  5. import shutil
  6. import subprocess
  7. import sys
  8. ROOT = Path(__file__).parent.parent.absolute()
  9. class TestOptions(NamedTuple):
  10. exe: Path
  11. toolchain: str
  12. def run_test_dir(dir: Path, opts: TestOptions) -> bool:
  13. print(f'Running test: {dir.stem} ', end='')
  14. out_dir = dir / '_build'
  15. if out_dir.exists():
  16. shutil.rmtree(out_dir)
  17. res = subprocess.run(
  18. [
  19. str(opts.exe),
  20. 'build',
  21. '--export',
  22. '--warnings',
  23. '--tests',
  24. f'--toolchain={opts.toolchain}',
  25. f'--out-dir={out_dir}',
  26. f'--export-name={dir.stem}',
  27. ],
  28. cwd=dir,
  29. stdout=subprocess.PIPE,
  30. stderr=subprocess.STDOUT,
  31. )
  32. if res.returncode != 0:
  33. print('- FAILED')
  34. print(f'Test failed with exit code '
  35. f'[{res.returncode}]:\n{res.stdout.decode()}')
  36. else:
  37. print('- PASSED')
  38. return res.returncode == 0
  39. def run_tests(opts: TestOptions) -> int:
  40. print('Sanity check...')
  41. subprocess.check_output([str(opts.exe), '--help'])
  42. tests_subdir = ROOT / 'tests'
  43. test_dirs = tests_subdir.glob('*.test')
  44. ret = 0
  45. for td in test_dirs:
  46. if not run_test_dir(td, opts):
  47. ret = 1
  48. return ret
  49. def bootstrap_self(opts: TestOptions):
  50. # Copy the exe to another location, as windows refuses to let a binary be
  51. # replaced while it is executing
  52. new_exe = ROOT / '_ddslime.bootstrap-test.exe'
  53. shutil.copy2(opts.exe, new_exe)
  54. res = subprocess.run([
  55. str(new_exe),
  56. 'build',
  57. f'-FT{opts.toolchain}',
  58. ])
  59. if res.returncode != 0:
  60. print('The bootstrap test failed!', file=sys.stderr)
  61. return False
  62. print('Bootstrap test PASSED!')
  63. return True
  64. def main(argv: List[str]) -> int:
  65. parser = argparse.ArgumentParser()
  66. parser.add_argument(
  67. '--exe',
  68. '-e',
  69. help='Path to the ddslim executable to test',
  70. required=True)
  71. parser.add_argument(
  72. '--toolchain',
  73. '-T',
  74. help='The ddslim toolchain to use while testing',
  75. required=True,
  76. )
  77. parser.add_argument(
  78. '--skip-bootstrap-test',
  79. action='store_true',
  80. help='Skip the self-bootstrap test',
  81. )
  82. args = parser.parse_args(argv)
  83. opts = TestOptions(exe=Path(args.exe).absolute(), toolchain=args.toolchain)
  84. if not args.skip_bootstrap_test and not bootstrap_self(opts):
  85. return 2
  86. return run_tests(opts)
  87. if __name__ == "__main__":
  88. sys.exit(main(sys.argv[1:]))