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 def make_empty_file(fname): 33 open(fname, "w", encoding="utf-8").close() 34 35 36 def setup_module(): 37 """Setup testenvironment for the module: 38 39 """ 40 # Do not mask exceptions here. In particular, catching WindowsError is a 41 # problem because that exception is only defined on Windows... 42 Path(TMP_TEST_DIR / "xmod").mkdir(parents=True) 43 Path(TMP_TEST_DIR / "nomod").mkdir(parents=True) 44 make_empty_file(TMP_TEST_DIR / "xmod/__init__.py") 45 make_empty_file(TMP_TEST_DIR / "xmod/sub.py") 46 make_empty_file(TMP_TEST_DIR / "pack.py") 47 make_empty_file(TMP_TEST_DIR / "packpyc.pyc") 48 sys.path = [str(TMP_TEST_DIR)] 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 def test_tempdir(): 63 """ 64 Ensure the test are done with a temporary file that have a dot somewhere. 65 """ 66 assert "." in str(TMP_TEST_DIR) 67 68 69 def test_find_mod_1(): 70 """ 71 Search for a directory's file path. 72 Expected output: a path to that directory's __init__.py file. 73 """ 74 modpath = TMP_TEST_DIR / "xmod" / "__init__.py" 75 assert Path(mp.find_mod("xmod")) == modpath 76 77 def test_find_mod_2(): 78 """ 79 Search for a directory's file path. 80 Expected output: a path to that directory's __init__.py file. 81 TODO: Confirm why this is a duplicate test. 82 """ 83 modpath = TMP_TEST_DIR / "xmod" / "__init__.py" 84 assert Path(mp.find_mod("xmod")) == modpath 85 86 def test_find_mod_3(): 87 """ 88 Search for a directory + a filename without its .py extension 89 Expected output: full path with .py extension. 90 """ 91 modpath = TMP_TEST_DIR / "xmod" / "sub.py" 92 assert Path(mp.find_mod("xmod.sub")) == modpath 93 94 def test_find_mod_4(): 95 """ 96 Search for a filename without its .py extension 97 Expected output: full path with .py extension 98 """ 99 modpath = TMP_TEST_DIR / "pack.py" 100 assert Path(mp.find_mod("pack")) == modpath 101 102 def test_find_mod_5(): 103 """ 104 Search for a filename with a .pyc extension 105 Expected output: TODO: do we exclude or include .pyc files? 106 """ 107 assert mp.find_mod("packpyc") == None 108