diff options
| author | Jonathan Lange <jml@canonical.com> | 2012-08-04 18:22:03 +0100 |
|---|---|---|
| committer | Jonathan Lange <jml@canonical.com> | 2012-08-04 18:22:03 +0100 |
| commit | 2f036bbafe1cb6977d883d96604eac595fe92a95 (patch) | |
| tree | f445185f42d4c063dbb2b69df00132a08e82734a | |
| parent | 37eb13139dc4aa50cbe403075872a8ceb05be77f (diff) | |
| download | fixtures-git-2f036bbafe1cb6977d883d96604eac595fe92a95.tar.gz | |
Remove filetree cruft.
| -rw-r--r-- | lib/fixtures/_fixtures/tempdir.py | 87 | ||||
| -rw-r--r-- | lib/fixtures/tests/_fixtures/test_tempdir.py | 150 |
2 files changed, 1 insertions, 236 deletions
diff --git a/lib/fixtures/_fixtures/tempdir.py b/lib/fixtures/_fixtures/tempdir.py index 3af8d00..fd5502c 100644 --- a/lib/fixtures/_fixtures/tempdir.py +++ b/lib/fixtures/_fixtures/tempdir.py @@ -18,8 +18,6 @@ __all__ = [ 'TempDir', ] -import errno -import os import shutil import tempfile @@ -45,20 +43,6 @@ class TempDir(fixtures.Fixture): self.path = tempfile.mkdtemp(dir=self.rootdir) self.addCleanup(shutil.rmtree, self.path, ignore_errors=True) - def make_tree(self, *shape): - """Make a tree of files and directories underneath this temp dir. - - :param shape: A list of descriptions of files and directories to make. - Generally directories are described as ``"directory/"`` and - files are described as ``("filename", contents)``. Filenames can - also be specified without contents, in which case we'll make - something up. - - Directories can also be specified as ``(directory, None)`` or - ``(directory,)``. - """ - create_normal_shape(self.path, normalize_shape(shape)) - class NestedTempfile(fixtures.Fixture): """Nest all temporary files and directories inside another directory. @@ -74,74 +58,3 @@ class NestedTempfile(fixtures.Fixture): tempdir = self.useFixture(TempDir()).path patch = fixtures.MonkeyPatch("tempfile.tempdir", tempdir) self.useFixture(patch) - - -def normalize_entry(entry): - """Normalize a file shape entry. - - 'Normal' entries are either ("file", "content") or ("directory/", None). - - Standalone strings get turned into 2-tuples, with files getting made-up - contents. Singletons are treated the same. - - If something that looks like a file has no content, or something that - looks like a directory has content, we raise an error, as we don't know - whether the developer really intends a file or really intends a directory. - - :return: A list of 2-tuples containing paths and contents. - """ - if isinstance(entry, basestring): - if entry[-1] == '/': - return (entry, None) - else: - return (entry, "The file '%s'." % (entry,)) - 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): - """Normalize a shape of a file tree to create. - - Normalizes each entry and returns a sorted list of entries. - """ - return sorted(map(normalize_entry, shape)) - - -def create_normal_shape(base_directory, shape): - """Create a file tree from 'shape' in 'base_directory'. - - 'shape' must be a list of 2-tuples of (name, contents). If name ends with - '/', then contents must be None, as it will be created as a directory. - Otherwise, contents must be provided. - - If either a file or directory is specified but the parent directory - doesn't exist, will create the parent directory. - """ - for name, contents in shape: - name = os.path.join(base_directory, name) - if name[-1] == '/': - os.makedirs(name) - else: - base_dir = os.path.dirname(name) - try: - os.makedirs(base_dir) - except OSError, e: - if e.errno != errno.EEXIST: - raise - f = open(name, 'w') - f.write(contents) - f.close() diff --git a/lib/fixtures/tests/_fixtures/test_tempdir.py b/lib/fixtures/tests/_fixtures/test_tempdir.py index c253a68..cacebb5 100644 --- a/lib/fixtures/tests/_fixtures/test_tempdir.py +++ b/lib/fixtures/tests/_fixtures/test_tempdir.py @@ -17,22 +17,12 @@ import os import tempfile import testtools -from testtools.matchers import ( - DirContains, - DirExists, - FileContains, - StartsWith, - ) +from testtools.matchers import StartsWith from fixtures import ( NestedTempfile, TempDir, ) -from fixtures._fixtures.tempdir import ( - create_normal_shape, - normalize_entry, - normalize_shape, - ) from fixtures.tests.helpers import HasNoAttribute @@ -83,141 +73,3 @@ class NestedTempfileTest(testtools.TestCase): raise ContrivedException except ContrivedException: self.assertFalse(os.path.isdir(nested_tempdir)) - - -class TestFileTree(testtools.TestCase): - - 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 = TempDir() - with fixture: - fixture.make_tree('a/b/', 'a/') - 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()) - - def test_not_even_creating_parents(self): - fixture = TempDir() - with fixture: - fixture.make_tree('a/b/foo.txt', 'c/d/e/') - path = fixture.path - self.assertThat( - os.path.join(path, 'a', 'b', 'foo.txt'), - FileContains("The file 'a/b/foo.txt'.")) - self.assertThat(os.path.join(path, 'c', 'd', 'e'), DirExists()) - - -class TestNormalizeEntry(testtools.TestCase): - - def test_file_as_tuple(self): - # A tuple of filenames and contents is already normalized. - entry = normalize_entry(('foo', 'foo contents')) - self.assertEqual(('foo', 'foo contents'), entry) - - def test_directories_as_tuples(self): - # A tuple of directory name and None is already normalized. - directory = normalize_entry(('foo/', None)) - self.assertEqual(('foo/', None), directory) - - def test_directories_as_singletons(self): - # A singleton tuple of directory name is normalized to a 2-tuple of - # the directory name and None. - directory = normalize_entry(('foo/',)) - self.assertEqual(('foo/', None), directory) - - def test_directories_as_strings(self): - # If directories are just given as strings, then they are normalized - # to tuples of directory names and None. - 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(testtools.TestCase): - - def test_empty(self): - # The normal form of an empty list is the empty list. - empty = normalize_shape([]) - self.assertEqual([], empty) - - def test_sorts_entries(self): - # The normal form a list of entries is the sorted list of normal - # entries. - entries = normalize_shape(['a/b/', 'a/']) - self.assertEqual([('a/', None), ('a/b/', None)], entries) - - -class TestCreateNormalShape(testtools.TestCase): - - def test_empty(self): - tempdir = self.useFixture(TempDir()).path - create_normal_shape(tempdir, []) - self.assertThat(tempdir, DirContains([])) - - def test_creates_files(self): - # When given a list of file specifications, it creates those files - # underneath the temporary directory. - path = self.useFixture(TempDir()).path - create_normal_shape(path, [('a', 'foo'), ('b', 'bar')]) - self.assertThat(path, DirContains(['a', 'b'])) - self.assertThat(os.path.join(path, 'a'), FileContains('foo')) - self.assertThat(os.path.join(path, 'b'), FileContains('bar')) - - def test_creates_directories(self): - # When given directory specifications, it creates those directories. - path = self.useFixture(TempDir()).path - create_normal_shape(path, [('a/', None), ('b/', None)]) - self.assertThat(path, DirContains(['a', 'b'])) - self.assertThat(os.path.join(path, 'a'), DirExists()) - self.assertThat(os.path.join(path, 'b'), DirExists()) - - def test_creates_parent_directories(self): - # If the parents of a file or directory don't exist, they get created - # too. - path = self.useFixture(TempDir()).path - create_normal_shape(path, [('a/b/', None), ('c/d.txt', 'text')]) - self.assertThat(path, DirContains(['a', 'c'])) - self.assertThat(os.path.join(path, 'a'), DirContains('b')) - self.assertThat(os.path.join(path, 'a', 'b'), DirExists()) - self.assertThat(os.path.join(path, 'c'), DirExists()) - self.assertThat(os.path.join(path, 'c', 'd.txt'), FileContains('text')) |
