Alex Gaynor | d27856c | 2015-01-01 20:29:13 -0800 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
| 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
| 7 | import sys |
| 8 | import types |
| 9 | import warnings |
| 10 | |
Cory Benfield | beee981 | 2016-03-21 11:38:20 +0000 | [diff] [blame^] | 11 | import pytest |
| 12 | |
Alex Gaynor | d27856c | 2015-01-01 20:29:13 -0800 | [diff] [blame] | 13 | from cryptography.utils import deprecated |
| 14 | |
| 15 | |
| 16 | class TestDeprecated(object): |
| 17 | def test_deprecated(self, monkeypatch): |
| 18 | mod = types.ModuleType("TestDeprecated/test_deprecated") |
| 19 | monkeypatch.setitem(sys.modules, mod.__name__, mod) |
| 20 | mod.X = deprecated( |
| 21 | value=1, |
| 22 | module_name=mod.__name__, |
| 23 | message="deprecated message text", |
| 24 | warning_class=DeprecationWarning |
| 25 | ) |
| 26 | mod.Y = deprecated( |
| 27 | value=2, |
| 28 | module_name=mod.__name__, |
| 29 | message="more deprecated text", |
| 30 | warning_class=PendingDeprecationWarning, |
| 31 | ) |
| 32 | mod = sys.modules[mod.__name__] |
Alex Gaynor | 75341e1 | 2015-01-02 09:18:17 -0800 | [diff] [blame] | 33 | mod.Z = 3 |
Alex Gaynor | d27856c | 2015-01-01 20:29:13 -0800 | [diff] [blame] | 34 | |
| 35 | with warnings.catch_warnings(record=True) as log: |
| 36 | warnings.simplefilter("always", PendingDeprecationWarning) |
| 37 | warnings.simplefilter("always", DeprecationWarning) |
| 38 | assert mod.X == 1 |
| 39 | assert mod.Y == 2 |
Alex Gaynor | 75341e1 | 2015-01-02 09:18:17 -0800 | [diff] [blame] | 40 | assert mod.Z == 3 |
Alex Gaynor | d27856c | 2015-01-01 20:29:13 -0800 | [diff] [blame] | 41 | |
| 42 | [msg1, msg2] = log |
| 43 | assert msg1.category is DeprecationWarning |
| 44 | assert msg1.message.args == ("deprecated message text",) |
| 45 | |
| 46 | assert msg2.category is PendingDeprecationWarning |
| 47 | assert msg2.message.args == ("more deprecated text",) |
Alex Gaynor | 75341e1 | 2015-01-02 09:18:17 -0800 | [diff] [blame] | 48 | |
| 49 | assert "Y" in dir(mod) |
Cory Benfield | beee981 | 2016-03-21 11:38:20 +0000 | [diff] [blame^] | 50 | |
| 51 | def test_deleting_deprecated_members(self, monkeypatch): |
| 52 | mod = types.ModuleType("TestDeprecated/test_deprecated") |
| 53 | monkeypatch.setitem(sys.modules, mod.__name__, mod) |
| 54 | mod.X = deprecated( |
| 55 | value=1, |
| 56 | module_name=mod.__name__, |
| 57 | message="deprecated message text", |
| 58 | warning_class=DeprecationWarning |
| 59 | ) |
| 60 | mod.Y = deprecated( |
| 61 | value=2, |
| 62 | module_name=mod.__name__, |
| 63 | message="more deprecated text", |
| 64 | warning_class=PendingDeprecationWarning, |
| 65 | ) |
| 66 | mod = sys.modules[mod.__name__] |
| 67 | mod.Z = 3 |
| 68 | |
| 69 | with warnings.catch_warnings(record=True) as log: |
| 70 | warnings.simplefilter("always", PendingDeprecationWarning) |
| 71 | warnings.simplefilter("always", DeprecationWarning) |
| 72 | del mod.X |
| 73 | del mod.Y |
| 74 | del mod.Z |
| 75 | |
| 76 | [msg1, msg2] = log |
| 77 | assert msg1.category is DeprecationWarning |
| 78 | assert msg1.message.args == ("deprecated message text",) |
| 79 | |
| 80 | assert msg2.category is PendingDeprecationWarning |
| 81 | assert msg2.message.args == ("more deprecated text",) |
| 82 | |
| 83 | assert "X" not in dir(mod) |
| 84 | assert "Y" not in dir(mod) |
| 85 | assert "Z" not in dir(mod) |
| 86 | |
| 87 | with pytest.raises(AttributeError): |
| 88 | del mod.X |