test_stash.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # test_stash.py -- tests for stash
  2. # Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
  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. """Tests for stashes."""
  22. import os
  23. import shutil
  24. import tempfile
  25. from dulwich.objects import Blob, Tree
  26. from dulwich.repo import Repo
  27. from dulwich.stash import DEFAULT_STASH_REF, Stash
  28. from . import TestCase
  29. class StashTests(TestCase):
  30. """Tests for stash."""
  31. def setUp(self):
  32. self.test_dir = tempfile.mkdtemp()
  33. self.repo_dir = os.path.join(self.test_dir, "repo")
  34. os.makedirs(self.repo_dir)
  35. self.repo = Repo.init(self.repo_dir)
  36. # Create initial commit so we can have a HEAD
  37. blob = Blob()
  38. blob.data = b"initial data"
  39. self.repo.object_store.add_object(blob)
  40. tree = Tree()
  41. tree.add(b"initial.txt", 0o100644, blob.id)
  42. tree_id = self.repo.object_store.add_object(tree)
  43. self.commit_id = self.repo.do_commit(b"Initial commit", tree=tree_id)
  44. def tearDown(self):
  45. shutil.rmtree(self.test_dir)
  46. def test_obtain(self) -> None:
  47. stash = Stash.from_repo(self.repo)
  48. self.assertIsInstance(stash, Stash)
  49. def test_empty_stash(self) -> None:
  50. stash = Stash.from_repo(self.repo)
  51. # Make sure logs directory exists for reflog
  52. os.makedirs(os.path.join(self.repo.commondir(), "logs"), exist_ok=True)
  53. self.assertEqual(0, len(stash))
  54. self.assertEqual([], list(stash.stashes()))
  55. def test_push_stash(self) -> None:
  56. stash = Stash.from_repo(self.repo)
  57. # Make sure logs directory exists for reflog
  58. os.makedirs(os.path.join(self.repo.commondir(), "logs"), exist_ok=True)
  59. # Create a file and add it to the index
  60. file_path = os.path.join(self.repo_dir, "testfile.txt")
  61. with open(file_path, "wb") as f:
  62. f.write(b"test data")
  63. self.repo.stage(["testfile.txt"])
  64. # Push to stash
  65. commit_id = stash.push(message=b"Test stash message")
  66. self.assertIsNotNone(commit_id)
  67. # Verify stash was created
  68. self.assertEqual(1, len(stash))
  69. # Verify stash entry
  70. entry = stash[0]
  71. self.assertEqual(commit_id, entry.new_sha)
  72. self.assertTrue(b"Test stash message" in entry.message)
  73. def test_drop_stash(self) -> None:
  74. stash = Stash.from_repo(self.repo)
  75. # Make sure logs directory exists for reflog
  76. logs_dir = os.path.join(self.repo.commondir(), "logs")
  77. os.makedirs(logs_dir, exist_ok=True)
  78. # Create a couple of files and stash them
  79. file1_path = os.path.join(self.repo_dir, "testfile1.txt")
  80. with open(file1_path, "wb") as f:
  81. f.write(b"test data 1")
  82. self.repo.stage(["testfile1.txt"])
  83. commit_id1 = stash.push(message=b"Test stash 1")
  84. file2_path = os.path.join(self.repo_dir, "testfile2.txt")
  85. with open(file2_path, "wb") as f:
  86. f.write(b"test data 2")
  87. self.repo.stage(["testfile2.txt"])
  88. stash.push(message=b"Test stash 2")
  89. self.assertEqual(2, len(stash))
  90. # Drop the newest stash
  91. stash.drop(0)
  92. self.assertEqual(1, len(stash))
  93. self.assertEqual(commit_id1, stash[0].new_sha)
  94. # Drop the remaining stash
  95. stash.drop(0)
  96. self.assertEqual(0, len(stash))
  97. self.assertNotIn(DEFAULT_STASH_REF, self.repo.refs)
  98. def test_custom_ref(self) -> None:
  99. custom_ref = b"refs/custom_stash"
  100. stash = Stash(self.repo, ref=custom_ref)
  101. self.assertEqual(custom_ref, stash._ref)