diff options
Diffstat (limited to 'lib/fixtures')
-rw-r--r-- | lib/fixtures/_fixtures/filetree.py | 22 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_filetree.py | 60 |
2 files changed, 44 insertions, 38 deletions
diff --git a/lib/fixtures/_fixtures/filetree.py b/lib/fixtures/_fixtures/filetree.py index f261fc9..f8c5989 100644 --- a/lib/fixtures/_fixtures/filetree.py +++ b/lib/fixtures/_fixtures/filetree.py @@ -54,6 +54,17 @@ def normalize_shape(shape): return normal_shape +def create_normal_shape(root, shape): + for name, contents in shape: + name = os.path.join(root, name) + if name[-1] == '/': + os.mkdir(name) + else: + f = open(name, 'w') + f.write(contents) + f.close() + + class FileTree(Fixture): """A structure of files and directories on disk.""" @@ -71,12 +82,5 @@ class FileTree(Fixture): def setUp(self): super(FileTree, self).setUp() tempdir = self.useFixture(TempDir()) - self.path = path = tempdir.path - 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(contents) - f.close() + self.path = tempdir.path + create_normal_shape(self.path, self._shape) diff --git a/lib/fixtures/tests/_fixtures/test_filetree.py b/lib/fixtures/tests/_fixtures/test_filetree.py index cd796d6..2da6ff6 100644 --- a/lib/fixtures/tests/_fixtures/test_filetree.py +++ b/lib/fixtures/tests/_fixtures/test_filetree.py @@ -23,8 +23,12 @@ from testtools.matchers import ( Not, ) -from fixtures import FileTree +from fixtures import ( + FileTree, + TempDir, + ) from fixtures._fixtures.filetree import ( + create_normal_shape, normalize_entry, normalize_shape, ) @@ -49,34 +53,6 @@ class TestFileTree(TestCase): fixture.cleanUp() self.assertThat(fixture.path, Not(DirExists())) - def test_creates_files(self): - # When given a list of file specifications, it creates those files - # underneath the temporary directory. - fixture = FileTree([('a', 'foo'), ('b', 'bar')]) - with fixture: - path = fixture.path - 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. - fixture = FileTree([('a/', None), ('b/',)]) - with fixture: - path = fixture.path - self.assertThat(path, DirContains(['a', 'b'])) - self.assertThat(os.path.join(path, 'a'), DirExists()) - self.assertThat(os.path.join(path, 'b'), DirExists()) - - def test_simpler_directory_syntax(self): - # Directory specifications don't have to be tuples. They can be single - # strings. - fixture = FileTree(['a/']) - with fixture: - 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. @@ -164,3 +140,29 @@ class TestNormalizeShape(TestCase): # entries. entries = normalize_shape(['a/b/', 'a/']) self.assertEqual([('a/', None), ('a/b/', None)], entries) + + +class TestCreateNormalShape(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()) + |