##// END OF EJS Templates
Fix extension detection...
Fix extension detection If the path leading up to the Python installation's `site-packages` contains a `.`, this check does not do what it says it does :( For instance, on macOS, the `site-packages` directory is located at `/usr/local/lib/python3.7/site-packages`, which means that trying to use the magic `%run -m my.module` will always fail, because `split_path[1]` will always start with `7/site-packages`. There are better ways to check that a file extension matches expectations, but I thought it was cute that I could fix this bug by inserting a single character :) My current workaround looks like this: ```python import importlib import_path = importlib.util.find_spec('my.module').origin %run $import_path ```

File last commit:

r24460:2e9fe8cd
r24836:525c7fe6
Show More
test_module_paths.py
107 lines | 3.2 KiB | text/x-python | PythonLexer
# encoding: utf-8
"""Tests for IPython.utils.module_paths.py"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import shutil
import sys
import tempfile
from os.path import join, abspath, split
from IPython.testing.tools import make_tempfile
import IPython.utils.module_paths as mp
import nose.tools as nt
env = os.environ
TEST_FILE_PATH = split(abspath(__file__))[0]
TMP_TEST_DIR = tempfile.mkdtemp()
#
# Setup/teardown functions/decorators
#
old_syspath = sys.path
def make_empty_file(fname):
f = open(fname, 'w')
f.close()
def setup():
"""Setup testenvironment for the module:
"""
# Do not mask exceptions here. In particular, catching WindowsError is a
# problem because that exception is only defined on Windows...
os.makedirs(join(TMP_TEST_DIR, "xmod"))
os.makedirs(join(TMP_TEST_DIR, "nomod"))
make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py"))
make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py"))
make_empty_file(join(TMP_TEST_DIR, "pack.py"))
make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc"))
sys.path = [TMP_TEST_DIR]
def teardown():
"""Teardown testenvironment for the module:
- Remove tempdir
- restore sys.path
"""
# Note: we remove the parent test dir, which is the root of all test
# subdirs we may have created. Use shutil instead of os.removedirs, so
# that non-empty directories are all recursively removed.
shutil.rmtree(TMP_TEST_DIR)
sys.path = old_syspath
def test_find_mod_1():
"""
Search for a directory's file path.
Expected output: a path to that directory's __init__.py file.
"""
modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
nt.assert_equal(mp.find_mod("xmod"), modpath)
def test_find_mod_2():
"""
Search for a directory's file path.
Expected output: a path to that directory's __init__.py file.
TODO: Confirm why this is a duplicate test.
"""
modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
nt.assert_equal(mp.find_mod("xmod"), modpath)
def test_find_mod_3():
"""
Search for a directory + a filename without its .py extension
Expected output: full path with .py extension.
"""
modpath = join(TMP_TEST_DIR, "xmod", "sub.py")
nt.assert_equal(mp.find_mod("xmod.sub"), modpath)
def test_find_mod_4():
"""
Search for a filename without its .py extension
Expected output: full path with .py extension
"""
modpath = join(TMP_TEST_DIR, "pack.py")
nt.assert_equal(mp.find_mod("pack"), modpath)
def test_find_mod_5():
"""
Search for a filename with a .pyc extension
Expected output: TODO: do we exclude or include .pyc files?
"""
nt.assert_equal(mp.find_mod("packpyc"), None)