-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_main.py
More file actions
52 lines (42 loc) · 1.33 KB
/
test_main.py
File metadata and controls
52 lines (42 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
import shlex
import sys
from contextlib import contextmanager
from pathlib import Path
from typing import List
import pytest
from bids2table import __main__ as cli
BIDS_EXAMPLES = Path(__file__).parents[1] / "bids-examples"
@contextmanager
def patch_argv(argv: List[str]):
old_argv = sys.argv
try:
sys.argv = argv.copy()
yield
finally:
sys.argv = old_argv
@pytest.mark.parametrize(
"cmd,output",
[
("index -o {out_dir}/ds102.parquet {examples}/ds102", "ds102.parquet"),
(
"index -o {out_dir}/ds101_ds102.parquet {examples}/ds101 {examples}/ds102",
"ds101_ds102.parquet",
),
("index -o {out_dir}/ds10N.parquet '{examples}/ds10?'", "ds10N.parquet"),
],
)
def test_main_index(cmd: str, output: str | None, tmp_path: Path):
cmd_fmt = cmd.format(out_dir=tmp_path, examples=BIDS_EXAMPLES)
prog = str(Path(cli.__file__).absolute())
argv = [prog] + shlex.split(cmd_fmt)
with patch_argv(argv):
cli.main()
if output:
assert (tmp_path / output).exists()
@pytest.mark.parametrize("cmd", ["find {examples}"])
def test_main_find(cmd: str):
cmd_fmt = cmd.format(examples=BIDS_EXAMPLES)
prog = str(Path(cli.__file__).absolute())
argv = [prog] + shlex.split(cmd_fmt)
with patch_argv(argv):
cli.main()