Skip to content

Commit a805c70

Browse files
committed
allow to define header filenames via env vars
1 parent 2026ae2 commit a805c70

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

nested_diff/diff_tool.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Nested diff command line tool."""
1616

1717
import argparse
18+
import os
1819
import sys
1920

2021
import nested_diff
@@ -97,8 +98,17 @@ def generate_diffs(self):
9798

9899
equal, diff = self.diff(a['data'], b['data'])
99100

101+
try:
102+
name_a = os.environ['HEADER_NAME_A']
103+
name_b = os.environ['HEADER_NAME_B']
104+
except KeyError:
105+
name_a = a['name']
106+
name_b = b['name']
107+
else:
108+
headers_enabled = True
109+
100110
if headers_enabled:
101-
header = self.dumper.get_diff_header(a['name'], b['name'])
111+
header = self.dumper.get_diff_header(name_a, name_b)
102112

103113
a = b
104114

tests/cli/test_diff_tool.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,40 @@ def test_show_several_args_tty(capsys, rpath, stringio_tty):
629629
assert stringio_tty.getvalue().startswith('\033[33m--- /dev/null')
630630

631631

632+
@mock.patch('os.environ', {'HEADER_NAME_A': 'foo', 'HEADER_NAME_B': 'bar'})
633+
def test_header_names_from_env(capsys, rpath):
634+
app = nested_diff.diff_tool.App(
635+
args=(
636+
rpath('shared.lists.a.json'),
637+
rpath('shared.lists.b.json'),
638+
),
639+
)
640+
exit_code = app.run()
641+
642+
captured = capsys.readouterr()
643+
assert captured.err == ''
644+
assert exit_code == 1
645+
646+
assert captured.out.startswith('--- foo\n+++ bar\n')
647+
648+
649+
@mock.patch('os.environ', {'HEADER_NAME_A': 'foo'})
650+
def test_header_names_from_env_partial(capsys, rpath):
651+
app = nested_diff.diff_tool.App(
652+
args=(
653+
rpath('shared.lists.a.json'),
654+
rpath('shared.lists.b.json'),
655+
),
656+
)
657+
exit_code = app.run()
658+
659+
captured = capsys.readouterr()
660+
assert captured.err == ''
661+
assert exit_code == 1
662+
663+
assert not captured.out.startswith('--- ')
664+
665+
632666
def test_values_none(capsys, expected, rpath):
633667
exit_code = nested_diff.diff_tool.App(
634668
args=(

0 commit comments

Comments
 (0)