Skip to content

Commit

Permalink
Add testing for resolving imports relative to pythonpath. #2
Browse files Browse the repository at this point in the history
  • Loading branch information
a-feld committed Jan 27, 2018
1 parent 469f46a commit 7761789
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pytest
import shutil
import tempfile as _tempfile
from pytest_skippy.parse import get_imported_modules

Expand Down Expand Up @@ -168,3 +169,31 @@ def test_relative_import_without_submodule(tempfile):

assert modules == expected_modules
assert confirmed == expected_confirmed


def test_relative_import_converts_with_sys_path(monkeypatch):
try:
base_dir = _tempfile.mkdtemp()
pkg_dir = _tempfile.mkdtemp(dir=str(base_dir))
subpkg_dir = _tempfile.mkdtemp(dir=pkg_dir)
with _tempfile.NamedTemporaryFile(delete=False, dir=subpkg_dir) as f:
f.write(b'''
from . import bar
''')

# Add the directory containing the directory to the sys.path
import os.path
monkeypatch.syspath_prepend(base_dir)

pkg_name = os.path.basename(pkg_dir)
subpkg_name = os.path.basename(subpkg_dir)

# Get the imported modules
modules, confirmed = get_imported_modules(f.name)
finally:
shutil.rmtree(base_dir)

# Check that the absolute path contains the root package name
expected_confirmed = {pkg_name, pkg_name+'.'+subpkg_name}
assert confirmed == expected_confirmed
assert modules == expected_confirmed | {pkg_name+'.'+subpkg_name+'.bar'}

0 comments on commit 7761789

Please sign in to comment.