Skip to content

Commit 9161735

Browse files
committed
[__main__] add support for head and tail in fdiff executable
adds new --head and --tail options that take integer argument for number of lines
1 parent 0beed29 commit 9161735

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

lib/fdiff/__main__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from fdiff import __version__
99
from fdiff.color import color_unified_diff_line
1010
from fdiff.diff import u_diff
11+
from fdiff.textiter import head, tail
1112
from fdiff.utils import file_exists, get_tables_argument_list
1213

1314

@@ -59,6 +60,16 @@ def run(argv):
5960
default=None,
6061
help="Comma separated list of tables to exclude",
6162
)
63+
parser.add_argument(
64+
"--head",
65+
type=int,
66+
help="Display first n lines of output"
67+
)
68+
parser.add_argument(
69+
"--tail",
70+
type=int,
71+
help="Display last n lines of output"
72+
)
6273
parser.add_argument("PREFILE", help="Font file path 1")
6374
parser.add_argument("POSTFILE", help="Font file path 2")
6475

@@ -129,9 +140,18 @@ def run(argv):
129140
)
130141
sys.exit(1)
131142

143+
# re-define the line contents of the diff iterable
144+
# if head or tail is requested
145+
if args.head:
146+
iterable = head(diff, args.head)
147+
elif args.tail:
148+
iterable = tail(diff, args.tail)
149+
else:
150+
iterable = diff
151+
132152
# print unified diff results to standard output stream
133153
if args.color:
134-
for line in diff:
154+
for line in iterable:
135155
sys.stdout.write(color_unified_diff_line(line))
136156
else:
137-
sys.stdout.writelines(diff)
157+
sys.stdout.writelines(iterable)

tests/test_main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,55 @@ def test_main_exclude_with_bad_table_definition_in_multi_table_request(capsys):
251251
captured = capsys.readouterr()
252252
assert captured.error.startswith("[*] ERROR:")
253253
assert exit_info.value.code == 1
254+
255+
256+
def test_main_head_request(capsys):
257+
args = ["--head", "4", ROBOTO_BEFORE_PATH, ROBOTO_AFTER_PATH]
258+
259+
run(args)
260+
captured = capsys.readouterr()
261+
res_string_list = captured.out.split("\n")
262+
263+
# includes a newline at the end of the last line of output
264+
# which makes the total # of lines in the list == n + 1
265+
assert len(res_string_list) == 5
266+
267+
# have to handle the tests for the top two file path lines
268+
# differently than the rest of the comparisons because
269+
# the time is defined using local platform settings
270+
# which makes tests fail on different remote CI testing services
271+
for x, line in enumerate(res_string_list):
272+
# treat top two lines of the diff as comparison of first 10 chars only
273+
if x in (0, 1):
274+
assert "tests/testfiles/Roboto-Regular.subset" in line
275+
elif x == 2:
276+
assert line == "@@ -4,34 +4,34 @@"
277+
elif x == 3:
278+
assert line == " <GlyphOrder>"
279+
else:
280+
assert line == ""
281+
282+
283+
def test_main_tail_request(capsys):
284+
args = ["--tail", "2", ROBOTO_BEFORE_PATH, ROBOTO_AFTER_PATH]
285+
286+
run(args)
287+
captured = capsys.readouterr()
288+
res_string_list = captured.out.split("\n")
289+
290+
# includes a newline at the end of the last line of output
291+
# which makes the total # of lines in the list == n + 1
292+
assert len(res_string_list) == 3
293+
294+
# have to handle the tests for the top two file path lines
295+
# differently than the rest of the comparisons because
296+
# the time is defined using local platform settings
297+
# which makes tests fail on different remote CI testing services
298+
for x, line in enumerate(res_string_list):
299+
# treat top two lines of the diff as comparison of first 10 chars only
300+
if x == 0:
301+
assert line == " </Lookup>"
302+
elif x == 1:
303+
assert line == " </LookupList>"
304+
else:
305+
assert line == ""

0 commit comments

Comments
 (0)