Skip to content

Commit

Permalink
set plaintext as fallback ifmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Jun 28, 2024
1 parent 4374fa7 commit a803e71
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
12 changes: 8 additions & 4 deletions nested_diff/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ def get_dumper(fmt, **kwargs):

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

@staticmethod
def guess_fmt(fp, default, ignore_fps=(sys.stdin, sys.stdout, sys.stderr)):
def guess_fmt(
self,
fp,
default,
ignore_fps=(sys.stdin, sys.stdout, sys.stderr),
):
"""Guess format of a file object according it's extension."""
if fp in ignore_fps:
return default
Expand All @@ -168,7 +172,7 @@ def guess_fmt(fp, default, ignore_fps=(sys.stdin, sys.stdout, sys.stderr)):
if fmt == 'yml':
fmt = 'yaml'

return fmt if fmt else default
return fmt if fmt in self.supported_ifmts else default

@staticmethod
def get_loader(fmt, **kwargs):
Expand Down Expand Up @@ -209,7 +213,7 @@ def load(self, file_):
"""
if self.args.ifmt == 'auto':
fmt = self.guess_fmt(file_, 'json')
fmt = self.guess_fmt(file_, 'plaintext')
else:
fmt = self.args.ifmt

Expand Down
3 changes: 3 additions & 0 deletions tests/cli/shared.a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
one
two
three
2 changes: 2 additions & 0 deletions tests/cli/shared.b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
one
two
12 changes: 8 additions & 4 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,22 @@ def test_excepthook_raise_system_exit_127():
assert e.value.code == 127


def test_guess_fmt_aliases():
def test_guess_fmt():
class FakeFP:
name = None

aliases = {
exts = {
'ini': 'ini',
'json': 'json',
'py': 'default',
'txt': 'default',
'yml': 'yaml',
}
fake_fp = FakeFP()

for ext in sorted(aliases):
for ext, fmt in sorted(exts.items()):
fake_fp.name = f'filename.{ext}'
assert cli.App(args=()).guess_fmt(fake_fp, 'default') == aliases[ext]
assert cli.App(args=()).guess_fmt(fake_fp, 'default') == fmt


def test_guess_fmt_ignore_fp_defaults():
Expand Down
15 changes: 15 additions & 0 deletions tests/cli/test_diff_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,21 @@ def test_html_ofmt_wrappings(capsys, expected, rpath):
assert captured.out == expected


def test_fallback_ifmt(capsys, expected, rpath):
exit_code = nested_diff.diff_tool.App(
args=(
rpath('shared.a.txt'),
rpath('shared.b.txt'),
),
).run()

captured = capsys.readouterr()
assert captured.err == ''
assert exit_code == 1

assert captured.out == expected


def test_plaintext_ifmt(capsys, expected, rpath):
exit_code = nested_diff.diff_tool.App(
args=(
Expand Down
6 changes: 6 additions & 0 deletions tests/cli/test_diff_tool.test_fallback_ifmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# <str>
@@ -1,4 +1,3 @@
one
two
- three

0 comments on commit a803e71

Please sign in to comment.