Skip to content

Commit

Permalink
feat: Make ctx.run return the command output
Browse files Browse the repository at this point in the history
Issue #4: #4
  • Loading branch information
pawamoy committed Jun 20, 2021
1 parent ddbf7a2 commit 1810623
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["task-runner", "task", "runner", "cross-platform"]
dynamic = ["version", "classifiers"]
classifiers = ["Development Status :: 4 - Beta"]
dependencies = [
"failprint~=0.6",
"failprint~=0.7",
"cached-property~=1.5; python_version < '3.8'",
]

Expand Down
15 changes: 10 additions & 5 deletions src/duty/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, options, options_override=None) -> None:
self._option_stack: List[Dict[str, Any]] = []
self._options_override = options_override or {}

def run(self, cmd: CmdType, **options):
def run(self, cmd: CmdType, **options) -> str:
"""
Run a command in a subprocess or a Python callable.
Expand All @@ -41,6 +41,9 @@ def run(self, cmd: CmdType, **options):
Raises:
DutyFailure: When the exit code / function result is greather than 0.
Returns:
The output of the command.
"""
final_options = dict(self._options)
final_options.update(options)
Expand All @@ -53,12 +56,14 @@ def run(self, cmd: CmdType, **options):

with self.cd(workdir):
try:
code = failprint_run(cmd, **final_options)
result = failprint_run(cmd, **final_options)
except KeyboardInterrupt:
code = 130
raise DutyFailure(130) # noqa: WPS432 (ctrl-c)

if result.code:
raise DutyFailure(result.code)

if code:
raise DutyFailure(code)
return result.output

@contextmanager
def options(self, **opts):
Expand Down
11 changes: 7 additions & 4 deletions tests/test_context.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Tests for the `context` module."""

import os
from collections import namedtuple
from pathlib import Path

import pytest

from duty import context
from duty.exceptions import DutyFailure

_RunResult = namedtuple("RunResult", "code output")


def test_allow_overrides(monkeypatch):
"""
Expand All @@ -18,7 +21,7 @@ def test_allow_overrides(monkeypatch):
"""
ctx = context.Context({"a": 1}, {"a": 2})
records = []
monkeypatch.setattr(context, "failprint_run", lambda _, **opts: records.append(opts))
monkeypatch.setattr(context, "failprint_run", lambda _, **opts: _RunResult(records.append(opts), ""))
ctx.run("")
ctx.run("", allow_overrides=False)
ctx.run("", allow_overrides=True)
Expand All @@ -38,7 +41,7 @@ def test_options_context_manager(monkeypatch):
"""
ctx = context.Context({"a": 1}, {"a": 2})
records = []
monkeypatch.setattr(context, "failprint_run", lambda _, **opts: records.append(opts))
monkeypatch.setattr(context, "failprint_run", lambda _, **opts: _RunResult(records.append(opts), ""))

with ctx.options(a=3):
ctx.run("") # should be overridden by 2
Expand All @@ -61,7 +64,7 @@ def test_workdir(monkeypatch):
monkeypatch: A Pytest fixture to monkeypatch objects.
"""
ctx = context.Context({})
monkeypatch.setattr(context, "failprint_run", lambda _: len(Path(os.getcwd()).parts))
monkeypatch.setattr(context, "failprint_run", lambda _: _RunResult(len(Path(os.getcwd()).parts), ""))
records = []
with pytest.raises(DutyFailure) as failure: # noqa: WPS440,PT012
ctx.run("")
Expand All @@ -80,7 +83,7 @@ def test_workdir_as_context_manager(monkeypatch):
monkeypatch: A Pytest fixture to monkeypatch objects.
"""
ctx = context.Context({})
monkeypatch.setattr(context, "failprint_run", lambda _: len(Path(os.getcwd()).parts))
monkeypatch.setattr(context, "failprint_run", lambda _: _RunResult(len(Path(os.getcwd()).parts), ""))
records = []
with pytest.raises(DutyFailure) as failure: # noqa: WPS440,PT012
with ctx.options(workdir=".."):
Expand Down

0 comments on commit 1810623

Please sign in to comment.