-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_adbfs.py
56 lines (47 loc) · 2.35 KB
/
test_adbfs.py
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
# -*- coding: utf-8 -*-
import os
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
import unittest
from unittest import mock
module_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'adbfs')
spec = spec_from_loader("adbfs", SourceFileLoader("adbfs", module_path))
adbfs = module_from_spec(spec)
spec.loader.exec_module(adbfs)
LISTING = '''\
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/Grüß Gott
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/\x80
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/Γεια σας
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/Здравствуйте
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/שָׁלוֹם
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/السَّلامُ عَلَيْكُمْ
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/გამარჯობა
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/こんにちは。
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/안녕하십니까
''' # noqa
class TestCheckOutput(unittest.TestCase):
@mock.patch('subprocess.check_output')
def test_check_output(self, out):
"""
As for Python2 (and its last version: 2.7), subprocess.check_output
always return string like objects, contrary to bytes - no conversion
to string is needed.
Python3 treats string as unicode objects, but subprocess.check_output
returns bytes object, which is equvalend for py2 string… annoying.
"""
out.return_value = bytes(LISTING, 'utf-8')
result = adbfs.check_output(None)
self.assertEqual(result, LISTING)
@mock.patch('subprocess.check_output')
def test_check_output_py3_invalid_char(self, out):
"""
Special case for py3. We have bytes with some weird character - like
some system write something with codepage, instead of utf8.
"""
line = (b'-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 '
b'/storage/emulated/0/\xe2\n') # Latin 1 char â
out.return_value = bytes(line)
result = adbfs.check_output(None)
self.assertEqual(result, line.decode('iso-8859-1'))
if __name__ == "__main__":
unittest.main()