Skip to content

Commit 220688e

Browse files
authored
Merge branch 'main' into doc/change_to_entity
2 parents 1cd274a + 5e6c713 commit 220688e

File tree

3 files changed

+142
-77
lines changed

3 files changed

+142
-77
lines changed

cubids/cli.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,48 @@
1717

1818

1919
def _path_exists(path, parser):
20-
"""Ensure a given path exists."""
21-
if path is None or not Path(path).exists():
22-
raise parser.error(f"Path does not exist: <{path}>.")
23-
return Path(path).absolute()
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+
"""
34+
if path is not None:
35+
path = Path(path)
36+
37+
if path is None or not path.exists():
38+
raise parser.error(f"Path does not exist: <{path.absolute()}>.")
39+
return path.absolute()
2440

2541

2642
def _is_file(path, parser):
27-
"""Ensure a given path exists and it is a file."""
43+
"""Ensure a given path exists and it is a file.
44+
45+
Parameters
46+
----------
47+
path : str
48+
Path to check for existence.
49+
parser : argparse.ArgumentParser
50+
Argument parser object.
51+
52+
Returns
53+
-------
54+
pathlib.Path
55+
Absolute path to the given location.
56+
"""
2857
path = _path_exists(path, parser)
2958
if not path.is_file():
30-
raise parser.error(f"Path should point to a file (or symlink of file): <{path}>.")
59+
raise parser.error(
60+
f"Path should point to a file (or symlink of file): <{path.absolute()}>."
61+
)
3162
return path
3263

3364

cubids/tests/test_cli.py

Lines changed: 90 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,102 @@
1-
"""
2-
This file contains unit tests for the command-line interface (CLI) of the CuBIDS package.
1+
"""Unit tests for the command-line interface (CLI) of the CuBIDS package.
32
43
The tests cover the following functions:
54
- _path_exists: Tests whether a given path exists or not.
65
- _is_file: Tests whether a given path is a file or a directory.
7-
- _get_parser: Tests the creation and configuration of the argument parser.
8-
- _main: Tests the main function of the CLI.
96
107
Each test case includes assertions to verify the expected behavior of the corresponding function.
11-
12-
Author: [Your Name]
13-
Date: [Current Date]
148
"""
159

1610
import argparse
11+
from functools import partial
1712

1813
import pytest
1914

20-
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)
49-
50-
51-
def _test_get_parser():
52-
"""Test the creation and configuration of the argument parser.
53-
54-
This function tests the `_get_parser` function by asserting that the returned object is an
55-
instance of `argparse.ArgumentParser`, and that it has the expected `prog` and `description`
56-
attributes.
57-
Additional assertions can be added to test the configuration of the parser.
58-
"""
59-
parser = _get_parser()
60-
assert isinstance(parser, argparse.ArgumentParser)
61-
assert parser.prog == "cubids"
62-
assert parser.description == "Console script for cubids"
63-
# Add more assertions for the parser configuration
64-
65-
66-
def _test_main():
67-
"""Test the main function of the CLI.
68-
69-
This function tests the `_main` function by providing different sets of arguments.
70-
It asserts that the function returns the expected exit code (0 or 1) based on the provided
71-
arguments.
72-
"""
73-
# Test the main function with valid arguments
74-
argv = ["validate", "/path/to/dataset"]
75-
assert _main(argv) == 0
76-
77-
# Test the main function with invalid arguments
78-
argv = ["invalid-command", "/path/to/dataset"]
79-
assert _main(argv) == 1
80-
81-
# Test the main function with missing arguments
82-
argv = []
83-
assert _main(argv) == 1
15+
from cubids.cli import _is_file, _path_exists
16+
from cubids.tests.utils import chdir
17+
18+
19+
def test_path_exists(tmp_path):
20+
"""Test whether a given path exists or not."""
21+
parser = argparse.ArgumentParser()
22+
23+
# Test with an existing path
24+
existing_path = tmp_path / "existing_file.txt"
25+
existing_path.touch() # Create the file
26+
result = _path_exists(str(existing_path), parser)
27+
assert result == existing_path.absolute()
28+
29+
# Test with just filename
30+
with chdir(tmp_path):
31+
result = _path_exists("existing_file.txt", parser)
32+
assert result == existing_path.absolute()
33+
34+
# Test with a non-existing path
35+
non_existing_path = tmp_path / "non_existing_file.txt"
36+
with pytest.raises(SystemExit):
37+
_path_exists(str(non_existing_path), parser)
38+
39+
# Test within an argument parser
40+
parser = argparse.ArgumentParser(
41+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
42+
)
43+
PathExists = partial(_path_exists, parser=parser)
44+
parser.add_argument(
45+
"existing_folder",
46+
type=PathExists,
47+
action="store",
48+
)
49+
50+
# Test with an existing path within an argument parser
51+
parser.parse_args([str(existing_path)])
52+
53+
# Test with just filename
54+
with chdir(tmp_path):
55+
parser.parse_args(["existing_file.txt"])
56+
57+
# Test with a non-existing path within an argument parser
58+
with pytest.raises(SystemExit):
59+
parser.parse_args([str(non_existing_path)])
60+
61+
62+
def test_is_file(tmp_path):
63+
"""Test whether a given path exists or not."""
64+
parser = argparse.ArgumentParser()
65+
66+
# Test with an existing path
67+
existing_path = tmp_path / "existing_file.txt"
68+
existing_path.touch() # Create the file
69+
result = _is_file(str(existing_path), parser)
70+
assert result == existing_path.absolute()
71+
72+
# Test with just filename
73+
with chdir(tmp_path):
74+
result = _is_file("existing_file.txt", parser)
75+
assert result == existing_path.absolute()
76+
77+
# Test with a non-existing path
78+
non_existing_path = tmp_path / "non_existing_file.txt"
79+
with pytest.raises(SystemExit):
80+
_is_file(str(non_existing_path), parser)
81+
82+
# Test within an argument parser
83+
parser = argparse.ArgumentParser(
84+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
85+
)
86+
IsFile = partial(_is_file, parser=parser)
87+
parser.add_argument(
88+
"existing_file",
89+
type=IsFile,
90+
action="store",
91+
)
92+
93+
# Test with an existing path within an argument parser
94+
parser.parse_args([str(existing_path)])
95+
96+
# Test with just filename
97+
with chdir(tmp_path):
98+
parser.parse_args(["existing_file.txt"])
99+
100+
# Test with a non-existing path within an argument parser
101+
with pytest.raises(SystemExit):
102+
parser.parse_args([str(non_existing_path)])

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
@@ -80,3 +81,17 @@ def _edit_a_json(json_file):
8081
metadata["THIS_IS_A_TEST"] = True
8182
with open(json_file, "w") as metadataw:
8283
json.dump(metadata, metadataw)
84+
85+
86+
@contextmanager
87+
def chdir(path):
88+
"""Temporarily change directories.
89+
90+
Taken from https://stackoverflow.com/a/37996581/2589328.
91+
"""
92+
oldpwd = os.getcwd()
93+
os.chdir(path)
94+
try:
95+
yield
96+
finally:
97+
os.chdir(oldpwd)

0 commit comments

Comments
 (0)