units.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. import logging
  3. import re
  4. import sys
  5. import serial
  6. from await_flipper import flp_serial_by_name
  7. def main():
  8. logging.basicConfig(
  9. format="%(asctime)s %(levelname)-8s %(message)s",
  10. level=logging.INFO,
  11. datefmt="%Y-%m-%d %H:%M:%S",
  12. )
  13. logging.info("Trying to run units on flipper")
  14. flp_serial = flp_serial_by_name(sys.argv[1])
  15. if flp_serial == "":
  16. logging.error("Flipper not found!")
  17. sys.exit(1)
  18. with serial.Serial(flp_serial, timeout=1) as flipper:
  19. logging.info(f"Found Flipper at {flp_serial}")
  20. flipper.baudrate = 230400
  21. flipper.flushOutput()
  22. flipper.flushInput()
  23. flipper.timeout = 180
  24. flipper.read_until(b">: ").decode("utf-8")
  25. flipper.write(b"unit_tests\r")
  26. data = flipper.read_until(b">: ").decode("utf-8")
  27. lines = data.split("\r\n")
  28. tests_re = r"Failed tests: \d{0,}"
  29. time_re = r"Consumed: \d{0,}"
  30. leak_re = r"Leaked: \d{0,}"
  31. status_re = r"Status: \w{3,}"
  32. tests_pattern = re.compile(tests_re)
  33. time_pattern = re.compile(time_re)
  34. leak_pattern = re.compile(leak_re)
  35. status_pattern = re.compile(status_re)
  36. tests, time, leak, status = None, None, None, None
  37. total = 0
  38. for line in lines:
  39. logging.info(line)
  40. if "()" in line:
  41. total += 1
  42. if not tests:
  43. tests = re.match(tests_pattern, line)
  44. if not time:
  45. time = re.match(time_pattern, line)
  46. if not leak:
  47. leak = re.match(leak_pattern, line)
  48. if not status:
  49. status = re.match(status_pattern, line)
  50. if None in (tests, time, leak, status):
  51. logging.error(f"Failed to parse output: {leak} {time} {leak} {status}")
  52. sys.exit(1)
  53. leak = int(re.findall(r"[- ]\d+", leak.group(0))[0])
  54. status = re.findall(r"\w+", status.group(0))[1]
  55. tests = int(re.findall(r"\d+", tests.group(0))[0])
  56. time = int(re.findall(r"\d+", time.group(0))[0])
  57. if tests > 0 or status != "PASSED":
  58. logging.error(f"Got {tests} failed tests.")
  59. logging.error(f"Leaked (not failing on this stat): {leak}")
  60. logging.error(f"Status: {status}")
  61. logging.error(f"Time: {time/1000} seconds")
  62. sys.exit(1)
  63. logging.info(f"Leaked (not failing on this stat): {leak}")
  64. logging.info(
  65. f"Tests ran successfully! Time elapsed {time/1000} seconds. Passed {total} tests."
  66. )
  67. sys.exit(0)
  68. if __name__ == "__main__":
  69. main()