test___init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # test___init__.py -- tests for __init__.py
  2. # Copyright (C) 2025 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 dulwich __init__ module."""
  22. import sys
  23. import warnings
  24. from unittest import mock
  25. from . import TestCase
  26. class ReplaceMeDecoratorTests(TestCase):
  27. """Tests for the replace_me decorator fallback implementation."""
  28. def test_replace_me_with_since_only(self) -> None:
  29. """Test replace_me decorator with only 'since' parameter."""
  30. # Mock dissolve to not be available
  31. with mock.patch.dict(sys.modules, {"dissolve": None}):
  32. # Need to reimport to get the fallback implementation
  33. import importlib
  34. import dulwich
  35. importlib.reload(dulwich)
  36. @dulwich.replace_me(since=(0, 1, 0))
  37. def deprecated_func():
  38. return "result"
  39. with warnings.catch_warnings(record=True) as w:
  40. warnings.simplefilter("always")
  41. result = deprecated_func()
  42. self.assertEqual("result", result)
  43. self.assertEqual(1, len(w))
  44. self.assertTrue(issubclass(w[0].category, DeprecationWarning))
  45. self.assertEqual(
  46. "deprecated_func is deprecated since (0, 1, 0)",
  47. str(w[0].message),
  48. )
  49. def test_replace_me_with_remove_in_only(self) -> None:
  50. """Test replace_me decorator with only 'remove_in' parameter."""
  51. with mock.patch.dict(sys.modules, {"dissolve": None}):
  52. import importlib
  53. import dulwich
  54. importlib.reload(dulwich)
  55. @dulwich.replace_me(remove_in=(2, 0, 0))
  56. def deprecated_func():
  57. return "result"
  58. with warnings.catch_warnings(record=True) as w:
  59. warnings.simplefilter("always")
  60. result = deprecated_func()
  61. self.assertEqual("result", result)
  62. self.assertEqual(1, len(w))
  63. self.assertTrue(issubclass(w[0].category, DeprecationWarning))
  64. self.assertEqual(
  65. "deprecated_func is deprecated and will be removed in (2, 0, 0)",
  66. str(w[0].message),
  67. )
  68. def test_replace_me_with_neither_parameter(self) -> None:
  69. """Test replace_me decorator with neither 'since' nor 'remove_in'."""
  70. with mock.patch.dict(sys.modules, {"dissolve": None}):
  71. import importlib
  72. import dulwich
  73. importlib.reload(dulwich)
  74. @dulwich.replace_me()
  75. def deprecated_func():
  76. return "result"
  77. with warnings.catch_warnings(record=True) as w:
  78. warnings.simplefilter("always")
  79. result = deprecated_func()
  80. self.assertEqual("result", result)
  81. self.assertEqual(1, len(w))
  82. self.assertTrue(issubclass(w[0].category, DeprecationWarning))
  83. self.assertEqual(
  84. "deprecated_func is deprecated and will be removed in a future version",
  85. str(w[0].message),
  86. )
  87. def test_replace_me_with_both_parameters(self) -> None:
  88. """Test replace_me decorator with both 'since' and 'remove_in'."""
  89. with mock.patch.dict(sys.modules, {"dissolve": None}):
  90. import importlib
  91. import dulwich
  92. importlib.reload(dulwich)
  93. @dulwich.replace_me(since=(0, 1, 0), remove_in=(2, 0, 0))
  94. def deprecated_func():
  95. return "result"
  96. with warnings.catch_warnings(record=True) as w:
  97. warnings.simplefilter("always")
  98. result = deprecated_func()
  99. self.assertEqual("result", result)
  100. self.assertEqual(1, len(w))
  101. self.assertTrue(issubclass(w[0].category, DeprecationWarning))
  102. self.assertEqual(
  103. "deprecated_func is deprecated since (0, 1, 0) and will be removed in (2, 0, 0)",
  104. str(w[0].message),
  105. )