Beispiel #1
0
    def test_write_string(self):
        testfile_name = "testfile3"
        test_string = "this is a written string\n"
        with local.open_zip(os.path.abspath(self.zip_file_path), 'w') as z:
            with z.open(testfile_name, "w") as zipped_file:
                zipped_file.write(test_string)

        with local.open_zip(os.path.abspath(self.zip_file_path)) as z:
            with z.open(testfile_name, "r") as zipped_file:
                self.assertEqual(test_string, zipped_file.readline())
Beispiel #2
0
    def test_write_bytes(self):
        testfile_name = "testfile3"
        test_string = "this is a written string\n"
        test_string_b = test_string.encode("utf-8")

        with local.open_zip(os.path.abspath(self.zip_file_path), 'w') as z:
            with z.open(testfile_name, "wb") as zipped_file:
                zipped_file.write(test_string_b)

        with local.open_zip(os.path.abspath(self.zip_file_path)) as z:
            with z.open(testfile_name, "rb") as zipped_file:
                self.assertEqual(test_string_b, zipped_file.readline())
Beispiel #3
0
    def test_open_non_exist(self):

        non_exist_file = "non_exist_file.txt"

        with local.open_zip(os.path.abspath(self.zip_file_path)) as z:
            # ZipFile raises KeyError while io module raises IOError
            self.assertRaises(KeyError, z.open, non_exist_file)
Beispiel #4
0
    def test_list_with_errors(self, path_or_prefix, error):
        with local.open_zip(self.zip_file_name) as z:
            with self.assertRaises(error):
                list(z.list(path_or_prefix))

            with self.assertRaises(error):
                list(z.list(path_or_prefix, recursive=True))
Beispiel #5
0
    def test_mode_w_exception(self):
        testfile_name = "testfile3"
        test_string = "this is a written string\n"

        with local.open_zip(os.path.abspath(self.zip_file_path)) as z:
            with self.assertRaises(ValueError):
                with z.open(testfile_name, "w") as zipped_file:
                    zipped_file.write(test_string)
Beispiel #6
0
    def test_writing_after_listing(self):
        testfile_name = "testfile3"
        test_string = "this is a written string\n"

        with local.open_zip(os.path.abspath(self.zip_file_path), 'w') as z:
            list(z.list())

            with z.open(testfile_name, "w") as zipped_file:
                zipped_file.write(test_string)
Beispiel #7
0
    def test_nested_zip(self):
        with local.open_zip(self.zip_file_path) as z:
            with z.open_zip(self.nested_zip_path) as nested_zip:
                with nested_zip.open(self.nested_zipped_file_path) as f:
                    self.assertEqual(f.read(), self.nested_test_string)

                with nested_zip.open(self.nested_zipped_file_path, "r") as f:
                    self.assertEqual(f.read(), self.nested_test_string)

                with nested_zip.open(self.nested_zipped_file_path, "rb") as f:
                    self.assertEqual(f.read(), self.nested_test_string_b)
Beispiel #8
0
    def test_pickle(self):
        pickle_file_name = "test_pickle.pickle"
        test_data = {'test_elem1': b'balabala', 'test_elem2': 'balabala'}
        pickle_zip = "test_pickle.zip"

        with open(pickle_file_name, "wb") as f:
            pickle.dump(test_data, f)

        with ZipFile(pickle_zip, "w") as test_zip:
            test_zip.write(pickle_file_name)

        with local.open_zip(pickle_zip) as z:
            with z.open(pickle_file_name, 'rb') as f:
                loaded_obj = pickle.load(f)
                self.assertEqual(test_data, loaded_obj)

        os.remove(pickle_file_name)
        os.remove(pickle_zip)
