1
# Copyright (C) 2013 Canonical Ltd.
2
# Author: Colin Watson <cjwatson@ubuntu.com>
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 3 of the License.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
"""Unit tests for click.arfile."""
18
from __future__ import print_function
29
from click.arfile import ArFile
30
from click.tests.helpers import TestCase, touch
33
class TestArFile(TestCase):
35
super(TestArFile, self).setUp()
38
def test_init_rejects_mode_r(self):
39
self.assertRaises(ValueError, ArFile, mode="r")
41
def test_init_name(self):
42
path = os.path.join(self.temp_dir, "foo.a")
43
with ArFile(name=path, mode="w") as arfile:
44
self.assertEqual("w", arfile.mode)
45
self.assertEqual("wb", arfile.real_mode)
46
self.assertEqual(path, arfile.name)
47
self.assertEqual(path, arfile.fileobj.name)
48
self.assertTrue(arfile.opened_fileobj)
49
self.assertFalse(arfile.closed)
51
def test_init_rejects_readonly_fileobj(self):
52
path = os.path.join(self.temp_dir, "foo.a")
54
with open(path, "rb") as fileobj:
55
self.assertRaises(ValueError, ArFile, fileobj=fileobj)
57
def test_init_fileobj(self):
58
path = os.path.join(self.temp_dir, "foo.a")
59
with open(path, "wb") as fileobj:
60
arfile = ArFile(fileobj=fileobj)
61
self.assertEqual("w", arfile.mode)
62
self.assertEqual("wb", arfile.real_mode)
63
self.assertEqual(path, arfile.name)
64
self.assertEqual(fileobj, arfile.fileobj)
65
self.assertFalse(arfile.opened_fileobj)
66
self.assertFalse(arfile.closed)
68
def test_writes_valid_ar_file(self):
69
member_path = os.path.join(self.temp_dir, "member")
70
with open(member_path, "wb") as member:
71
member.write(b"\x00\x01\x02\x03\x04\x05\x06\x07")
72
path = os.path.join(self.temp_dir, "foo.a")
73
with ArFile(name=path, mode="w") as arfile:
75
arfile.add_data("data-member", b"some data")
76
arfile.add_file("file-member", member_path)
77
extract_path = os.path.join(self.temp_dir, "extract")
78
os.mkdir(extract_path)
79
subprocess.call(["ar", "x", path], cwd=extract_path)
80
self.assertCountEqual(
81
["data-member", "file-member"], os.listdir(extract_path))
82
with open(os.path.join(extract_path, "data-member"), "rb") as member:
83
self.assertEqual(b"some data", member.read())
84
with open(os.path.join(extract_path, "file-member"), "rb") as member:
86
b"\x00\x01\x02\x03\x04\x05\x06\x07", member.read())