# 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 shutil
import sys
import tempfile

from pathlib import Path

from IPython.testing.tools import make_tempfile

import IPython.utils.module_paths as mp

import nose.tools as nt

TEST_FILE_PATH = Path(__file__).resolve().parent

TMP_TEST_DIR = Path(tempfile.mkdtemp(suffix="with.dot"))
#
# Setup/teardown functions/decorators
#

old_syspath = sys.path

def make_empty_file(fname):
    open(fname, 'w').close()


def setup_module():
    """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...
    Path(TMP_TEST_DIR / "xmod").mkdir(parents=True)
    Path(TMP_TEST_DIR / "nomod").mkdir(parents=True)
    make_empty_file(TMP_TEST_DIR / "xmod/__init__.py")
    make_empty_file(TMP_TEST_DIR / "xmod/sub.py")
    make_empty_file(TMP_TEST_DIR / "pack.py")
    make_empty_file(TMP_TEST_DIR / "packpyc.pyc")
    sys.path = [str(TMP_TEST_DIR)]

def teardown_module():
    """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_tempdir():
    """
    Ensure the test are done with a temporary file that have a dot somewhere.
    """
    nt.assert_in(".", str(TMP_TEST_DIR))


def test_find_mod_1():
    """
    Search for a directory's file path.
    Expected output: a path to that directory's __init__.py file.
    """
    modpath = TMP_TEST_DIR / "xmod" / "__init__.py"
    nt.assert_equal(Path(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 = TMP_TEST_DIR / "xmod" / "__init__.py"
    nt.assert_equal(Path(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 = TMP_TEST_DIR / "xmod" / "sub.py"
    nt.assert_equal(Path(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 = TMP_TEST_DIR / "pack.py"
    nt.assert_equal(Path(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)

@@ -741,33 +741,8 @@
 mp%0A%0A
-import nose.tools as nt%0A%0A
 TEST
@@ -2048,25 +2048,21 @@
     
-nt.
 assert
-_in(%22.%22,
+ %22.%22 in
  str
@@ -2075,17 +2075,16 @@
 EST_DIR)
-)
 %0A%0A%0Adef t
@@ -2279,32 +2279,23 @@
 py%22%0A    
-nt.
 assert
-_equal(
+ 
 Path(mp.
@@ -2307,33 +2307,35 @@
 mod(%22xmod%22))
-,
+ ==
  modpath
 )%0A%0Adef test_
@@ -2314,33 +2314,32 @@
 od%22)) == modpath
-)
 %0A%0Adef test_find_
@@ -2573,32 +2573,23 @@
 py%22%0A    
-nt.
 assert
-_equal(
+ 
 Path(mp.
@@ -2605,25 +2605,27 @@
 %22xmod%22))
-,
+ ==
  modpath
 )%0A%0Adef t
@@ -2608,33 +2608,32 @@
 od%22)) == modpath
-)
 %0A%0Adef test_find_
@@ -2825,32 +2825,23 @@
 py%22%0A    
-nt.
 assert
-_equal(
+ 
 Path(mp.
@@ -2861,25 +2861,27 @@
 d.sub%22))
-,
+ ==
  modpath
 )%0A%0Adef t
@@ -2864,33 +2864,32 @@
 ub%22)) == modpath
-)
 %0A%0Adef test_find_
@@ -3058,32 +3058,23 @@
 py%22%0A    
-nt.
 assert
-_equal(
+ 
 Path(mp.
@@ -3094,17 +3094,19 @@
 k%22))
-,
+ ==
  modpath
 )%0A%0Ad
@@ -3101,17 +3101,16 @@
  modpath
-)
 %0A%0Adef te
@@ -3262,24 +3262,15 @@
     
-nt.
 assert
-_equal(
+ 
 mp.f
@@ -3291,12 +3291,13 @@
 yc%22)
-,
+ ==
  None
-)
 %0A

Remove nose