from easyprocess import EasyProcess
from easyprocess.unicodeutil import split_command, EasyProcessUnicodeError
from nose.tools import eq_
from unittest import TestCase
import six
u = six.u
OMEGA = u('\u03A9')


class Test(TestCase):

    def test_str(self):
        eq_(EasyProcess(u('ls -la')).call().return_code, 0)

    def test_ls(self):
        eq_(EasyProcess([u('ls'), u('-la')]).call().return_code, 0)

    def test_parse(self):
        eq_(EasyProcess(u('ls -la')).cmd, ['ls', '-la'])
        eq_(EasyProcess(u('ls "abc"')).cmd, ['ls', 'abc'])
        eq_(EasyProcess(u('ls "ab c"')).cmd, ['ls', 'ab c'])

    def test_split(self):
        # list -> list
        eq_(split_command([str('x'), str('y')]), ['x', 'y'])
        eq_(split_command([str('x'), u('y')]), ['x', 'y'])
        eq_(split_command([str('x'), OMEGA]), ['x', OMEGA])

        # str -> list
        eq_(split_command(str('x y')), ['x', 'y'])
        eq_(split_command(u('x y')), ['x', 'y'])
        if six.PY3:
            eq_(split_command(u('x ') + OMEGA), ['x', OMEGA])
        else:
            self.assertRaises(EasyProcessUnicodeError, lambda:
                              split_command(u('x ') + OMEGA))

    def test_echo(self):
        eq_(EasyProcess(u('echo hi')).call().stdout, 'hi')

        if six.PY3:
            eq_(EasyProcess(u('echo ') + OMEGA).call().stdout, OMEGA)
        else:
            # unicode is not supported
            self.assertRaises(EasyProcessUnicodeError,
                              lambda: EasyProcess(u('echo ') + OMEGA).call().stdout)

        eq_(EasyProcess(['echo', OMEGA]).call().stdout, OMEGA)

@@ -1594,28 +1594,464 @@
 EGA%5D).call().stdout, OMEGA)%0A
+%0A    def test_invalid_stdout(self):%0A        %22%22%22invalid utf-8 byte in stdout.%22%22%22%0A        # https://en.wikipedia.org/wiki/UTF-8#Codepage_layout%0A%0A        # 0x92  continuation byte%0A#         eq_(EasyProcess(%5B'bash', '-c', 'printf %22%5C%5Cx$(printf %25x 0x92)%22'%5D).call().stdout, '')%0A%0A        # 0xFF must never appear in a valid UTF-8 sequence%0A        eq_(EasyProcess(%0A            %5B'bash', '-c', 'printf %22%5C%5Cx$(printf %25x 0xFF)%22'%5D).call().stdout, '')%0A

test for invalid utf-8 byte in stdout #14