| 1 |
# encoding: utf-8 |
| 2 |
"""Tests for IPython.utils.module_paths.py""" |
| 3 |
|
| 4 |
# ----------------------------------------------------------------------------- |
| 5 |
# Copyright (C) 2008-2011 The IPython Development Team |
| 6 |
# |
| 7 |
# Distributed under the terms of the BSD License. The full license is in |
| 8 |
# the file COPYING, distributed as part of this software. |
| 9 |
# ----------------------------------------------------------------------------- |
| 10 |
|
| 11 |
# ----------------------------------------------------------------------------- |
| 12 |
# Imports |
| 13 |
# ----------------------------------------------------------------------------- |
| 14 |
|
| 15 |
import shutil |
| 16 |
import sys |
| 17 |
import tempfile |
| 18 |
|
| 19 |
from pathlib import Path |
| 20 |
|
| 21 |
import IPython.utils.module_paths as mp |
| 22 |
|
| 23 |
TEST_FILE_PATH = Path(__file__).resolve().parent |
| 24 |
|
| 25 |
TMP_TEST_DIR = Path(tempfile.mkdtemp(suffix="with.dot")) |
| 26 |
# |
| 27 |
# Setup/teardown functions/decorators |
| 28 |
# |
| 29 |
|
| 30 |
old_syspath = sys.path |
| 31 |
|
| 32 |
|
| 33 |
def make_empty_file(fname): |
| 34 |
open(fname, "w", encoding="utf-8").close() |
| 35 |
|
| 36 |
|
| 37 |
def setup_module(): |
| 38 |
"""Setup testenvironment for the module:""" |
| 39 |
# Do not mask exceptions here. In particular, catching WindowsError is a |
| 40 |
# problem because that exception is only defined on Windows... |
| 41 |
Path(TMP_TEST_DIR / "xmod").mkdir(parents=True) |
| 42 |
Path(TMP_TEST_DIR / "nomod").mkdir(parents=True) |
| 43 |
make_empty_file(TMP_TEST_DIR / "xmod/__init__.py") |
| 44 |
make_empty_file(TMP_TEST_DIR / "xmod/sub.py") |
| 45 |
make_empty_file(TMP_TEST_DIR / "pack.py") |
| 46 |
make_empty_file(TMP_TEST_DIR / "packpyc.pyc") |
| 47 |
sys.path = [str(TMP_TEST_DIR)] |
| 48 |
|
| 49 |
|
| 50 |
def teardown_module(): |
| 51 |
"""Teardown testenvironment for the module: |
| 52 |
|
| 53 |
- Remove tempdir |
| 54 |
- restore sys.path |
| 55 |
""" |
| 56 |
# Note: we remove the parent test dir, which is the root of all test |
| 57 |
# subdirs we may have created. Use shutil instead of os.removedirs, so |
| 58 |
# that non-empty directories are all recursively removed. |
| 59 |
shutil.rmtree(TMP_TEST_DIR) |
| 60 |
sys.path = old_syspath |
| 61 |
|
| 62 |
|
| 63 |
def test_tempdir(): |
| 64 |
""" |
| 65 |
Ensure the test are done with a temporary file that have a dot somewhere. |
| 66 |
""" |
| 67 |
assert "." in str(TMP_TEST_DIR) |
| 68 |
|
| 69 |
|
| 70 |
def test_find_mod_1(): |
| 71 |
""" |
| 72 |
Search for a directory's file path. |
| 73 |
Expected output: a path to that directory's __init__.py file. |
| 74 |
""" |
| 75 |
modpath = TMP_TEST_DIR / "xmod" / "__init__.py" |
| 76 |
assert Path(mp.find_mod("xmod")) == modpath |
| 77 |
|
| 78 |
|
| 79 |
def test_find_mod_2(): |
| 80 |
""" |
| 81 |
Search for a directory's file path. |
| 82 |
Expected output: a path to that directory's __init__.py file. |
| 83 |
TODO: Confirm why this is a duplicate test. |
| 84 |
""" |
| 85 |
modpath = TMP_TEST_DIR / "xmod" / "__init__.py" |
| 86 |
assert Path(mp.find_mod("xmod")) == modpath |
| 87 |
|
| 88 |
|
| 89 |
def test_find_mod_3(): |
| 90 |
""" |
| 91 |
Search for a directory + a filename without its .py extension |
| 92 |
Expected output: full path with .py extension. |
| 93 |
""" |
| 94 |
modpath = TMP_TEST_DIR / "xmod" / "sub.py" |
| 95 |
assert Path(mp.find_mod("xmod.sub")) == modpath |
| 96 |
|
| 97 |
|
| 98 |
def test_find_mod_4(): |
| 99 |
""" |
| 100 |
Search for a filename without its .py extension |
| 101 |
Expected output: full path with .py extension |
| 102 |
""" |
| 103 |
modpath = TMP_TEST_DIR / "pack.py" |
| 104 |
assert Path(mp.find_mod("pack")) == modpath |
| 105 |
|
| 106 |
|
| 107 |
def test_find_mod_5(): |
| 108 |
""" |
| 109 |
Search for a filename with a .pyc extension |
| 110 |
Expected output: TODO: do we exclude or include .pyc files? |
| 111 |
""" |
| 112 |
assert mp.find_mod("packpyc") == None |