autoupdate_testdata.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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("files", nargs="*")
  40. args = parser.parse_args()
  41. if args.non_fatal_checks:
  42. if build_mode == "opt":
  43. exit(
  44. "`--non-fatal-checks` is incompatible with inferred "
  45. "`-c opt` build mode"
  46. )
  47. configs.append("--config=non-fatal-checks")
  48. argv = [
  49. bazel,
  50. "run",
  51. "-c",
  52. build_mode,
  53. *configs,
  54. "--experimental_convenience_symlinks=ignore",
  55. "--ui_event_filters=-info,-stdout,-stderr,-finish",
  56. "//toolchain/testing:file_test",
  57. "--",
  58. "--autoupdate",
  59. ]
  60. # Support specifying tests to update, such as:
  61. # ./autoupdate_testdata.py lex/**/*
  62. if args.files:
  63. repo_root = Path(__file__).parents[1]
  64. file_tests = []
  65. # Filter down to just test files.
  66. for f in args.files:
  67. if f.endswith(".carbon"):
  68. path = str(Path(f).resolve().relative_to(repo_root))
  69. if path.count("/testdata/"):
  70. file_tests.append(path)
  71. if not file_tests:
  72. sys.exit(
  73. "Args do not seem to be test files; for example, "
  74. f"{args.files[0]}"
  75. )
  76. argv.append("--file_tests=" + ",".join(file_tests))
  77. # Provide an empty stdin so that the driver tests that read from stdin
  78. # don't block waiting for input. This matches the behavior of `bazel test`.
  79. subprocess.run(argv, check=True)
  80. if __name__ == "__main__":
  81. main()