• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# coding: utf-8
2from __future__ import unicode_literals, division, absolute_import, print_function
3
4import imp
5import os
6import unittest
7
8
9__version__ = '1.4.0'
10__version_info__ = (1, 4, 0)
11
12
13def _import_from(mod, path, mod_dir=None):
14    """
15    Imports a module from a specific path
16
17    :param mod:
18        A unicode string of the module name
19
20    :param path:
21        A unicode string to the directory containing the module
22
23    :param mod_dir:
24        If the sub directory of "path" is different than the "mod" name,
25        pass the sub directory as a unicode string
26
27    :return:
28        None if not loaded, otherwise the module
29    """
30
31    if mod_dir is None:
32        mod_dir = mod
33
34    if not os.path.exists(path):
35        return None
36
37    if not os.path.exists(os.path.join(path, mod_dir)):
38        return None
39
40    try:
41        mod_info = imp.find_module(mod_dir, [path])
42        return imp.load_module(mod, *mod_info)
43    except ImportError:
44        return None
45
46
47def make_suite():
48    """
49    Constructs a unittest.TestSuite() of all tests for the package. For use
50    with setuptools.
51
52    :return:
53        A unittest.TestSuite() object
54    """
55
56    loader = unittest.TestLoader()
57    suite = unittest.TestSuite()
58    for test_class in test_classes():
59        tests = loader.loadTestsFromTestCase(test_class)
60        suite.addTests(tests)
61    return suite
62
63
64def test_classes():
65    """
66    Returns a list of unittest.TestCase classes for the package
67
68    :return:
69        A list of unittest.TestCase classes
70    """
71
72    # If we are in a source folder and these tests aren't installed as a
73    # package, we want to load asn1crypto from this source folder
74    tests_dir = os.path.dirname(os.path.abspath(__file__))
75
76    asn1crypto = None
77    if os.path.basename(tests_dir) == 'tests':
78        asn1crypto = _import_from(
79            'asn1crypto',
80            os.path.join(tests_dir, '..')
81        )
82    if asn1crypto is None:
83        import asn1crypto
84
85    if asn1crypto.__version__ != __version__:
86        raise AssertionError(
87            ('asn1crypto_tests version %s can not be run with ' % __version__) +
88            ('asn1crypto version %s' % asn1crypto.__version__)
89        )
90
91    from .test_algos import AlgoTests
92    from .test_cms import CMSTests
93    from .test_crl import CRLTests
94    from .test_csr import CSRTests
95    from .test_init import InitTests
96    from .test_keys import KeysTests
97    from .test_ocsp import OCSPTests
98    from .test_pem import PEMTests
99    from .test_pkcs12 import PKCS12Tests
100    from .test_tsp import TSPTests
101    from .test_x509 import X509Tests
102    from .test_util import UtilTests
103    from .test_parser import ParserTests
104    from .test_core import CoreTests
105
106    return [
107        AlgoTests,
108        CMSTests,
109        CRLTests,
110        CSRTests,
111        InitTests,
112        KeysTests,
113        OCSPTests,
114        PEMTests,
115        PKCS12Tests,
116        TSPTests,
117        UtilTests,
118        ParserTests,
119        X509Tests,
120        CoreTests
121    ]
122