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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
import platform
from pathlib import Path
import pytest
# Default encoding is platform specific.
if platform.platform() == "Windows":
CONSOLE_ENCODING = "cp437"
else:
CONSOLE_ENCODING = "UTF-8"
def test_no_overrides(mock_sub):
"""With no overrides, there are still kwargs."""
assert mock_sub.final_kwargs() == {
"text": True,
"encoding": CONSOLE_ENCODING,
"errors": "backslashreplace",
}
def test_explicit_no_overrides(mock_sub):
"""With explicitly no overrides, there are still kwargs."""
assert mock_sub.final_kwargs(env=None) == {
"env": {
"VAR1": "Value 1",
"PS1": "\nLine 2\n\nLine 4",
"PWD": "/home/user/",
},
"text": True,
"encoding": CONSOLE_ENCODING,
"errors": "backslashreplace",
}
def test_env_overrides(mock_sub):
"""If environmental overrides are provided, they supersede the default
environment."""
assert mock_sub.final_kwargs(env={"NEWVAR": "value", "VAR1": "New Value"}) == {
"env": {
"VAR1": "New Value",
"PS1": "\nLine 2\n\nLine 4",
"PWD": "/home/user/",
"NEWVAR": "value",
},
"text": True,
"encoding": CONSOLE_ENCODING,
"errors": "backslashreplace",
}
def test_env_overrides_none(mock_sub):
"""Any overrides set to None are absent in the final kwargs."""
assert mock_sub.final_kwargs(env={"NEWVAR": "val", "VAR1": None, "VAR2": None}) == {
"env": {
"PS1": "\nLine 2\n\nLine 4",
"PWD": "/home/user/",
"NEWVAR": "val",
},
"text": True,
"encoding": CONSOLE_ENCODING,
"errors": "backslashreplace",
}
def test_cwd_provided(mock_sub):
"""If a cwd is provided, it is reflected in the environment."""
cwd_override = "/my/current/path"
assert mock_sub.final_kwargs(cwd=cwd_override) == {
"text": True,
"encoding": CONSOLE_ENCODING,
"errors": "backslashreplace",
"cwd": cwd_override,
}
def test_non_str_cwd_provided(mock_sub):
"""If the cwd isn't a string, it's converted to string."""
cwd_override = Path("/my/current/path")
assert mock_sub.final_kwargs(cwd=cwd_override) == {
"text": True,
"encoding": CONSOLE_ENCODING,
"errors": "backslashreplace",
"cwd": str(cwd_override),
}
@pytest.mark.parametrize(
"in_kwargs, final_kwargs",
[
# Default handling
(
{},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "backslashreplace"},
),
# Explicit text/universal_newlines provided
(
{"text": True},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "backslashreplace"},
),
({"text": False}, {"text": False}),
({"universal_newlines": False}, {"text": False}),
(
{"universal_newlines": True},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "backslashreplace"},
),
# Explicit encoding provided
(
{"encoding": "ibm850"},
{"text": True, "encoding": "ibm850", "errors": "backslashreplace"},
),
(
{"text": True, "encoding": "ibm850"},
{"text": True, "encoding": "ibm850", "errors": "backslashreplace"},
),
(
{"text": False, "encoding": "ibm850"},
{"text": True, "encoding": "ibm850", "errors": "backslashreplace"},
),
(
{"universal_newlines": False, "encoding": "ibm850"},
{"text": True, "encoding": "ibm850", "errors": "backslashreplace"},
),
(
{"universal_newlines": True, "encoding": "ibm850"},
{"text": True, "encoding": "ibm850", "errors": "backslashreplace"},
),
# Explicit errors provided
(
{"errors": "emojireplace"},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "emojireplace"},
),
(
{"encoding": "ascii", "errors": "emojireplace"},
{"text": True, "encoding": "ascii", "errors": "emojireplace"},
),
(
{"text": True, "errors": "emojireplace"},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "emojireplace"},
),
(
{"text": False, "errors": "emojireplace"},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "emojireplace"},
),
(
{"universal_newlines": True, "errors": "emojireplace"},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "emojireplace"},
),
(
{"universal_newlines": False, "errors": "emojireplace"},
{"text": True, "encoding": CONSOLE_ENCODING, "errors": "emojireplace"},
),
],
)
def test_text_conversion(mock_sub, in_kwargs, final_kwargs):
"""Text/universal_newlines is correctly inserted/overridden, with encoding."""
assert mock_sub.final_kwargs(**in_kwargs) == final_kwargs
|