Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python3.4
      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 
     17 import os
     18 import re
     19 import sys
     20 import uuid
     21 
     22 if 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')
     37 else:
     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 
     48 PY_FILE_REGEX = re.compile('.+\.py$')
     49 
     50 BLACKLIST = [
     51     'acts/controllers/native.py', 'acts/controllers/native_android_device.py'
     52 ]
     53 
     54 
     55 class ActsImportTestUtilsTest(unittest.TestCase):
     56     """Test that all acts framework imports work.
     57     """
     58 
     59     def test_import_acts_successful(self):
     60         """ Test that importing acts works.
     61         """
     62         acts = import_acts()
     63         self.assertIsNotNone(acts)
     64 
     65     def test_import_framework_successful(self):
     66         """ Dynamically test all imports from the framework.
     67         """
     68         acts = import_acts()
     69         if hasattr(acts, '__path__') and len(acts.__path__) > 0:
     70             acts_path = acts.__path__[0]
     71         else:
     72             acts_path = os.path.dirname(acts.__file__)
     73 
     74         for root, _, files in os.walk(acts_path):
     75             for f in files:
     76                 full_path = os.path.join(root, f)
     77                 if any(full_path.endswith(e) for e in BLACKLIST):
     78                     continue
     79 
     80                 path = os.path.relpath(os.path.join(root, f), os.getcwd())
     81 
     82                 if PY_FILE_REGEX.match(full_path):
     83                     with self.subTest(msg='import %s' % path):
     84                         fake_module_name = str(uuid.uuid4())
     85                         module = import_module(fake_module_name, path)
     86 
     87                         self.assertIsNotNone(module)
     88 
     89 
     90 if __name__ == "__main__":
     91     unittest.main()
     92