1+ #!/usr/bin/env python3
2+
3+ import os
4+ import sys
5+
6+ import pytest
7+
8+ from fdiff .__main__ import run
9+
10+ ROBOTO_BEFORE_PATH = os .path .join ("tests" , "testfiles" , "Roboto-Regular.subset1.ttf" )
11+ ROBOTO_AFTER_PATH = os .path .join ("tests" , "testfiles" , "Roboto-Regular.subset2.ttf" )
12+ ROBOTO_EXTDIFF_EXPECTED_PATH = os .path .join ("tests" , "testfiles" , "roboto_extdiff_expected.txt" )
13+ ROBOTO_EXTDIFF_COLOR_EXPECTED_PATH = os .path .join ("tests" , "testfiles" , "roboto_extdiff_color_expected.txt" )
14+
15+ ROBOTO_BEFORE_URL = "https://github.com/source-foundry/fdiff/raw/master/tests/testfiles/Roboto-Regular.subset1.ttf"
16+ ROBOTO_AFTER_URL = "https://github.com/source-foundry/fdiff/raw/master/tests/testfiles/Roboto-Regular.subset2.ttf"
17+
18+
19+ # Setup: define the expected diff text for unified diff
20+ with open (ROBOTO_EXTDIFF_EXPECTED_PATH , "r" ) as robo_extdiff :
21+ ROBOTO_EXTDIFF_EXPECTED = robo_extdiff .read ()
22+
23+ # Setup: define the expected diff text for unified color diff
24+ with open (ROBOTO_EXTDIFF_COLOR_EXPECTED_PATH , "r" ) as robo_extdiff_color :
25+ ROBOTO_EXTDIFF_COLOR_EXPECTED = robo_extdiff_color .read ()
26+
27+
28+ # # these tests rely on a PATH install of `diff` executable on Unix
29+ # # they are not executed on Windows platforms
30+ if sys .platform .startswith ("win" ):
31+ pytest .skip ("skipping windows-only tests" , allow_module_level = True )
32+
33+
34+ def test_main_external_diff_default (capsys ):
35+ args = ["--external" , "diff -u" , ROBOTO_BEFORE_PATH , ROBOTO_AFTER_PATH ]
36+ expected_string_list = ROBOTO_EXTDIFF_EXPECTED .split ("\n " )
37+
38+ with pytest .raises (SystemExit ):
39+ run (args )
40+
41+ captured = capsys .readouterr ()
42+ res_string_list = captured .out .split ("\n " )
43+ for x , line in enumerate (res_string_list ):
44+ # treat top two lines of the diff as comparison of first 3 chars only
45+ if x in (0 , 1 ):
46+ assert line [0 :2 ] == expected_string_list [x ][0 :2 ]
47+ elif x in range (2 , 10 ):
48+ assert line == expected_string_list [x ]
49+ else :
50+ # skip lines beyond first 10
51+ pass
52+
53+
54+ def test_main_external_diff_remote (capsys ):
55+ args = ["--external" , "diff -u" , ROBOTO_BEFORE_URL , ROBOTO_AFTER_URL ]
56+ expected_string_list = ROBOTO_EXTDIFF_EXPECTED .split ("\n " )
57+
58+ with pytest .raises (SystemExit ):
59+ run (args )
60+
61+ captured = capsys .readouterr ()
62+ res_string_list = captured .out .split ("\n " )
63+ for x , line in enumerate (res_string_list ):
64+ # treat top two lines of the diff as comparison of first 3 chars only
65+ if x in (0 , 1 ):
66+ assert line [0 :2 ] == expected_string_list [x ][0 :2 ]
67+ elif x in range (2 , 10 ):
68+ assert line == expected_string_list [x ]
69+ else :
70+ # skip lines beyond first 10
71+ pass
72+
73+
74+ def test_main_external_diff_color (capsys ):
75+ args = ["--external" , "diff -u" , "--color" , ROBOTO_BEFORE_PATH , ROBOTO_AFTER_PATH ]
76+ expected_string_list = ROBOTO_EXTDIFF_COLOR_EXPECTED .split ("\n " )
77+
78+ with pytest .raises (SystemExit ):
79+ run (args )
80+
81+ captured = capsys .readouterr ()
82+ res_string_list = captured .out .split ("\n " )
83+ for x , line in enumerate (res_string_list ):
84+ # treat top two lines of the diff as comparison of first 3 chars only
85+ if x in (0 , 1 ):
86+ assert line [0 :2 ] == expected_string_list [x ][0 :2 ]
87+ elif x in range (2 , 10 ):
88+ assert line == expected_string_list [x ]
89+ else :
90+ # skip lines beyond first 10
91+ pass
92+
93+
94+ def test_main_external_diff_with_head_fails (capsys ):
95+ args = ["--external" , "diff -u" , "--head" , "1" , ROBOTO_BEFORE_PATH , ROBOTO_AFTER_PATH ]
96+
97+ with pytest .raises (SystemExit ) as exit_info :
98+ run (args )
99+
100+ captured = capsys .readouterr ()
101+ assert "[ERROR] The head and tail options are not supported" in captured .err
102+ assert exit_info .value .code == 1
103+
104+
105+ def test_main_external_diff_with_tail_fails (capsys ):
106+ args = ["--external" , "diff -u" , "--tail" , "1" , ROBOTO_BEFORE_PATH , ROBOTO_AFTER_PATH ]
107+
108+ with pytest .raises (SystemExit ) as exit_info :
109+ run (args )
110+
111+ captured = capsys .readouterr ()
112+ assert "[ERROR] The head and tail options are not supported" in captured .err
113+ assert exit_info .value .code == 1
114+
115+
116+ def test_main_external_diff_with_lines_fails (capsys ):
117+ args = ["--external" , "diff -u" , "--lines" , "1" , ROBOTO_BEFORE_PATH , ROBOTO_AFTER_PATH ]
118+
119+ with pytest .raises (SystemExit ) as exit_info :
120+ run (args )
121+
122+ captured = capsys .readouterr ()
123+ assert "[ERROR] The lines option is not supported" in captured .err
124+ assert exit_info .value .code == 1
0 commit comments