diff options
author | Jonathan Lange <jml@canonical.com> | 2012-07-21 22:04:13 +0100 |
---|---|---|
committer | Jonathan Lange <jml@canonical.com> | 2012-07-21 22:04:13 +0100 |
commit | 86644ef8ed0296ecfa66435af17ab30678c27ea9 (patch) | |
tree | 90da556b7dfe501f8de8e9b7d9ff4eef99299d38 | |
parent | 23befe163d2497aabaedc9351555df440db8222b (diff) | |
download | fixtures-86644ef8ed0296ecfa66435af17ab30678c27ea9.tar.gz |
Extract normalize_shape
-rw-r--r-- | lib/fixtures/_fixtures/filetree.py | 26 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_filetree.py | 53 |
2 files changed, 72 insertions, 7 deletions
diff --git a/lib/fixtures/_fixtures/filetree.py b/lib/fixtures/_fixtures/filetree.py index 5fee966..a48669c 100644 --- a/lib/fixtures/_fixtures/filetree.py +++ b/lib/fixtures/_fixtures/filetree.py @@ -23,6 +23,22 @@ from fixtures import Fixture from fixtures._fixtures.tempdir import TempDir +def normalize_shape(shape): + normal_shape = [] + for entry in sorted(shape): + if isinstance(entry, basestring): + if entry[-1] == '/': + normal_shape.append((entry, None)) + else: + normal_shape.append((entry, "The file '%s'." % (entry,))) + else: + if entry[0][-1] == '/': + normal_shape.append((entry[0], None)) + else: + normal_shape.append(entry) + return normal_shape + + class FileTree(Fixture): """A structure of files and directories on disk.""" @@ -35,21 +51,17 @@ class FileTree(Fixture): Directories can also be written as ``("dirname/",)``. """ super(FileTree, self).__init__() - self._shape = shape + self._shape = normalize_shape(shape) def setUp(self): super(FileTree, self).setUp() tempdir = self.useFixture(TempDir()) self.path = path = tempdir.path - for description in self._shape: - if isinstance(description, basestring): - name = description - else: - name = description[0] + for name, contents in self._shape: name = os.path.join(path, name) if name[-1] == '/': os.mkdir(name) else: f = open(name, 'w') - f.write(description[1]) + f.write(contents) f.close() diff --git a/lib/fixtures/tests/_fixtures/test_filetree.py b/lib/fixtures/tests/_fixtures/test_filetree.py index aab1fb2..89c2b0d 100644 --- a/lib/fixtures/tests/_fixtures/test_filetree.py +++ b/lib/fixtures/tests/_fixtures/test_filetree.py @@ -24,6 +24,7 @@ from testtools.matchers import ( ) from fixtures import FileTree +from fixtures._fixtures.filetree import normalize_shape from fixtures.tests.helpers import NotHasattr @@ -72,3 +73,55 @@ class TestFileTree(TestCase): path = fixture.path self.assertThat(path, DirContains(['a'])) self.assertThat(os.path.join(path, 'a'), DirExists()) + + def test_out_of_order(self): + # If a file or a subdirectory is listed before its parent directory, + # that doesn't matter. We'll create the directory first. + fixture = FileTree(['a/b/', 'a/']) + with fixture: + path = fixture.path + self.assertThat(path, DirContains(['a'])) + self.assertThat(os.path.join(path, 'a'), DirContains(['b'])) + self.assertThat(os.path.join(path, 'a', 'b'), DirExists()) + + +class TestNormalizeShape(TestCase): + + def test_empty(self): + # The normal form of an empty list is the empty list. + empty = normalize_shape([]) + self.assertEqual([], empty) + + def test_files_as_tuples(self): + # A list of tuples of filenames and contents is already normalized, + # well, once it's alpha-sorted. + files = normalize_shape( + [('foo', 'foo contents'), ('bar', 'bar contents')]) + self.assertEqual( + [('bar', 'bar contents'), ('foo', 'foo contents')], files) + + def test_directories_as_tuples(self): + # A list of tuples of directory names and None is already normalized, + # well, once it's alpha-sorted. + directories = normalize_shape([('foo/', None), ('bar/', None)]) + self.assertEqual([('bar/', None), ('foo/', None)], directories) + + def test_directories_as_singletons(self): + # A list of tuples of directory names and None is already normalized, + # well, once it's alpha-sorted. + directories = normalize_shape([('foo/',), ('bar/',)]) + self.assertEqual([('bar/', None), ('foo/', None)], directories) + + def test_directories_as_strings(self): + # If directories are just given as strings, then they are normalized + # to tuples of directory names and None. + directories = normalize_shape(['foo/', 'bar/']) + self.assertEqual([('bar/', None), ('foo/', None)], directories) + + 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. + files = normalize_shape(['foo', 'bar']) + self.assertEqual( + [('bar', "The file 'bar'."), ('foo', "The file 'foo'.")], files) + |