|
| 1 | +"""Test parser.""" |
| 2 | +from packaging.version import Version |
| 3 | +import pytest |
| 4 | +from ..parser import _build_parser |
| 5 | +from .. import version as _version |
| 6 | +from ... import config |
| 7 | + |
| 8 | +MIN_ARGS = ['data/', 'out/', 'participant'] |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize('args,code', [ |
| 12 | + ([], 2), |
| 13 | + (MIN_ARGS, 2), # bids_dir does not exist |
| 14 | + (MIN_ARGS + ['--fs-license-file'], 2), |
| 15 | + (MIN_ARGS + ['--fs-license-file', 'fslicense.txt'], 2), |
| 16 | +]) |
| 17 | +def test_parser_errors(args, code): |
| 18 | + """Check behavior of the parser.""" |
| 19 | + with pytest.raises(SystemExit) as error: |
| 20 | + _build_parser().parse_args(args) |
| 21 | + |
| 22 | + assert error.value.code == code |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize('args', [ |
| 26 | + MIN_ARGS, |
| 27 | + MIN_ARGS + ['--fs-license-file'], |
| 28 | +]) |
| 29 | +def test_parser_valid(tmp_path, args): |
| 30 | + """Check valid arguments.""" |
| 31 | + datapath = (tmp_path / 'data').absolute() |
| 32 | + datapath.mkdir(exist_ok=True) |
| 33 | + args[0] = str(datapath) |
| 34 | + |
| 35 | + if '--fs-license-file' in args: |
| 36 | + _fs_file = tmp_path / 'license.txt' |
| 37 | + _fs_file.write_text('') |
| 38 | + args.insert(args.index('--fs-license-file') + 1, |
| 39 | + str(_fs_file.absolute())) |
| 40 | + |
| 41 | + opts = _build_parser().parse_args(args) |
| 42 | + |
| 43 | + assert opts.bids_dir == datapath |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize('argval,gb', [ |
| 47 | + ('1G', 1), |
| 48 | + ('1GB', 1), |
| 49 | + ('1000', 1), # Default units are MB |
| 50 | + ('32000', 32), # Default units are MB |
| 51 | + ('4000', 4), # Default units are MB |
| 52 | + ('1000M', 1), |
| 53 | + ('1000MB', 1), |
| 54 | + ('1T', 1000), |
| 55 | + ('1TB', 1000), |
| 56 | + ('%dK' % 1e6, 1), |
| 57 | + ('%dKB' % 1e6, 1), |
| 58 | + ('%dB' % 1e9, 1), |
| 59 | +]) |
| 60 | +def test_memory_arg(tmp_path, argval, gb): |
| 61 | + """Check the correct parsing of the memory argument.""" |
| 62 | + datapath = (tmp_path / 'data').absolute() |
| 63 | + datapath.mkdir(exist_ok=True) |
| 64 | + _fs_file = tmp_path / 'license.txt' |
| 65 | + _fs_file.write_text('') |
| 66 | + |
| 67 | + args = MIN_ARGS + ['--fs-license-file', str(_fs_file)] \ |
| 68 | + + ['--mem', argval] |
| 69 | + args[0] = str(datapath) |
| 70 | + opts = _build_parser().parse_args(args) |
| 71 | + |
| 72 | + assert opts.memory_gb == gb |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize('current,latest', [ |
| 76 | + ('1.0.0', '1.3.2'), |
| 77 | + ('1.3.2', '1.3.2') |
| 78 | +]) |
| 79 | +def test_get_parser_update(monkeypatch, capsys, current, latest): |
| 80 | + """Make sure the out-of-date banner is shown.""" |
| 81 | + expectation = Version(current) < Version(latest) |
| 82 | + |
| 83 | + def _mock_check_latest(*args, **kwargs): |
| 84 | + return Version(latest) |
| 85 | + |
| 86 | + monkeypatch.setattr(config.environment, 'version', current) |
| 87 | + monkeypatch.setattr(_version, 'check_latest', _mock_check_latest) |
| 88 | + |
| 89 | + _build_parser() |
| 90 | + captured = capsys.readouterr().err |
| 91 | + |
| 92 | + msg = """\ |
| 93 | +You are using dMRIPrep-%s, and a newer version of dMRIPrep is available: %s. |
| 94 | +Please check out our documentation about how and when to upgrade: |
| 95 | +https://dmriprep.readthedocs.io/en/latest/faq.html#upgrading""" % (current, latest) |
| 96 | + |
| 97 | + assert (msg in captured) is expectation |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize('flagged', [ |
| 101 | + (True, None), |
| 102 | + (True, 'random reason'), |
| 103 | + (False, None), |
| 104 | +]) |
| 105 | +def test_get_parser_blacklist(monkeypatch, capsys, flagged): |
| 106 | + """Make sure the blacklisting banner is shown.""" |
| 107 | + def _mock_is_bl(*args, **kwargs): |
| 108 | + return flagged |
| 109 | + |
| 110 | + monkeypatch.setattr(_version, 'is_flagged', _mock_is_bl) |
| 111 | + |
| 112 | + _build_parser() |
| 113 | + captured = capsys.readouterr().err |
| 114 | + |
| 115 | + assert ('FLAGGED' in captured) is flagged[0] |
| 116 | + if flagged[0]: |
| 117 | + assert ((flagged[1] or 'reason: unknown') in captured) |
0 commit comments