autoupdate_testdata.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. """Autoupdates testdata in toolchain."""
  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. import argparse
  9. import re
  10. import shlex
  11. import subprocess
  12. import sys
  13. from pathlib import Path
  14. def main() -> None:
  15. bazel = str(Path(__file__).parents[1] / "scripts" / "run_bazel.py")
  16. configs = []
  17. # Use the most recently used build mode, or `fastbuild` if missing
  18. # `bazel-bin`.
  19. build_mode = "fastbuild"
  20. workspace = subprocess.check_output(
  21. [
  22. bazel,
  23. "info",
  24. "workspace",
  25. "--ui_event_filters=stdout",
  26. ],
  27. encoding="utf-8",
  28. ).strip()
  29. bazel_bin_path = Path(workspace).joinpath("bazel-bin")
  30. if bazel_bin_path.exists():
  31. link = str(bazel_bin_path.readlink())
  32. m = re.search(r"-(\w+)/bin$", link)
  33. if m:
  34. build_mode = m[1]
  35. else:
  36. exit(f"Build mode not found in `bazel-bin` symlink: {link}")
  37. # Parse arguments.
  38. parser = argparse.ArgumentParser(__doc__)
  39. parser.add_argument("--non-fatal-checks", action="store_true")
  40. parser.add_argument(
  41. "--print_slowest_tests", default=0, help="Forwarded to file_test"
  42. )
  43. parser.add_argument("--threads", help="Forwarded to file_test")
  44. parser.add_argument("files", nargs="*")
  45. args = parser.parse_args()
  46. if args.non_fatal_checks:
  47. if build_mode == "optimize":
  48. exit(
  49. "`--non-fatal-checks` is incompatible with inferred "
  50. "`-c optimize` build mode"
  51. )
  52. configs.append("--config=non-fatal-checks")
  53. argv = [
  54. bazel,
  55. "run",
  56. "-c",
  57. build_mode,
  58. *configs,
  59. "--experimental_convenience_symlinks=ignore",
  60. "--ui_event_filters=-info,-stdout,-stderr,-finish",
  61. "//toolchain/testing:file_test",
  62. "--",
  63. "--autoupdate",
  64. "--print_slowest_tests",
  65. str(args.print_slowest_tests),
  66. ]
  67. if args.threads:
  68. argv += ["--threads", args.threads]
  69. # Support specifying tests to update, such as:
  70. # ./autoupdate_testdata.py lex/**/*
  71. if args.files:
  72. repo_root = Path(__file__).parents[1]
  73. file_tests = []
  74. # Filter down to just test files.
  75. for f in args.files:
  76. if f.endswith(".carbon"):
  77. path = str(Path(f).resolve().relative_to(repo_root))
  78. if path.count("/testdata/"):
  79. file_tests.append(path)
  80. if not file_tests:
  81. sys.exit(
  82. "Args do not seem to be test files; for example, "
  83. f"{args.files[0]}"
  84. )
  85. argv.append("--file_tests=" + ",".join(file_tests))
  86. # Provide an empty stdin so that the driver tests that read from stdin
  87. # don't block waiting for input. This matches the behavior of `bazel test`.
  88. result = subprocess.run(argv)
  89. if result.returncode != 0:
  90. sys.exit(
  91. f"Command `{shlex.join(argv)}` failed with exit code "
  92. f"`{result.returncode}`"
  93. )
  94. if __name__ == "__main__":
  95. try:
  96. main()
  97. except KeyboardInterrupt:
  98. sys.exit(1)