Skip to content

Commit

Permalink
Remove trailing spaces from fmt_dict output
Browse files Browse the repository at this point in the history
And cover the missing two lines to get to coverage threshold
  • Loading branch information
tpoliaw committed Sep 9, 2024
1 parent 0192ef0 commit b2d12b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/blueapi/cli/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
def fmt_dict(t: dict[str, Any] | Any, ind: int = 1) -> str:
"""Format a (possibly nested) dict into a human readable tree"""
if not isinstance(t, dict):
return t
return f" {t}"
pre = " " * (ind * 4)
return NL + NL.join(f"{pre}{k}: {fmt_dict(v, ind+1)}" for k, v in t.items() if v)
return NL + NL.join(f"{pre}{k}:{fmt_dict(v, ind+1)}" for k, v in t.items() if v)


class OutputFormat(str, enum.Enum):
Expand Down Expand Up @@ -61,7 +61,7 @@ def display_full(obj: Any, stream: Stream):
for proto in dev.protocols:
print(" " + proto)
case DataEvent(name=name, doc=doc):
print(f"{name.title()}: {fmt_dict(doc)}")
print(f"{name.title()}:{fmt_dict(doc)}")
case WorkerEvent(state=st, task_status=task):
print(
f"WorkerEvent: {st.name}{fmt_dict(task.model_dump() if task else {})}"
Expand Down
34 changes: 25 additions & 9 deletions tests/unit_tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,13 @@ def test_event_formatting():
_assert_matching_formatting(
OutputFormat.FULL,
data,
(
"Start: \n"
" foo: bar\n"
" fizz: \n"
" buzz: (1, 2, 3)\n"
" hello: world\n"
),
dedent("""\
Start:
foo: bar
fizz:
buzz: (1, 2, 3)
hello: world
"""),
)

_assert_matching_formatting(
Expand Down Expand Up @@ -587,11 +587,11 @@ def test_unknown_object_formatting():

def test_dict_formatting():
demo = {"name": "foo", "keys": [1, 2, 3], "metadata": {"fizz": "buzz"}}
exp = """\nname: foo\nkeys: [1, 2, 3]\nmetadata: \n fizz: buzz"""
exp = """\nname: foo\nkeys: [1, 2, 3]\nmetadata:\n fizz: buzz"""
assert fmt_dict(demo, 0) == exp

demo = "not a dict"
assert fmt_dict(demo, 0) == "not a dict"
assert fmt_dict(demo, 0) == " not a dict"


def test_generic_base_model_formatting():
Expand All @@ -601,6 +601,22 @@ def test_generic_base_model_formatting():
OutputFormat.JSON.display(obj, output)
assert exp == output.getvalue()

model = ExtendedModel(
name="demo_model", keys=[1, 2, 3], metadata={"foo": "bar", "fizz": "buzz"}
)
_assert_matching_formatting(
OutputFormat.FULL,
model,
dedent("""\
ExtendedModel
name: demo_model
keys: [1, 2, 3]
metadata:
foo: bar
fizz: buzz
"""),
)


@patch("blueapi.cli.cli.setup_scratch")
def test_init_scratch_calls_setup_scratch(mock_setup_scratch: Mock, runner: CliRunner):
Expand Down

0 comments on commit b2d12b6

Please sign in to comment.