Skip to content

Commit e2f29bc

Browse files
committed
Check for individual fail.
1 parent e17443f commit e2f29bc

2 files changed

Lines changed: 31 additions & 18 deletions

File tree

‎usr/lib/python3/dist-packages/linuxmusterCli/pytests/test_typers_student.py‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,27 @@ def test_add_and_remove_parents_both_shown_and_force_parents_listing(self, runne
7676
# get_parents was never explicitly requested but is implied by add/remove:
7777
assert 'No parent found!' in result.output
7878

79-
def test_add_parent_failure_aborts_the_rest_of_the_loop(self, runner, monkeypatch):
80-
# BUG-like current behavior (documented, not fixed): the try/except in
81-
# student.py wraps the WHOLE for-loop, not each iteration. So a single
82-
# failing add_parent() call aborts processing of any parents after it
83-
# in the same --add-parents list.
79+
def test_add_parent_failure_does_not_abort_the_rest_of_the_loop(self, runner, monkeypatch):
80+
# Each parent is now tried independently: a failing add_parent() call
81+
# is reported but does not stop the remaining parents in the same
82+
# --add-parents list from being processed.
8483
monkeypatch.setattr(student, 'LMNStudent', FakeStudent)
8584

8685
result = runner.invoke(student.app, ['--add-parents', 'good1,bad,good2', 'johndoe'])
8786

8887
assert result.exit_code == 0
8988
assert 'cannot add bad' in result.output
90-
# "Parents ... added!" success message is never printed since the
91-
# exception happened inside the try, before reaching typer.secho(...).
92-
assert 'added!' not in result.output
89+
# Only the parents that actually succeeded are listed.
90+
assert 'Parents good1,good2 added!' in result.output
91+
92+
def test_remove_parent_failure_does_not_abort_the_rest_of_the_loop(self, runner, monkeypatch):
93+
monkeypatch.setattr(student, 'LMNStudent', FakeStudent)
94+
95+
result = runner.invoke(student.app, ['--remove-parents', 'good1,bad,good2', 'johndoe'])
96+
97+
assert result.exit_code == 0
98+
assert 'cannot remove bad' in result.output
99+
assert 'Parents good1,good2 removed!' in result.output
93100

94101
def test_get_parents_shows_table(self, runner, monkeypatch):
95102
class FakeStudentWithParents(FakeStudent):

‎usr/lib/python3/dist-packages/linuxmusterCli/typers/student.py‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,27 @@ def manage(
3333

3434
if add_parents:
3535
get_parents = True
36-
try:
37-
for parent in add_parents.split(','):
36+
added = []
37+
for parent in add_parents.split(','):
38+
try:
3839
student.add_parent(parent)
39-
typer.secho(f"Parents {add_parents} added!\n", fg=typer.colors.GREEN)
40-
except Exception as e:
41-
typer.secho(str(e), fg=typer.colors.RED)
40+
added.append(parent)
41+
except Exception as e:
42+
typer.secho(str(e), fg=typer.colors.RED)
43+
if added:
44+
typer.secho(f"Parents {','.join(added)} added!\n", fg=typer.colors.GREEN)
4245

4346
if remove_parents:
4447
get_parents = True
45-
try:
46-
for parent in remove_parents.split(','):
48+
removed = []
49+
for parent in remove_parents.split(','):
50+
try:
4751
student.remove_parent(parent)
48-
typer.secho(f"Parents {remove_parents} removed!\n", fg=typer.colors.GREEN)
49-
except Exception as e:
50-
typer.secho(str(e), fg=typer.colors.RED)
52+
removed.append(parent)
53+
except Exception as e:
54+
typer.secho(str(e), fg=typer.colors.RED)
55+
if removed:
56+
typer.secho(f"Parents {','.join(removed)} removed!\n", fg=typer.colors.GREEN)
5157

5258
if get_parents:
5359
if student.parents:

0 commit comments

Comments
 (0)