1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
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, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from ..errors import BinaryFile
19
from ..sixish import (
22
from . import TestCase, TestCaseInTempDir
23
from ..textfile import text_file, check_text_lines, check_text_path
26
class TextFile(TestCase):
28
def test_text_file(self):
29
s = BytesIO(b'ab' * 2048)
30
self.assertEqual(text_file(s).read(), s.getvalue())
31
s = BytesIO(b'a' * 1023 + b'\x00')
32
self.assertRaises(BinaryFile, text_file, s)
33
s = BytesIO(b'a' * 1024 + b'\x00')
34
self.assertEqual(text_file(s).read(), s.getvalue())
36
def test_check_text_lines(self):
37
lines = [b'ab' * 2048]
38
check_text_lines(lines)
39
lines = [b'a' * 1023 + b'\x00']
40
self.assertRaises(BinaryFile, check_text_lines, lines)
43
class TextPath(TestCaseInTempDir):
45
def test_text_file(self):
46
with open('boo', 'wb') as f:
48
check_text_path('boo')
49
with open('boo', 'wb') as f:
50
f.write(b'a' * 1023 + b'\x00')
51
self.assertRaises(BinaryFile, check_text_path, 'boo')