|
8 | 8 | import os |
9 | 9 | import sys |
10 | 10 | import imp |
| 11 | +import shutil |
| 12 | +import tempfile |
| 13 | +import subprocess |
11 | 14 | import contextlib |
12 | 15 |
|
13 | 16 | from nose.tools import ( |
@@ -92,6 +95,66 @@ def test_sip_api_qtpy(): |
92 | 95 | "instead is %s" |
93 | 96 | % sip.getapi("QString")) |
94 | 97 |
|
| 98 | + |
| 99 | +def test_vendoring(): |
| 100 | + """Qt.py may be bundled along with another library/project |
| 101 | + |
| 102 | + Create toy project |
| 103 | + |
| 104 | + from project.vendor import Qt # Absolute |
| 105 | + from .vendor import Qt # Relative |
| 106 | + |
| 107 | + project/ |
| 108 | + vendor/ |
| 109 | + __init__.py |
| 110 | + __init__.py |
| 111 | +
|
| 112 | + """ |
| 113 | + |
| 114 | + dirname = os.path.dirname(__file__) |
| 115 | + tempdir = tempfile.mkdtemp() |
| 116 | + |
| 117 | + try: |
| 118 | + project = os.path.join(tempdir, "myproject") |
| 119 | + vendor = os.path.join(tempdir, "myproject", "vendor") |
| 120 | + |
| 121 | + os.makedirs(vendor) |
| 122 | + |
| 123 | + # Make packages out of folders |
| 124 | + with open(os.path.join(project, "__init__.py"), "w") as f: |
| 125 | + f.write("from .vendor.Qt import QtWidgets") |
| 126 | + |
| 127 | + with open(os.path.join(vendor, "__init__.py"), "w") as f: |
| 128 | + pass |
| 129 | + |
| 130 | + shutil.copy(os.path.join(dirname, "Qt.py"), |
| 131 | + os.path.join(vendor, "Qt.py")) |
| 132 | + |
| 133 | + print("Testing relative import..") |
| 134 | + assert subprocess.call( |
| 135 | + ["python", "-c", "import myproject"], |
| 136 | + cwd=tempdir, |
| 137 | + env={} |
| 138 | + ) == 0 |
| 139 | + |
| 140 | + print("Testing absolute import..") |
| 141 | + assert subprocess.call( |
| 142 | + ["python", "-c", "from myproject.vendor.Qt import QtWidgets"], |
| 143 | + cwd=tempdir, |
| 144 | + env={} |
| 145 | + ) == 0 |
| 146 | + |
| 147 | + print("Testing direct import..") |
| 148 | + assert subprocess.call( |
| 149 | + ["python", "-c", "import myproject.vendor.Qt"], |
| 150 | + cwd=tempdir, |
| 151 | + env={} |
| 152 | + ) == 0 |
| 153 | + |
| 154 | + finally: |
| 155 | + shutil.rmtree(tempdir) |
| 156 | + |
| 157 | + |
95 | 158 | if PYTHON == 2: |
96 | 159 | def test_sip_api_already_set(): |
97 | 160 | """Raise ImportError if sip API v1 was already set (Python 2.x only)""" |
|
0 commit comments