Skip to content

Commit 2cd69b5

Browse files
committed
boosting coverage by testing main
1 parent a311d7b commit 2cd69b5

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

nibabel/cmdline/diff.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,38 @@ def display_diff(files, diff):
161161
return output
162162

163163

164-
def main():
164+
def main(args=None):
165165
"""Getting the show on the road"""
166+
try:
167+
parser = get_opt_parser()
168+
(opts, files) = parser.parse_args()
166169

167-
parser = get_opt_parser()
168-
(opts, files) = parser.parse_args()
170+
nibabel.cmdline.utils.verbose_level = opts.verbose
169171

170-
nibabel.cmdline.utils.verbose_level = opts.verbose
172+
if nibabel.cmdline.utils.verbose_level < 3:
173+
# suppress nibabel format-compliance warnings
174+
nib.imageglobals.logger.level = 50
171175

172-
assert len(files) >= 2, "Please enter at least two files"
176+
except SystemExit:
177+
opts = None
178+
files = None
179+
180+
if args:
181+
files = args
173182

174-
if nibabel.cmdline.utils.verbose_level < 3:
175-
# suppress nibabel format-compliance warnings
176-
nib.imageglobals.logger.level = 50
183+
assert len(files) >= 2, "Please enter at least two files"
177184

178185
file_headers = [nib.load(f).header for f in files]
179186

180-
if opts.header_fields: # will almost always have a header field
187+
if opts: # will almost always have a header field
181188
# signals "all fields"
182189
if opts.header_fields == 'all':
183190
# TODO: header fields might vary across file types, thus prior sensing would be needed
184191
header_fields = file_headers[0].keys()
185192
else:
186193
header_fields = opts.header_fields.split(',')
194+
else:
195+
header_fields = file_headers[0].keys()
187196

188197
diff = get_headers_diff(file_headers, header_fields)
189198
data_diff = get_data_md5sums(files)
@@ -192,8 +201,11 @@ def main():
192201
diff['DATA(md5)'] = data_diff
193202

194203
if diff:
195-
sys.stdout.write(display_diff(files, diff))
196-
raise SystemExit(1)
204+
if opts:
205+
sys.stdout.write(display_diff(files, diff))
206+
raise SystemExit(1)
207+
else:
208+
return diff # this functionality specifically for testing main
197209

198210
else:
199211
print("These files are identical.")

nibabel/cmdline/tests/test_utils.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"""
77

88
from nose.tools import assert_equal
9+
from numpy.testing import assert_raises
910

1011
import nibabel as nib
1112
import numpy as np
1213
from nibabel.cmdline.utils import *
13-
from nibabel.cmdline.diff import get_headers_diff, display_diff
14+
from nibabel.cmdline.diff import get_headers_diff, display_diff, main
1415
from os.path import (join as pjoin)
1516
from nibabel.testing import data_path
1617
from collections import OrderedDict
@@ -106,3 +107,44 @@ def test_display_diff():
106107
"\n"
107108

108109
assert_equal(display_diff(bogus_names, dict_values), expected_output)
110+
111+
112+
def test_main():
113+
test_names = [pjoin(data_path, f)
114+
for f in ('standard.nii.gz', 'example4d.nii.gz')]
115+
expected_difference = OrderedDict([
116+
("regular", [np.asarray("".encode("utf-8")), np.asarray("r".encode("utf-8"))]),
117+
("dim_info", [np.asarray(0).astype(dtype="uint8"), np.asarray(57).astype(dtype="uint8")]),
118+
("dim", [np.array([3, 4, 5, 7, 1, 1, 1, 1]).astype(dtype="int16"),
119+
np.array([4, 128, 96, 24, 2, 1, 1, 1]).astype(dtype="int16")]),
120+
("datatype", [np.array(2).astype(dtype="uint8"), np.array(4).astype(dtype="uint8")]),
121+
("bitpix", [np.array(8).astype(dtype="uint8"), np.array(16).astype(dtype="uint8")]),
122+
("pixdim", [np.array([1., 1., 3., 2., 1., 1., 1., 1.]).astype(dtype="float32"), np.array(
123+
[-1.00000000e+00, 2.00000000e+00, 2.00000000e+00, 2.19999909e+00, 2.00000000e+03, 1.00000000e+00,
124+
1.00000000e+00, 1.00000000e+00]).astype(dtype="float32")]),
125+
("slice_end", [np.array(0).astype(dtype="uint8"), np.array(23).astype(dtype="uint8")]),
126+
("xyzt_units", [np.array(0).astype(dtype="uint8"), np.array(10).astype(dtype="uint8")]),
127+
("cal_max", [np.array(0.0).astype(dtype="float32"), np.asarray(1162.0).astype(dtype="float32")]),
128+
("descrip", [np.array("".encode("utf-8")).astype(dtype="S80"),
129+
np.array("FSL3.3\x00 v2.25 NIfTI-1 Single file format".encode("utf-8")).astype(dtype="S80")]),
130+
("qform_code", [np.array(0).astype(dtype="int16"), np.array(1).astype(dtype="int16")]),
131+
("sform_code", [np.array(2).astype(dtype="int16"), np.array(1).astype(dtype="int16")]),
132+
("quatern_b", [np.array(0.0).astype(dtype="float32"),
133+
np.array(-1.9451068140294884e-26).astype(dtype="float32")]),
134+
("quatern_c", [np.array(0.0).astype(dtype="float32"), np.array(-0.9967085123062134).astype(dtype="float32")]),
135+
("quatern_d", [np.array(0.0).astype(dtype="float32"), np.array(-0.0810687392950058).astype(dtype="float32")]),
136+
("qoffset_x", [np.array(0.0).astype(dtype="float32"), np.array(117.8551025390625).astype(dtype="float32")]),
137+
("qoffset_y", [np.array(0.0).astype(dtype="float32"), np.array(-35.72294235229492).astype(dtype="float32")]),
138+
("qoffset_z", [np.array(0.0).astype(dtype="float32"), np.array(-7.248798370361328).astype(dtype="float32")]),
139+
("srow_x", [np.array([1., 0., 0., 0.]).astype(dtype="float32"),
140+
np.array([-2.00000000e+00, 6.71471565e-19, 9.08102451e-18,
141+
1.17855103e+02]).astype(dtype="float32")]),
142+
("srow_y", [np.array([0., 3., 0., 0.]).astype(dtype="float32"),
143+
np.array([-6.71471565e-19, 1.97371149e+00, -3.55528235e-01, -3.57229424e+01]).astype(
144+
dtype="float32")]),
145+
("srow_z", [np.array([0., 0., 2., 0.]).astype(dtype="float32"),
146+
np.array([8.25548089e-18, 3.23207617e-01, 2.17108178e+00,
147+
-7.24879837e+00]).astype(dtype="float32")]),
148+
('DATA(md5)', ['0a2576dd6badbb25bfb3b12076df986b', 'b0abbc492b4fd533b2c80d82570062cf'])])
149+
150+
np.testing.assert_equal(main(test_names), expected_difference)

0 commit comments

Comments
 (0)