summaryrefslogtreecommitdiff
path: root/tests/archive_test.py
blob: 4c604c6f0bb17d991277b270dd6c6e1897abee41 (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
# Copyright 2015 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.
"""Testing for archive."""

import unittest

from python.runfiles import runfiles
from pkg.private import archive


class SimpleArReaderTest(unittest.TestCase):
  """Testing for SimpleArReader class."""

  def setUp(self):
    super(SimpleArReaderTest, self).setUp()
    self.data_files = runfiles.Create()

  def assertArFileContent(self, arfile, content):
    """Assert that arfile contains exactly the entry described by `content`.

    Args:
      arfile: the path to the AR file to test.
      content: an array describing the expected content of the AR file.
          Each entry in that list should be a dictionary where each field
          is a field to test in the corresponding SimpleArFileEntry. For
          testing the presence of a file "x", then the entry could simply
          be `{"filename": "x"}`, the missing field will be ignored.
    """
    print("READING: %s" % arfile)
    with archive.SimpleArReader(arfile) as f:
      current = f.next()
      i = 0
      while current:
        error_msg = "Extraneous file at end of archive %s: %s" % (
            arfile,
            current.filename
            )
        self.assertLess(i, len(content), error_msg)
        for k, v in content[i].items():
          value = getattr(current, k)
          error_msg = " ".join([
              "Value `%s` for key `%s` of file" % (value, k),
              "%s in archive %s does" % (current.filename, arfile),
              "not match expected value `%s`" % v
              ])
          self.assertEqual(value, v, error_msg)
        current = f.next()
        i += 1
      if i < len(content):
        self.fail("Missing file %s in archive %s" % (content[i], arfile))

  def testEmptyArFile(self):
    self.assertArFileContent(
        self.data_files.Rlocation("rules_pkg/tests/testdata/empty.ar"),
        [])

  def assertSimpleFileContent(self, names):
    datafile = self.data_files.Rlocation(
        "rules_pkg/tests/testdata/" + "_".join(names) + ".ar")
    # pylint: disable=g-complex-comprehension
    content = [{"filename": n,
                "size": len(n.encode("utf-8")),
                "data": n.encode("utf-8")}
               for n in names]
    self.assertArFileContent(datafile, content)

  def testAFile(self):
    self.assertSimpleFileContent(["a"])

  def testBFile(self):
    self.assertSimpleFileContent(["b"])

  def testABFile(self):
    self.assertSimpleFileContent(["ab"])

  def testA_BFile(self):
    self.assertSimpleFileContent(["a", "b"])

  def testA_ABFile(self):
    self.assertSimpleFileContent(["a", "ab"])

  def testA_B_ABFile(self):
    self.assertSimpleFileContent(["a", "b", "ab"])


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