# -*- 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()