test_warnings.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from inspect import cleandoc
  2. import pytest
  3. from setuptools.warnings import SetuptoolsDeprecationWarning, SetuptoolsWarning
  4. _EXAMPLES = {
  5. "default": dict(
  6. args=("Hello {x}", "\n\t{target} {v:.1f}"),
  7. kwargs={"x": 5, "v": 3, "target": "World"},
  8. expected="""
  9. Hello 5
  10. !!
  11. ********************************************************************************
  12. World 3.0
  13. ********************************************************************************
  14. !!
  15. """,
  16. ),
  17. "futue_due_date": dict(
  18. args=("Summary", "Lorem ipsum"),
  19. kwargs={"due_date": (9999, 11, 22)},
  20. expected="""
  21. Summary
  22. !!
  23. ********************************************************************************
  24. Lorem ipsum
  25. By 9999-Nov-22, you need to update your project and remove deprecated calls
  26. or your builds will no longer be supported.
  27. ********************************************************************************
  28. !!
  29. """,
  30. ),
  31. "past_due_date_with_docs": dict(
  32. args=("Summary", "Lorem ipsum"),
  33. kwargs={"due_date": (2000, 11, 22), "see_docs": "some_page.html"},
  34. expected="""
  35. Summary
  36. !!
  37. ********************************************************************************
  38. Lorem ipsum
  39. This deprecation is overdue, please update your project and remove deprecated
  40. calls to avoid build errors in the future.
  41. See https://setuptools.pypa.io/en/latest/some_page.html for details.
  42. ********************************************************************************
  43. !!
  44. """,
  45. ),
  46. }
  47. @pytest.mark.parametrize("example_name", _EXAMPLES.keys())
  48. def test_formatting(monkeypatch, example_name):
  49. """
  50. It should automatically handle indentation, interpolation and things like due date.
  51. """
  52. args = _EXAMPLES[example_name]["args"]
  53. kwargs = _EXAMPLES[example_name]["kwargs"]
  54. expected = _EXAMPLES[example_name]["expected"]
  55. monkeypatch.setenv("SETUPTOOLS_ENFORCE_DEPRECATION", "false")
  56. with pytest.warns(SetuptoolsWarning) as warn_info:
  57. SetuptoolsWarning.emit(*args, **kwargs)
  58. assert _get_message(warn_info) == cleandoc(expected)
  59. def test_due_date_enforcement(monkeypatch):
  60. class _MyDeprecation(SetuptoolsDeprecationWarning):
  61. _SUMMARY = "Summary"
  62. _DETAILS = "Lorem ipsum"
  63. _DUE_DATE = (2000, 11, 22)
  64. _SEE_DOCS = "some_page.html"
  65. monkeypatch.setenv("SETUPTOOLS_ENFORCE_DEPRECATION", "true")
  66. with pytest.raises(SetuptoolsDeprecationWarning) as exc_info:
  67. _MyDeprecation.emit()
  68. expected = """
  69. Summary
  70. !!
  71. ********************************************************************************
  72. Lorem ipsum
  73. This deprecation is overdue, please update your project and remove deprecated
  74. calls to avoid build errors in the future.
  75. See https://setuptools.pypa.io/en/latest/some_page.html for details.
  76. ********************************************************************************
  77. !!
  78. """
  79. assert str(exc_info.value) == cleandoc(expected)
  80. def _get_message(warn_info):
  81. return next(warn.message.args[0] for warn in warn_info)