test_partial_clone.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # test_partial_clone.py -- Tests for partial clone filter specifications
  2. # Copyright (C) 2024 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 published 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 partial clone filter specifications."""
  22. from dulwich.partial_clone import (
  23. BlobLimitFilter,
  24. BlobNoneFilter,
  25. CombineFilter,
  26. SparseOidFilter,
  27. TreeDepthFilter,
  28. parse_filter_spec,
  29. )
  30. from . import TestCase
  31. class ParseFilterSpecTests(TestCase):
  32. """Test parse_filter_spec function."""
  33. def test_parse_blob_none(self):
  34. """Test parsing 'blob:none' filter."""
  35. filter_spec = parse_filter_spec("blob:none")
  36. self.assertIsInstance(filter_spec, BlobNoneFilter)
  37. self.assertEqual("blob:none", filter_spec.to_spec_string())
  38. def test_parse_blob_none_bytes(self):
  39. """Test parsing 'blob:none' as bytes."""
  40. filter_spec = parse_filter_spec(b"blob:none")
  41. self.assertIsInstance(filter_spec, BlobNoneFilter)
  42. def test_parse_blob_limit_bytes(self):
  43. """Test parsing 'blob:limit=100' in bytes."""
  44. filter_spec = parse_filter_spec("blob:limit=100")
  45. self.assertIsInstance(filter_spec, BlobLimitFilter)
  46. self.assertEqual(100, filter_spec.limit)
  47. def test_parse_blob_limit_kb(self):
  48. """Test parsing 'blob:limit=10k'."""
  49. filter_spec = parse_filter_spec("blob:limit=10k")
  50. self.assertIsInstance(filter_spec, BlobLimitFilter)
  51. self.assertEqual(10 * 1024, filter_spec.limit)
  52. def test_parse_blob_limit_mb(self):
  53. """Test parsing 'blob:limit=5m'."""
  54. filter_spec = parse_filter_spec("blob:limit=5m")
  55. self.assertIsInstance(filter_spec, BlobLimitFilter)
  56. self.assertEqual(5 * 1024 * 1024, filter_spec.limit)
  57. def test_parse_blob_limit_gb(self):
  58. """Test parsing 'blob:limit=1g'."""
  59. filter_spec = parse_filter_spec("blob:limit=1g")
  60. self.assertIsInstance(filter_spec, BlobLimitFilter)
  61. self.assertEqual(1024 * 1024 * 1024, filter_spec.limit)
  62. def test_parse_tree_depth(self):
  63. """Test parsing 'tree:0' filter."""
  64. filter_spec = parse_filter_spec("tree:0")
  65. self.assertIsInstance(filter_spec, TreeDepthFilter)
  66. self.assertEqual(0, filter_spec.max_depth)
  67. def test_parse_tree_depth_nonzero(self):
  68. """Test parsing 'tree:3' filter."""
  69. filter_spec = parse_filter_spec("tree:3")
  70. self.assertIsInstance(filter_spec, TreeDepthFilter)
  71. self.assertEqual(3, filter_spec.max_depth)
  72. def test_parse_sparse_oid(self):
  73. """Test parsing 'sparse:oid=<oid>' filter."""
  74. oid = b"1234567890abcdef1234567890abcdef12345678"
  75. filter_spec = parse_filter_spec(f"sparse:oid={oid.decode('ascii')}")
  76. self.assertIsInstance(filter_spec, SparseOidFilter)
  77. self.assertEqual(oid, filter_spec.oid)
  78. def test_parse_combine(self):
  79. """Test parsing 'combine:blob:none+tree:0' filter."""
  80. filter_spec = parse_filter_spec("combine:blob:none+tree:0")
  81. self.assertIsInstance(filter_spec, CombineFilter)
  82. self.assertEqual(2, len(filter_spec.filters))
  83. self.assertIsInstance(filter_spec.filters[0], BlobNoneFilter)
  84. self.assertIsInstance(filter_spec.filters[1], TreeDepthFilter)
  85. def test_parse_combine_multiple(self):
  86. """Test parsing combine filter with 3+ filters."""
  87. filter_spec = parse_filter_spec("combine:blob:none+tree:0+blob:limit=1m")
  88. self.assertIsInstance(filter_spec, CombineFilter)
  89. self.assertEqual(3, len(filter_spec.filters))
  90. def test_parse_unknown_spec(self):
  91. """Test that unknown filter specs raise ValueError."""
  92. with self.assertRaises(ValueError) as cm:
  93. parse_filter_spec("unknown:spec")
  94. self.assertIn("Unknown filter specification", str(cm.exception))
  95. def test_parse_invalid_tree_depth(self):
  96. """Test that invalid tree depth raises ValueError."""
  97. with self.assertRaises(ValueError) as cm:
  98. parse_filter_spec("tree:invalid")
  99. self.assertIn("Invalid tree depth", str(cm.exception))
  100. def test_parse_invalid_blob_limit(self):
  101. """Test that invalid blob limit raises ValueError."""
  102. with self.assertRaises(ValueError) as cm:
  103. parse_filter_spec("blob:limit=invalid")
  104. self.assertIn("Invalid size specification", str(cm.exception))
  105. class BlobNoneFilterTests(TestCase):
  106. """Test BlobNoneFilter class."""
  107. def test_should_include_blob(self):
  108. """Test that BlobNoneFilter excludes all blobs."""
  109. filter_spec = BlobNoneFilter()
  110. self.assertFalse(filter_spec.should_include_blob(0))
  111. self.assertFalse(filter_spec.should_include_blob(100))
  112. self.assertFalse(filter_spec.should_include_blob(1024 * 1024))
  113. def test_should_include_tree(self):
  114. """Test that BlobNoneFilter includes all trees."""
  115. filter_spec = BlobNoneFilter()
  116. self.assertTrue(filter_spec.should_include_tree(0))
  117. self.assertTrue(filter_spec.should_include_tree(1))
  118. self.assertTrue(filter_spec.should_include_tree(100))
  119. def test_to_spec_string(self):
  120. """Test conversion back to spec string."""
  121. filter_spec = BlobNoneFilter()
  122. self.assertEqual("blob:none", filter_spec.to_spec_string())
  123. def test_repr(self):
  124. """Test repr output."""
  125. filter_spec = BlobNoneFilter()
  126. self.assertEqual("BlobNoneFilter()", repr(filter_spec))
  127. class BlobLimitFilterTests(TestCase):
  128. """Test BlobLimitFilter class."""
  129. def test_should_include_blob_under_limit(self):
  130. """Test that blobs under limit are included."""
  131. filter_spec = BlobLimitFilter(1024)
  132. self.assertTrue(filter_spec.should_include_blob(0))
  133. self.assertTrue(filter_spec.should_include_blob(512))
  134. self.assertTrue(filter_spec.should_include_blob(1024))
  135. def test_should_include_blob_over_limit(self):
  136. """Test that blobs over limit are excluded."""
  137. filter_spec = BlobLimitFilter(1024)
  138. self.assertFalse(filter_spec.should_include_blob(1025))
  139. self.assertFalse(filter_spec.should_include_blob(2048))
  140. def test_should_include_tree(self):
  141. """Test that BlobLimitFilter includes all trees."""
  142. filter_spec = BlobLimitFilter(1024)
  143. self.assertTrue(filter_spec.should_include_tree(0))
  144. self.assertTrue(filter_spec.should_include_tree(100))
  145. def test_to_spec_string_bytes(self):
  146. """Test conversion to spec string with bytes."""
  147. filter_spec = BlobLimitFilter(100)
  148. self.assertEqual("blob:limit=100", filter_spec.to_spec_string())
  149. def test_to_spec_string_kb(self):
  150. """Test conversion to spec string with KB."""
  151. filter_spec = BlobLimitFilter(10 * 1024)
  152. self.assertEqual("blob:limit=10k", filter_spec.to_spec_string())
  153. def test_to_spec_string_mb(self):
  154. """Test conversion to spec string with MB."""
  155. filter_spec = BlobLimitFilter(5 * 1024 * 1024)
  156. self.assertEqual("blob:limit=5m", filter_spec.to_spec_string())
  157. def test_to_spec_string_gb(self):
  158. """Test conversion to spec string with GB."""
  159. filter_spec = BlobLimitFilter(2 * 1024 * 1024 * 1024)
  160. self.assertEqual("blob:limit=2g", filter_spec.to_spec_string())
  161. def test_to_spec_string_not_round(self):
  162. """Test conversion to spec string with non-round size."""
  163. filter_spec = BlobLimitFilter(1500)
  164. self.assertEqual("blob:limit=1500", filter_spec.to_spec_string())
  165. def test_repr(self):
  166. """Test repr output."""
  167. filter_spec = BlobLimitFilter(1024)
  168. self.assertEqual("BlobLimitFilter(limit=1024)", repr(filter_spec))
  169. class TreeDepthFilterTests(TestCase):
  170. """Test TreeDepthFilter class."""
  171. def test_should_include_blob(self):
  172. """Test that TreeDepthFilter includes all blobs."""
  173. filter_spec = TreeDepthFilter(0)
  174. self.assertTrue(filter_spec.should_include_blob(0))
  175. self.assertTrue(filter_spec.should_include_blob(1024))
  176. def test_should_include_tree_at_depth(self):
  177. """Test that trees at or below max_depth are included."""
  178. filter_spec = TreeDepthFilter(2)
  179. self.assertTrue(filter_spec.should_include_tree(0))
  180. self.assertTrue(filter_spec.should_include_tree(1))
  181. self.assertTrue(filter_spec.should_include_tree(2))
  182. def test_should_include_tree_beyond_depth(self):
  183. """Test that trees beyond max_depth are excluded."""
  184. filter_spec = TreeDepthFilter(2)
  185. self.assertFalse(filter_spec.should_include_tree(3))
  186. self.assertFalse(filter_spec.should_include_tree(10))
  187. def test_to_spec_string(self):
  188. """Test conversion back to spec string."""
  189. filter_spec = TreeDepthFilter(3)
  190. self.assertEqual("tree:3", filter_spec.to_spec_string())
  191. def test_repr(self):
  192. """Test repr output."""
  193. filter_spec = TreeDepthFilter(2)
  194. self.assertEqual("TreeDepthFilter(max_depth=2)", repr(filter_spec))
  195. class SparseOidFilterTests(TestCase):
  196. """Test SparseOidFilter class."""
  197. def test_should_include_blob(self):
  198. """Test that SparseOidFilter includes all blobs."""
  199. oid = b"1234567890abcdef1234567890abcdef12345678"
  200. filter_spec = SparseOidFilter(oid)
  201. self.assertTrue(filter_spec.should_include_blob(0))
  202. self.assertTrue(filter_spec.should_include_blob(1024))
  203. def test_should_include_tree(self):
  204. """Test that SparseOidFilter includes all trees."""
  205. oid = b"1234567890abcdef1234567890abcdef12345678"
  206. filter_spec = SparseOidFilter(oid)
  207. self.assertTrue(filter_spec.should_include_tree(0))
  208. self.assertTrue(filter_spec.should_include_tree(10))
  209. def test_to_spec_string(self):
  210. """Test conversion back to spec string."""
  211. oid = b"1234567890abcdef1234567890abcdef12345678"
  212. filter_spec = SparseOidFilter(oid)
  213. expected = "sparse:oid=1234567890abcdef1234567890abcdef12345678"
  214. self.assertEqual(expected, filter_spec.to_spec_string())
  215. def test_repr(self):
  216. """Test repr output."""
  217. oid = b"1234567890abcdef1234567890abcdef12345678"
  218. filter_spec = SparseOidFilter(oid)
  219. self.assertIn("SparseOidFilter", repr(filter_spec))
  220. self.assertIn("1234567890abcdef1234567890abcdef12345678", repr(filter_spec))
  221. class CombineFilterTests(TestCase):
  222. """Test CombineFilter class."""
  223. def test_should_include_blob_all_allow(self):
  224. """Test that blob is included when all filters allow it."""
  225. filters = [BlobLimitFilter(1024), BlobLimitFilter(2048)]
  226. filter_spec = CombineFilter(filters)
  227. self.assertTrue(filter_spec.should_include_blob(512))
  228. def test_should_include_blob_one_denies(self):
  229. """Test that blob is excluded when one filter denies it."""
  230. filters = [BlobLimitFilter(1024), BlobNoneFilter()]
  231. filter_spec = CombineFilter(filters)
  232. self.assertFalse(filter_spec.should_include_blob(512))
  233. def test_should_include_tree_all_allow(self):
  234. """Test that tree is included when all filters allow it."""
  235. filters = [TreeDepthFilter(2), TreeDepthFilter(3)]
  236. filter_spec = CombineFilter(filters)
  237. self.assertTrue(filter_spec.should_include_tree(1))
  238. def test_should_include_tree_one_denies(self):
  239. """Test that tree is excluded when one filter denies it."""
  240. filters = [TreeDepthFilter(2), TreeDepthFilter(1)]
  241. filter_spec = CombineFilter(filters)
  242. self.assertFalse(filter_spec.should_include_tree(2))
  243. def test_to_spec_string(self):
  244. """Test conversion back to spec string."""
  245. filters = [BlobNoneFilter(), TreeDepthFilter(0)]
  246. filter_spec = CombineFilter(filters)
  247. self.assertEqual("combine:blob:none+tree:0", filter_spec.to_spec_string())
  248. def test_repr(self):
  249. """Test repr output."""
  250. filters = [BlobNoneFilter()]
  251. filter_spec = CombineFilter(filters)
  252. self.assertIn("CombineFilter", repr(filter_spec))