-
Notifications
You must be signed in to change notification settings - Fork 13
Test CLI utils #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Test CLI utils #349
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8c43ef5
Test CLI utils.
tsalo ee4df72
Test Python 3.12.
tsalo c29e15f
Streamline file.
tsalo 005a8b6
Update cubids/cli.py
tsalo 8450734
Fix line length.
tsalo e09daa0
Update cli.py
tsalo 7529c4a
Merge branch 'main' into test-cli-utils
tsalo 2936925
Merge branch 'main' into test-cli-utils
tien-tong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,7 @@ workflows: | |
| - "3.9" | ||
| - "3.10" | ||
| - "3.11" | ||
| - "3.12" | ||
| filters: | ||
| tags: | ||
| only: /.*/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,102 @@ | ||
| """ | ||
| This file contains unit tests for the command-line interface (CLI) of the CuBIDS package. | ||
| """Unit tests for the command-line interface (CLI) of the CuBIDS package. | ||
|
|
||
| The tests cover the following functions: | ||
| - _path_exists: Tests whether a given path exists or not. | ||
| - _is_file: Tests whether a given path is a file or a directory. | ||
| - _get_parser: Tests the creation and configuration of the argument parser. | ||
| - _main: Tests the main function of the CLI. | ||
|
|
||
| Each test case includes assertions to verify the expected behavior of the corresponding function. | ||
|
|
||
| Author: [Your Name] | ||
| Date: [Current Date] | ||
| """ | ||
|
|
||
| import argparse | ||
| from functools import partial | ||
|
|
||
| import pytest | ||
|
|
||
| from cubids.cli import _get_parser, _is_file, _main, _path_exists | ||
|
|
||
|
|
||
| def _test_path_exists(): | ||
| """Test whether a given path exists or not. | ||
|
|
||
| This function tests the `_path_exists` function by providing a path that exists | ||
| and a path that does not exist. | ||
| It asserts that the function returns the expected path when the path exists, | ||
| and raises an `argparse.ArgumentTypeError` when the path does not exist. | ||
| """ | ||
| assert _path_exists("/path/to/existing/file", None) == "/path/to/existing/file" | ||
|
|
||
| with pytest.raises(argparse.ArgumentTypeError): | ||
| _path_exists("/path/to/nonexistent/file", None) | ||
|
|
||
|
|
||
| def _test_is_file(): | ||
| """Test whether a given path is a file or a directory. | ||
|
|
||
| This function tests the `_is_file` function by providing a path that is a file | ||
| and a path that is a directory. | ||
| It asserts that the function returns the expected path when the path is a file, | ||
| and raises an `argparse.ArgumentTypeError` when the path is a directory. | ||
| """ | ||
| assert _is_file("/path/to/file.txt", None) == "/path/to/file.txt" | ||
|
|
||
| with pytest.raises(argparse.ArgumentTypeError): | ||
| _is_file("/path/to/directory", None) | ||
|
|
||
|
|
||
| def _test_get_parser(): | ||
| """Test the creation and configuration of the argument parser. | ||
|
|
||
| This function tests the `_get_parser` function by asserting that the returned object is an | ||
| instance of `argparse.ArgumentParser`, and that it has the expected `prog` and `description` | ||
| attributes. | ||
| Additional assertions can be added to test the configuration of the parser. | ||
| """ | ||
| parser = _get_parser() | ||
| assert isinstance(parser, argparse.ArgumentParser) | ||
| assert parser.prog == "cubids" | ||
| assert parser.description == "Console script for cubids" | ||
| # Add more assertions for the parser configuration | ||
|
|
||
|
|
||
| def _test_main(): | ||
| """Test the main function of the CLI. | ||
|
|
||
| This function tests the `_main` function by providing different sets of arguments. | ||
| It asserts that the function returns the expected exit code (0 or 1) based on the provided | ||
| arguments. | ||
| """ | ||
| # Test the main function with valid arguments | ||
| argv = ["validate", "/path/to/dataset"] | ||
| assert _main(argv) == 0 | ||
|
|
||
| # Test the main function with invalid arguments | ||
| argv = ["invalid-command", "/path/to/dataset"] | ||
| assert _main(argv) == 1 | ||
|
|
||
| # Test the main function with missing arguments | ||
| argv = [] | ||
| assert _main(argv) == 1 | ||
| from cubids.cli import _is_file, _path_exists | ||
| from cubids.tests.utils import chdir | ||
|
|
||
|
|
||
| def test_path_exists(tmp_path): | ||
| """Test whether a given path exists or not.""" | ||
| parser = argparse.ArgumentParser() | ||
|
|
||
| # Test with an existing path | ||
| existing_path = tmp_path / "existing_file.txt" | ||
| existing_path.touch() # Create the file | ||
| result = _path_exists(str(existing_path), parser) | ||
| assert result == existing_path.absolute() | ||
|
|
||
| # Test with just filename | ||
| with chdir(tmp_path): | ||
| result = _path_exists("existing_file.txt", parser) | ||
| assert result == existing_path.absolute() | ||
|
|
||
| # Test with a non-existing path | ||
| non_existing_path = tmp_path / "non_existing_file.txt" | ||
| with pytest.raises(SystemExit): | ||
| _path_exists(str(non_existing_path), parser) | ||
|
|
||
| # Test within an argument parser | ||
| parser = argparse.ArgumentParser( | ||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
| ) | ||
| PathExists = partial(_path_exists, parser=parser) | ||
| parser.add_argument( | ||
| "existing_folder", | ||
| type=PathExists, | ||
| action="store", | ||
| ) | ||
|
|
||
| # Test with an existing path within an argument parser | ||
| parser.parse_args([str(existing_path)]) | ||
|
|
||
| # Test with just filename | ||
| with chdir(tmp_path): | ||
| parser.parse_args(["existing_file.txt"]) | ||
|
|
||
| # Test with a non-existing path within an argument parser | ||
| with pytest.raises(SystemExit): | ||
| parser.parse_args([str(non_existing_path)]) | ||
|
|
||
|
|
||
| def test_is_file(tmp_path): | ||
| """Test whether a given path exists or not.""" | ||
| parser = argparse.ArgumentParser() | ||
|
|
||
| # Test with an existing path | ||
| existing_path = tmp_path / "existing_file.txt" | ||
| existing_path.touch() # Create the file | ||
| result = _is_file(str(existing_path), parser) | ||
| assert result == existing_path.absolute() | ||
|
|
||
| # Test with just filename | ||
| with chdir(tmp_path): | ||
| result = _is_file("existing_file.txt", parser) | ||
| assert result == existing_path.absolute() | ||
|
|
||
| # Test with a non-existing path | ||
| non_existing_path = tmp_path / "non_existing_file.txt" | ||
| with pytest.raises(SystemExit): | ||
| _is_file(str(non_existing_path), parser) | ||
|
|
||
| # Test within an argument parser | ||
| parser = argparse.ArgumentParser( | ||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
| ) | ||
| IsFile = partial(_is_file, parser=parser) | ||
| parser.add_argument( | ||
| "existing_file", | ||
| type=IsFile, | ||
| action="store", | ||
| ) | ||
|
|
||
| # Test with an existing path within an argument parser | ||
| parser.parse_args([str(existing_path)]) | ||
|
|
||
| # Test with just filename | ||
| with chdir(tmp_path): | ||
| parser.parse_args(["existing_file.txt"]) | ||
|
|
||
| # Test with a non-existing path within an argument parser | ||
| with pytest.raises(SystemExit): | ||
| parser.parse_args([str(non_existing_path)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.