Skip to content

Commit

Permalink
allow to define header filenames via env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Jul 13, 2024
1 parent 2026ae2 commit a805c70
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion nested_diff/diff_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Nested diff command line tool."""

import argparse
import os
import sys

import nested_diff
Expand Down Expand Up @@ -97,8 +98,17 @@ def generate_diffs(self):

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

try:
name_a = os.environ['HEADER_NAME_A']
name_b = os.environ['HEADER_NAME_B']
except KeyError:
name_a = a['name']
name_b = b['name']
else:
headers_enabled = True

if headers_enabled:
header = self.dumper.get_diff_header(a['name'], b['name'])
header = self.dumper.get_diff_header(name_a, name_b)

a = b

Expand Down
34 changes: 34 additions & 0 deletions tests/cli/test_diff_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,40 @@ def test_show_several_args_tty(capsys, rpath, stringio_tty):
assert stringio_tty.getvalue().startswith('\033[33m--- /dev/null')


@mock.patch('os.environ', {'HEADER_NAME_A': 'foo', 'HEADER_NAME_B': 'bar'})
def test_header_names_from_env(capsys, rpath):
app = nested_diff.diff_tool.App(
args=(
rpath('shared.lists.a.json'),
rpath('shared.lists.b.json'),
),
)
exit_code = app.run()

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

assert captured.out.startswith('--- foo\n+++ bar\n')


@mock.patch('os.environ', {'HEADER_NAME_A': 'foo'})
def test_header_names_from_env_partial(capsys, rpath):
app = nested_diff.diff_tool.App(
args=(
rpath('shared.lists.a.json'),
rpath('shared.lists.b.json'),
),
)
exit_code = app.run()

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

assert not captured.out.startswith('--- ')


def test_values_none(capsys, expected, rpath):
exit_code = nested_diff.diff_tool.App(
args=(
Expand Down

0 comments on commit a805c70

Please sign in to comment.