File: test_import.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (127 lines) | stat: -rw-r--r-- 4,715 bytes parent folder | download | duplicates (2)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""Test that importing vispy subpackages do not pull
in any more vispy submodules than strictly necessary.
"""

import sys
import os

from vispy.testing import (assert_in, assert_not_in, requires_pyopengl,
                           run_tests_if_main, assert_equal)
from vispy.util import run_subprocess
import vispy


# minimum that will be imported when importing vispy
_min_modules = ['vispy', 'vispy.util', 'vispy.ext', 'vispy.version']


def loaded_vispy_modules(import_module, depth=None, all_modules=False):
    """Import the given module in subprocess and return loaded modules

    Import a certain module in a clean subprocess and return the
    vispy modules that are subsequently loaded. The given depth
    indicates the module level (i.e. depth=1 will only yield 'vispy.app'
    but not 'vispy.app.backends').
    """
    vispy_dir = os.path.dirname(os.path.dirname(vispy.__file__))

    # Get the loaded modules in a clean interpreter
    code = "import sys, %s; print(', '.join(sys.modules))" % import_module
    res = run_subprocess([sys.executable, '-c', code], cwd=vispy_dir)[0]
    loaded_modules = [name.strip() for name in res.split(',')]

    if all_modules:
        return loaded_modules

    # Get only vispy modules at the given depth
    vispy_modules = set()
    for m in loaded_modules:
        # pkg_resources from vispy/__init__.py shows up as vispy.pkg_resources in python 2.7
        if m.startswith('vispy') and '__future__' not in m and 'pkg_resources' not in m:
            if depth:
                parts = m.split('.')
                m = '.'.join(parts[:depth])
            vispy_modules.add(m)

    return vispy_modules


def test_import_nothing():
    """Not importing vispy should not import any vispy modules."""
    modnames = loaded_vispy_modules('os', 2)
    assert_equal(modnames, set())


def test_import_vispy():
    """Importing vispy should only pull in other vispy.util submodule."""
    modnames = loaded_vispy_modules('vispy', 2)
    assert_equal(modnames, set(_min_modules))


def test_import_vispy_util():
    """Importing vispy.util should not pull in other vispy submodules."""
    modnames = loaded_vispy_modules('vispy.util', 2)
    assert_equal(modnames, set(_min_modules))


def test_import_vispy_app1():
    """Importing vispy.app should not pull in other vispy submodules."""
    # Since the introduction of the GLContext to gloo, app depends on gloo
    modnames = loaded_vispy_modules('vispy.app', 2)
    assert_equal(modnames, set(_min_modules + ['vispy.app', 'vispy.gloo',
                                               'vispy.glsl', 'vispy.color']))


def test_import_vispy_app2():
    """Importing vispy.app should not pull in any backend toolkit."""
    allmodnames = loaded_vispy_modules('vispy.app', 2, True)
    assert_not_in('PySide', allmodnames)
    assert_not_in('PySide2', allmodnames)
    assert_not_in('PySide6', allmodnames)
    assert_not_in('PyQt4', allmodnames)
    assert_not_in('PyQt5', allmodnames)
    assert_not_in('PyQt6', allmodnames)
    assert_not_in('pyglet', allmodnames)


def test_import_vispy_gloo():
    """Importing vispy.gloo should not pull in other vispy submodules."""
    modnames = loaded_vispy_modules('vispy.gloo', 2)
    assert_equal(modnames, set(_min_modules + ['vispy.gloo',
                                               'vispy.glsl',
                                               'vispy.color']))


def test_import_vispy_no_pyopengl():
    """Importing vispy.gloo.gl.gl2 should not import PyOpenGL."""
    # vispy.gloo desktop backend
    allmodnames = loaded_vispy_modules('vispy.gloo.gl.gl2', 2, True)
    assert_not_in('OpenGL', allmodnames)
    # vispy.app
    allmodnames = loaded_vispy_modules('vispy.app', 2, True)
    assert_not_in('OpenGL', allmodnames)
    # vispy.scene
    allmodnames = loaded_vispy_modules('vispy.scene', 2, True)
    assert_not_in('OpenGL', allmodnames)


@requires_pyopengl()
def test_import_vispy_pyopengl():
    """Importing vispy.gloo.gl.pyopengl2 should import PyOpenGL."""
    allmodnames = loaded_vispy_modules('vispy.gloo.gl.pyopengl2', 2, True)
    assert_in('OpenGL', allmodnames)


def test_import_vispy_scene():
    """Importing vispy.gloo.gl.desktop should not import PyOpenGL."""
    modnames = loaded_vispy_modules('vispy.scene', 2)
    more_modules = ['vispy.app', 'vispy.gloo', 'vispy.glsl', 'vispy.scene',
                    'vispy.color',
                    'vispy.io', 'vispy.geometry', 'vispy.visuals']
    assert_equal(modnames, set(_min_modules + more_modules))


run_tests_if_main()