|
7 | 7 | import tarfile |
8 | 8 | import tempfile |
9 | 9 | import unittest |
10 | | - |
11 | 10 | import pytest |
12 | 11 |
|
13 | 12 | from fs import tarfs |
14 | 13 | from fs.enums import ResourceType |
15 | 14 | from fs.compress import write_tar |
16 | 15 | from fs.opener import open_fs |
17 | 16 | from fs.opener.errors import NotWriteable |
| 17 | +from fs.errors import NoURL |
18 | 18 | from fs.test import FSTestCases |
19 | 19 |
|
20 | 20 | from .test_archives import ArchiveTestCases |
@@ -93,15 +93,6 @@ def destroy_fs(self, fs): |
93 | 93 | os.remove(fs._tar_file) |
94 | 94 | del fs._tar_file |
95 | 95 |
|
96 | | - def assert_is_bzip(self): |
97 | | - try: |
98 | | - tarfile.open(fs._tar_file, "r:gz") |
99 | | - except tarfile.ReadError: |
100 | | - self.fail("{} is not a valid gz archive".format(fs._tar_file)) |
101 | | - for other_comps in ["xz", "bz2", ""]: |
102 | | - with self.assertRaises(tarfile.ReadError): |
103 | | - tarfile.open(fs._tar_file, "r:{}".format(other_comps)) |
104 | | - |
105 | 96 |
|
106 | 97 | @pytest.mark.skipif(six.PY2, reason="Python2 does not support LZMA") |
107 | 98 | class TestWriteXZippedTarFS(FSTestCases, unittest.TestCase): |
@@ -181,11 +172,44 @@ def test_read_from_filename(self): |
181 | 172 | except: |
182 | 173 | self.fail("Couldn't open tarfs from filename") |
183 | 174 |
|
| 175 | + def test_read_non_existent_file(self): |
| 176 | + fs = tarfs.TarFS(open(self._temp_path, "rb")) |
| 177 | + # it has been very difficult to catch exception in __del__() |
| 178 | + del fs._tar |
| 179 | + try: |
| 180 | + fs.close() |
| 181 | + except AttributeError: |
| 182 | + self.fail("Could not close tar fs properly") |
| 183 | + except Exception: |
| 184 | + self.fail("Strange exception in closing fs") |
| 185 | + |
184 | 186 | def test_getinfo(self): |
185 | 187 | super(TestReadTarFS, self).test_getinfo() |
186 | 188 | top = self.fs.getinfo("top.txt", ["tar"]) |
187 | 189 | self.assertTrue(top.get("tar", "is_file")) |
188 | 190 |
|
| 191 | + def test_geturl_for_fs(self): |
| 192 | + test_fixtures = [ |
| 193 | + # test_file, expected |
| 194 | + ["foo/bar/egg/foofoo", "foo/bar/egg/foofoo"], |
| 195 | + ["foo/bar egg/foo foo", "foo/bar%20egg/foo%20foo"], |
| 196 | + ] |
| 197 | + tar_file_path = self._temp_path.replace("\\", "/") |
| 198 | + for test_file, expected_file in test_fixtures: |
| 199 | + expected = "tar://{tar_file_path}!/{file_inside_tar}".format( |
| 200 | + tar_file_path=tar_file_path, file_inside_tar=expected_file |
| 201 | + ) |
| 202 | + self.assertEqual(self.fs.geturl(test_file, purpose="fs"), expected) |
| 203 | + |
| 204 | + def test_geturl_for_fs_but_file_is_binaryio(self): |
| 205 | + self.fs._file = six.BytesIO() |
| 206 | + self.assertRaises(NoURL, self.fs.geturl, "test", "fs") |
| 207 | + |
| 208 | + def test_geturl_for_download(self): |
| 209 | + test_file = "foo/bar/egg/foofoo" |
| 210 | + with self.assertRaises(NoURL): |
| 211 | + self.fs.geturl(test_file) |
| 212 | + |
189 | 213 |
|
190 | 214 | class TestBrokenPaths(unittest.TestCase): |
191 | 215 | @classmethod |
|
0 commit comments