comtest.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python3
  2. """End-to-end UI test: C writes a real .COM, verified by reading the disk.
  3. uitest.py covers the error path (compile fails -> editor opens on the error).
  4. This covers the success path, which is the one that produces an artifact:
  5. 1. W loads a work file (LoadWorkFile ends with a Pause: needs a filler key)
  6. 2. O opens the Options submenu and C selects Destination = .COM
  7. 3. ESC returns to the main menu
  8. 4. C compiles, and the shell reports the .COM it wrote
  9. 5. The .COM is then INDEPENDENTLY checked on disk: the runtime's first
  10. bytes, a fully zero code/data gap, a size that covers the data area, and
  11. the program header words
  12. Step 5 is the point. "The shell said it wrote a file" is not evidence that
  13. the file is right, and the shell reports success from the same state that
  14. produced the bytes, so the check that matters happens outside the shell.
  15. The .COM is removed afterwards so the test can be re-run.
  16. Usage: comtest.py [fixture.pas]
  17. Passing a fixture that does NOT compile is expected to be RED - that is the
  18. test being non-vacuous, not a failure of the shell. It was checked that way
  19. (t14_types.pas, which raises ENoLib for 'array' at its point of use).
  20. """
  21. import os
  22. import sys
  23. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  24. from ptyharness import SHELL_DIR, Screen, drain, reap, send, spawn, status_str, visible
  25. FIXTURE = os.path.abspath(sys.argv[1]) if len(sys.argv) > 1 else os.path.join(
  26. os.path.dirname(os.path.abspath(__file__)), "fixtures", "t26_str_mixed_args.pas")
  27. COM = os.path.splitext(FIXTURE)[0] + ".COM"
  28. # Restated here on purpose - the checker must not ask the code under test.
  29. RTSZ = 385 # Runtime.RT_Size()
  30. DATAB = RTSZ + 0x1000 # Compiler: data base = rtSz + 1000H
  31. HEAD = "8B C0 8B 54 04 8B 4C 08" # initmem: MOV AX,AX / MOV DX,[SI+4] / MOV CX,[SI+8]
  32. def check_com(path, expected_src_len):
  33. """Return a list of problems (empty = the .COM is correct)."""
  34. errs = []
  35. if not os.path.exists(path):
  36. return ["no .COM file was written"]
  37. d = open(path, "rb").read()
  38. if d[:8].hex(" ").upper() != HEAD:
  39. errs.append("runtime not at offset 0: first bytes %s" % d[:8].hex(" ").upper())
  40. if len(d) < DATAB:
  41. errs.append("file is %d bytes, shorter than the data base %d - the "
  42. "globals would be outside the file" % (len(d), DATAB))
  43. # the runtime must be followed immediately by the program header,
  44. # whose first word is the "image is valid" flag
  45. if len(d) >= RTSZ + 2:
  46. flag = int.from_bytes(d[RTSZ:RTSZ + 2], "little")
  47. if flag != 1:
  48. errs.append("hdrFlag=%d at the program header, want 1" % flag)
  49. if len(d) >= RTSZ + 10:
  50. ds = int.from_bytes(d[RTSZ + 4:RTSZ + 6], "little")
  51. heap = int.from_bytes(d[RTSZ + 6:RTSZ + 8], "little")
  52. if ds != DATAB:
  53. errs.append("hdrDS=%d, want %d" % (ds, DATAB))
  54. if heap < ds:
  55. errs.append("hdrHeap=%d < hdrDS=%d" % (heap, ds))
  56. # the gap between the end of the code and the data area must be all zero
  57. cs = int.from_bytes(d[RTSZ + 2:RTSZ + 4], "little") if len(d) >= RTSZ + 4 else 0
  58. if cs and cs < DATAB:
  59. nz = sum(1 for b in d[cs:DATAB] if b)
  60. if nz:
  61. errs.append("%d non-zero bytes in the code/data gap" % nz)
  62. return errs
  63. def main():
  64. if os.path.exists(COM):
  65. os.unlink(COM)
  66. pid, fd = spawn()
  67. checks = []
  68. try:
  69. drain(fd, quiet=0.6)
  70. send(fd, b"w")
  71. send(fd, FIXTURE.encode())
  72. send(fd, b"\r", quiet=0.8)
  73. send(fd, b" ", quiet=0.5) # clears LoadWorkFile's Pause
  74. # Options submenu -> Destination = .COM.
  75. # The submenu is left with Q (that is its Quit key, as in the
  76. # original's optmenu); ESC is swallowed by its REPEAT loop.
  77. send(fd, b"o", quiet=0.8)
  78. opt = visible(send(fd, b"c", quiet=0.5))
  79. checks.append(("Options submenu opened", "Compile" in opt))
  80. checks.append(("selecting C set Destination = COM", "COM" in opt))
  81. after = visible(send(fd, b"q", quiet=0.8))
  82. checks.append(("Q left the submenu for the main menu", "Main file" in after))
  83. comp = visible(send(fd, b"c", quiet=2.0))
  84. checks.append(("compile reported success", "Compiled OK" in comp))
  85. checks.append(("shell reported the Destination it used",
  86. "Destination" in comp))
  87. checks.append(("shell named the .COM it wrote",
  88. os.path.basename(COM) in comp))
  89. send(fd, b"\x1b", quiet=0.6)
  90. send(fd, b"q", quiet=0.8)
  91. finally:
  92. status = reap(fd, pid)
  93. checks.append(("shell exited cleanly (status 0)", os.WIFEXITED(status)
  94. and os.WEXITSTATUS(status) == 0))
  95. # now verify the artifact independently
  96. errs = check_com(COM, len(open(FIXTURE, "rb").read()))
  97. checks.append((".COM exists on disk and passes the independent check"
  98. if not errs else ".COM check: " + "; ".join(errs), not errs))
  99. checks.append((".COM was written next to the source, not in the shell's cwd",
  100. os.path.dirname(os.path.abspath(COM)) == os.path.dirname(FIXTURE)))
  101. print("UI TEST: %s" % os.path.basename(FIXTURE))
  102. print("-" * 60)
  103. ok = True
  104. for name, passed in checks:
  105. print("%-58s %s" % (name[:58], "PASS" if passed else "FAIL"))
  106. ok = ok and passed
  107. print("-" * 60)
  108. print("child: %s" % status_str(status))
  109. print("RESULT: %s" % ("ALL PASS" if ok else "FAILURES PRESENT"))
  110. if os.path.exists(COM):
  111. print("(removed %s)" % COM)
  112. os.unlink(COM)
  113. return 0 if ok else 1
  114. if __name__ == "__main__":
  115. sys.exit(main())