def test_load_multiple_root_dirs(self): fp = os.path.join(self.temp_dir.name, 'multiple-root-dirs.zip') self.archiver.save(fp) # Add another semi-valid root dir. second_root_dir = str(uuid.uuid4()) with zipfile.ZipFile(fp, mode='a') as zf: zf.writestr('%s/VERSION' % second_root_dir, "foo") with zipfile.ZipFile(fp, mode='r') as zf: root_dir = str(self.archiver.uuid) expected = { '%s/VERSION' % root_dir, '%s/metadata.yaml' % root_dir, '%s/data/ints.txt' % root_dir, '%s/provenance/metadata.yaml' % root_dir, '%s/provenance/VERSION' % root_dir, '%s/provenance/action/action.yaml' % root_dir, '%s/VERSION' % second_root_dir } observed = set(zf.namelist()) self.assertEqual(observed, expected) with self.assertRaisesRegex(ValueError, 'multiple root directories'): Archiver.load(fp)
def test_load_multiple_root_dirs(self): fp = os.path.join(self.temp_dir.name, 'multiple-root-dirs.zip') self.archiver.save(fp) # Add another semi-valid root dir. second_root_dir = str(uuid.uuid4()) with zipfile.ZipFile(fp, mode='a') as zf: zf.writestr('%s/VERSION' % second_root_dir, "foo") with zipfile.ZipFile(fp, mode='r') as zf: root_dir = str(self.archiver.uuid) expected = { '%s/VERSION' % root_dir, '%s/checksums.md5' % root_dir, '%s/metadata.yaml' % root_dir, '%s/data/ints.txt' % root_dir, '%s/provenance/metadata.yaml' % root_dir, '%s/provenance/VERSION' % root_dir, '%s/provenance/citations.bib' % root_dir, '%s/provenance/action/action.yaml' % root_dir, '%s/VERSION' % second_root_dir } observed = set(zf.namelist()) self.assertEqual(observed, expected) with self.assertRaisesRegex(ValueError, 'multiple root directories'): Archiver.load(fp)
def test_load_empty_archive(self): fp = os.path.join(self.temp_dir.name, 'empty.zip') with zipfile.ZipFile(fp, mode='w') as zf: pass with zipfile.ZipFile(fp, mode='r') as zf: expected = set() observed = set(zf.namelist()) self.assertEqual(observed, expected) with self.assertRaisesRegex(ValueError, 'visible root directory'): Archiver.load(fp)
def test_load_ignores_directory_members(self): # Directory members aren't created by Python's zipfile module but can # be present if the archive is unzipped and then rezipped, for example, # using a command-line zip program. fp = os.path.join(self.temp_dir.name, 'archive.zip') self.archiver.save(fp) # Add directory entries to the archive. root_dir = str(self.archiver.uuid) with zipfile.ZipFile(fp, mode='a') as zf: zf.writestr('%s/' % root_dir, "") zf.writestr('%s/data/' % root_dir, "") zf.writestr('%s/data/nested/' % root_dir, "") zf.writestr('%s/data/nested/foo.txt' % root_dir, "bar") # Assert the expected files exist in the archive to verify this test # case is testing what we want it to. expected = { '', # Expected path: `root_dir`/ 'data/', 'data/nested/', 'VERSION', 'checksums.md5', 'metadata.yaml', 'data/ints.txt', 'data/nested/foo.txt', 'provenance/metadata.yaml', 'provenance/VERSION', 'provenance/citations.bib', 'provenance/action/action.yaml' } self.assertArchiveMembers(fp, root_dir, expected) archiver = Archiver.load(fp) self.assertEqual(archiver.uuid, self.archiver.uuid) self.assertEqual(archiver.type, IntSequence1) self.assertEqual(archiver.format, IntSequenceDirectoryFormat) archiver.save(fp) root_dir = str(archiver.uuid) expected = { # Directory entries should not be present. 'VERSION', 'checksums.md5', 'metadata.yaml', 'data/ints.txt', 'data/nested/foo.txt', 'provenance/metadata.yaml', 'provenance/VERSION', 'provenance/citations.bib', 'provenance/action/action.yaml' } self.assertArchiveMembers(fp, root_dir, expected)
def test_load_archive(self): fp = os.path.join(self.temp_dir.name, 'archive.zip') self.archiver.save(fp) archiver = Archiver.load(fp) self.assertEqual(archiver.uuid, self.archiver.uuid) self.assertEqual(archiver.type, IntSequence1) self.assertEqual(archiver.format, IntSequenceDirectoryFormat) self.assertEqual({str(p.relative_to(archiver.data_dir)) for p in archiver.data_dir.iterdir()}, {'ints.txt'})
def test_load_dotfile_only_archive(self): fp = os.path.join(self.temp_dir.name, 'dotfiles-only.zip') with zipfile.ZipFile(fp, mode='w') as zf: zf.writestr('.DS_Store', "The world's most beloved file\n") zf.writestr('.hidden-file', "You can't see me if I can't see you\n") zf.writestr('.hidden-dir/ignored-file', "I'm ignored because I live in a hidden dir :(\n") with zipfile.ZipFile(fp, mode='r') as zf: expected = { '.DS_Store', '.hidden-file', '.hidden-dir/ignored-file' } observed = set(zf.namelist()) self.assertEqual(observed, expected) with self.assertRaisesRegex(ValueError, 'visible root directory'): Archiver.load(fp)
def test_load_ignores_root_dotfiles(self): fp = os.path.join(self.temp_dir.name, 'archive.zip') self.archiver.save(fp) # Add some dotfiles to the archive. with zipfile.ZipFile(fp, mode='a') as zf: zf.writestr('.DS_Store', "The world's most beloved file\n") zf.writestr('.hidden-file', "You can't see me if I can't see you\n") zf.writestr('.hidden-dir/ignored-file', "I'm ignored because I live in a hidden dir :(\n") # Assert the expected files exist in the archive to verify this test # case is testing what we want it to. with zipfile.ZipFile(fp, mode='r') as zf: root_dir = str(self.archiver.uuid) expected = { '.DS_Store', '.hidden-file', '.hidden-dir/ignored-file', '%s/VERSION' % root_dir, '%s/checksums.md5' % root_dir, '%s/metadata.yaml' % root_dir, '%s/data/ints.txt' % root_dir, '%s/provenance/metadata.yaml' % root_dir, '%s/provenance/VERSION' % root_dir, '%s/provenance/citations.bib' % root_dir, '%s/provenance/action/action.yaml' % root_dir } observed = set(zf.namelist()) # Not using self.assertArchiveMembers() because it accepts paths # relative to root_dir, and we have extra paths at the same level # as root_dir. self.assertEqual(observed, expected) archiver = Archiver.load(fp) self.assertEqual(archiver.uuid, self.archiver.uuid) self.assertEqual(archiver.type, IntSequence1) self.assertEqual(archiver.format, IntSequenceDirectoryFormat) self.assertEqual( { str(p.relative_to(archiver.data_dir)) for p in archiver.data_dir.iterdir() }, {'ints.txt'})
def test_load_ignores_root_dotfiles(self): fp = os.path.join(self.temp_dir.name, 'archive.zip') self.archiver.save(fp) # Add some dotfiles to the archive. with zipfile.ZipFile(fp, mode='a') as zf: zf.writestr('.DS_Store', "The world's most beloved file\n") zf.writestr('.hidden-file', "You can't see me if I can't see you\n") zf.writestr('.hidden-dir/ignored-file', "I'm ignored because I live in a hidden dir :(\n") # Assert the expected files exist in the archive to verify this test # case is testing what we want it to. with zipfile.ZipFile(fp, mode='r') as zf: root_dir = str(self.archiver.uuid) expected = { '.DS_Store', '.hidden-file', '.hidden-dir/ignored-file', '%s/VERSION' % root_dir, '%s/checksums.md5' % root_dir, '%s/metadata.yaml' % root_dir, '%s/data/ints.txt' % root_dir, '%s/provenance/metadata.yaml' % root_dir, '%s/provenance/VERSION' % root_dir, '%s/provenance/citations.bib' % root_dir, '%s/provenance/action/action.yaml' % root_dir } observed = set(zf.namelist()) # Not using self.assertArchiveMembers() because it accepts paths # relative to root_dir, and we have extra paths at the same level # as root_dir. self.assertEqual(observed, expected) archiver = Archiver.load(fp) self.assertEqual(archiver.uuid, self.archiver.uuid) self.assertEqual(archiver.type, IntSequence1) self.assertEqual(archiver.format, IntSequenceDirectoryFormat) self.assertEqual({str(p.relative_to(archiver.data_dir)) for p in archiver.data_dir.iterdir()}, {'ints.txt'})