Skip to content

Commit

Permalink
add plaintext ofmt to patch tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Jun 29, 2024
1 parent a803e71 commit e96b96c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
6 changes: 4 additions & 2 deletions nested_diff/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class App:
'toml',
'yaml',
)
supported_ofmts = ('auto', 'ini', 'json', 'toml', 'yaml')
supported_ofmts = supported_ifmts

version = nested_diff.__version__

Expand Down Expand Up @@ -154,6 +154,8 @@ def get_dumper(fmt, **kwargs):
return IniDumper(**kwargs)
if fmt == 'toml':
return TomlDumper(**kwargs)
if fmt == 'plaintext':
return Dumper(**kwargs)

raise RuntimeError(f'Unsupported output format: {fmt}')

Expand Down Expand Up @@ -271,7 +273,7 @@ def encode(self, data):
NotImplementedError: Must be implemented in derivatives.
"""
raise NotImplementedError
return data

@staticmethod
def get_opts(opts):
Expand Down
25 changes: 25 additions & 0 deletions tests/cli/shared.txt.patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"D": [
{
"I": [
0,
4,
0,
3
]
},
{
"U": "one"
},
{
"U": "two"
},
{
"R": "three"
},
{
"U": ""
}
],
"E": 5
}
7 changes: 3 additions & 4 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ def encode(self, data):
return data


def test_abstract_dumper_encode():
with pytest.raises(NotImplementedError):
cli.Dumper().encode('data')
def test_base_dumper_encode():
assert cli.Dumper().encode('data') == 'data'


def test_abstract_loader_decode():
def test_base_loader_decode():
assert cli.Loader().decode('data') == 'data'


Expand Down
24 changes: 24 additions & 0 deletions tests/cli/test_patch_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@ def test_toml_fmt(capsys, content, rpath, tmp_path):
assert content(result_file_name) == expected


def test_plaintext_fmt(capsys, content, rpath, tmp_path):
result_file_name = f'{tmp_path}.got.txt'
copyfile(
rpath('shared.a.txt'),
result_file_name,
)
exit_code = nested_diff.patch_tool.App(
args=(
'--ofmt',
'plaintext',
result_file_name,
rpath('shared.txt.patch.json'),
),
).run()

captured = capsys.readouterr()
assert captured.out == ''
assert captured.err == ''
assert exit_code == 0

expected = content(rpath('shared.b.txt'))
assert content(result_file_name) == expected


def test_entry_point(capsys):
with mock.patch('sys.argv', ['nested_patch', '-h']):
with pytest.raises(SystemExit) as e:
Expand Down

0 comments on commit e96b96c

Please sign in to comment.