Skip to content

Commit

Permalink
Fixes for CI with new black release (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
jborean93 authored Jan 31, 2022
1 parent be9fd0e commit f7c18b6
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:

jobs:
build:
name: build sdist and wheel
name: build library
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -35,7 +35,7 @@ jobs:
- name: install build pre-requisites
run: pip install build

- name: build sdist
- name: build sdist and wheel
run: python -m build --sdist --wheel

- uses: actions/upload-artifact@v2
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.1.0
hooks:
- id: black

Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ krb5 ; sys_platform != 'win32'
isort
mypy
pre-commit
pycodestyle
pytest
pytest-cov
pytest-mock
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ packages = find:
include_package_data = True
install_requires =
cryptography
pyspnego<=1.0.0
pyspnego<1.0.0
requests>=2.9.1

[options.packages.find]
Expand Down
6 changes: 3 additions & 3 deletions src/pypsrp/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self) -> None:
self.cipher: typing.Any = None
# Finds C0, C1 and surrogate pairs in a unicode string for us to
# encode according to the PSRP rules
self._serial_str = re.compile(u"[\u0000-\u001F" u"\u007F-\u009F" u"\U00010000-\U0010FFFF]")
self._serial_str = re.compile("[\u0000-\u001F\u007F-\u009F\U00010000-\U0010FFFF]")

# to support surrogate UTF-16 pairs we need to use a UTF-16 regex
# so we can replace the UTF-16 string representation with the actual
Expand Down Expand Up @@ -574,15 +574,15 @@ def rplcr(matchobj):
hex_char = to_unicode(binascii.hexlify(byte_char)).upper()
hex_split = [hex_char[i : i + 4] for i in range(0, len(hex_char), 4)]

return u"".join([u"_x%s_" % i for i in hex_split])
return "".join(["_x%s_" % i for i in hex_split])

# before running the translation we need to make sure _ before x is
# encoded, normally _ isn't encoded except when preceding x
string_value = to_unicode(value)

# The MS-PSRP docs don't state this but the _x0000_ matcher is case insensitive so we need to make sure we
# escape _X as well as _x.
string_value = re.sub(u"(?i)_(x)", u"_x005F_\\1", string_value)
string_value = re.sub("(?i)_(x)", "_x005F_\\1", string_value)
string_value = re.sub(self._serial_str, rplcr, string_value)

return string_value
Expand Down
22 changes: 11 additions & 11 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_client_copy_file_really_large(self, wsman_conn, monkeypatch, mocker):
os.remove(path)

# verify the returned object is the full path
assert actual == u"C:\\Users\\vagrant\\Documents\\test_file"
assert actual == "C:\\Users\\vagrant\\Documents\\test_file"

@pytest.mark.parametrize("wsman_conn", [[True, "test_client_copy_file_failure"]], indirect=True)
def test_client_copy_file_failure(self, wsman_conn, monkeypatch, mocker):
Expand Down Expand Up @@ -183,12 +183,12 @@ def test_client_execute_cmd(self, wsman_conn):
actual = client.execute_cmd("dir")
actual_args = client.execute_cmd("echo abc")

assert u"Volume in drive C" in actual[0]
assert actual[1] == u""
assert "Volume in drive C" in actual[0]
assert actual[1] == ""
assert actual[2] == 0

assert actual_args[0] == u"abc\r\n"
assert actual_args[1] == u""
assert actual_args[0] == "abc\r\n"
assert actual_args[1] == ""
assert actual_args[2] == 0

@pytest.mark.parametrize("wsman_conn", [[True, "test_client_execute_cmd_environment"]], indirect=True)
Expand Down Expand Up @@ -217,11 +217,11 @@ def test_client_execute_ps(self, wsman_conn):
client = self._get_client(wsman_conn)

expected_stdout = (
u"winrm\nRunning\n\nStatus Name "
u"DisplayName \n------"
u" ---- ----------- "
u" \nRunning winrm Wi"
u"ndows Remote Management (WS-Manag...\n\n"
"winrm\nRunning\n\nStatus Name "
"DisplayName \n------"
" ---- ----------- "
" \nRunning winrm Wi"
"ndows Remote Management (WS-Manag...\n\n"
)
actual = client.execute_ps("$serv = Get-Service -Name winrm; $serv.Name; $serv.Status; $serv")
assert actual[0] == expected_stdout
Expand Down Expand Up @@ -256,7 +256,7 @@ def test_client_execute_ps_failure(self, wsman_conn):

