File: test_import_isolation.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (53 lines) | stat: -rw-r--r-- 1,657 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Authors:   pietro

Copyright: (C) 2015 pietro

           This program is free software under the GNU General Public
           License (>=v2). Read the file COPYING that comes with GRASS
           for details.

Created on  Wed Jul 15 11:34:32 2015
"""
from __future__ import absolute_import

import sys
import fnmatch

from grass.gunittest.case import TestCase
from grass.gunittest.main import test


def check(*patterns):
    """Return a set of the imported libraries that soddisfies several patterns.
    """
    result = []
    imports = sorted(sys.modules.keys())
    for pattern in patterns:
        result.extend(fnmatch.filter(imports, pattern))
    return set(result)


class TestImportIsolation(TestCase):
    patterns = ['grass.lib*']

    def test_import_isolation(self):
        """Check that modules  classes are not using ctypes"""
        isolate = set()
        self.assertEqual(isolate, check(*self.patterns),
                         msg="Test isolation before any import.")
        # same import done in __init__ file
        from grass.pygrass.modules.interface import Module, ParallelModuleQueue
        from grass.pygrass.modules import shortcuts
        self.assertEqual(isolate, check(*self.patterns),
                         msg="Test isolation after import Module.")
        # test the other way round
        from grass.pygrass.vector import VectorTopo
        self.assertNotEqual(isolate, check(*self.patterns),
                            msg=("Test the isolation is broken, therefore "
                                 "the defined patterns are correct"))


if __name__ == '__main__':
    test()