[ci skip] Add note that this change may break SetOption() + ninja usage with fix
[scons.git] / SCons / ErrorsTests.py
blob4241750e3ed5cd725bebe555bd7fe371bbd03a2c
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 import unittest
26 import SCons.Errors
29 class ErrorsTestCase(unittest.TestCase):
30 def test_BuildError(self):
31 """Test the BuildError exception."""
32 try:
33 raise SCons.Errors.BuildError(
34 errstr = "foo", status=57, filename="file", exc_info=(1,2,3),
35 node = "n", executor="e", action="a", command="c")
36 except SCons.Errors.BuildError as e:
37 assert e.errstr == "foo"
38 assert e.status == 57
39 assert e.exitstatus == 2, e.exitstatus
40 assert e.filename == "file"
41 assert e.exc_info == (1,2,3)
43 assert e.node == "n"
44 assert e.executor == "e"
45 assert e.action == "a"
46 assert e.command == "c"
48 try:
49 raise SCons.Errors.BuildError("n", "foo", 57, 3, "file",
50 "e", "a", "c", (1,2,3))
51 except SCons.Errors.BuildError as e:
52 assert e.errstr == "foo", e.errstr
53 assert e.status == 57, e.status
54 assert e.exitstatus == 3, e.exitstatus
55 assert e.filename == "file", e.filename
56 assert e.exc_info == (1,2,3), e.exc_info
58 assert e.node == "n"
59 assert e.executor == "e"
60 assert e.action == "a"
61 assert e.command == "c"
63 try:
64 raise SCons.Errors.BuildError()
65 except SCons.Errors.BuildError as e:
66 assert e.errstr == "Unknown error"
67 assert e.status == 2
68 assert e.exitstatus == 2
69 assert e.filename is None
70 assert e.exc_info == (None, None, None)
72 assert e.node is None
73 assert e.executor is None
74 assert e.action is None
75 assert e.command is None
77 def test_InternalError(self):
78 """Test the InternalError exception."""
79 try:
80 raise SCons.Errors.InternalError("test internal error")
81 except SCons.Errors.InternalError as e:
82 assert e.args == ("test internal error",)
84 def test_UserError(self):
85 """Test the UserError exception."""
86 try:
87 raise SCons.Errors.UserError("test user error")
88 except SCons.Errors.UserError as e:
89 assert e.args == ("test user error",)
91 def test_ExplicitExit(self):
92 """Test the ExplicitExit exception."""
93 try:
94 raise SCons.Errors.ExplicitExit("node")
95 except SCons.Errors.ExplicitExit as e:
96 assert e.node == "node"
98 def test_convert_EnvironmentError_to_BuildError(self) -> None:
99 """Test convert_to_BuildError on SConsEnvironmentError."""
100 ee = SCons.Errors.SConsEnvironmentError("test env error")
101 be = SCons.Errors.convert_to_BuildError(ee)
102 with self.subTest():
103 self.assertEqual(be.errstr, "test env error")
104 with self.subTest():
105 self.assertEqual(be.status, 2)
106 with self.subTest():
107 self.assertEqual(be.exitstatus, 2)
108 with self.subTest():
109 self.assertIsNone(be.filename)
111 def test_convert_OSError_to_BuildError(self) -> None:
112 """Test convert_to_BuildError on OSError."""
113 ose = OSError(7, 'test oserror')
114 be = SCons.Errors.convert_to_BuildError(ose)
115 with self.subTest():
116 self.assertEqual(be.errstr, 'test oserror')
117 with self.subTest():
118 self.assertEqual(be.status, 7)
119 with self.subTest():
120 self.assertEqual(be.exitstatus, 2)
121 with self.subTest():
122 self.assertIsNone(be.filename)
124 def test_convert_phony_OSError_to_BuildError(self) -> None:
125 """Test convert_to_BuildError on OSError with defaults."""
126 class PhonyException(OSError):
127 def __init__(self, name):
128 OSError.__init__(self, name) # most fields will default to None
129 self.name = name
131 ose = PhonyException("test oserror")
132 be = SCons.Errors.convert_to_BuildError(ose)
133 with self.subTest():
134 self.assertEqual(be.errstr, 'test oserror')
135 with self.subTest():
136 self.assertEqual(be.status, 2)
137 with self.subTest():
138 self.assertEqual(be.exitstatus, 2)
139 with self.subTest():
140 self.assertIsNone(be.filename)
143 if __name__ == "__main__":
144 unittest.main()
146 # Local Variables:
147 # tab-width:4
148 # indent-tabs-mode:nil
149 # End:
150 # vim: set expandtab tabstop=4 shiftwidth=4: