test_porcelain.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # test_porcelain .py -- Tests for dulwich.porcelain/CGit compatibility
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Compatibility tests for dulwich.porcelain."""
  21. import os
  22. from unittest import skipIf
  23. from dulwich import porcelain
  24. from dulwich.tests.utils import build_commit_graph
  25. from ..test_porcelain import PorcelainGpgTestCase
  26. from .utils import CompatTestCase, run_git_or_fail
  27. try:
  28. import gpgme
  29. except ImportError:
  30. gpgme = None
  31. @skipIf(
  32. gpgme is None,
  33. "gpgme not available, skipping tests that require GPG signing",
  34. )
  35. class TagCreateSignTestCase(PorcelainGpgTestCase, CompatTestCase):
  36. def test_sign(self) -> None:
  37. # Test that dulwich signatures can be verified by CGit
  38. c1, c2, c3 = build_commit_graph(
  39. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  40. )
  41. self.repo.refs[b"HEAD"] = c3.id
  42. cfg = self.repo.get_config()
  43. cfg.set(("user",), "signingKey", PorcelainGpgTestCase.DEFAULT_KEY_ID)
  44. self.import_default_key()
  45. porcelain.tag_create(
  46. self.repo.path,
  47. b"tryme",
  48. b"foo <foo@bar.com>",
  49. b"bar",
  50. annotated=True,
  51. sign=True,
  52. )
  53. run_git_or_fail(
  54. [f"--git-dir={self.repo.controldir()}", "tag", "-v", "tryme"],
  55. env={"GNUPGHOME": os.environ["GNUPGHOME"]},
  56. )
  57. def test_verify(self) -> None:
  58. # Test that CGit signatures can be verified by dulwich
  59. c1, c2, c3 = build_commit_graph(
  60. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  61. )
  62. self.repo.refs[b"HEAD"] = c3.id
  63. self.import_default_key()
  64. run_git_or_fail(
  65. [
  66. f"--git-dir={self.repo.controldir()}",
  67. "tag",
  68. "-u",
  69. PorcelainGpgTestCase.DEFAULT_KEY_ID,
  70. "-m",
  71. "foo",
  72. "verifyme",
  73. ],
  74. env={
  75. "GNUPGHOME": os.environ["GNUPGHOME"],
  76. "GIT_COMMITTER_NAME": "Joe Example",
  77. "GIT_COMMITTER_EMAIL": "joe@example.com",
  78. },
  79. )
  80. tag = self.repo[b"refs/tags/verifyme"]
  81. self.assertNotEqual(tag.signature, None)
  82. tag.verify()
  83. @skipIf(
  84. gpgme is None,
  85. "gpgme not available, skipping tests that require GPG signing",
  86. )
  87. class CommitCreateSignTestCase(PorcelainGpgTestCase, CompatTestCase):
  88. def test_sign(self):
  89. # Test that dulwich signatures can be verified by CGit
  90. cfg = self.repo.get_config()
  91. cfg.set(("user",), "signingKey", PorcelainGpgTestCase.DEFAULT_KEY_ID)
  92. self.import_default_key()
  93. porcelain.commit(
  94. self.repo.path,
  95. b"messy message messiah",
  96. b"foo <foo@b.ar>",
  97. signoff=True,
  98. )
  99. run_git_or_fail(
  100. [f"--git-dir={self.repo.controldir()}", "verify-commit", "-v", "HEAD"],
  101. env={"GNUPGHOME": os.environ["GNUPGHOME"]},
  102. )
  103. def test_verify(self):
  104. # Test that CGit signatures can be verified by dulwich
  105. self.import_default_key()
  106. run_git_or_fail(
  107. [
  108. f"--git-dir={self.repo.controldir()}",
  109. "commit",
  110. "--allow-empty",
  111. "-S" + PorcelainGpgTestCase.DEFAULT_KEY_ID,
  112. "-m",
  113. "foo",
  114. ],
  115. env={
  116. "GNUPGHOME": os.environ["GNUPGHOME"],
  117. "GIT_COMMITTER_NAME": "Joe Example",
  118. "GIT_COMMITTER_EMAIL": "joe@example.com",
  119. },
  120. )
  121. commit = self.repo[b"HEAD"]
  122. self.assertNotEqual(commit.gpgsig, None)
  123. commit.verify()
  124. def test_verify_with_empty_message(self):
  125. # Test that CGit signatures can be verified by dulwich
  126. self.import_default_key()
  127. run_git_or_fail(
  128. [
  129. f"--git-dir={self.repo.controldir()}",
  130. "commit",
  131. "--allow-empty",
  132. "-S" + PorcelainGpgTestCase.DEFAULT_KEY_ID,
  133. "--allow-empty-message",
  134. "-m",
  135. "",
  136. ],
  137. env={
  138. "GNUPGHOME": os.environ["GNUPGHOME"],
  139. "GIT_COMMITTER_NAME": "Joe Example",
  140. "GIT_COMMITTER_EMAIL": "joe@example.com",
  141. },
  142. )
  143. commit = self.repo[b"HEAD"]
  144. self.assertNotEqual(commit.gpgsig, None)
  145. commit.verify()