update_checks.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. """Updates the CHECK: lines in lit tests based on the AUTOUPDATE line."""
  3. __copyright__ = """
  4. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  5. Exceptions. See /LICENSE for license information.
  6. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. """
  8. from concurrent import futures
  9. import os
  10. import subprocess
  11. import sys
  12. _BINDIR = "./bazel-bin/executable_semantics"
  13. _TESTDATA = "executable_semantics/testdata"
  14. # TODO: Right now this is a static string used. In theory maybe we should use
  15. # the command; it's included for that flexibility.
  16. _AUTOUPDATE_MARKER = "// AUTOUPDATE: executable_semantics %s\n"
  17. def _get_tests():
  18. """Get the list of tests from the filesystem."""
  19. tests = set()
  20. for root, _, files in os.walk(_TESTDATA):
  21. for f in files:
  22. if f == "lit.cfg":
  23. # Ignore the lit config.
  24. continue
  25. if os.path.splitext(f)[1] == ".carbon":
  26. tests.add(os.path.join(root, f))
  27. else:
  28. sys.exit("Unrecognized file type in testdata: %s" % f)
  29. return tests
  30. def _update_check(test):
  31. """Updates the CHECK: lines for `test` by running executable_semantics."""
  32. with open(test) as f:
  33. orig_lines = f.readlines()
  34. if _AUTOUPDATE_MARKER not in orig_lines:
  35. raise ValueError("No autoupdate marker in %s" % test)
  36. # Run executable_semantics to general output.
  37. # (`bazel run` would serialize)
  38. p = subprocess.run(
  39. ["%s/executable_semantics" % _BINDIR, test],
  40. stdout=subprocess.PIPE,
  41. stderr=subprocess.STDOUT,
  42. )
  43. out = p.stdout.decode("utf-8")
  44. # `lit` uses full paths to the test file, so use a regex to ignore paths
  45. # when used.
  46. # TODO: Maybe revisit and see if lit can be convinced to give a
  47. # root-relative path.
  48. out = out.replace(test, "{{.*}}/%s" % test)
  49. # Remove old OUT.
  50. lines_without_check = [
  51. x for x in orig_lines if not x.startswith("// CHECK:")
  52. ]
  53. autoupdate_index = lines_without_check.index(_AUTOUPDATE_MARKER)
  54. assert autoupdate_index >= 0
  55. with open(test, "w") as f:
  56. f.writelines(lines_without_check[: autoupdate_index + 1])
  57. f.writelines(["// CHECK: %s\n" % x for x in out.splitlines()])
  58. f.writelines(lines_without_check[autoupdate_index + 1 :])
  59. print(".", end="", flush=True)
  60. def _update_checks():
  61. """Runs bazel to update CHECK: lines in lit tests."""
  62. # TODO: It may be helpful if a list of tests can be passed in args; would
  63. # want to use argparse for this.
  64. tests = _get_tests()
  65. # Build all tests at once in order to allow parallel updates.
  66. print("Building executable_semantics...")
  67. subprocess.check_call(["bazel", "build", "//executable_semantics"])
  68. print("Updating %d lit tests..." % len(tests))
  69. with futures.ThreadPoolExecutor() as exec:
  70. # list() iterates to propagate exceptions.
  71. list(exec.map(_update_check, tests))
  72. # Run again, because the previous run may have changed line numbers.
  73. list(exec.map(_update_check, tests))
  74. # Each update call indicates progress with a dot without a newline, so put a
  75. # newline to wrap.
  76. print("\nUpdated lit tests.")
  77. def main():
  78. # Go to the repository root so that paths will match bazel's view.
  79. os.chdir(os.path.join(os.path.dirname(__file__), ".."))
  80. _update_checks()
  81. if __name__ == "__main__":
  82. main()