Skip to content

Commit 5df4855

Browse files
committed
add main module tests
1 parent 5a786cc commit 5df4855

File tree

1 file changed

+115
-2
lines changed

1 file changed

+115
-2
lines changed

tests/test_main.py

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,121 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
import os
5+
46
import pytest
57

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_UDIFF_EXPECTED_PATH = os.path.join("tests", "testfiles", "roboto_udiff_expected.txt")
13+
ROBOTO_UDIFF_COLOR_EXPECTED_PATH = os.path.join("tests", "testfiles", "roboto_udiff_color_expected.txt")
14+
ROBOTO_UDIFF_1CONTEXT_EXPECTED_PATH = os.path.join("tests", "testfiles", "roboto_udiff_1context_expected.txt")
15+
16+
17+
# Setup: define the expected diff text for unified diff
18+
with open(ROBOTO_UDIFF_EXPECTED_PATH, "r") as robo_udiff:
19+
ROBOTO_UDIFF_EXPECTED = robo_udiff.read()
20+
21+
# Setup: define the expected diff text for unified color diff
22+
with open(ROBOTO_UDIFF_COLOR_EXPECTED_PATH, "r") as robo_udiff_color:
23+
ROBOTO_UDIFF_COLOR_EXPECTED = robo_udiff_color.read()
24+
25+
26+
# Setup: define the expected diff text for unified color diff
27+
with open(ROBOTO_UDIFF_1CONTEXT_EXPECTED_PATH, "r") as robo_udiff_contextlines:
28+
ROBOTO_UDIFF_1CONTEXT_EXPECTED = robo_udiff_contextlines.read()
29+
30+
#
31+
# File path validations tests
32+
#
33+
34+
35+
def test_main_filepath_validations_false_firstfont(capsys):
36+
test_path = os.path.join("tests", "testfiles", "bogus-font.ttf")
37+
args = [test_path, test_path]
38+
39+
with pytest.raises(SystemExit) as exit_info:
40+
run(args)
41+
captured = capsys.readouterr()
42+
assert captured.error.startswith("[*] ERROR: The file path")
43+
assert exit_info.value.code == 1
44+
45+
46+
def test_main_filepath_validations_false_secondfont(capsys):
47+
test_path_2 = os.path.join("tests", "testfiles", "bogus-font.ttf")
48+
args = [ROBOTO_BEFORE_PATH, test_path_2]
49+
50+
with pytest.raises(SystemExit) as exit_info:
51+
run(args)
52+
captured = capsys.readouterr()
53+
assert captured.error.startswith("[*] ERROR: The file path")
54+
assert exit_info.value.code == 1
55+
56+
57+
#
58+
# Unified diff integration tests
59+
#
60+
61+
def test_main_run_unified_default(capsys):
62+
args = [ROBOTO_BEFORE_PATH, ROBOTO_AFTER_PATH]
63+
64+
run(args)
65+
captured = capsys.readouterr()
66+
67+
res_string_list = captured.out.split("\n")
68+
expected_string_list = ROBOTO_UDIFF_EXPECTED.split("\n")
69+
70+
# have to handle the tests for the top two file path lines
71+
# differently than the rest of the comparisons because
72+
# the time is defined using local platform settings
73+
# which makes tests fail on different remote CI testing services
74+
for x, line in enumerate(res_string_list):
75+
# treat top two lines of the diff as comparison of first 10 chars only
76+
if x in (0, 1):
77+
assert line[0:9] == expected_string_list[x][0:9]
78+
else:
79+
assert line == expected_string_list[x]
80+
81+
82+
def test_main_run_unified_color(capsys):
83+
args = ["-c", ROBOTO_BEFORE_PATH, ROBOTO_AFTER_PATH]
84+
85+
run(args)
86+
captured = capsys.readouterr()
87+
88+
res_string_list = captured.out.split("\n")
89+
expected_string_list = ROBOTO_UDIFF_COLOR_EXPECTED.split("\n")
90+
91+
# have to handle the tests for the top two file path lines
92+
# differently than the rest of the comparisons because
93+
# the time is defined using local platform settings
94+
# which makes tests fail on different remote CI testing services
95+
for x, line in enumerate(res_string_list):
96+
# treat top two lines of the diff as comparison of first 10 chars only
97+
if x in (0, 1):
98+
assert line[0:9] == expected_string_list[x][0:9]
99+
else:
100+
assert line == expected_string_list[x]
101+
102+
103+
def test_main_run_unified_context_lines_1(capsys):
104+
args = ["-l", "1", ROBOTO_BEFORE_PATH, ROBOTO_AFTER_PATH]
105+
106+
run(args)
107+
captured = capsys.readouterr()
108+
109+
res_string_list = captured.out.split("\n")
110+
expected_string_list = ROBOTO_UDIFF_1CONTEXT_EXPECTED.split("\n")
6111

7-
def test_template():
8-
return True
112+
# have to handle the tests for the top two file path lines
113+
# differently than the rest of the comparisons because
114+
# the time is defined using local platform settings
115+
# which makes tests fail on different remote CI testing services
116+
for x, line in enumerate(res_string_list):
117+
# treat top two lines of the diff as comparison of first 10 chars only
118+
if x in (0, 1):
119+
assert line[0:9] == expected_string_list[x][0:9]
120+
else:
121+
assert line == expected_string_list[x]

0 commit comments

Comments
 (0)