2
2
# vi: set ft=python sts=4 ts=4 sw=4 et:
3
3
""" Test scripts
4
4
5
- If we appear to be running from the development directory, use the scripts in
6
- the top-level folder ``scripts``. Otherwise try and get the scripts from the
7
- path
5
+ Test running scripts
8
6
"""
9
7
from __future__ import division , print_function , absolute_import
10
8
11
- import sys
12
9
import os
13
- from os .path import dirname , join as pjoin , isfile , isdir , abspath , realpath , pardir
10
+ from os .path import dirname , join as pjoin , abspath
14
11
import re
15
12
16
- from subprocess import Popen , PIPE
17
-
18
13
import numpy as np
19
14
20
15
from ..tmpdirs import InTemporaryDirectory
24
19
25
20
from numpy .testing import assert_almost_equal
26
21
22
+ from .scriptrunner import ScriptRunner
23
+
24
+
25
+ def _proc_stdout (stdout ):
26
+ stdout_str = stdout .decode ('latin1' ).strip ()
27
+ return stdout_str .replace (os .linesep , '\n ' )
28
+
29
+
30
+ runner = ScriptRunner (
31
+ script_sdir = 'bin' ,
32
+ debug_print_var = 'NIPY_DEBUG_PRINT' ,
33
+ output_processor = _proc_stdout )
34
+ run_command = runner .run_command
35
+
36
+
27
37
def script_test (func ):
28
38
# Decorator to label test as a script_test
29
39
func .script_test = True
30
40
return func
31
41
script_test .__test__ = False # It's not a test
32
42
33
- # Need shell to get path to correct executables
34
- USE_SHELL = True
35
-
36
- DEBUG_PRINT = os .environ .get ('NIPY_DEBUG_PRINT' , False )
37
-
38
43
DATA_PATH = abspath (pjoin (dirname (__file__ ), 'data' ))
39
- IMPORT_PATH = abspath (pjoin (dirname (__file__ ), pardir , pardir ))
40
-
41
- def local_script_dir (script_sdir ):
42
- # Check for presence of scripts in development directory. ``realpath``
43
- # checks for the situation where the development directory has been linked
44
- # into the path.
45
- below_us_2 = realpath (pjoin (dirname (__file__ ), '..' , '..' ))
46
- devel_script_dir = pjoin (below_us_2 , script_sdir )
47
- if isfile (pjoin (below_us_2 , 'setup.py' )) and isdir (devel_script_dir ):
48
- return devel_script_dir
49
- return None
50
-
51
- LOCAL_SCRIPT_DIR = local_script_dir ('bin' )
52
-
53
- def run_command (cmd ):
54
- if LOCAL_SCRIPT_DIR is None :
55
- env = None
56
- else : # We are running scripts local to the source tree (not installed)
57
- # Windows can't run script files without extensions natively so we need
58
- # to run local scripts (no extensions) via the Python interpreter. On
59
- # Unix, we might have the wrong incantation for the Python interpreter
60
- # in the hash bang first line in the source file. So, either way, run
61
- # the script through the Python interpreter
62
- cmd = "%s %s" % (sys .executable , pjoin (LOCAL_SCRIPT_DIR , cmd ))
63
- # If we're testing local script files, point subprocess to consider
64
- # current nibabel in favor of possibly installed different version
65
- env = {'PYTHONPATH' : '%s:%s'
66
- % (IMPORT_PATH , os .environ .get ('PYTHONPATH' , '' ))}
67
- if DEBUG_PRINT :
68
- print ("Running command '%s'" % cmd )
69
- proc = Popen (cmd , stdout = PIPE , stderr = PIPE , shell = USE_SHELL ,
70
- env = env )
71
- stdout , stderr = proc .communicate ()
72
- if proc .poll () == None :
73
- proc .terminate ()
74
- if proc .returncode != 0 :
75
- raise RuntimeError ('Command "%s" failed with stdout\n %s\n stderr\n %s\n '
76
- % (cmd , stdout , stderr ))
77
- return proc .returncode , stdout , stderr
78
-
79
-
80
- def _proc_stdout (stdout ):
81
- stdout_str = stdout .decode ('latin1' ).strip ()
82
- return stdout_str .replace (os .linesep , '\n ' )
83
-
84
44
85
45
@script_test
86
46
def test_nib_ls ():
87
47
# test nib-ls script
88
48
fname = pjoin (DATA_PATH , 'example4d.nii.gz' )
89
49
expected_re = (" (int16|[<>]i2) \[128, 96, 24, 2\] "
90
50
"2.00x2.00x2.20x2000.00 #exts: 2 sform$" )
91
- # Need to quote out path in case it has spaces
92
- cmd = 'nib-ls "%s"' % (fname )
51
+ cmd = ['nib-ls' , fname ]
93
52
code , stdout , stderr = run_command (cmd )
94
- res = _proc_stdout (stdout )
95
- assert_equal (fname , res [:len (fname )])
96
- assert_not_equal (re .match (expected_re , res [len (fname ):]), None )
53
+ assert_equal (fname , stdout [:len (fname )])
54
+ assert_not_equal (re .match (expected_re , stdout [len (fname ):]), None )
97
55
98
56
99
57
@script_test
100
58
def test_nib_nifti_dx ():
101
59
# Test nib-nifti-dx script
102
60
clean_hdr = pjoin (DATA_PATH , 'nifti1.hdr' )
103
- cmd = 'nib-nifti-dx "%s"' % ( clean_hdr ,)
61
+ cmd = [ 'nib-nifti-dx' , clean_hdr ]
104
62
code , stdout , stderr = run_command (cmd )
105
- assert_equal (stdout .strip (). decode ( 'latin1' ) , 'Header for "%s" is clean' % clean_hdr )
63
+ assert_equal (stdout .strip (), 'Header for "%s" is clean' % clean_hdr )
106
64
dirty_hdr = pjoin (DATA_PATH , 'analyze.hdr' )
107
- cmd = 'nib-nifti-dx "%s"' % ( dirty_hdr ,)
65
+ cmd = [ 'nib-nifti-dx' , dirty_hdr ]
108
66
code , stdout , stderr = run_command (cmd )
109
67
expected = """Picky header check output for "%s"
110
68
111
69
pixdim[0] (qfac) should be 1 (default) or -1
112
70
magic string "" is not valid
113
71
sform_code 11776 not valid""" % (dirty_hdr ,)
114
72
# Split strings to remove line endings
115
- assert_equal (_proc_stdout ( stdout ) , expected )
73
+ assert_equal (stdout , expected )
116
74
117
75
118
76
def vox_size (affine ):
@@ -122,14 +80,13 @@ def vox_size(affine):
122
80
@script_test
123
81
def test_parrec2nii ():
124
82
# Test parrec2nii script
125
- cmd = 'parrec2nii --help'
83
+ cmd = [ 'parrec2nii' , ' --help']
126
84
code , stdout , stderr = run_command (cmd )
127
- stdout = stdout .decode ('latin1' )
128
85
assert_true (stdout .startswith ('Usage' ))
129
86
in_fname = pjoin (DATA_PATH , 'phantom_EPI_asc_CLEAR_2_1.PAR' )
130
87
out_froot = 'phantom_EPI_asc_CLEAR_2_1.nii'
131
88
with InTemporaryDirectory ():
132
- run_command ('parrec2nii "{0}"' . format ( in_fname ) )
89
+ run_command ([ 'parrec2nii' , in_fname ] )
133
90
img = load (out_froot )
134
91
assert_equal (img .shape , (64 , 64 , 9 , 3 ))
135
92
assert_equal (img .get_data_dtype (), np .dtype (np .int16 ))
0 commit comments