OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # -*- coding: utf-8 -*- |
| 3 """Tests that all parsers and plugins are imported correctly.""" |
| 4 |
| 5 from __future__ import unicode_literals |
| 6 |
| 7 import io |
| 8 import glob |
| 9 import os |
| 10 import re |
| 11 import unittest |
| 12 |
| 13 from tests import test_lib |
| 14 |
| 15 |
| 16 class ParserImportTest(test_lib.BaseTestCase): |
| 17 """Tests that parser classes are imported correctly.""" |
| 18 |
| 19 _PARSERS_PATH = os.path.join(os.getcwd(), 'plaso', 'parsers') |
| 20 _FILENAME_REGEXP = re.compile(r'^[^_]*\.py$') |
| 21 _IGNORABLE_FILES = frozenset( |
| 22 ['manager.py', 'presets.py', 'mediator.py', 'interface.py', 'plugins.py']) |
| 23 |
| 24 def _AssertFilesImportedInInit(self, path, ignorable_files): |
| 25 """Checks that files in path are imported in __init__.py |
| 26 |
| 27 Args: |
| 28 path (str): path to directory containing an __init__.py file and other |
| 29 Python files which should be imported. |
| 30 ignorable_files (list[str]): list of file names of Python files that |
| 31 don't need to appear in __init__.py. For example, 'manager.py'. |
| 32 """ |
| 33 init_path = '{0:s}/__init__.py'.format(path) |
| 34 with io.open(init_path, mode='r', encoding='utf-8') as init_file: |
| 35 init_content = init_file.read() |
| 36 |
| 37 for file_path in os.listdir(path): |
| 38 filename = os.path.basename(file_path) |
| 39 if filename in ignorable_files: |
| 40 continue |
| 41 if self._FILENAME_REGEXP.search(filename): |
| 42 import_name, _, _ = filename.partition('.') |
| 43 import_expression = re.compile(r' import {0:s}\b'.format(import_name)) |
| 44 |
| 45 self.assertRegexpMatches( |
| 46 init_content, import_expression, |
| 47 '{0:s} not imported in {1:s}'.format(import_name, init_path)) |
| 48 |
| 49 def testParsersImported(self): |
| 50 """Tests that all parsers are imported.""" |
| 51 self._AssertFilesImportedInInit(self._PARSERS_PATH, self._IGNORABLE_FILES) |
| 52 |
| 53 def testPluginsImported(self): |
| 54 """Tests that all plugins are imported.""" |
| 55 parsers_glob = '{0:s}/*_plugins/'.format(self._PARSERS_PATH) |
| 56 plugin_directories = glob.glob(parsers_glob) |
| 57 for plugin_directory in plugin_directories: |
| 58 plugin_directory_path = os.path.join( |
| 59 self._PARSERS_PATH, plugin_directory) |
| 60 self._AssertFilesImportedInInit( |
| 61 plugin_directory_path, self._IGNORABLE_FILES) |
| 62 |
| 63 |
| 64 if __name__ == '__main__': |
| 65 unittest.main() |
OLD | NEW |