Beispiel #9
0
    def test_stat_directory(self):
        test_dir_name = 'testdir2/'
        expected = ZipFile(self.zip_file_path).getinfo(test_dir_name)

        with local.open_zip(self.zip_file_path) as z:
            stat = z.stat(test_dir_name)
            self.assertIsInstance(stat, ZipFileStat)
            self.assertTrue(stat.filename.endswith(test_dir_name))
            self.assertEqual(stat.size, expected.file_size)
            self.assertEqual(stat.mode, expected.external_attr >> 16)
            self.assertTrue(stat.isdir())

            expected_mtime = datetime(*expected.date_time).timestamp()
            self.assertIsInstance(stat.last_modified, float)
            self.assertEqual(stat.last_modified, expected_mtime)

            for k in ('filename', 'orig_filename', 'comment', 'create_system',
                      'create_version', 'extract_version', 'flag_bits',
                      'volume', 'internal_attr', 'external_attr', 'CRC',
                      'header_offset', 'compress_size', 'compress_type'):
                self.assertEqual(getattr(stat, k), getattr(expected, k))
Beispiel #10
0
    def test_read_multi_processes(self):
        barrier = multiprocessing.Barrier(2)
        with local.open_zip(os.path.abspath(self.zip_file_path)) as z:
            with z.open(self.testfile_name) as f:
                f.read()

            def func():
                # accessing the shared container isn't supported in v2
                with self.assertRaises(RuntimeError):
                    with z.open(self.testfile_name) as f:
                        barrier.wait()
                        f.read()

            p1 = multiprocessing.Process(target=func)
            p2 = multiprocessing.Process(target=func)
            p1.start()
            p2.start()

            p1.join(timeout=1)
            p2.join(timeout=1)

            self.assertEqual(p1.exitcode, 0)
            self.assertEqual(p2.exitcode, 0)
Beispiel #11
0
 def test_remove(self):
     with local.open_zip(self.zip_file_path) as z:
         self.assertRaises(io.UnsupportedOperation, z.remove, "test/test",
                           False)
Beispiel #12
0
 def test_list(self, path_or_prefix, expected_list, recursive):
     with local.open_zip(self.zip_file_name) as z:
         zip_generator = z.list(path_or_prefix, recursive=recursive)
         zip_list = list(zip_generator)
         self.assertEqual(sorted(expected_list), sorted(zip_list))
Beispiel #13
0
 def test_open_non_normalized_path(self, path_or_prefix):
     with local.open_zip(os.path.abspath(self.zip_file_path)) as z:
         with z.open(path_or_prefix, "r") as zipped_file:
             self.assertEqual(self.test_string, zipped_file.read())
Beispiel #14
0
 def test_isdir_non_exist(self, path_or_prefix):
     with local.open_zip(self.zip_file_path) as z:
         self.assertFalse(z.isdir(path_or_prefix))
Beispiel #15
0
 def test_stat_non_exist(self, path_or_prefix):
     with local.open_zip(self.zip_file_path) as z:
         with self.assertRaises(FileNotFoundError):
             z.stat(path_or_prefix)
Beispiel #16
0
 def test_stat(self, path_or_prefix, expected):
     with local.open_zip(self.zip_file_path) as z:
         self.assertEqual(expected, z.stat(path_or_prefix).filename)
Beispiel #17
0
 def test_makedirs(self):
     with local.open_zip(self.zip_file_path) as z:
         self.assertRaises(io.UnsupportedOperation, z.makedirs, "test/test")
Beispiel #18
0
 def test_isdir(self, path_or_prefix, expected):
     with local.open_zip(self.zip_file_name) as z:
         self.assertEqual(z.isdir(path_or_prefix), expected)
Beispiel #19
0
 def test_not_exists(self, non_exist_file):
     with local.open_zip(self.zip_file_path) as z:
         self.assertFalse(z.exists(non_exist_file))
Beispiel #20
0
 def test_exists(self, path_or_prefix, expected):
     with local.open_zip(self.zip_file_path) as z:
         self.assertEqual(z.exists(path_or_prefix), expected)
Beispiel #21
0
 def test_isdir_not_exist(self, dir):
     with local.open_zip(self.zip_file_name) as z:
         self.assertFalse(z.isdir(dir))
Beispiel #22
0
 def test_read_string(self):
     with local.open_zip(os.path.abspath(self.zip_file_path)) as z:
         with z.open(self.zipped_file_path, "r") as zipped_file:
             self.assertEqual(self.test_string, zipped_file.readline())