Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /build /zip_helpers_unittest.py
blob: 19000273b5cae6fbb7817a675deb6d35d59916a3 [file] [log] [blame]
Andrew Grieve60efd26f2023-04-17 16:19:07[diff] [blame]1#!/usr/bin/env python3
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7import pathlib
8import shutil
9import sys
10import tempfile
11import unittest
12import zipfile
13
14import zip_helpers
15
16
17def _make_test_zips(tmp_dir, create_conflct=False):
18 zip1= os.path.join(tmp_dir,'A.zip')
19 zip2= os.path.join(tmp_dir,'B.zip')
20with zipfile.ZipFile(zip1,'w')as z:
21 z.writestr('file1','AAAAA')
22 z.writestr('file2','BBBBB')
23with zipfile.ZipFile(zip2,'w')as z:
24 z.writestr('file2','ABABA'if create_conflctelse'BBBBB')
25 z.writestr('file3','CCCCC')
26return zip1, zip2
27
28
29classZipHelpersTest(unittest.TestCase):
30def test_merge_zips__identical_file(self):
31with tempfile.TemporaryDirectory()as tmp_dir:
32 zip1, zip2= _make_test_zips(tmp_dir)
33
34 merged_zip= os.path.join(tmp_dir,'merged.zip')
35 zip_helpers.merge_zips(merged_zip,[zip1, zip2])
36
37with zipfile.ZipFile(merged_zip)as z:
38 self.assertEqual(z.namelist(),['file1','file2','file3'])
39
40def test_merge_zips__conflict(self):
41with tempfile.TemporaryDirectory()as tmp_dir:
42 zip1, zip2= _make_test_zips(tmp_dir, create_conflct=True)
43
44 merged_zip= os.path.join(tmp_dir,'merged.zip')
45with self.assertRaises(Exception):
46 zip_helpers.merge_zips(merged_zip,[zip1, zip2])
47
48def test_merge_zips__conflict_with_append(self):
49with tempfile.TemporaryDirectory()as tmp_dir:
50 zip1, zip2= _make_test_zips(tmp_dir, create_conflct=True)
51
52with self.assertRaises(Exception):
53with zipfile.ZipFile(zip1,'a')as dst_zip:
54 zip_helpers.merge_zips(dst_zip,[zip2])
55
56
57if __name__=='__main__':
58 unittest.main()

[8]ページ先頭

©2009-2025 Movatter.jp