Added a few missing use cases
This commit is contained in:
parent
a840a8ec9d
commit
04a43c8819
|
@ -17,7 +17,7 @@ Using Simport
|
||||||
|
|
||||||
# For modules not in the Python Path
|
# For modules not in the Python Path
|
||||||
function = simport.load('/path/to/file.py|module_name:myfunction')
|
function = simport.load('/path/to/file.py|module_name:myfunction')
|
||||||
class_method = simport.load('/path/to/file.py|module_name:MyClasss.mymethod')
|
class_method = simport.load('/path/to/file.py|module_name:MyClass.mymethod')
|
||||||
|
|
||||||
Running Tests
|
Running Tests
|
||||||
=============
|
=============
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
import imp
|
import imp
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,16 +53,23 @@ def _get_module(target):
|
||||||
the module loaded as normal.
|
the module loaded as normal.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
directory, sep, namespace = target.rpartition('|')
|
filepath, sep, namespace = target.rpartition('|')
|
||||||
|
if sep and not filepath:
|
||||||
|
raise BadDirectory("Path to file not supplied.")
|
||||||
|
|
||||||
module, sep, class_or_function = namespace.rpartition(':')
|
module, sep, class_or_function = namespace.rpartition(':')
|
||||||
if not module:
|
if (sep and not module) or (filepath and not module):
|
||||||
raise MissingModule("Need a module path for %s (%s)" %
|
raise MissingModule("Need a module path for %s (%s)" %
|
||||||
(namespace, target))
|
(namespace, target))
|
||||||
|
|
||||||
if directory and directory not in sys.path:
|
path = ""
|
||||||
if not os.path.isdir(directory):
|
filename = ""
|
||||||
raise BadDirectory("No such directory: '%s'" % directory)
|
if filepath:
|
||||||
sys.path.append(directory)
|
path, filename = os.path.split(filepath)
|
||||||
|
if path and path not in sys.path:
|
||||||
|
if not os.path.isdir(path):
|
||||||
|
raise BadDirectory("No such directory: '%s'" % path)
|
||||||
|
sys.path.append(path)
|
||||||
|
|
||||||
if not class_or_function:
|
if not class_or_function:
|
||||||
raise MissingMethodOrFunction("No Method or Function specified")
|
raise MissingMethodOrFunction("No Method or Function specified")
|
||||||
|
|
|
@ -16,8 +16,9 @@ class DummyClass(object):
|
||||||
|
|
||||||
class TestSimport(unittest.TestCase):
|
class TestSimport(unittest.TestCase):
|
||||||
def test_bad_targets(self):
|
def test_bad_targets(self):
|
||||||
self.assertRaises(simport.MissingModule, simport._get_module,
|
self.assertRaises(simport.BadDirectory, simport._get_module,
|
||||||
"missing.py")
|
"|foo.Class")
|
||||||
|
|
||||||
self.assertRaises(simport.MissingModule, simport._get_module,
|
self.assertRaises(simport.MissingModule, simport._get_module,
|
||||||
"missing.py|")
|
"missing.py|")
|
||||||
|
|
||||||
|
@ -25,6 +26,8 @@ class TestSimport(unittest.TestCase):
|
||||||
"simport_tests/localmodule.py|")
|
"simport_tests/localmodule.py|")
|
||||||
self.assertRaises(simport.MissingModule, simport._get_module,
|
self.assertRaises(simport.MissingModule, simport._get_module,
|
||||||
"simport_tests/localmodule.py|Foo")
|
"simport_tests/localmodule.py|Foo")
|
||||||
|
self.assertRaises(simport.BadDirectory, simport._get_module,
|
||||||
|
"/does/not/exist/foo.py|foo:Class")
|
||||||
|
|
||||||
self.assertFalse("AnyModuleName" in sys.modules)
|
self.assertFalse("AnyModuleName" in sys.modules)
|
||||||
self.assertRaises(simport.MissingMethodOrFunction, simport._get_module,
|
self.assertRaises(simport.MissingMethodOrFunction, simport._get_module,
|
||||||
|
|
Loading…
Reference in New Issue