Skip to content

Commit 1c73113

Browse files
Merge pull request #160 from dmitry-lipetsk/D20241207_001--remote_op-expect_error
RemoteOperations::exec_command must not raise an exception when 'expect_error' is True (#159)
2 parents 364d358 + cb87d0a commit 1c73113

File tree

6 files changed

+98
-18
lines changed

6 files changed

+98
-18
lines changed

testgres/operations/remote_ops.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,26 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
8383

8484
exit_status = process.returncode
8585

86-
if encoding:
87-
result = result.decode(encoding)
88-
error = error.decode(encoding)
89-
90-
if expect_error:
91-
raise Exception(result, error)
86+
assert type(result) == bytes # noqa: E721
87+
assert type(error) == bytes # noqa: E721
9288

9389
if not error:
94-
error_found = 0
90+
error_found = False
9591
else:
96-
error = normalize_error(error)
9792
error_found = exit_status != 0 or any(
98-
marker in error for marker in ['error', 'Permission denied', 'fatal', 'No such file or directory']
93+
marker in error for marker in [b'error', b'Permission denied', b'fatal', b'No such file or directory']
9994
)
10095

101-
if not ignore_errors and error_found:
102-
if isinstance(error, bytes):
103-
message = b"Utility exited with non-zero code. Error: " + error
104-
else:
105-
message = f"Utility exited with non-zero code. Error: {error}"
96+
assert type(error_found) == bool # noqa: E721
97+
98+
if encoding:
99+
result = result.decode(encoding)
100+
error = error.decode(encoding)
101+
102+
if not ignore_errors and error_found and not expect_error:
103+
error = normalize_error(error)
104+
assert type(error) == str # noqa: E721
105+
message = "Utility exited with non-zero code. Error: " + error
106106
raise ExecUtilException(message=message, command=cmd, exit_code=exit_status, out=result)
107107

108108
if verbose:

tests/__init__.py

Whitespace-only changes.

tests/helpers/__init__.py

Whitespace-only changes.

tests/helpers/run_conditions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
import platform
3+
4+
5+
class RunConditions:
6+
# It is not a test kit!
7+
__test__ = False
8+
9+
def skip_if_windows():
10+
if platform.system().lower() == "windows":
11+
pytest.skip("This test does not support Windows.")

tests/test_local.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pytest
2+
3+
from testgres import ExecUtilException
4+
from testgres import LocalOperations
5+
6+
from .helpers.run_conditions import RunConditions
7+
8+
9+
class TestLocalOperations:
10+
11+
@pytest.fixture(scope="function", autouse=True)
12+
def setup(self):
13+
self.operations = LocalOperations()
14+
15+
def test_exec_command_success(self):
16+
"""
17+
Test exec_command for successful command execution.
18+
"""
19+
RunConditions.skip_if_windows()
20+
21+
cmd = "python3 --version"
22+
response = self.operations.exec_command(cmd, wait_exit=True, shell=True)
23+
24+
assert b'Python 3.' in response
25+
26+
def test_exec_command_failure(self):
27+
"""
28+
Test exec_command for command execution failure.
29+
"""
30+
RunConditions.skip_if_windows()
31+
32+
cmd = "nonexistent_command"
33+
while True:
34+
try:
35+
self.operations.exec_command(cmd, wait_exit=True, shell=True)
36+
except ExecUtilException as e:
37+
error = e.message
38+
break
39+
raise Exception("We wait an exception!")
40+
assert error == "Utility exited with non-zero code. Error `b'/bin/sh: 1: nonexistent_command: not found\\n'`"
41+
42+
def test_exec_command_failure__expect_error(self):
43+
"""
44+
Test exec_command for command execution failure.
45+
"""
46+
RunConditions.skip_if_windows()
47+
48+
cmd = "nonexistent_command"
49+
50+
exit_status, result, error = self.operations.exec_command(cmd, verbose=True, wait_exit=True, shell=True, expect_error=True)
51+
52+
assert error == b'/bin/sh: 1: nonexistent_command: not found\n'
53+
assert exit_status == 127
54+
assert result == b''

tests/test_remote.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,27 @@ def test_exec_command_failure(self):
3030
Test exec_command for command execution failure.
3131
"""
3232
cmd = "nonexistent_command"
33-
try:
34-
exit_status, result, error = self.operations.exec_command(cmd, verbose=True, wait_exit=True)
35-
except ExecUtilException as e:
36-
error = e.message
33+
while True:
34+
try:
35+
self.operations.exec_command(cmd, verbose=True, wait_exit=True)
36+
except ExecUtilException as e:
37+
error = e.message
38+
break
39+
raise Exception("We wait an exception!")
3740
assert error == b'Utility exited with non-zero code. Error: bash: line 1: nonexistent_command: command not found\n'
3841

42+
def test_exec_command_failure__expect_error(self):
43+
"""
44+
Test exec_command for command execution failure.
45+
"""
46+
cmd = "nonexistent_command"
47+
48+
exit_status, result, error = self.operations.exec_command(cmd, verbose=True, wait_exit=True, shell=True, expect_error=True)
49+
50+
assert error == b'bash: line 1: nonexistent_command: command not found\n'
51+
assert exit_status == 127
52+
assert result == b''
53+
3954
def test_is_executable_true(self):
4055
"""
4156
Test is_executable for an existing executable.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy