added release.txt blurb. Fixed spelling typo in Defaults.xml
[scons.git] / SCons / WarningsTests.py
blob70b459896e898f910842de03d28be8b2fda9b8cb
1 # MIT License
3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """Test Warnings module."""
26 import unittest
28 import SCons.Warnings
29 from SCons.Warnings import (
30 DependencyWarning,
31 DeprecatedWarning,
32 MandatoryDeprecatedWarning,
33 SConsWarning,
34 WarningOnByDefault,
38 class TestOutput:
39 """Callable class to use as ``_warningOut`` for capturing test output.
41 If we've already been called can reset by ``instance.out = None``
42 """
43 def __init__(self) -> None:
44 self.out = None
46 def __call__(self, x) -> None:
47 args = x.args[0]
48 if len(args) == 1:
49 args = args[0]
50 self.out = str(args)
52 class WarningsTestCase(unittest.TestCase):
53 def setUp(self) -> None:
54 # Setup global state
55 SCons.Warnings._enabled = []
56 SCons.Warnings._warningAsException = False
57 to = TestOutput()
58 SCons.Warnings._warningOut = to
60 def test_Warning(self) -> None:
61 """Test warn function."""
62 to = SCons.Warnings._warningOut
64 SCons.Warnings.enableWarningClass(SConsWarning)
65 SCons.Warnings.warn(DeprecatedWarning, "Foo")
66 assert to.out == "Foo", to.out
67 SCons.Warnings.warn(DependencyWarning, "Foo", 1)
68 assert to.out == "('Foo', 1)", to.out
70 def test_WarningAsExc(self) -> None:
71 """Test warnings as exceptions."""
72 to = SCons.Warnings._warningOut
74 SCons.Warnings.enableWarningClass(WarningOnByDefault)
75 old = SCons.Warnings.warningAsException(True)
76 self.assertFalse(old)
77 # an enabled warning should raise exception
78 self.assertRaises(SConsWarning, SCons.Warnings.warn, WarningOnByDefault, "Foo")
79 to.out = None
80 # a disabled exception should not raise
81 SCons.Warnings.warn(DeprecatedWarning, "Foo")
82 assert to.out == None, to.out
84 # make sure original behavior can be restored
85 prev = SCons.Warnings.warningAsException(old)
86 self.assertTrue(prev)
87 SCons.Warnings.warn(WarningOnByDefault, "Foo")
88 assert to.out == "Foo", to.out
90 def test_Disable(self) -> None:
91 """Test disabling/enabling warnings."""
92 to = SCons.Warnings._warningOut
94 # No warnings by default
95 SCons.Warnings.warn(DeprecatedWarning, "Foo")
96 assert to.out is None, to.out
98 SCons.Warnings.enableWarningClass(SConsWarning)
99 SCons.Warnings.warn(DeprecatedWarning, "Foo")
100 assert to.out == "Foo", to.out
102 to.out = None
103 SCons.Warnings.suppressWarningClass(DeprecatedWarning)
104 SCons.Warnings.warn(DeprecatedWarning, "Foo")
105 assert to.out is None, to.out
107 SCons.Warnings.warn(MandatoryDeprecatedWarning, "Foo")
108 assert to.out is None, to.out
110 # Dependency warnings should still be enabled though
111 SCons.Warnings.enableWarningClass(SConsWarning)
112 SCons.Warnings.warn(DependencyWarning, "Foo")
113 assert to.out == "Foo", to.out
115 # Try reenabling all warnings...
116 SCons.Warnings.enableWarningClass(SConsWarning)
118 SCons.Warnings.enableWarningClass(SConsWarning)
119 SCons.Warnings.warn(DeprecatedWarning, "Foo")
120 assert to.out == "Foo", to.out
122 if __name__ == "__main__":
123 unittest.main()
125 # Local Variables:
126 # tab-width:4
127 # indent-tabs-mode:nil
128 # End:
129 # vim: set expandtab tabstop=4 shiftwidth=4: