test_rust.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import threading
  2. import subprocess
  3. import tempfile
  4. import os
  5. from unittest import SkipTest
  6. from typing import *
  7. from sccd.test.static.syntax import TestVariant
  8. from sccd.statechart.codegen.rust import UnsupportedFeature
  9. from sccd.test.codegen.write_crate import write_crate
  10. from sccd.util.indenting_writer import IndentingWriter
  11. from sccd.util.debug import *
  12. import os
  13. import sccd
  14. RUST_DIR = os.path.dirname(sccd.__file__) + "/../../rust"
  15. # Generate Rust code from the test case. This Rust code is piped to a Rust compiler (rustc) process, which reads from stdin. The Rust compiler outputs a binary in a temp dir. We then run the created binary as a subprocess.
  16. # If the result code of either the Rust compiler or the created binary is not 0 ("success"), the 'unittest' fails.
  17. def run_rust_test(path: str, unittest):
  18. if DEBUG:
  19. stdout = None
  20. stderr = None
  21. else:
  22. stdout = subprocess.DEVNULL
  23. stderr = subprocess.STDOUT
  24. from hashlib import sha1
  25. output = tempfile.gettempdir() + "/sccd_test_crate"
  26. print_debug("Writing crate to " + output)
  27. try:
  28. write_crate(path, output)
  29. except UnsupportedFeature as e:
  30. raise SkipTest("unsupported feature: " + str(e))
  31. print_debug("Done. Running crate...")
  32. with subprocess.Popen(["cargo", "run"],
  33. cwd=output,
  34. stdout=stdout,
  35. stderr=subprocess.PIPE) as cargo:
  36. cargostderr = cargo.stderr.read().decode('UTF-8')
  37. status = cargo.wait()
  38. if DEBUG:
  39. print(cargostderr)
  40. if status != 0:
  41. unittest.fail("Test status %d. Stderr:\n%s" % (status, cargostderr))