Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit cf1193a

Browse files
committed
Better poetry outputs
1 parent 3a5e5f9 commit cf1193a

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

src/git_portfolio/use_cases/poetry.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class PoetryUseCase:
1313
"""Execution of poetry use case."""
1414

1515
def execute(
16-
self, git_selected_repos: list[str], command: str, args: tuple[str]
16+
self, git_selected_repos: list[str], command: str, args: tuple[str, ...]
1717
) -> list[res.Response]:
1818
"""Batch `poetry` command.
1919
@@ -35,23 +35,18 @@ def execute(
3535
output = f"{folder_name}: "
3636
try:
3737
popen = subprocess.Popen( # noqa: S603, S607
38-
[command, *args],
38+
# --ansi option makes output with colors
39+
[command, *args, "--ansi"],
3940
stdout=subprocess.PIPE,
4041
stderr=subprocess.PIPE,
4142
cwd=os.path.join(cwd, folder_name),
4243
)
43-
stdout, error = popen.communicate()
44+
stdout, _ = popen.communicate()
45+
stdout_str = stdout.decode("utf-8")
46+
output += f"{stdout_str}"
4447
if popen.returncode == 0:
45-
# case for command with no output on success
46-
if not stdout:
47-
output += f"{command} successful.\n"
48-
else:
49-
stdout_str = stdout.decode("utf-8")
50-
output += f"{stdout_str}\n"
5148
responses.append(res.ResponseSuccess(output))
5249
else:
53-
error_str = error.decode("utf-8")
54-
output += f"{error_str}"
5550
responses.append(
5651
res.ResponseFailure(res.ResponseTypes.RESOURCE_ERROR, output)
5752
)

tests/use_cases/test_poetry.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,7 @@ def test_execute_success(
3434

3535
assert len(responses) == 2
3636
assert isinstance(responses[0], res.ResponseSuccess)
37-
assert responses[0].value == f"{REPO_NAME}: some output\n"
38-
39-
40-
# TODO: this is only to maintain logic from git use case
41-
# no known poetry command has no output when merging
42-
# with GitUseCase remove this non-sense example.
43-
def test_execute_success_no_output(mock_popen: MockerFixture) -> None:
44-
"""It returns success message."""
45-
mock_popen.return_value.communicate.return_value = (b"", b"")
46-
47-
responses = poetry.PoetryUseCase().execute(
48-
[REPO], "poetry", ("someoptionwithoutoutput",)
49-
)
50-
51-
assert isinstance(responses[0], res.ResponseSuccess)
52-
assert responses[0].value == f"{REPO_NAME}: poetry successful.\n"
37+
assert responses[0].value == f"{REPO_NAME}: some output"
5338

5439

5540
def test_execute_poetry_not_installed(mock_command_checker: MockerFixture) -> None:
@@ -71,23 +56,27 @@ def test_execute_no_folder(
7156
mock_exception.filename = "/path/x"
7257
mock_popen.side_effect = mock_exception
7358

74-
responses = poetry.PoetryUseCase().execute(["user/x"], "poetry", ("version",))
59+
responses = poetry.PoetryUseCase().execute([REPO], "poetry", ("version",))
7560

7661
assert isinstance(responses[0], res.ResponseFailure)
77-
assert responses[0].value["message"] == "x: No such file or directory: /path/x\n"
62+
assert (
63+
responses[0].value["message"]
64+
== f"{REPO_NAME}: No such file or directory: /path/x\n"
65+
)
7866

7967

8068
def test_execute_error_during_execution(mock_popen: MockerFixture) -> None:
8169
"""It returns error message."""
8270
mock_popen.return_value.returncode = 1
8371
mock_popen().communicate.return_value = (
72+
b"ValueError\n\nPackage nonexistingpackage not found",
8473
b"",
85-
b"error: pathspec 'xyz' did not match any file(s) known to poetry",
8674
)
87-
responses = poetry.PoetryUseCase().execute([REPO], "poetry", ("version",))
75+
responses = poetry.PoetryUseCase().execute(
76+
[REPO], "poetry", ("remove", "nonexistingpackage")
77+
)
8878

8979
assert isinstance(responses[0], res.ResponseFailure)
9080
assert responses[0].value["message"] == (
91-
f"{REPO_NAME}: error: pathspec 'xyz' did not match any file(s) known to "
92-
"poetry"
81+
f"{REPO_NAME}: ValueError\n\nPackage nonexistingpackage not found"
9382
)

0 commit comments

Comments
 (0)