blob: dcb70efa78a55e6f6f79ddafe18edcd048ae84f5 [file] [log] [blame]
markdrdd1893d2018-02-05 17:13:47 -08001#!/usr/bin/env python3
Benny Peake2be7f952016-12-20 15:48:13 -08002#
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
Benny Peake2be7f952016-12-20 15:48:13 -080020import uuid
21
markdrb2e826a2017-08-04 21:42:08 -070022if sys.version_info < (3, ):
Benny Peake3d4c4d92016-12-21 17:18:05 -080023 import warnings
Benny Peake2be7f952016-12-20 15:48:13 -080024
Benny Peake3d4c4d92016-12-21 17:18:05 -080025 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):
Benny Peake2be7f952016-12-20 15:48:13 -080033 return imp.load_source(name, path)
Benny Peake3d4c4d92016-12-21 17:18:05 -080034
35 def import_acts():
36 return importlib.import_module('acts')
Benny Peake2be7f952016-12-20 15:48:13 -080037else:
38 import importlib.machinery
Benny Peake3d4c4d92016-12-21 17:18:05 -080039 import unittest
Benny Peake2be7f952016-12-20 15:48:13 -080040
Benny Peake3d4c4d92016-12-21 17:18:05 -080041 def import_module(name, path):
Benny Peake2be7f952016-12-20 15:48:13 -080042 return importlib.machinery.SourceFileLoader(name, path).load_module()
Benny Peake3d4c4d92016-12-21 17:18:05 -080043
44 def import_acts():
45 return importlib.import_module('acts')
Benny Peake2be7f952016-12-20 15:48:13 -080046
47
48PY_FILE_REGEX = re.compile('.+\.py$')
49
Benny Peake3d4c4d92016-12-21 17:18:05 -080050BLACKLIST = [
markdrb2e826a2017-08-04 21:42:08 -070051 'acts/controllers/native.py',
52 'acts/controllers/native_android_device.py',
53 'acts/test_utils/wifi/wifi_power_test_utils.py',
markdr346ad892017-10-19 11:59:41 -070054 'acts/controllers/packet_sender.py',
Omar El Ayachdfdbd3e2017-10-02 15:23:53 -070055 'acts/test_utils/wifi/wifi_retail_ap.py',
markdrb629b322017-05-22 13:44:06 -070056 'acts/test_utils/bt/bt_power_test_utils.py',
globaledgec16ad342018-03-19 17:53:16 +053057 'acts/test_utils/coex/coex_test_utils.py',
Mark De Ruyter48f7c832018-05-08 15:26:03 -070058 'acts/framework/acts/test_utils/coex/CoexBaseTest.py',
59 'acts/framework/acts/test_utils/coex/CoexPerformanceBaseTest.py',
Mark De Ruyter40e68852018-05-17 11:12:41 -070060 'acts/framework/acts/test_utils/power/PowerWiFiBaseTest.py',
61 'acts/framework/acts/test_utils/power/PowerCoexBaseTest.py',
62 'acts/framework/acts/test_utils/power/PowerBaseTest.py',
63 'acts/framework/acts/test_utils/power/PowerBTBaseTest.py',
Benny Peake3d4c4d92016-12-21 17:18:05 -080064]
Benny Peake2be7f952016-12-20 15:48:13 -080065
Mark De Ruytera4b9cd32018-10-17 16:26:02 -070066BLACKLIST_DIRECTORIES = [
67 'acts/framework/acts/test_utils/'
68]
69
Benny Peake2be7f952016-12-20 15:48:13 -080070
71class ActsImportTestUtilsTest(unittest.TestCase):
Mark De Ruytera4b9cd32018-10-17 16:26:02 -070072 """Test that all acts framework imports work."""
Benny Peake2be7f952016-12-20 15:48:13 -080073
74 def test_import_acts_successful(self):
Mark De Ruytera4b9cd32018-10-17 16:26:02 -070075 """Test that importing ACTS works."""
Benny Peake3d4c4d92016-12-21 17:18:05 -080076 acts = import_acts()
Benny Peake2be7f952016-12-20 15:48:13 -080077 self.assertIsNotNone(acts)
78
79 def test_import_framework_successful(self):
Mark De Ruytera4b9cd32018-10-17 16:26:02 -070080 """Dynamically test all imports from the framework."""
Benny Peake3d4c4d92016-12-21 17:18:05 -080081 acts = import_acts()
Benny Peake2be7f952016-12-20 15:48:13 -080082 if hasattr(acts, '__path__') and len(acts.__path__) > 0:
83 acts_path = acts.__path__[0]
84 else:
85 acts_path = os.path.dirname(acts.__file__)
86
87 for root, _, files in os.walk(acts_path):
88 for f in files:
89 full_path = os.path.join(root, f)
Mark De Ruytera4b9cd32018-10-17 16:26:02 -070090 if (any(full_path.endswith(e) for e in BLACKLIST) or
91 any(e in full_path for e in BLACKLIST_DIRECTORIES)):
Benny Peake2be7f952016-12-20 15:48:13 -080092 continue
93
94 path = os.path.relpath(os.path.join(root, f), os.getcwd())
95
96 if PY_FILE_REGEX.match(full_path):
97 with self.subTest(msg='import %s' % path):
98 fake_module_name = str(uuid.uuid4())
Benny Peake3d4c4d92016-12-21 17:18:05 -080099 module = import_module(fake_module_name, path)
Benny Peake2be7f952016-12-20 15:48:13 -0800100
101 self.assertIsNotNone(module)
102
103
Mark De Ruytera4b9cd32018-10-17 16:26:02 -0700104if __name__ == '__main__':
Benny Peake2be7f952016-12-20 15:48:13 -0800105 unittest.main()