|
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. |
3 | 2 |
|
4 | 3 | The tests cover the following functions: |
5 | 4 | - _path_exists: Tests whether a given path exists or not. |
6 | 5 | - _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. |
9 | 6 |
|
10 | 7 | Each test case includes assertions to verify the expected behavior of the corresponding function. |
11 | | -
|
12 | | -Author: [Your Name] |
13 | | -Date: [Current Date] |
14 | 8 | """ |
15 | 9 |
|
16 | 10 | import argparse |
| 11 | +from functools import partial |
17 | 12 |
|
18 | 13 | import pytest |
19 | 14 |
|
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)]) |
0 commit comments