Mike Frysinger | 057905f | 2021-02-18 23:28:32 -0500 | [diff] [blame] | 1 | # 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 | |
| 17 | import inspect |
| 18 | import pickle |
| 19 | import unittest |
| 20 | |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 21 | import command |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 22 | import error |
| 23 | import fetch |
| 24 | import git_command |
| 25 | import project |
Jason Chang | a6413f5 | 2023-07-26 13:23:40 -0700 | [diff] [blame] | 26 | from subcmds import all_modules |
| 27 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 28 | |
Jason Chang | a6413f5 | 2023-07-26 13:23:40 -0700 | [diff] [blame] | 29 | imports = all_modules + [ |
| 30 | error, |
| 31 | project, |
| 32 | git_command, |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 33 | fetch, |
| 34 | command, |
Jason Chang | a6413f5 | 2023-07-26 13:23:40 -0700 | [diff] [blame] | 35 | ] |
Mike Frysinger | 057905f | 2021-02-18 23:28:32 -0500 | [diff] [blame] | 36 | |
| 37 | |
| 38 | class PickleTests(unittest.TestCase): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 39 | """Make sure all our custom exceptions can be pickled.""" |
Mike Frysinger | 057905f | 2021-02-18 23:28:32 -0500 | [diff] [blame] | 40 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 41 | def getExceptions(self): |
| 42 | """Return all our custom exceptions.""" |
Jason Chang | a6413f5 | 2023-07-26 13:23:40 -0700 | [diff] [blame] | 43 | 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 Frysinger | 057905f | 2021-02-18 23:28:32 -0500 | [diff] [blame] | 48 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 49 | 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 Frysinger | 057905f | 2021-02-18 23:28:32 -0500 | [diff] [blame] | 55 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 56 | 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)) |