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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
import sys
from easyprocess import EasyProcess
from easyprocess.unicodeutil import split_command
OMEGA = "\u03A9"
python = sys.executable
def platform_is_win():
return sys.platform == "win32"
def py_minor():
return sys.version_info[1]
def test_str():
assert EasyProcess("ls -la").call().return_code == 0
def test_ls():
assert EasyProcess(["ls", "-la"]).call().return_code == 0
def test_parse():
assert EasyProcess("ls -la").cmd == ["ls", "-la"]
assert EasyProcess('ls "abc"').cmd == ["ls", "abc"]
assert EasyProcess('ls "ab c"').cmd == ["ls", "ab c"]
def test_split():
# list -> list
assert split_command([str("x"), str("y")]) == ["x", "y"]
assert split_command([str("x"), "y"]) == ["x", "y"]
assert split_command([str("x"), OMEGA]) == ["x", OMEGA]
# str -> list
assert split_command(str("x y")) == ["x", "y"]
assert split_command("x y") == ["x", "y"]
assert split_command("x " + OMEGA) == ["x", OMEGA]
# split windows paths #12
assert split_command("c:\\temp\\a.exe someArg", posix=False) == [
"c:\\temp\\a.exe",
"someArg",
]
def test_echo():
assert EasyProcess("echo hi").call().stdout == "hi"
if not platform_is_win():
assert EasyProcess("echo " + OMEGA).call().stdout == OMEGA
assert EasyProcess(["echo", OMEGA]).call().stdout == OMEGA
def test_argv():
assert (
EasyProcess([python, "-c", r"import sys;assert sys.argv[1]=='123'", "123"])
.call()
.return_code
== 0
)
assert (
EasyProcess([python, "-c", r"import sys;assert sys.argv[1]=='\u03a9'", OMEGA])
.call()
.return_code
== 0
)
if py_minor() > 6: # sys.stdout.reconfigure from py3.7
def test_py_print():
assert (
EasyProcess(
[
python,
"-c",
r"import sys;sys.stdout.reconfigure(encoding='utf-8');print('\u03a9')",
]
)
.call()
.stdout
== OMEGA
)
assert (
EasyProcess(
[
python,
"-c",
r"import sys;sys.stdout.reconfigure(encoding='utf-8');print(sys.argv[1])",
OMEGA,
]
)
.call()
.stdout
== OMEGA
)
def test_py_stdout_write():
assert (
EasyProcess(
[
python,
"-c",
r"import sys;sys.stdout.buffer.write('\u03a9'.encode('utf-8'))",
]
)
.call()
.stdout
== OMEGA
)
def test_invalid_stdout():
"""invalid utf-8 byte in stdout."""
# https://en.wikipedia.org/wiki/UTF-8#Codepage_layout
# 0x92 continuation byte
cmd = [python, "-c", "import sys;sys.stdout.buffer.write(b'\\x92')"]
p = EasyProcess(cmd).call()
assert p.return_code == 0
assert p.stdout == ""
# 0xFF must never appear in a valid UTF-8 sequence
cmd = [python, "-c", "import sys;sys.stdout.buffer.write(b'\\xFF')"]
p = EasyProcess(cmd).call()
assert p.return_code == 0
assert p.stdout == ""
|