Skip to content

Commit 8c43ef5

Browse files
committed
Test CLI utils.
1 parent 42c5f94 commit 8c43ef5

File tree

3 files changed

+131
-30
lines changed

3 files changed

+131
-30
lines changed

cubids/cli.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,40 @@
1717

1818

1919
def _path_exists(path, parser):
20-
"""Ensure a given path exists."""
20+
"""Ensure a given path exists.
21+
22+
Parameters
23+
----------
24+
path : str
25+
Path to check for existence.
26+
parser : argparse.ArgumentParser
27+
Argument parser object.
28+
29+
Returns
30+
-------
31+
pathlib.Path
32+
Absolute path to the given location.
33+
"""
2134
if path is None or not Path(path).exists():
2235
raise parser.error(f"Path does not exist: <{path}>.")
2336
return Path(path).absolute()
2437

2538

2639
def _is_file(path, parser):
27-
"""Ensure a given path exists and it is a file."""
40+
"""Ensure a given path exists and it is a file.
41+
42+
Parameters
43+
----------
44+
path : str
45+
Path to check for existence.
46+
parser : argparse.ArgumentParser
47+
Argument parser object.
48+
49+
Returns
50+
-------
51+
pathlib.Path
52+
Absolute path to the given location.
53+
"""
2854
path = _path_exists(path, parser)
2955
if not path.is_file():
3056
raise parser.error(f"Path should point to a file (or symlink of file): <{path}>.")

cubids/tests/test_cli.py

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,98 @@
1414
"""
1515

1616
import argparse
17+
from functools import partial
1718

1819
import pytest
1920

2021
from cubids.cli import _get_parser, _is_file, _main, _path_exists
21-
22-
23-
def _test_path_exists():
24-
"""Test whether a given path exists or not.
25-
26-
This function tests the `_path_exists` function by providing a path that exists
27-
and a path that does not exist.
28-
It asserts that the function returns the expected path when the path exists,
29-
and raises an `argparse.ArgumentTypeError` when the path does not exist.
30-
"""
31-
assert _path_exists("/path/to/existing/file", None) == "/path/to/existing/file"
32-
33-
with pytest.raises(argparse.ArgumentTypeError):
34-
_path_exists("/path/to/nonexistent/file", None)
35-
36-
37-
def _test_is_file():
38-
"""Test whether a given path is a file or a directory.
39-
40-
This function tests the `_is_file` function by providing a path that is a file
41-
and a path that is a directory.
42-
It asserts that the function returns the expected path when the path is a file,
43-
and raises an `argparse.ArgumentTypeError` when the path is a directory.
44-
"""
45-
assert _is_file("/path/to/file.txt", None) == "/path/to/file.txt"
46-
47-
with pytest.raises(argparse.ArgumentTypeError):
48-
_is_file("/path/to/directory", None)
22+
from cubids.tests.utils import chdir
23+
24+
25+
def test_path_exists(tmp_path):
26+
"""Test whether a given path exists or not."""
27+
parser = argparse.ArgumentParser()
28+
29+
# Test with an existing path
30+
existing_path = tmp_path / "existing_file.txt"
31+
existing_path.touch() # Create the file
32+
result = _path_exists(str(existing_path), parser)
33+
assert result == existing_path.absolute()
34+
35+
# Test with just filename
36+
with chdir(tmp_path):
37+
result = _path_exists("existing_file.txt", parser)
38+
assert result == existing_path.absolute()
39+
40+
# Test with a non-existing path
41+
non_existing_path = tmp_path / "non_existing_file.txt"
42+
with pytest.raises(SystemExit):
43+
_path_exists(str(non_existing_path), parser)
44+
45+
# Test within an argument parser
46+
parser = argparse.ArgumentParser(
47+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
48+
)
49+
PathExists = partial(_path_exists, parser=parser)
50+
parser.add_argument(
51+
"existing_folder",
52+
type=PathExists,
53+
action="store",
54+
)
55+
56+
# Test with an existing path within an argument parser
57+
parser.parse_args([str(existing_path)])
58+
59+
# Test with just filename
60+
with chdir(tmp_path):
61+
parser.parse_args(["existing_file.txt"])
62+
63+
# Test with a non-existing path within an argument parser
64+
with pytest.raises(SystemExit):
65+
parser.parse_args([str(non_existing_path)])
66+
67+
68+
def test_is_file(tmp_path):
69+
"""Test whether a given path exists or not."""
70+
parser = argparse.ArgumentParser()
71+
72+
# Test with an existing path
73+
existing_path = tmp_path / "existing_file.txt"
74+
existing_path.touch() # Create the file
75+
result = _is_file(str(existing_path), parser)
76+
assert result == existing_path.absolute()
77+
78+
# Test with just filename
79+
with chdir(tmp_path):
80+
result = _is_file("existing_file.txt", parser)
81+
assert result == existing_path.absolute()
82+
83+
# Test with a non-existing path
84+
non_existing_path = tmp_path / "non_existing_file.txt"
85+
with pytest.raises(SystemExit):
86+
_is_file(str(non_existing_path), parser)
87+
88+
# Test within an argument parser
89+
parser = argparse.ArgumentParser(
90+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
91+
)
92+
IsFile = partial(_is_file, parser=parser)
93+
parser.add_argument(
94+
"existing_file",
95+
type=IsFile,
96+
action="store",
97+
)
98+
99+
# Test with an existing path within an argument parser
100+
parser.parse_args([str(existing_path)])
101+
102+
# Test with just filename
103+
with chdir(tmp_path):
104+
parser.parse_args(["existing_file.txt"])
105+
106+
# Test with a non-existing path within an argument parser
107+
with pytest.raises(SystemExit):
108+
parser.parse_args([str(non_existing_path)])
49109

50110

51111
def _test_get_parser():

cubids/tests/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import os
66
import shutil
7+
from contextlib import contextmanager
78
from pathlib import Path
89

910
import nibabel as nb
@@ -92,3 +93,17 @@ def _edit_a_json(json_file):
9293
metadata["THIS_IS_A_TEST"] = True
9394
with open(json_file, "w") as metadataw:
9495
json.dump(metadata, metadataw)
96+
97+
98+
@contextmanager
99+
def chdir(path):
100+
"""Temporarily change directories.
101+
102+
Taken from https://stackoverflow.com/a/37996581/2589328.
103+
"""
104+
oldpwd = os.getcwd()
105+
os.chdir(path)
106+
try:
107+
yield
108+
finally:
109+
os.chdir(oldpwd)

0 commit comments

Comments
 (0)