Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/git_portfolio/use_cases/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PoetryUseCase:
"""Execution of poetry use case."""

def execute(
self, git_selected_repos: list[str], command: str, args: tuple[str]
self, git_selected_repos: list[str], command: str, args: tuple[str, ...]
) -> list[res.Response]:
"""Batch `poetry` command.

Expand All @@ -35,23 +35,18 @@ def execute(
output = f"{folder_name}: "
try:
popen = subprocess.Popen( # noqa: S603, S607
[command, *args],
# --ansi option makes output with colors
[command, *args, "--ansi"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.join(cwd, folder_name),
)
stdout, error = popen.communicate()
stdout, _ = popen.communicate()
stdout_str = stdout.decode("utf-8")
output += f"{stdout_str}"
if popen.returncode == 0:
# case for command with no output on success
if not stdout:
output += f"{command} successful.\n"
else:
stdout_str = stdout.decode("utf-8")
output += f"{stdout_str}\n"
responses.append(res.ResponseSuccess(output))
else:
error_str = error.decode("utf-8")
output += f"{error_str}"
responses.append(
res.ResponseFailure(res.ResponseTypes.RESOURCE_ERROR, output)
)
Expand Down
33 changes: 11 additions & 22 deletions tests/use_cases/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,7 @@ def test_execute_success(

assert len(responses) == 2
assert isinstance(responses[0], res.ResponseSuccess)
assert responses[0].value == f"{REPO_NAME}: some output\n"


# TODO: this is only to maintain logic from git use case
# no known poetry command has no output when merging
# with GitUseCase remove this non-sense example.
def test_execute_success_no_output(mock_popen: MockerFixture) -> None:
"""It returns success message."""
mock_popen.return_value.communicate.return_value = (b"", b"")

responses = poetry.PoetryUseCase().execute(
[REPO], "poetry", ("someoptionwithoutoutput",)
)

assert isinstance(responses[0], res.ResponseSuccess)
assert responses[0].value == f"{REPO_NAME}: poetry successful.\n"
assert responses[0].value == f"{REPO_NAME}: some output"


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

responses = poetry.PoetryUseCase().execute(["user/x"], "poetry", ("version",))
responses = poetry.PoetryUseCase().execute([REPO], "poetry", ("version",))

assert isinstance(responses[0], res.ResponseFailure)
assert responses[0].value["message"] == "x: No such file or directory: /path/x\n"
assert (
responses[0].value["message"]
== f"{REPO_NAME}: No such file or directory: /path/x\n"
)


def test_execute_error_during_execution(mock_popen: MockerFixture) -> None:
"""It returns error message."""
mock_popen.return_value.returncode = 1
mock_popen().communicate.return_value = (
b"ValueError\n\nPackage nonexistingpackage not found",
b"",
b"error: pathspec 'xyz' did not match any file(s) known to poetry",
)
responses = poetry.PoetryUseCase().execute([REPO], "poetry", ("version",))
responses = poetry.PoetryUseCase().execute(
[REPO], "poetry", ("remove", "nonexistingpackage")
)

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