actual = client.execute_ps("Get-ServiceTypo -Name winrm")

assert actual[0] == u""
assert actual[0] == ""
assert len(actual[1].error) == 1
assert (
str(actual[1].error[0]) == "The term 'Get-ServiceTypo' is not recognized as the name of a "
Expand Down
4 changes: 2 additions & 2 deletions tests/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def test_psrp_pshost_ui_mocked_methods(self, wsman_conn, monkeypatch, mocker):

with RunspacePool(wsman_conn, host=host) as pool:
pool.exchange_keys()
mock_read_line_as_ss.return_value = pool.serialize(u"ReadLineAsSecureString response", ObjectMeta("SS"))
mock_ps_credential = PSCredential(username="username", password=u"password")
mock_read_line_as_ss.return_value = pool.serialize("ReadLineAsSecureString response", ObjectMeta("SS"))
mock_ps_credential = PSCredential(username="username", password="password")
mock_prompt_credential.return_value = mock_ps_credential

ps = PowerShell(pool)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_psrp(self, functional_transports):
ps.add_cmdlet("Get-Item").add_parameter("Path", "C:\\Windows")
ps.add_statement()

sec_string = pool.serialize(u"super secret", ObjectMeta("SS"))
sec_string = pool.serialize("super secret", ObjectMeta("SS"))
ps.add_cmdlet("Set-Variable")
ps.add_parameter("Name", "password")
ps.add_parameter("Value", sec_string)
Expand All @@ -122,8 +122,8 @@ def test_psrp(self, functional_transports):
assert ps.had_errors is False
assert len(actual) == 3
assert str(actual[0]) == "C:\\Windows"
assert actual[1] == u"super secret"
assert actual[2] == u"host secret"
assert actual[1] == "super secret"
assert actual[2] == "host secret"
assert str(ps.streams.verbose[0]) == large_string

def test_psrp_jea(self, functional_transports):
Expand Down
26 changes: 13 additions & 13 deletions tests/test_powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def test_psrp_multiple_commands(self, wsman_conn, monkeypatch):
ps = PowerShell(pool)

# Test out Secure Strings
sec_string = pool.serialize(u"Hello World", ObjectMeta("SS"))
sec_string = pool.serialize("Hello World", ObjectMeta("SS"))
ps.add_cmdlet("Set-Variable")
ps.add_parameter("Name", "sec_string")
ps.add_parameter("Value", sec_string)
Expand All @@ -501,7 +501,7 @@ def test_psrp_multiple_commands(self, wsman_conn, monkeypatch):
ps.add_parameter("Force")

# Test out Unicode and complex info
string_value = u"こんにちは - actual_x000A_string\nnewline: %s" % b"\xD8\x01\xDC\x37".decode("utf-16-be")
string_value = "こんにちは - actual_x000A_string\nnewline: %s" % b"\xD8\x01\xDC\x37".decode("utf-16-be")
ps.add_statement().add_cmdlet("Set-Variable")
ps.add_parameter("Name", "unicode_string")
ps.add_parameter("Value", string_value)
Expand Down Expand Up @@ -531,10 +531,10 @@ def test_psrp_multiple_commands(self, wsman_conn, monkeypatch):
pool.close()

assert len(output) == 7
assert output[0] == u"Hello World"
assert output[1] == u"abc"
assert output[0] == "Hello World"
assert output[1] == "abc"
assert output[2] == string_value
assert output[3] == u'hi"'
assert output[3] == 'hi"'
# this result differs on whether this is mocked or not
if type(wsman_conn.transport).__name__ == "TransportFake":
assert output[4] == "win-nnmu24vvkj0\\vagrant"
Expand Down Expand Up @@ -587,9 +587,9 @@ def test_psrp_run_protocol_version(self, wsman_conn):
assert len(actual) == 4
assert str(actual[0]) == "error"
assert isinstance(actual[0], ErrorRecord)
assert actual[1] == u"message 1"
assert actual[1] == "message 1"
assert actual[2] == 2
assert actual[3] == [u"3", 3]
assert actual[3] == ["3", 3]
assert ps.state == PSInvocationState.COMPLETED
assert ps.had_errors is False
assert ps.streams.error == []
Expand Down Expand Up @@ -808,7 +808,7 @@ def test_psrp_with_input(self, wsman_conn):
)
actual = ps.invoke(["1", 2, {"a": "b"}, ["a", "b"]])

