From b5e4338fa4bf9da3edcfa7c37cd4c1fb6b67a118 2012-12-05 17:12:27 From: Greg Caporaso Date: 2012-12-05 17:12:27 Subject: [PATCH] added tests to confirm that error is raise when FileLink is passed a directory. Also specifically disallowed passing a file to FileLinks for consistency. --- diff --git a/IPython/lib/display.py b/IPython/lib/display.py index 59825b2..c315c50 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -87,7 +87,7 @@ class FileLink(object): """ if isdir(path): raise ValueError,\ - ("Cannot display a directory as a FileLink object. " + ("Cannot display a directory using FileLink. " "Use FileLinks to display '%s'." % path) self.path = path self.url_prefix = url_prefix @@ -171,6 +171,10 @@ class FileLinks(FileLink): place, can be passed here to support alternative formatting. """ + if isfile(path): + raise ValueError,\ + ("Cannot display a file using FileLinks. " + "Use FileLink to display '%s'." % path) self.included_suffixes = included_suffixes # remove trailing slashs for more consistent output formatting path = path.rstrip('/') diff --git a/IPython/lib/tests/test_display.py b/IPython/lib/tests/test_display.py index e0c33fb..0e89afb 100644 --- a/IPython/lib/tests/test_display.py +++ b/IPython/lib/tests/test_display.py @@ -58,6 +58,12 @@ def test_existing_path_FileLink_repr(): expected = tf.name nt.assert_equal(actual,expected) +def test_error_on_directory_to_FileLink(): + """FileLink: Raises error when passed directory + """ + td = mkdtemp() + nt.assert_raises(ValueError,display.FileLink,td) + #-------------------------- # FileLinks tests #-------------------------- @@ -137,4 +143,11 @@ def test_existing_path_FileLinks_repr_alt_formatter(): expected.sort() # We compare the sorted list of links here as that's more reliable nt.assert_equal(actual,expected) + +def test_error_on_file_to_FileLinks(): + """FileLinks: Raises error when passed file + """ + td = mkdtemp() + tf1 = NamedTemporaryFile(dir=td) + nt.assert_raises(ValueError,display.FileLinks,tf1.name)