• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2016 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import os
18import re
19import sys
20import uuid
21
22if sys.version_info < (3, ):
23    import warnings
24
25    with warnings.catch_warnings():
26        warnings.filterwarnings('ignore', category=PendingDeprecationWarning)
27        import imp
28
29    import importlib
30    import unittest2 as unittest
31
32    def import_module(name, path):
33        return imp.load_source(name, path)
34
35    def import_acts():
36        return importlib.import_module('acts')
37else:
38    import importlib.machinery
39    import unittest
40
41    def import_module(name, path):
42        return importlib.machinery.SourceFileLoader(name, path).load_module()
43
44    def import_acts():
45        return importlib.import_module('acts')
46
47
48PY_FILE_REGEX = re.compile('.+\.py$')
49
50DENYLIST = [
51    'acts/controllers/rohdeschwarz_lib/contest.py',
52    'acts/controllers/native.py',
53    'acts/controllers/native_android_device.py',
54    'acts/controllers/packet_sender.py',
55    'acts/controllers/buds_lib/dev_utils/proto/gen/nanopb_pb2.py'
56]
57
58DENYLIST_DIRECTORIES = [
59    'acts/controllers/buds_lib'
60]
61
62
63class ActsImportUnitTest(unittest.TestCase):
64    """Test that all acts framework imports work."""
65
66    def test_import_acts_successful(self):
67        """Test that importing ACTS works."""
68        acts = import_acts()
69        self.assertIsNotNone(acts)
70
71    def test_import_framework_successful(self):
72        """Dynamically test all imports from the framework."""
73        acts = import_acts()
74        if hasattr(acts, '__path__') and len(acts.__path__) > 0:
75            acts_path = acts.__path__[0]
76        else:
77            acts_path = os.path.dirname(acts.__file__)
78
79        for root, _, files in os.walk(acts_path):
80            for f in files:
81                full_path = os.path.join(root, f)
82                if (any(full_path.endswith(e) for e in DENYLIST)
83                        or any(e in full_path
84                               for e in DENYLIST_DIRECTORIES)):
85                    continue
86
87                path = os.path.relpath(os.path.join(root, f), os.getcwd())
88
89                if PY_FILE_REGEX.match(full_path):
90                    with self.subTest(msg='import %s' % path):
91                        fake_module_name = str(uuid.uuid4())
92                        module = import_module(fake_module_name, path)
93                        self.assertIsNotNone(module)
94
95
96if __name__ == '__main__':
97    unittest.main()
98