1
0
mirror of https://github.com/gryf/mc_adbfs.git synced 2025-12-18 20:10:21 +01:00
Files
mc_adbfs/test_adbfs.py
gryf 5ece2d579c Fix bad filenames for Python3.
In Python3, if there are filenames encoded with 8-bit encodings, there
might be an issues with converting them into unicode objects. This is a
workaround on this subject. Python2 is not affected.

Other than that, there was tests added to cover this case, appropriate
Makefile which automate creating venvs for both: Python 2 and 3, and
also there is a check against pep8 rules using flake8.
2019-05-14 21:14:24 +02:00

72 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
import os
import six
import unittest
try:
from unittest import mock
except ImportError:
import mock
FILE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'adbfs')
try:
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
spec = spec_from_loader("adbfs", SourceFileLoader("adbfs", FILE))
adbfs = module_from_spec(spec)
spec.loader.exec_module(adbfs)
except ImportError:
# py27
import imp
adbfs = imp.load_source('adbfs', FILE)
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.
"""
if six.PY3:
out.return_value = bytes(LISTING, 'utf-8')
else:
out.return_value = LISTING
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.
"""
if six.PY2:
# doesn't affect Python 2
return
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()