summaryrefslogtreecommitdiff
path: root/lib/fixtures
diff options
context:
space:
mode:
authorJonathan Lange <jml@canonical.com>2012-07-21 22:27:49 +0100
committerJonathan Lange <jml@canonical.com>2012-07-21 22:27:49 +0100
commit8d5cab9afdde3fd3bdaa2efcbc5c0822e5cf25d5 (patch)
tree965ff505fe265904918f49d7fdbf917fcc35ac70 /lib/fixtures
parent2bf0b8e30184bd6470dd867334dc59071ab571e5 (diff)
downloadfixtures-git-8d5cab9afdde3fd3bdaa2efcbc5c0822e5cf25d5.tar.gz
Richer error messages.
Diffstat (limited to 'lib/fixtures')
-rw-r--r--lib/fixtures/_fixtures/filetree.py17
-rw-r--r--lib/fixtures/tests/_fixtures/test_filetree.py33
2 files changed, 47 insertions, 3 deletions
diff --git a/lib/fixtures/_fixtures/filetree.py b/lib/fixtures/_fixtures/filetree.py
index d5ea1f9..f261fc9 100644
--- a/lib/fixtures/_fixtures/filetree.py
+++ b/lib/fixtures/_fixtures/filetree.py
@@ -30,10 +30,21 @@ def normalize_entry(entry):
else:
return (entry, "The file '%s'." % (entry,))
else:
- if entry[0][-1] == '/':
- return (entry[0], None)
- else:
+ if len(entry) == 1:
+ return normalize_entry(entry[0])
+ elif len(entry) == 2:
+ name, content = entry
+ is_dir = (name[-1] == '/')
+ if ((is_dir and content is not None)
+ or (not is_dir and content is None)):
+ raise ValueError(
+ "Directories must end with '/' and have no content, "
+ "files do not end with '/' and must have content, got %r"
+ % (entry,))
return entry
+ else:
+ raise ValueError(
+ "Invalid file or directory description: %r" % (entry,))
def normalize_shape(shape):
diff --git a/lib/fixtures/tests/_fixtures/test_filetree.py b/lib/fixtures/tests/_fixtures/test_filetree.py
index 78f3c2b..cd796d6 100644
--- a/lib/fixtures/tests/_fixtures/test_filetree.py
+++ b/lib/fixtures/tests/_fixtures/test_filetree.py
@@ -112,12 +112,45 @@ class TestNormalizeEntry(TestCase):
directory = normalize_entry('foo/')
self.assertEqual(('foo/', None), directory)
+ def test_directories_with_content(self):
+ # If we're given a directory with content, we raise an error, since
+ # it's ambiguous and we don't want to guess.
+ bad_entry = ('dir/', "stuff")
+ e = self.assertRaises(ValueError, normalize_entry, bad_entry)
+ self.assertEqual(
+ "Directories must end with '/' and have no content, files do not "
+ "end with '/' and must have content, got %r" % (bad_entry,),
+ str(e))
+
def test_filenames_as_strings(self):
# If file names are just given as strings, then they are normalized to
# tuples of filenames and made-up contents.
entry = normalize_entry('foo')
self.assertEqual(('foo', "The file 'foo'."), entry)
+ def test_filenames_as_singletons(self):
+ # A singleton tuple of a filename is normalized to a 2-tuple of
+ # the file name and made-up contents.
+ entry = normalize_entry(('foo',))
+ self.assertEqual(('foo', "The file 'foo'."), entry)
+
+ def test_filenames_without_content(self):
+ # If we're given a filename without content, we raise an error, since
+ # it's ambiguous and we don't want to guess.
+ bad_entry = ('filename', None)
+ e = self.assertRaises(ValueError, normalize_entry, bad_entry)
+ self.assertEqual(
+ "Directories must end with '/' and have no content, files do not "
+ "end with '/' and must have content, got %r" % (bad_entry,),
+ str(e))
+
+ def test_too_long_tuple(self):
+ bad_entry = ('foo', 'bar', 'baz')
+ e = self.assertRaises(ValueError, normalize_entry, bad_entry)
+ self.assertEqual(
+ "Invalid file or directory description: %r" % (bad_entry,),
+ str(e))
+
class TestNormalizeShape(TestCase):