test_imglob.py 5.67 KB
Newer Older
Paul McCarthy's avatar
Paul McCarthy committed
1
2
#!/usr/bin/env python

3
4
5
6

from unittest import mock

import pytest
Paul McCarthy's avatar
Paul McCarthy committed
7
8
9
10
11
12
13

import fsl.scripts.imglob as imglob

from .. import testdir
from .. import CaptureStdout


14
def test_imglob_shouldPass1():
Paul McCarthy's avatar
Paul McCarthy committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

    # (files to create, args, expected)
    tests = [
        ('file.hdr file.img', 'file',                          'file'),
        ('file.hdr file.img', '-extension file',               'file.hdr'),
        ('file.hdr file.img', '-extensions file',              'file.hdr file.img'),
        ('file.hdr file.img', 'file.hdr',                      'file'),
        ('file.hdr file.img', ' -extension file.hdr',           'file.hdr'),
        ('file.hdr file.img', '-extensions file.hdr',          'file.hdr file.img'),
        ('file.hdr file.img', 'file.img',                      'file'),
        ('file.hdr file.img', '-extension file.img',           'file.hdr'),
        ('file.hdr file.img', '-extensions file.img',          'file.hdr file.img'),
        ('file.hdr file.img', 'file.hdr file.img',             'file'),
        ('file.hdr file.img', '-extension file.hdr file.img',  'file.hdr'),
        ('file.hdr file.img', '-extensions file.hdr file.img', 'file.hdr file.img'),
        ('file.hdr file.img', 'file file.img',                 'file'),
        ('file.hdr file.img', '-extension file file.img',      'file.hdr'),
        ('file.hdr file.img', '-extensions file file.img',     'file.hdr file.img'),

        # no file or incomplete prefix
        ('file.hdr file.img', 'bag',             ''),
        ('file.hdr file.img', '-extension  bag', ''),
        ('file.hdr file.img', '-extensions bag', ''),
        ('file.hdr file.img', 'fi',              ''),
        ('file.hdr file.img', '-extension  fi',  ''),
        ('file.hdr file.img', '-extensions fi',  ''),
    ]

    capture = CaptureStdout()

    for to_create, args, expected in tests:
        with testdir(to_create.split()) as td:

            capture.reset()

            with capture:
                assert imglob.main(args.split()) == 0

            assert capture.stdout.strip().split() == expected.split()


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
def test_imglob_shouldPass2():

    # (files to create, paths, output, expected)
    tests = [

        # normal usage, one file
        ('file.hdr file.img', 'file',  None,     'file'),
        ('file.hdr file.img', 'file', 'prefix',  'file'),
        ('file.hdr file.img', 'file', 'primary', 'file.hdr'),
        ('file.hdr file.img', 'file', 'all',     'file.hdr file.img'),

        # incomplete file pair
        ('file.hdr', 'file',               'prefix',  'file'),
        ('file.hdr', 'file.hdr',           'prefix',  'file'),
        ('file.hdr', 'file.img',           'prefix',  'file'),
        ('file.hdr', 'file',               'primary', 'file.hdr'),
        ('file.hdr', 'file.hdr',           'primary', 'file.hdr'),
        ('file.hdr', 'file.img',           'primary', 'file.hdr'),
        ('file.hdr', 'file',               'all',     'file.hdr'),
        ('file.hdr', 'file.hdr',           'all',     'file.hdr'),
        ('file.hdr', 'file.img',           'all',     'file.hdr'),

        # same file specified multiple times
        ('file.hdr file.img', 'file file',              'prefix',  'file'),
        ('file.hdr file.img', 'file file.img',          'prefix',  'file'),
        ('file.hdr file.img', 'file file.img file.hdr', 'prefix',  'file'),
        ('file.hdr file.img', 'file file',              'primary', 'file.hdr'),
        ('file.hdr file.img', 'file file.img',          'primary', 'file.hdr'),
        ('file.hdr file.img', 'file file.img file.hdr', 'primary', 'file.hdr'),
        ('file.hdr file.img', 'file file',              'all',     'file.hdr file.img'),
        ('file.hdr file.img', 'file file.img',          'all',     'file.hdr file.img'),
        ('file.hdr file.img', 'file file.img file.hdr', 'all',     'file.hdr file.img'),

        # multiple files same prefix
        ('file.hdr file.img file.nii', 'file', 'prefix',  'file'),
        ('file.hdr file.img file.nii', 'file', 'primary', 'file.hdr file.nii'),
        ('file.hdr file.img file.nii', 'file', 'all',     'file.hdr file.img file.nii'),

        # multiple files
        ('file1.hdr file1.img file2.nii', 'file1', 'prefix',  'file1'),
        ('file1.hdr file1.img file2.nii', 'file1', 'primary', 'file1.hdr'),
        ('file1.hdr file1.img file2.nii', 'file1', 'all',     'file1.hdr file1.img'),

        ('file1.hdr file1.img file2.nii', 'file1 file2', 'prefix',  'file1 file2'),
        ('file1.hdr file1.img file2.nii', 'file1 file2', 'primary', 'file1.hdr file2.nii'),
        ('file1.hdr file1.img file2.nii', 'file1 file2', 'all',     'file1.hdr file1.img file2.nii'),

Paul McCarthy's avatar
Paul McCarthy committed
103
104
105
        # muiltiple files, given wildcard
        ('file1.nii file2.nii', 'file*', 'prefix',  'file1 file2'),

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        # no file
        ('file.nii', 'bag', 'prefix',  ''),
        ('file.nii', 'bag', 'primary', ''),
        ('file.nii', 'bag', 'all',     ''),

        # incomplete prefix
        ('file.nii', 'fi', 'prefix',  ''),
        ('file.nii', 'fi', 'primary', ''),
        ('file.nii', 'fi', 'all',     ''),
    ]


    for to_create, paths, output, expected in tests:
        with testdir(to_create.split()) as td:

            paths    = paths.split()
            expected = expected.split()
            result   = imglob.imglob(paths, output)

            assert sorted(result) == sorted(expected)



Paul McCarthy's avatar
Paul McCarthy committed
129
130
def test_imglob_script_shouldFail():

131
132
133
    with pytest.raises(ValueError):
        imglob.imglob([], 'bag')

Paul McCarthy's avatar
Paul McCarthy committed
134
135
136
137
138
139
140
141
142
143
144
    capture = CaptureStdout()

    with capture:
        assert imglob.main([]) != 0

    assert capture.stdout.strip().lower().startswith('usage:')

    with capture, mock.patch('sys.argv', ['imglob']):
        assert imglob.main() != 0

    assert capture.stdout.strip().lower().startswith('usage:')