From 862f24d536ffde32cc636151e833c38d909584b2 Mon Sep 17 00:00:00 2001 From: Scott Poore Date: Feb 28 2018 19:11:24 +0000 Subject: drop extra newline from stdin processing https://pagure.io/python-pytest-multihost/issue/12 * Modified run_command to add -n to echo * added test_piping_input * dropped newline from assert for: - test_escaping - test_escaping_binary - test_background_explicit_wait - test_background_context echo -e used by stdin_text processing is appending an extra newline character to stdin. For some commands this causes problems. $ echo -e test|base64 dGVzdAo= vs: $ echo -en test|base64 dGVzdA== and: $ echo -en test> test_file $ cat test_file|base64 dGVzdA== So if stdin is a string that doesn't already contain a newline, we shouldn't append it as extra to the end of what we pass to the command. Signed-off-by: Scott Poore --- diff --git a/pytest_multihost/host.py b/pytest_multihost/host.py index 3a488f4..2937296 100644 --- a/pytest_multihost/host.py +++ b/pytest_multihost/host.py @@ -249,7 +249,7 @@ class BaseHost(object): command.stdin.write(encode(self.command_prelude)) if stdin_text: - command.stdin.write(b"echo -e ") + command.stdin.write(b"echo -en ") command.stdin.write(_echo_quote(encode(stdin_text))) command.stdin.write(b" | ") @@ -273,7 +273,7 @@ class BaseHost(object): def _echo_quote(bytestring): - """Encode a bytestring for use with bash & "echo -e" + """Encode a bytestring for use with bash & "echo -en" """ bytestring = bytestring.replace(b"\\", br"\\") bytestring = bytestring.replace(b"\0", br"\x00") diff --git a/test_pytestmultihost/test_localhost.py b/test_pytestmultihost/test_localhost.py index 22f7e62..62a3486 100644 --- a/test_pytestmultihost/test_localhost.py +++ b/test_pytestmultihost/test_localhost.py @@ -246,7 +246,7 @@ class TestLocalhost(object): raiseonerr=False, ) print(tee.stderr_text) - assert tee.stdout_text == stdin_text + '\n' + assert tee.stdout_text == stdin_text with open(test_file_path, "r") as f: assert f.read() == tee.stdout_text @@ -262,10 +262,20 @@ class TestLocalhost(object): stdin_text=stdin_bytes, raiseonerr=False, ) - assert tee.stdout_bytes == stdin_bytes + b'\n' + assert tee.stdout_bytes == stdin_bytes with open(test_file_path, "rb") as f: assert f.read() == tee.stdout_bytes + def test_piping_input(self, multihost, tmpdir): + host = multihost.host + b64 = host.run_command(['base64'], stdin_text='test') + assert b64.stdout_text == 'dGVzdA==' + '\n' + + def test_piping_input_with_newline(self, multihost, tmpdir): + host = multihost.host + b64 = host.run_command(['base64'], stdin_text='test\n') + assert b64.stdout_text == 'dGVzdAo=' + '\n' + def test_background_explicit_wait(self, multihost, tmpdir): host = multihost.host @@ -278,7 +288,7 @@ class TestLocalhost(object): host.run_command('cat > ' + pipe_filename, stdin_text='expected value') cat.wait() - assert cat.stdout_text == 'expected value\n' + assert cat.stdout_text == 'expected value' assert cat.returncode == 0 def test_background_context(self, multihost, tmpdir): @@ -293,7 +303,7 @@ class TestLocalhost(object): host.run_command('cat > ' + pipe_filename, stdin_text='expected value') - assert cat.stdout_text == 'expected value\n' + assert cat.stdout_text == 'expected value' assert cat.returncode == 0