Home | History | Annotate | Download | only in utils
      1 # Copyright 2017 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import os
      6 import unittest
      7 import zipfile
      8 
      9 from devil.utils import zip_utils
     10 from py_utils import tempfile_ext
     11 
     12 
     13 class WriteZipFileTest(unittest.TestCase):
     14 
     15   def testSimple(self):
     16     with tempfile_ext.NamedTemporaryDirectory() as working_dir:
     17       file1 = os.path.join(working_dir, 'file1.txt')
     18       file2 = os.path.join(working_dir, 'file2.txt')
     19 
     20       with open(file1, 'w') as f1:
     21         f1.write('file1')
     22       with open(file2, 'w') as f2:
     23         f2.write('file2')
     24 
     25       zip_tuples = [
     26         (file1, 'foo/file1.txt'),
     27         (file2, 'bar/file2.txt'),
     28       ]
     29 
     30       zip_path = os.path.join(working_dir, 'out.zip')
     31       zip_utils.WriteZipFile(zip_path, zip_tuples)
     32 
     33       self.assertTrue(zipfile.is_zipfile(zip_path))
     34 
     35       actual = zipfile.ZipFile(zip_path)
     36       expected_files = [
     37         'foo/file1.txt',
     38         'bar/file2.txt',
     39       ]
     40 
     41       self.assertEquals(
     42           sorted(expected_files),
     43           sorted(actual.namelist()))
     44