Skip to content

Commit d9175e3

Browse files
rm failure (#578)
* rm failure * style * Vesion 4.3.2 * tests * --timeout * su sudo twice * --print * test fix
1 parent 234912e commit d9175e3

File tree

7 files changed

+77
-12
lines changed

7 files changed

+77
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## moler 4.3.2
2+
* rm fails on permission denied
3+
14
## moler 4.3.1
25
* duplicates in ping
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![image](https://img.shields.io/badge/pypi-v4.3.0-blue.svg)](https://pypi.org/project/moler/)
1+
[![image](https://img.shields.io/badge/pypi-v4.3.2-blue.svg)](https://pypi.org/project/moler/)
22
[![image](https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue.svg)](https://pypi.org/project/moler/)
33
[![Build Status](https://github.com/nokia/moler/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/nokia/moler/actions)
44
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](./LICENSE)

moler/cmd/unix/rm.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"""
55

66
__author__ = 'Bartosz Odziomek, Marcin Usielski'
7-
__copyright__ = 'Copyright (C) 2018-2023, Nokia'
7+
__copyright__ = 'Copyright (C) 2018-2025, Nokia'
88
99

10-
from moler.cmd.unix.genericunix import GenericUnixCommand
10+
from moler.helpers import copy_list
11+
from moler.cmd.unix.genericunix import GenericUnixCommand, cmd_failure_causes
12+
import re
1113

1214

1315
class Rm(GenericUnixCommand):
@@ -25,6 +27,10 @@ def __init__(self, connection, file, options=None, prompt=None, newline_chars=No
2527
self.file = file
2628
self.options = options
2729
self.ret_required = False
30+
_cmd_failure_causes = copy_list(cmd_failure_causes)
31+
_cmd_failure_causes.append(r"cannot remove\s*'.*':\s*Permission denied")
32+
r_cmd_failure_cause_alternatives = "|".join(_cmd_failure_causes)
33+
self.re_fail = re.compile(r_cmd_failure_cause_alternatives, re.IGNORECASE)
2834

2935
def build_command_string(self):
3036
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='moler',
15-
version='4.3.1',
15+
version='4.3.2',
1616
description='Moler is a library for working with terminals, mainly for automated tests', # Required
1717
long_description=long_description,
1818
long_description_content_type='text/markdown',

test/cmd/unix/test_cmd_rm.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@
33
Rm command module.
44
"""
55

6-
__author__ = 'Bartosz Odziomek'
7-
__copyright__ = 'Copyright (C) 2018, Nokia'
8-
__email__ = '[email protected]'
6+
__author__ = 'Bartosz Odziomek, Marcin Usielski'
7+
__copyright__ = 'Copyright (C) 2018-2025, Nokia'
8+
99

1010

1111
def test_rm_returns_proper_command_string(buffer_connection):
1212
from moler.cmd.unix.rm import Rm
1313
rm_cmd = Rm(connection=buffer_connection.moler_connection, file="test.txt")
1414
assert "rm test.txt" == rm_cmd.command_string
1515

16+
17+
18+
def test_rm_permission_denied(buffer_connection):
19+
from moler.cmd.unix.rm import Rm
20+
from moler.exceptions import MolerException
21+
output = """rm protected.txt
22+
rm: cannot remove 'protected.txt': Permission denied
23+
cannot remove'.*': Permission denied
24+
moler_bash#"""
25+
26+
rm_cmd = Rm(connection=buffer_connection.moler_connection, file="protected.txt")
27+
buffer_connection.remote_inject_response([output])
28+
try:
29+
rm_cmd()
30+
except MolerException as exc:
31+
assert "Permission denied" in str(exc)
32+
else:
33+
assert False, "Exception not raised for permission denied"

test/cmd/unix/test_cmd_su.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
__author__ = 'Agnieszka Bylica, Marcin Usielski'
7-
__copyright__ = 'Copyright (C) 2018-2023, Nokia'
7+
__copyright__ = 'Copyright (C) 2018-2025, Nokia'
88
99

1010
import pytest
@@ -66,6 +66,28 @@ def test_sudo_su(buffer_connection):
6666
assert ret == expected_dict
6767

6868

69+
def test_sudo_su_twice_failed_object(buffer_connection):
70+
from moler.cmd.unix.sudo import Sudo
71+
from moler.cmd.unix.rm import Rm
72+
from moler.exceptions import CommandFailure
73+
rm_output_denied = """rm important_file.txt
74+
rm: cannot remove 'important_file.txt': Permission denied
75+
moler_bash#"""
76+
77+
buffer_connection.remote_inject_response([rm_output_denied])
78+
79+
cmd_rm = Rm(connection=buffer_connection.moler_connection, file="important_file.txt")
80+
with pytest.raises(CommandFailure):
81+
cmd_rm()
82+
83+
cmd_su = Su(connection=buffer_connection.moler_connection, prompt=r"moler_bash#",
84+
cmd_object=cmd_rm)
85+
cmd_sudo = Sudo(connection=buffer_connection.moler_connection, cmd_object=cmd_su)
86+
with pytest.raises(CommandFailure) as exc:
87+
cmd_sudo()
88+
assert "Not allowed to run again" in str(exc.value)
89+
90+
6991
def test_sudo_su_object(buffer_connection, command_output_and_expected_result_ls_l):
7092
from moler.cmd.unix.sudo import Sudo
7193
from moler.cmd.unix.ls import Ls
@@ -129,7 +151,7 @@ def test_su_catches_missing_binary_failure(buffer_connection):
129151
@pytest.fixture
130152
def command_output_and_expected_result_auth():
131153
output = """xyz@debian:~/Moler$ su
132-
Password:
154+
Password:
133155
su: Authentication failure
134156
xyz@debian:~/Moler$"""
135157
result = dict()
@@ -138,7 +160,7 @@ def command_output_and_expected_result_auth():
138160

139161
@pytest.fixture
140162
def command_output_and_expected_result_command_format_failure():
141-
output = """xyz@debian:~/Moler$ su -g
163+
output = """xyz@debian:~/Moler$ su -g
142164
su: invalid option -- 'g'
143165
Usage: su [options] [LOGIN]
144166
@@ -201,7 +223,7 @@ def command_output_and_expected_result_ls_l():
201223
@pytest.fixture()
202224
def command_output_and_expected_result_pwd():
203225
output = """user@client:~/moler$ su -c 'pwd'
204-
password:
226+
password:
205227
/home/user/moler
206228
ute@debdev:~/moler$ """
207229
result = {

test/cmd/unix/test_cmd_sudo.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
__author__ = 'Marcin Usielski'
7-
__copyright__ = 'Copyright (C) 2018-2023, Nokia'
7+
__copyright__ = 'Copyright (C) 2018-2025, Nokia'
88
__email__ = '[email protected]'
99

1010
from moler.cmd.unix.sudo import Sudo
@@ -97,6 +97,22 @@ def test_failing_calling_twice_the_same_command_object(buffer_connection, comman
9797
cmd_sudo()
9898

9999

100+
def test_failing_calling_twice_the_same_command_object_failed(buffer_connection):
101+
from moler.cmd.unix.rm import Rm
102+
rm_output_denied = """rm important_file.txt
103+
rm: cannot remove 'protected.txt': Permission denied
104+
moler_bash#"""
105+
106+
buffer_connection.remote_inject_response([rm_output_denied])
107+
108+
cmd_rm = Rm(connection=buffer_connection.moler_connection, file="important_file.txt")
109+
with pytest.raises(CommandFailure):
110+
cmd_rm()
111+
cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="pass", cmd_object=cmd_rm)
112+
with pytest.raises(CommandFailure) as exc:
113+
cmd_sudo()
114+
assert "Not allowed to run again" in str(exc.value)
115+
100116
def test_failing_with_timeout(buffer_connection, command_output_and_expected_result_timeout):
101117
command_output = command_output_and_expected_result_timeout
102118
buffer_connection.remote_inject_response([command_output])

0 commit comments

Comments
 (0)