test_porcelain.py 5.3 KB

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