Skip to content

Commit

Permalink
Merge pull request mrijken#19 from paritytech/json-output
Browse files Browse the repository at this point in the history
Output `get` as JSON
  • Loading branch information
mrijken authored Jul 1, 2022
2 parents d8b2c9c + f6cf2af commit 5279b66
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
53 changes: 41 additions & 12 deletions tests/toml_cli/test_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pathlib

import pytest
import json

from typer.testing import CliRunner

Expand All @@ -9,6 +10,19 @@
runner = CliRunner()


def normalized(item):
if isinstance(item, dict):
return sorted((key, normalized(values)) for key, values in item.items())
if isinstance(item, list):
return sorted(normalized(x) for x in item)
else:
return item


def compare(item1, item2):
assert normalized(item1) == normalized(item2)


def test_get_value(tmp_path: pathlib.Path):
test_toml_path = tmp_path / "test.toml"
test_toml_path.write_text(
Expand All @@ -22,21 +36,36 @@ def test_get_value(tmp_path: pathlib.Path):
"""
)

result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person"])
assert result.exit_code == 0
assert "{'name': 'MyName', 'age': 12, 'education': {'name': 'University'}}" in result.stdout
def get(args):
result = runner.invoke(app, args)
assert result.exit_code == 0
return json.loads(result.stdout.strip())

compare(
get(["get", "--toml-path", str(test_toml_path), "person"]),
{
"name": "MyName",
"age": 12,
"education": {
"name": "University"
}
}
)

result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.education"])
assert result.exit_code == 0
assert "{'name': 'University'}" in result.stdout
compare(
get(["get", "--toml-path", str(test_toml_path), "person.education"]),
{ "name": "University" }
)

result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.education.name"])
assert result.exit_code == 0
assert "University" in result.stdout
compare(
get(["get", "--toml-path", str(test_toml_path), "person.education.name"]),
"University"
)

result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.age"])
assert result.exit_code == 0
assert "12" in result.stdout
compare(
get(["get", "--toml-path", str(test_toml_path), "person.age"]),
12
)


def test_set_value(tmp_path: pathlib.Path):
Expand Down
8 changes: 6 additions & 2 deletions toml_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import tomlkit
import tomlkit.exceptions
import typer
import json

app = typer.Typer()
app = typer.Typer(no_args_is_help=True)


@app.command("get")
Expand All @@ -17,7 +18,7 @@ def get(key: Optional[str] = typer.Argument(None), toml_path: pathlib.Path = typ
for key_part in key.split("."):
toml_part = toml_part[key_part]

typer.echo(toml_part)
typer.echo(json.dumps(toml_part))


@app.command("set")
Expand Down Expand Up @@ -83,3 +84,6 @@ def unset(key: str, toml_path: pathlib.Path = typer.Option(pathlib.Path("config.

def main():
app()

if __name__ == "__main__":
main()

0 comments on commit 5279b66

Please sign in to comment.