From 43beaa0e971a062575061a83df2929ed3e1a2930 Mon Sep 17 00:00:00 2001 From: Michael Samoglyadov Date: Sat, 29 Jun 2024 17:19:51 +0300 Subject: [PATCH] add plaintext ofmt to patch tool --- nested_diff/cli.py | 10 ++++++---- tests/cli/shared.txt.patch.json | 25 +++++++++++++++++++++++++ tests/cli/test_cli.py | 7 +++---- tests/cli/test_patch_tool.py | 24 ++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 tests/cli/shared.txt.patch.json diff --git a/nested_diff/cli.py b/nested_diff/cli.py index 13cc65a..8ab756b 100644 --- a/nested_diff/cli.py +++ b/nested_diff/cli.py @@ -35,7 +35,7 @@ class App: 'toml', 'yaml', ) - supported_ofmts = ('auto', 'ini', 'json', 'toml', 'yaml') + supported_ofmts = supported_ifmts version = nested_diff.__version__ @@ -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}') @@ -267,11 +269,11 @@ def encode(self, data): Args: data: Data to encode. - Raises: - NotImplementedError: Must be implemented in derivatives. + Returns: + Encoded data. """ - raise NotImplementedError + return data @staticmethod def get_opts(opts): diff --git a/tests/cli/shared.txt.patch.json b/tests/cli/shared.txt.patch.json new file mode 100644 index 0000000..ad06011 --- /dev/null +++ b/tests/cli/shared.txt.patch.json @@ -0,0 +1,25 @@ +{ + "D": [ + { + "I": [ + 0, + 4, + 0, + 3 + ] + }, + { + "U": "one" + }, + { + "U": "two" + }, + { + "R": "three" + }, + { + "U": "" + } + ], + "E": 5 +} \ No newline at end of file diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index e21fc9a..284a6b4 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -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' diff --git a/tests/cli/test_patch_tool.py b/tests/cli/test_patch_tool.py index b6407b9..72b6bbc 100644 --- a/tests/cli/test_patch_tool.py +++ b/tests/cli/test_patch_tool.py @@ -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: