blob: 073c699bc084c375d4dc32d4baa665da669e965a [file] [log] [blame]
Alex Gaynord27856c2015-01-01 20:29:13 -08001# 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
5from __future__ import absolute_import, division, print_function
6
7import sys
8import types
9import warnings
10
Cory Benfieldbeee9812016-03-21 11:38:20 +000011import pytest
12
Alex Gaynord27856c2015-01-01 20:29:13 -080013from cryptography.utils import deprecated
14
15
16class 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",
Lucia Lic6ba99d2021-11-08 22:06:11 +080024 warning_class=DeprecationWarning,
Alex Gaynord27856c2015-01-01 20:29:13 -080025 )
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 Gaynor75341e12015-01-02 09:18:17 -080033 mod.Z = 3
Alex Gaynord27856c2015-01-01 20:29:13 -080034
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 Gaynor75341e12015-01-02 09:18:17 -080040 assert mod.Z == 3
Alex Gaynord27856c2015-01-01 20:29:13 -080041
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 Gaynor75341e12015-01-02 09:18:17 -080048
49 assert "Y" in dir(mod)
Cory Benfieldbeee9812016-03-21 11:38:20 +000050
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",
Lucia Lic6ba99d2021-11-08 22:06:11 +080058 warning_class=DeprecationWarning,
Cory Benfieldbeee9812016-03-21 11:38:20 +000059 )
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