summaryrefslogtreecommitdiff
path: root/fs/tests/test_iotools.py
blob: e3ab9737f4ce6d531078a40577e694d190a65a2c (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
from __future__ import unicode_literals

from fs import iotools

import io
import unittest
from os.path import dirname, join, abspath

try:
    unicode
except NameError:
    unicode = str


class OpenFilelike(object):
    def __init__(self, make_f):
        self.make_f = make_f

    @iotools.filelike_to_stream
    def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs):
        return self.make_f()

    def __enter__(self):
        return self

    def __exit__(self, *args, **kwargs):
        self.f.close()


class TestIOTools(unittest.TestCase):

    def get_bin_file(self):
        path = join(dirname(abspath(__file__)), 'data/UTF-8-demo.txt')
        return io.open(path, 'rb')

    def test_make_stream(self):
        """Test make_stream"""
        with self.get_bin_file() as f:
            text = f.read()
            self.assert_(isinstance(text, bytes))

        with self.get_bin_file() as f:
            with iotools.make_stream("data/UTF-8-demo.txt", f, 'rt') as f2:
                text = f2.read()
                self.assert_(isinstance(text, unicode))

    def test_decorator(self):
        """Test filelike_to_stream decorator"""
        o = OpenFilelike(self.get_bin_file)
        with o.open('file', 'rb') as f:
            text = f.read()
            self.assert_(isinstance(text, bytes))

        with o.open('file', 'rt') as f:
            text = f.read()
            self.assert_(isinstance(text, unicode))