|
14 | 14 | """ |
15 | 15 |
|
16 | 16 | import argparse |
| 17 | +from functools import partial |
17 | 18 |
|
18 | 19 | import pytest |
19 | 20 |
|
20 | 21 | 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)]) |
49 | 109 |
|
50 | 110 |
|
51 | 111 | def _test_get_parser(): |
|
0 commit comments