cl_tests.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. # Copyright 2022 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. import os
  6. from pathlib import Path
  7. import shutil
  8. import sys
  9. import tempfile
  10. import unittest
  11. sys.path.append(os.path.dirname(sys.path[0]))
  12. from impl.common import CROSVM_ROOT, cmd, quoted, TOOLS_ROOT
  13. git = cmd("git")
  14. cl = cmd(f"{TOOLS_ROOT}/cl")
  15. class ClTests(unittest.TestCase):
  16. test_root: Path
  17. def setUp(self):
  18. self.test_root = Path(tempfile.mkdtemp())
  19. os.chdir(self.test_root)
  20. git("clone", CROSVM_ROOT, ".").fg(quiet=True)
  21. # Set up user name (it's not set up in Luci)
  22. git("config user.name Nobody").fg(quiet=True)
  23. git("config user.email nobody@chromium.org").fg(quiet=True)
  24. # Check out a detached head and delete all branches.
  25. git("checkout -d HEAD").fg(quiet=True)
  26. branch_list = git("branch").lines()
  27. for branch in branch_list:
  28. if not branch.startswith("*"):
  29. git("branch -D", branch).fg(quiet=True)
  30. # Set up the origin for testing uploads and rebases.
  31. git("remote set-url origin https://chromium.googlesource.com/crosvm/crosvm").fg(quiet=True)
  32. git("fetch -q origin main").fg(quiet=True)
  33. git("fetch -q origin chromeos").fg(quiet=True)
  34. def tearDown(self) -> None:
  35. shutil.rmtree(self.test_root)
  36. def create_test_commit(self, message: str, branch: str, upstream: str = "origin/main"):
  37. git("checkout -b", branch, "--track", upstream).fg(quiet=True)
  38. with Path("Cargo.toml").open("a") as file:
  39. file.write("# Foo")
  40. git("commit -a -m", quoted(message)).fg(quiet=True)
  41. return git("rev-parse HEAD").stdout()
  42. def test_cl_upload(self):
  43. sha = self.create_test_commit("Test Commit", "foo")
  44. expected = f"""\
  45. Uploading to origin/main:
  46. {sha} Test Commit
  47. Not running: git push origin HEAD:refs/for/main%"""
  48. self.assertEqual(cl("upload --dry-run").stdout(), expected)
  49. def test_cl_status(self):
  50. self.create_test_commit("Test Commit", "foo")
  51. expected = """\
  52. Branch foo tracking origin/main
  53. NOT_UPLOADED Test Commit"""
  54. self.assertEqual(cl("status").stdout(), expected)
  55. def test_cl_rebase(self):
  56. self.create_test_commit("Test Commit", "foo", "origin/chromeos")
  57. cl("rebase").fg()
  58. # Expect foo-upstream to be tracking `main` and have the same commit
  59. self.assertEqual(git("rev-parse --abbrev-ref foo-upstream@{u}").stdout(), "origin/main")
  60. self.assertEqual(
  61. git("log -1 --format=%s foo").stdout(),
  62. git("log -1 --format=%s foo-upstream").stdout(),
  63. )
  64. def test_cl_rebase_with_existing_branch(self):
  65. previous_sha = self.create_test_commit("Previous commit", "foo-upstream ")
  66. self.create_test_commit("Test Commit", "foo", "origin/chromeos")
  67. message = cl("rebase").stdout()
  68. # `cl rebase` will overwrite the branch, but we should print the previous sha in case
  69. # the user needs to recover it.
  70. self.assertIn(previous_sha, message)
  71. # Expect foo-upstream to be tracking `main` and have the same commit. The previous commit
  72. # would be dropped.
  73. self.assertEqual(git("rev-parse --abbrev-ref foo-upstream@{u}").stdout(), "origin/main")
  74. self.assertEqual(
  75. git("log -1 --format=%s foo").stdout(),
  76. git("log -1 --format=%s foo-upstream").stdout(),
  77. )
  78. def test_prune(self):
  79. self.create_test_commit("Test Commit", "foo")
  80. git("branch foo-no-commit origin/main").fg()
  81. cl("prune --force").fg()
  82. # `foo` has unsubmitted commits, it should still be there.
  83. self.assertTrue(git("rev-parse foo").success())
  84. # `foo-no-commit` has no commits, it should have been pruned.
  85. self.assertFalse(git("rev-parse foo-no-commit").success())
  86. if __name__ == "__main__":
  87. unittest.main(warnings="ignore")