Merge pull request #4655 from bdbaddog/fix_new_ninja_package
[scons.git] / SCons / Tool / MSCommon / MSVC / PolicyTests.py
bloba312d11917b92f628bea0c16ff61f23b035c57a7
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 """
25 Test Microsoft Visual C/C++ policy handlers.
26 """
28 import unittest
30 import SCons.Warnings
32 from SCons.Tool.MSCommon.MSVC import Policy
34 from SCons.Tool.MSCommon.MSVC.Exceptions import (
35 MSVCArgumentError,
36 MSVCVersionNotFound,
37 MSVCScriptExecutionError,
40 from SCons.Tool.MSCommon.MSVC.Warnings import (
41 MSVCScriptExecutionWarning,
44 class PolicyTests(unittest.TestCase):
46 def setUp(self) -> None:
47 self.warnstack = []
49 def push_warning_as_exception(self, warning_class) -> None:
50 SCons.Warnings.enableWarningClass(warning_class)
51 prev_state = SCons.Warnings.warningAsException()
52 self.warnstack.append((warning_class, prev_state))
54 def pop_warning_as_exception(self) -> None:
55 warning_class, prev_state = self.warnstack.pop()
56 SCons.Warnings.warningAsException(prev_state)
57 SCons.Warnings.suppressWarningClass(warning_class)
59 # msvc_set_notfound_policy, msvc_get_notfound_policy, and MSVC_NOTFOUND_POLICY
61 def test_notfound_func_valid_symbols(self) -> None:
62 def_policy = Policy.msvc_get_notfound_policy()
63 last_policy = def_policy
64 for notfound_def in Policy.MSVC_NOTFOUND_DEFINITION_LIST:
65 for symbol in [notfound_def.symbol, notfound_def.symbol.lower(), notfound_def.symbol.upper()]:
66 prev_policy = Policy.msvc_set_notfound_policy(symbol)
67 self.assertTrue(prev_policy == last_policy, "notfound policy: {} != {}".format(
68 repr(prev_policy), repr(last_policy)
70 cur_set_policy = Policy.msvc_set_notfound_policy()
71 cur_get_policy = Policy.msvc_get_notfound_policy()
72 self.assertTrue(cur_set_policy == cur_get_policy, "notfound policy: {} != {}".format(
73 repr(cur_set_policy), repr(cur_get_policy)
75 last_policy = cur_get_policy
76 Policy.msvc_set_notfound_policy(def_policy)
78 def test_notfound_func_invalid_symbol(self) -> None:
79 with self.assertRaises(MSVCArgumentError):
80 Policy.msvc_set_notfound_policy('Undefined')
82 def test_notfound_handler_invalid_symbol(self) -> None:
83 with self.assertRaises(MSVCArgumentError):
84 Policy.msvc_notfound_handler({'MSVC_NOTFOUND_POLICY': 'Undefined'}, '')
86 def test_notfound_handler_ignore(self) -> None:
87 def_policy = Policy.msvc_set_notfound_policy('Ignore')
88 Policy.msvc_notfound_handler(None, '')
89 Policy.msvc_notfound_handler({'MSVC_NOTFOUND_POLICY': None}, '')
90 Policy.msvc_set_notfound_policy(def_policy)
92 def test_notfound_handler_warning(self) -> None:
93 # treat warning as exception for testing
94 self.push_warning_as_exception(SCons.Warnings.VisualCMissingWarning)
95 def_policy = Policy.msvc_set_notfound_policy('Warning')
96 with self.assertRaises(SCons.Warnings.VisualCMissingWarning):
97 Policy.msvc_notfound_handler(None, '')
98 Policy.msvc_set_notfound_policy('Ignore')
99 with self.assertRaises(SCons.Warnings.VisualCMissingWarning):
100 Policy.msvc_notfound_handler({'MSVC_NOTFOUND_POLICY': 'Warning'}, '')
101 Policy.msvc_set_notfound_policy(def_policy)
102 self.pop_warning_as_exception()
104 def test_notfound_handler_error(self) -> None:
105 def_policy = Policy.msvc_set_notfound_policy('Error')
106 with self.assertRaises(MSVCVersionNotFound):
107 Policy.msvc_notfound_handler(None, '')
108 Policy.msvc_set_notfound_policy('Ignore')
109 with self.assertRaises(MSVCVersionNotFound):
110 Policy.msvc_notfound_handler({'MSVC_NOTFOUND_POLICY': 'Error'}, '')
111 Policy.msvc_set_notfound_policy(def_policy)
113 # msvc_set_scripterror_policy, msvc_get_scripterror_policy, and MSVC_SCRIPTERROR_POLICY
115 def test_scripterror_func_valid_symbols(self) -> None:
116 def_policy = Policy.msvc_get_scripterror_policy()
117 last_policy = def_policy
118 for scripterror_def in Policy.MSVC_SCRIPTERROR_DEFINITION_LIST:
119 for symbol in [scripterror_def.symbol, scripterror_def.symbol.lower(), scripterror_def.symbol.upper()]:
120 prev_policy = Policy.msvc_set_scripterror_policy(symbol)
121 self.assertTrue(prev_policy == last_policy, "scripterror policy: {} != {}".format(
122 repr(prev_policy), repr(last_policy)
124 cur_set_policy = Policy.msvc_set_scripterror_policy()
125 cur_get_policy = Policy.msvc_get_scripterror_policy()
126 self.assertTrue(cur_set_policy == cur_get_policy, "scripterror policy: {} != {}".format(
127 repr(cur_set_policy), repr(cur_get_policy)
129 last_policy = cur_get_policy
130 Policy.msvc_set_scripterror_policy(def_policy)
132 def test_scripterror_func_invalid_symbol(self) -> None:
133 with self.assertRaises(MSVCArgumentError):
134 Policy.msvc_set_scripterror_policy('Undefined')
136 def test_scripterror_handler_invalid_symbol(self) -> None:
137 with self.assertRaises(MSVCArgumentError):
138 Policy.msvc_scripterror_handler({'MSVC_SCRIPTERROR_POLICY': 'Undefined'}, '')
140 def test_scripterror_handler_ignore(self) -> None:
141 def_policy = Policy.msvc_set_scripterror_policy('Ignore')
142 Policy.msvc_scripterror_handler(None, '')
143 Policy.msvc_scripterror_handler({'MSVC_SCRIPTERROR_POLICY': None}, '')
144 Policy.msvc_set_scripterror_policy(def_policy)
146 def test_scripterror_handler_warning(self) -> None:
147 # treat warning as exception for testing
148 self.push_warning_as_exception(MSVCScriptExecutionWarning)
149 def_policy = Policy.msvc_set_scripterror_policy('Warning')
150 with self.assertRaises(MSVCScriptExecutionWarning):
151 Policy.msvc_scripterror_handler(None, '')
152 Policy.msvc_set_scripterror_policy('Ignore')
153 with self.assertRaises(MSVCScriptExecutionWarning):
154 Policy.msvc_scripterror_handler({'MSVC_SCRIPTERROR_POLICY': 'Warning'}, '')
155 Policy.msvc_set_scripterror_policy(def_policy)
156 self.pop_warning_as_exception()
158 def test_scripterror_handler_error(self) -> None:
159 def_policy = Policy.msvc_set_scripterror_policy('Error')
160 with self.assertRaises(MSVCScriptExecutionError):
161 Policy.msvc_scripterror_handler(None, '')
162 Policy.msvc_set_scripterror_policy('Ignore')
163 with self.assertRaises(MSVCScriptExecutionError):
164 Policy.msvc_scripterror_handler({'MSVC_SCRIPTERROR_POLICY': 'Error'}, '')
165 Policy.msvc_set_scripterror_policy(def_policy)
167 if __name__ == "__main__":
168 unittest.main()