File: io_test.py

package info (click to toggle)
pybtex 0.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,628 kB
  • sloc: python: 13,585; makefile: 181; sh: 39; javascript: 29
file content (129 lines) | stat: -rw-r--r-- 4,330 bytes parent folder | download | duplicates (3)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright (c) 2006-2021  Andrey Golovizin
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


from __future__ import unicode_literals

import errno
import posixpath
from unittest import TestCase

from pybtex import io


class MockFile(object):
    def __init__(self, name, mode):
        self.name = name
        self.mode = mode

    def __repr__(self):
        return "<mock open file '%s', mode '%s'>" % (self.name, self.mode)


class MockFilesystem(object):
    def __init__(self, files=(), writable_dirs=(), readonly_dirs=()):
        self.files = set(files)
        self.writable_dirs = set(writable_dirs)
        self.readonly_dirs = set(readonly_dirs)

    def add_file(self, path):
        self.files.add(path)

    def chdir(self, path):
        self.pwd = path

    def locate(self, filename):
        for path in self.files:
            if path.endswith(filename):
                return path

    def open_read(self, path, mode):
        if path in self.files:
            return MockFile(path, mode)
        else:
            raise IOError(errno.ENOENT, 'No such file or directory', path)

    def open_write(self, path, mode):
        dirname = posixpath.dirname(path)
        if dirname in self.writable_dirs:
            return MockFile(path, mode)
        else:
            raise IOError(errno.EACCES, 'Permission denied', path)

    def open(self, path, mode):
        full_path = posixpath.join(self.pwd, path)
        if 'w' in mode:
            return self.open_write(full_path, mode)
        else:
            return self.open_read(full_path, mode)


class IOTest(TestCase):
    def setUp(self):
        self.fs = MockFilesystem(
            files=(
                '/home/test/foo.bib',
                '/home/test/foo.bbl',
                '/usr/share/texmf/bibtex/bst/unsrt.bst',
            ),
            writable_dirs=('/home/test',),
            readonly_dirs=('/'),
        )
        self.fs.chdir('/home/test')

    def test_open_existing(self):
        file = io._open_existing(self.fs.open, 'foo.bbl', 'rb', locate=self.fs.locate)
        self.assertEqual(file.name, '/home/test/foo.bbl')
        self.assertEqual(file.mode, 'rb')

    def test_open_missing(self):
        self.assertRaises(
            EnvironmentError,
            io._open_existing, self.fs.open, 'nosuchfile.bbl', 'rb',
            locate=self.fs.locate,
        )

    def test_locate(self):
        file = io._open_existing(
            self.fs.open, 'unsrt.bst', 'rb', locate=self.fs.locate
        )
        self.assertEqual(file.name, '/usr/share/texmf/bibtex/bst/unsrt.bst')
        self.assertEqual(file.mode, 'rb')

    def test_create(self):
        file = io._open_or_create(self.fs.open, 'foo.bbl', 'wb', {})
        self.assertEqual(file.name, '/home/test/foo.bbl')
        self.assertEqual(file.mode, 'wb')

    def test_create_in_readonly_dir(self):
        self.fs.chdir('/')
        self.assertRaises(
            EnvironmentError,
            io._open_or_create, self.fs.open, 'foo.bbl', 'wb', {},
        )

    def test_create_in_fallback_dir(self):
        self.fs.chdir('/')
        file = io._open_or_create(
            self.fs.open, 'foo.bbl', 'wb', {'TEXMFOUTPUT': '/home/test'}
        )
        self.assertEqual(file.name, '/home/test/foo.bbl')
        self.assertEqual(file.mode, 'wb')