Skip to content

Commit a311d7b

Browse files
committed
moved functionality outside to test and increase coverage
1 parent 6613522 commit a311d7b

File tree

2 files changed

+69
-26
lines changed

2 files changed

+69
-26
lines changed

nibabel/cmdline/diff.py

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,50 @@ def get_data_md5sums(files):
117117
return md5sums
118118

119119

120+
def display_diff(files, diff):
121+
"""Format header differences into a nice string
122+
123+
Parameters
124+
----------
125+
files: list of files that were compared so we can print their names
126+
diff: dict of different valued header fields
127+
128+
Returns
129+
-------
130+
str
131+
string-formatted table of differences
132+
"""
133+
output = ""
134+
field_width = "{:<15}"
135+
value_width = "{:<55}"
136+
137+
output += "These files are different.\n"
138+
output += field_width.format('Field')
139+
140+
for f in files:
141+
output += value_width.format(os.path.basename(f))
142+
143+
output += "\n"
144+
145+
for key, value in diff.items():
146+
output += field_width.format(key)
147+
148+
for item in value:
149+
item_str = str(item)
150+
# Value might start/end with some invisible spacing characters so we
151+
# would "condition" it on both ends a bit
152+
item_str = re.sub('^[ \t]+', '<', item_str)
153+
item_str = re.sub('[ \t]+$', '>', item_str)
154+
# and also replace some other invisible symbols with a question
155+
# mark
156+
item_str = re.sub('[\x00]', '?', item_str)
157+
output += value_width.format(item_str)
158+
159+
output += "\n"
160+
161+
return output
162+
163+
120164
def main():
121165
"""Getting the show on the road"""
122166

@@ -148,30 +192,8 @@ def main():
148192
diff['DATA(md5)'] = data_diff
149193

150194
if diff:
151-
print("These files are different.")
152-
print("{:<15}".format('Field'), end="")
153-
154-
for f in files:
155-
print("{:<55}".format(os.path.basename(f)), end="")
156-
157-
print()
158-
159-
for key, value in diff.items():
160-
print("{:<15}".format(key), end="")
161-
162-
for item in value:
163-
item_str = str(item)
164-
# Value might start/end with some invisible spacing characters so we
165-
# would "condition" it on both ends a bit
166-
item_str = re.sub('^[ \t]+', '<', item_str)
167-
item_str = re.sub('[ \t]+$', '>', item_str)
168-
# and also replace some other invisible symbols with a question
169-
# mark
170-
item_str = re.sub('[\x00]', '?', item_str)
171-
print("{:<55}".format(item_str), end="")
172-
173-
print()
174-
195+
sys.stdout.write(display_diff(files, diff))
175196
raise SystemExit(1)
197+
176198
else:
177199
print("These files are identical.")

nibabel/cmdline/tests/test_utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
import nibabel as nib
1111
import numpy as np
1212
from nibabel.cmdline.utils import *
13-
from nibabel.cmdline.diff import get_headers_diff
13+
from nibabel.cmdline.diff import get_headers_diff, display_diff
1414
from os.path import (join as pjoin)
1515
from nibabel.testing import data_path
1616
from collections import OrderedDict
1717

1818

19-
2019
def test_table2string():
2120
assert_equal(table2string([["A", "B", "C", "D"], ["E", "F", "G", "H"]]), "A B C D\nE F G H\n")
2221
assert_equal(table2string([["Let's", "Make", "Tests", "And"], ["Have", "Lots", "Of", "Fun"],
@@ -85,3 +84,25 @@ def test_get_headers_diff():
8584
-7.24879837e+00]).astype(dtype="float32")])])
8685

8786
np.testing.assert_equal(actual_difference, expected_difference)
87+
88+
89+
def test_display_diff():
90+
bogus_names = ["hellokitty.nii.gz", "privettovarish.nii.gz"]
91+
92+
dict_values = OrderedDict([
93+
("datatype", [np.array(2).astype(dtype="uint8"), np.array(4).astype(dtype="uint8")]),
94+
("bitpix", [np.array(8).astype(dtype="uint8"), np.array(16).astype(dtype="uint8")])
95+
])
96+
97+
expected_output = "These files are different.\n" + "Field hellokitty.nii.gz" \
98+
" " \
99+
"privettovarish.nii.gz \n" \
100+
"datatype " \
101+
"2 " \
102+
"4 \n" \
103+
"bitpix " \
104+
"8 16" \
105+
" " \
106+
"\n"
107+
108+
assert_equal(display_diff(bogus_names, dict_values), expected_output)

0 commit comments

Comments
 (0)