autoupdate_testdata.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 re
  9. import subprocess
  10. import sys
  11. from pathlib import Path
  12. def main() -> None:
  13. bazel = str(Path(__file__).parents[1] / "scripts" / "run_bazel.py")
  14. # Use the most recently used build mode, or `fastbuild` if missing
  15. # `bazel-bin`.
  16. build_mode = "fastbuild"
  17. workspace = subprocess.check_output(
  18. [
  19. bazel,
  20. "info",
  21. "workspace",
  22. "--ui_event_filters=stdout",
  23. ],
  24. encoding="utf-8",
  25. ).strip()
  26. bazel_bin_path = Path(workspace).joinpath("bazel-bin")
  27. if bazel_bin_path.exists():
  28. link = str(bazel_bin_path.readlink())
  29. m = re.search(r"-(\w+)/bin$", link)
  30. if m:
  31. build_mode = m[1]
  32. else:
  33. exit(f"Build mode not found in `bazel-bin` symlink: {link}")
  34. argv = [
  35. bazel,
  36. "run",
  37. "-c",
  38. build_mode,
  39. "--experimental_convenience_symlinks=ignore",
  40. "--ui_event_filters=-info,-stdout,-stderr,-finish",
  41. "//toolchain/testing:file_test",
  42. "--",
  43. "--autoupdate",
  44. ]
  45. # Support specifying tests to update, such as:
  46. # ./autoupdate_testdata.py lex/**/*
  47. if len(sys.argv) > 1:
  48. repo_root = Path(__file__).parents[1]
  49. file_tests = []
  50. # Filter down to just test files.
  51. for f in sys.argv[1:]:
  52. if f.endswith(".carbon"):
  53. path = str(Path(f).resolve().relative_to(repo_root))
  54. if path.count("/testdata/"):
  55. file_tests.append(path)
  56. if not file_tests:
  57. sys.exit(
  58. f"Args do not seem to be test files; for example, {sys.argv[1]}"
  59. )
  60. argv.append("--file_tests=" + ",".join(file_tests))
  61. # Provide an empty stdin so that the driver tests that read from stdin
  62. # don't block waiting for input. This matches the behavior of `bazel test`.
  63. subprocess.run(argv, check=True)
  64. if __name__ == "__main__":
  65. main()