assert actual == [u"1", 2, {u"a": u"b"}, [u"a", u"b"]]
assert actual == ["1", 2, {"a": "b"}, ["a", "b"]]
assert str(ps.streams.debug[0]) == "Start Block"
assert str(ps.streams.debug[1]) == "End Block"

Expand Down Expand Up @@ -844,9 +844,9 @@ def test_psrp_small_msg_size(self, wsman_conn):
% ("a" * 30000)
)
actual = ps.invoke("input")
assert actual[0] == u"input"
assert actual[1] == u"a" * 20000
assert actual[2] == u"a" * 10000
assert actual[0] == "input"
assert actual[1] == "a" * 20000
assert actual[2] == "a" * 10000

@pytest.mark.parametrize(
"wsman_conn",
Expand All @@ -863,7 +863,7 @@ def test_psrp_long_running_cmdlet(self, wsman_conn):
ps.add_cmdlet("Start-Sleep").add_parameter("Seconds", 10)
ps.add_statement().add_script("echo hi")
actual = ps.invoke()
assert actual[0] == u"hi"
assert actual[0] == "hi"

@pytest.mark.parametrize("wsman_conn", [[True, "test_psrp_clear_commands"]], indirect=True)
def test_psrp_clear_command(self, wsman_conn):
Expand All @@ -873,7 +873,7 @@ def test_psrp_clear_command(self, wsman_conn):
ps.clear_commands()
ps.add_script("echo new")
actual = ps.invoke()
assert actual[0] == u"new"
assert actual[0] == "new"

@pytest.mark.parametrize("wsman_conn", [[True, "test_psrp_receive_failure"]], indirect=True)
def test_psrp_receive_failure(self, wsman_conn):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_serialize_string(self, input_val, expected):
@pytest.mark.parametrize(
"data, expected",
[
[u"a", "<S>a</S>"],
["a", "<S>a</S>"],
["a", "<S>a</S>"],
[1, "<I32>1</I32>"],
[True, "<B>true</B>"],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ def test_winrs_extra_opts(self, wsman_conn):
@pytest.mark.parametrize("wsman_conn", [[True, "test_winrs_unicode"]], indirect=True)
def test_winrs_unicode(self, wsman_conn):
with WinRS(wsman_conn, codepage=65001) as shell:
process = Process(shell, "powershell.exe", [u"Write-Host こんにちは"])
process = Process(shell, "powershell.exe", ["Write-Host こんにちは"])
process.invoke()
process.signal(SignalCode.CTRL_C)
assert process.rc == 0
assert process.stdout.decode("utf-8") == u"こんにちは\n"
assert process.stdout.decode("utf-8") == "こんにちは\n"
assert process.stderr == b""

@pytest.mark.parametrize(
Expand Down
14 changes: 7 additions & 7 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

def test_unicode_to_bytes_default():
expected = b"\x61\x62\x63"
actual = to_bytes(u"abc")
actual = to_bytes("abc")
assert actual == expected


def test_unicode_to_bytes_diff_encoding():
expected = b"\x61\x00\x62\x00\x63\x00"
actual = to_bytes(u"abc", encoding="utf-16-le")
actual = to_bytes("abc", encoding="utf-16-le")
assert actual == expected


Expand All @@ -37,25 +37,25 @@ def test_str_to_bytes():


def test_unicode_to_unicode():
expected = u"abc"
actual = to_unicode(u"abc")
expected = "abc"
actual = to_unicode("abc")
assert actual == expected


def test_byte_to_unicode():
expected = u"abc"
expected = "abc"
actual = to_unicode(b"\x61\x62\x63")
assert actual == expected


def test_byte_to_unicode_diff_encoding():
expected = u"abc"
expected = "abc"
actual = to_unicode(b"\x61\x00\x62\x00\x63\x00", encoding="utf-16-le")
assert actual == expected


def test_str_to_unicode():
expected = u"a\x00b\x00c\x00"
expected = "a\x00b\x00c\x00"
actual = to_unicode("a\x00b\x00c\x00", encoding="utf-16-le")
assert actual == expected

Expand Down

0 comments on commit f7c18b6

Please sign in to comment.