autoupdate_testdata.py 3.1 KB

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