blob: a74ed2d739c5a6e490072315565963686a1d97c5 [file] [log] [blame]
Mike Frysinger057905f2021-02-18 23:28:32 -05001# Copyright 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unittests for the error.py module."""
16
17import inspect
18import pickle
19import unittest
20
Jason Changf9aacd42023-08-03 14:38:00 -070021import command
Mike Frysinger64477332023-08-21 21:20:32 -040022import error
23import fetch
24import git_command
25import project
Jason Changa6413f52023-07-26 13:23:40 -070026from subcmds import all_modules
27
Mike Frysinger64477332023-08-21 21:20:32 -040028
Jason Changa6413f52023-07-26 13:23:40 -070029imports = all_modules + [
30 error,
31 project,
32 git_command,
Jason Changf9aacd42023-08-03 14:38:00 -070033 fetch,
34 command,
Jason Changa6413f52023-07-26 13:23:40 -070035]
Mike Frysinger057905f2021-02-18 23:28:32 -050036
37
38class PickleTests(unittest.TestCase):
Gavin Makea2e3302023-03-11 06:46:20 +000039 """Make sure all our custom exceptions can be pickled."""
Mike Frysinger057905f2021-02-18 23:28:32 -050040
Gavin Makea2e3302023-03-11 06:46:20 +000041 def getExceptions(self):
42 """Return all our custom exceptions."""
Jason Changa6413f52023-07-26 13:23:40 -070043 for entry in imports:
44 for name in dir(entry):
45 cls = getattr(entry, name)
46 if isinstance(cls, type) and issubclass(cls, Exception):
47 yield cls
Mike Frysinger057905f2021-02-18 23:28:32 -050048
Gavin Makea2e3302023-03-11 06:46:20 +000049 def testExceptionLookup(self):
50 """Make sure our introspection logic works."""
51 classes = list(self.getExceptions())
52 self.assertIn(error.HookError, classes)
53 # Don't assert the exact number to avoid being a change-detector test.
54 self.assertGreater(len(classes), 10)
Mike Frysinger057905f2021-02-18 23:28:32 -050055
Gavin Makea2e3302023-03-11 06:46:20 +000056 def testPickle(self):
57 """Try to pickle all the exceptions."""
58 for cls in self.getExceptions():
59 args = inspect.getfullargspec(cls.__init__).args[1:]
60 obj = cls(*args)
61 p = pickle.dumps(obj)
62 try:
63 newobj = pickle.loads(p)
64 except Exception as e: # pylint: disable=broad-except
65 self.fail(
66 "Class %s is unable to be pickled: %s\n"
67 "Incomplete super().__init__(...) call?" % (cls, e)
68 )
69 self.assertIsInstance(newobj, cls)
70 self.assertEqual(str(obj), str(newobj))