test_module_paths.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # encoding: utf-8
  2. """Tests for IPython.utils.module_paths.py"""
  3. #-----------------------------------------------------------------------------
  4. # Copyright (C) 2008-2011 The IPython Development Team
  5. #
  6. # Distributed under the terms of the BSD License. The full license is in
  7. # the file COPYING, distributed as part of this software.
  8. #-----------------------------------------------------------------------------
  9. #-----------------------------------------------------------------------------
  10. # Imports
  11. #-----------------------------------------------------------------------------
  12. from __future__ import with_statement
  13. import os
  14. import shutil
  15. import sys
  16. import tempfile
  17. from os.path import join, abspath, split
  18. from IPython.testing.tools import make_tempfile
  19. import IPython.utils.module_paths as mp
  20. import nose.tools as nt
  21. env = os.environ
  22. TEST_FILE_PATH = split(abspath(__file__))[0]
  23. TMP_TEST_DIR = tempfile.mkdtemp()
  24. #
  25. # Setup/teardown functions/decorators
  26. #
  27. old_syspath = sys.path
  28. def make_empty_file(fname):
  29. f = open(fname, 'w')
  30. f.close()
  31. def setup():
  32. """Setup testenvironment for the module:
  33. """
  34. # Do not mask exceptions here. In particular, catching WindowsError is a
  35. # problem because that exception is only defined on Windows...
  36. os.makedirs(join(TMP_TEST_DIR, "xmod"))
  37. os.makedirs(join(TMP_TEST_DIR, "nomod"))
  38. make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py"))
  39. make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py"))
  40. make_empty_file(join(TMP_TEST_DIR, "pack.py"))
  41. make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc"))
  42. sys.path = [TMP_TEST_DIR]
  43. def teardown():
  44. """Teardown testenvironment for the module:
  45. - Remove tempdir
  46. - restore sys.path
  47. """
  48. # Note: we remove the parent test dir, which is the root of all test
  49. # subdirs we may have created. Use shutil instead of os.removedirs, so
  50. # that non-empty directories are all recursively removed.
  51. shutil.rmtree(TMP_TEST_DIR)
  52. sys.path = old_syspath
  53. def test_get_init_1():
  54. """See if get_init can find __init__.py in this testdir"""
  55. with make_tempfile(join(TMP_TEST_DIR, "__init__.py")):
  56. assert mp.get_init(TMP_TEST_DIR)
  57. def test_get_init_2():
  58. """See if get_init can find __init__.pyw in this testdir"""
  59. with make_tempfile(join(TMP_TEST_DIR, "__init__.pyw")):
  60. assert mp.get_init(TMP_TEST_DIR)
  61. def test_get_init_3():
  62. """get_init can't find __init__.pyc in this testdir"""
  63. with make_tempfile(join(TMP_TEST_DIR, "__init__.pyc")):
  64. nt.assert_is_none(mp.get_init(TMP_TEST_DIR))
  65. def test_get_init_4():
  66. """get_init can't find __init__ in empty testdir"""
  67. nt.assert_is_none(mp.get_init(TMP_TEST_DIR))
  68. def test_find_mod_1():
  69. modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
  70. nt.assert_equal(mp.find_mod("xmod"), modpath)
  71. def test_find_mod_2():
  72. modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
  73. nt.assert_equal(mp.find_mod("xmod"), modpath)
  74. def test_find_mod_3():
  75. modpath = join(TMP_TEST_DIR, "xmod", "sub.py")
  76. nt.assert_equal(mp.find_mod("xmod.sub"), modpath)
  77. def test_find_mod_4():
  78. modpath = join(TMP_TEST_DIR, "pack.py")
  79. nt.assert_equal(mp.find_mod("pack"), modpath)
  80. def test_find_mod_5():
  81. modpath = join(TMP_TEST_DIR, "packpyc.pyc")
  82. nt.assert_equal(mp.find_mod("packpyc"), modpath)
  83. def test_find_module_1():
  84. modpath = join(TMP_TEST_DIR, "xmod")
  85. nt.assert_equal(mp.find_module("xmod"), modpath)
  86. def test_find_module_2():
  87. """Testing sys.path that is empty"""
  88. nt.assert_is_none(mp.find_module("xmod", []))
  89. def test_find_module_3():
  90. """Testing sys.path that is empty"""
  91. nt.assert_is_none(mp.find_module(None, None))
  92. def test_find_module_4():
  93. """Testing sys.path that is empty"""
  94. nt.assert_is_none(mp.find_module(None))
  95. def test_find_module_5():
  96. nt.assert_is_none(mp.find_module("xmod.nopack"))