bazel_test.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env vpython3
  2. # Copyright 2023 The ChromiumOS Authors
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # [VPYTHON:BEGIN]
  6. # python_version: "3.8"
  7. # [VPYTHON:END]
  8. """Tests for Bazel launcher."""
  9. import os
  10. from pathlib import Path
  11. import site
  12. import sys
  13. import unittest
  14. DEPOT_TOOLS_DIR = Path(__file__).resolve().parent.parent
  15. site.addsitedir(DEPOT_TOOLS_DIR)
  16. import bazel
  17. from testing_support import trial_dir
  18. class FindCrosUnittest(trial_dir.TestCase):
  19. """Test the _find_bazel_cros function."""
  20. def setUp(self):
  21. """Create the checkout and chromite files."""
  22. super().setUp()
  23. self.checkout_dir = Path(self.root_dir) / "chromiumos"
  24. self.chromite_dir = self.checkout_dir / "chromite"
  25. self.launcher = self.chromite_dir / "bin" / "bazel"
  26. self.launcher.parent.mkdir(exist_ok=True, parents=True)
  27. self.launcher.write_bytes(b"")
  28. self.launcher.chmod(0o775)
  29. self.orig_dir = Path.cwd()
  30. def tearDown(self):
  31. os.chdir(self.orig_dir)
  32. super().tearDown()
  33. def test_at_checkout_base(self):
  34. """Test we find the launcher at the base of the checkout."""
  35. os.chdir(self.checkout_dir)
  36. self.assertEqual(bazel._find_bazel_cros(), self.launcher)
  37. def test_in_checkout_subdir(self):
  38. """Test we find the launcher in a subdir of the checkout."""
  39. os.chdir(self.chromite_dir)
  40. self.assertEqual(bazel._find_bazel_cros(), self.launcher)
  41. def test_out_of_checkout(self):
  42. """Test we don't find the launcher outside of the checkout."""
  43. os.chdir(self.root_dir)
  44. self.assertIsNone(bazel._find_bazel_cros())
  45. class FindPathUnittest(trial_dir.TestCase):
  46. """Test the _find_next_bazel_in_path function."""
  47. def setUp(self):
  48. """Create the checkout and chromite files."""
  49. super().setUp()
  50. self.bin_dir = Path(self.root_dir) / "bin"
  51. self.bin_dir.mkdir(exist_ok=True, parents=True)
  52. self.orig_path = os.environ.get("PATH", os.defpath)
  53. # DEPOT_TOOLS_DIR is located twice in PATH for spice.
  54. os.environ["PATH"] = os.pathsep.join([
  55. str(DEPOT_TOOLS_DIR),
  56. str(self.bin_dir),
  57. str(DEPOT_TOOLS_DIR),
  58. ])
  59. def tearDown(self):
  60. """Restore actions from setUp()."""
  61. os.environ["PATH"] = self.orig_path
  62. def test_not_in_path(self):
  63. """Test we don't find anything in PATH when not present."""
  64. self.assertIsNone(bazel._find_next_bazel_in_path())
  65. def test_in_path(self):
  66. """Test we find the next Bazel in PATH when present."""
  67. if sys.platform == "win32":
  68. launcher = self.bin_dir / "bazel.exe"
  69. else:
  70. launcher = self.bin_dir / "bazel"
  71. launcher.write_bytes(b"")
  72. launcher.chmod(0o755)
  73. self.assertEqual(bazel._find_next_bazel_in_path(), launcher)
  74. if __name__ == '__main__':
  75. unittest.main()