~ubuntu-branches/ubuntu/saucy/click/saucy-proposed

« back to all changes in this revision

Viewing changes to click/tests/test_arfile.py

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2013-06-27 15:57:25 UTC
  • Revision ID: package-import@ubuntu.com-20130627155725-1kpggp0xots3peir
Tags: 0.1.3
Rename to click, per Mark Shuttleworth.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2013 Canonical Ltd.
 
2
# Author: Colin Watson <cjwatson@ubuntu.com>
 
3
 
 
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.
 
7
#
 
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.
 
12
#
 
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/>.
 
15
 
 
16
"""Unit tests for click.arfile."""
 
17
 
 
18
from __future__ import print_function
 
19
 
 
20
__metaclass__ = type
 
21
__all__ = [
 
22
    'TestArFile',
 
23
    ]
 
24
 
 
25
 
 
26
import os
 
27
import subprocess
 
28
 
 
29
from click.arfile import ArFile
 
30
from click.tests.helpers import TestCase, touch
 
31
 
 
32
 
 
33
class TestArFile(TestCase):
 
34
    def setUp(self):
 
35
        super(TestArFile, self).setUp()
 
36
        self.use_temp_dir()
 
37
 
 
38
    def test_init_rejects_mode_r(self):
 
39
        self.assertRaises(ValueError, ArFile, mode="r")
 
40
 
 
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)
 
50
 
 
51
    def test_init_rejects_readonly_fileobj(self):
 
52
        path = os.path.join(self.temp_dir, "foo.a")
 
53
        touch(path)
 
54
        with open(path, "rb") as fileobj:
 
55
            self.assertRaises(ValueError, ArFile, fileobj=fileobj)
 
56
 
 
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)
 
67
 
 
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:
 
74
            arfile.add_magic()
 
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:
 
85
            self.assertEqual(
 
86
                b"\x00\x01\x02\x03\x04\x05\x06\x07", member.read())