• 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    # TODO(b/190659975): Re-enable once permission issue is resolved.
72    @unittest.skip("Permission error: b/190659975")
73    def test_import_framework_successful(self):
74        """Dynamically test all imports from the framework."""
75        acts = import_acts()
76        if hasattr(acts, '__path__') and len(acts.__path__) > 0:
77            acts_path = acts.__path__[0]
78        else:
79            acts_path = os.path.dirname(acts.__file__)
80
81        for root, _, files in os.walk(acts_path):
82            for f in files:
83                full_path = os.path.join(root, f)
84                if (any(full_path.endswith(e) for e in DENYLIST)
85                        or any(e in full_path
86                               for e in DENYLIST_DIRECTORIES)):
87                    continue
88
89                path = os.path.relpath(os.path.join(root, f), os.getcwd())
90
91                if PY_FILE_REGEX.match(full_path):
92                    with self.subTest(msg='import %s' % path):
93                        fake_module_name = str(uuid.uuid4())
94                        module = import_module(fake_module_name, path)
95                        self.assertIsNotNone(module)
96
97
98if __name__ == '__main__':
99    unittest.main()
100