summaryrefslogtreecommitdiff
path: root/tests/zip/zip_test.py
blob: 89d2c04c823fc6ec50563574b2eea0253a21c6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import filecmp
import os
import unittest
import zipfile

from bazel_tools.tools.python.runfiles import runfiles
from tests.zip import zip_test_lib

HELLO_CRC = 2069210904
LOREM_CRC = 2178844372
EXECUTABLE_CRC = 342626072


class ZipContentsTests(zip_test_lib.ZipContentsTestBase):

  def test_empty(self):
    self.assertZipFileContent("test_zip_empty.zip", [])

  def test_basic(self):
    if os.name == "nt":
      expect = [
          {"filename": "an_executable.exe", "isexe": True},
          {"filename": "foodir/", "isdir": True, "attr": 0o711},
          {"filename": "hello.txt", "crc": HELLO_CRC},
          {"filename": "loremipsum.txt", "crc": LOREM_CRC},
          {"filename": "usr/bin/foo", "isexe": True, "data": "/usr/local/foo/foo.real"},
      ]
    else:
      expect = [
          {"filename": "an_executable", "isexe": True},
          {"filename": "foodir/", "isdir": True, "attr": 0o711},
          {"filename": "hello.txt", "crc": HELLO_CRC},
          {"filename": "loremipsum.txt", "crc": LOREM_CRC},
          {"filename": "usr/bin/foo", "isexe": True, "data": "/usr/local/foo/foo.real"},
      ]

    self.assertZipFileContent("test_zip_basic.zip", expect)

  def test_timestamp(self):
    self.assertZipFileContent("test_zip_timestamp.zip", [
        {"filename": "hello.txt", "crc": HELLO_CRC, "timestamp": 1234567890},
    ])

  def test_permissions(self):
    self.assertZipFileContent("test_zip_permissions.zip", [
        {
            "filename": "executable.sh",
            "crc": EXECUTABLE_CRC,
            "timestamp": 1234567890,
            "attr": 0o644,
        }
    ])

  def test_package_dir(self):
    self.assertZipFileContent("test_zip_package_dir0.zip", [
        {"filename": "abc/def/hello.txt", "crc": HELLO_CRC},
        {"filename": "abc/def/loremipsum.txt", "crc": LOREM_CRC},
    ])

  def test_package_dir_substitution(self):
    self.assertZipFileContent("test_zip_package_dir_substitution.zip", [
        {"filename": "level1/some_value/level3/hello.txt", "crc": HELLO_CRC},
        {"filename": "level1/some_value/level3/loremipsum.txt", "crc": LOREM_CRC},
    ])

  def test_zip_strip_prefix_empty(self):
    self.assertZipFileContent("test-zip-strip_prefix-empty.zip", [
        {"filename": "loremipsum.txt", "crc": LOREM_CRC},
    ])

  def test_zip_strip_prefix_none(self):
    self.assertZipFileContent("test-zip-strip_prefix-none.zip", [
        {"filename": "loremipsum.txt", "crc": LOREM_CRC},
    ])

  def test_zip_strip_prefix_zipcontent(self):
    self.assertZipFileContent("test-zip-strip_prefix-zipcontent.zip", [
        {"filename": "loremipsum.txt", "crc": LOREM_CRC},
    ])

  def test_zip_strip_prefix_dot(self):
    self.assertZipFileContent("test-zip-strip_prefix-dot.zip", [
        {"filename": "zipcontent/loremipsum.txt", "crc": LOREM_CRC},
    ])

  def test_zip_tree(self):
    self.assertZipFileContent("test_zip_tree.zip", [
        {"filename": "generate_tree/a/a"},
        {"filename": "generate_tree/a/b/c"},
        {"filename": "generate_tree/b/c/d"},
        {"filename": "generate_tree/b/d"},
        {"filename": "generate_tree/b/e"},
    ])


if __name__ == "__main__":
  unittest.main()