Skip to content

Commit 0af2932

Browse files
committed
tests: cover cli commands
1 parent 837a0a9 commit 0af2932

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

git_json_tree.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def decode(repo, tree_id):
126126
return []
127127
elif b'.object' in tree:
128128
return {}
129+
129130
raise TypeError('Unknown tree type.')
130131

131132
if all((isinstance(key[0], key_types) for key in items)):
@@ -151,7 +152,7 @@ def cli():
151152

152153

153154
@cli.command(name='encode')
154-
@click.option('--source', type=click.File('rb'), default='-')
155+
@click.option('--source', type=click.File('r'), default='-')
155156
@click.option('--git', type=click.Path(exists=True), default='.')
156157
def cli_encode(source, git):
157158
"""Encode a JSON object in a Git tree."""
@@ -187,7 +188,7 @@ def smudge(source, git):
187188

188189

189190
@cli.command()
190-
@click.option('--source', type=click.File('rb'), default='-')
191+
@click.option('--source', type=click.File('r'), default='-')
191192
@click.option('--git', type=click.Path(exists=True), default='.')
192193
def clean(source, git):
193194
"""Store a JSON file in Git repository."""

test_git_json_tree.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Test Git-JSON-Tree library."""
22

3+
import json
34
from string import printable
45

56
import hypothesis.strategies as st
67
import pytest
8+
from click.testing import CliRunner
79
from dulwich.repo import Repo
810
from hypothesis import assume, given, settings
911

10-
from git_json_tree import decode, encode
12+
from git_json_tree import cli, decode, encode
1113

1214

1315
@pytest.fixture()
@@ -16,6 +18,12 @@ def repo(tmpdir):
1618
yield Repo.init_bare(str(tmpdir))
1719

1820

21+
@pytest.fixture()
22+
def runner(repo):
23+
"""Define a click runner."""
24+
return CliRunner()
25+
26+
1927
def json_data():
2028
"""Generate JSON data."""
2129
return st.recursive(
@@ -33,7 +41,7 @@ def json_data():
3341
),
3442
children,
3543
),
36-
max_leaves=500,
44+
max_leaves=50,
3745
)
3846

3947

@@ -43,3 +51,27 @@ def test_encode_decode(data, repo):
4351
"""Test (d)encoding."""
4452
assume(isinstance(data, (dict, list)))
4553
assert decode(repo, encode(repo, data)) == data
54+
55+
56+
@given(data=json_data())
57+
@settings(max_examples=100, deadline=10000)
58+
def test_cli_encoder(data, runner):
59+
"""Test cli encoder."""
60+
assume(isinstance(data, (dict, list)) and data)
61+
encoded = runner.invoke(cli, ['encode'], input=json.dumps(data))
62+
assert encoded.exit_code == 0
63+
decoded = runner.invoke(cli, ['decode', encoded.output.strip()])
64+
assert decoded.exit_code == 0
65+
assert json.loads(decoded.output_bytes.decode('utf-8')) == data
66+
67+
68+
@given(data=json_data())
69+
@settings(max_examples=100, deadline=10000)
70+
def test_smudge_clean(data, runner):
71+
"""Test Git integration."""
72+
assume(isinstance(data, (dict, list)) and data)
73+
cleaned = runner.invoke(cli, ['clean'], input=json.dumps(data))
74+
assert cleaned.exit_code == 0
75+
smudged = runner.invoke(cli, ['smudge'], input=cleaned.output)
76+
assert smudged.exit_code == 0
77+
assert json.loads(smudged.output_bytes.decode('utf-8')) == data

0 commit comments

Comments
 (0)