autoupdate_testdata.py 2.1 KB

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