-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
fido.py
moving CLI argument parsing into its own `cli_args…
….py`. Add some tests to the argument parsing. Add the CONFIG_DIR to the dictionary of defaults in fido.py.
- Loading branch information
1 parent
aaa38b0
commit 630be3a
Showing
3 changed files
with
103 additions
and
184 deletions.
There are no files selected for viewing
This file contains 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 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 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,61 +1,54 @@ | ||
import argparse | ||
|
||
import pytest | ||
|
||
from fido.cli_args import build_parser, parse_args | ||
from fido.cli_args import parse_cli_args | ||
from fido.fido import defaults | ||
|
||
# Common argument string | ||
ARG_STRING = ( | ||
"-v -q -recurse -zip -noextension -nocontainer -pronom_only" | ||
"-input files.txt" | ||
"-useformats=fmt1,fmt2 -nouseformats=fmt3,fmt4" | ||
) | ||
|
||
ARG_STRING = ( | ||
"-v -q -recurse -zip -noextension -nocontainer -pronom_only" | ||
"-input files.txt" | ||
"-useformats=fmt1,fmt2 -nouseformats=fmt3,fmt4" | ||
) | ||
|
||
|
||
def test_build_parser(): | ||
parser = build_parser() | ||
assert isinstance(parser, argparse.ArgumentParser) | ||
|
||
# Check if all expected arguments are present | ||
expected_args = ARG_STRING.split() | ||
for arg in expected_args: | ||
assert arg in parser._option_string_actions | ||
|
||
|
||
def test_parse_args_valid(): | ||
parser = build_parser() | ||
|
||
args = parse_args(parser.parse_args(ARG_STRING.split())) | ||
|
||
assert args.v is True | ||
assert args.q is True | ||
assert args.recurse is True | ||
assert args.zip is True | ||
assert args.noextension is True | ||
assert args.nocontainer is True | ||
assert args.pronom_only is True | ||
assert args.input == "input_file" | ||
assert args.files == ["file1", "file2"] | ||
assert args.filename == "filename" | ||
|
||
|
||
def test_parse_args_input_valid(): | ||
arg_string = ( | ||
"-v -q -recurse -zip -noextension -nocontainer -pronom_only " | ||
"-input files.txt " | ||
"-useformats=fmt1,fmt2 -nouseformats=fmt3,fmt4" | ||
) | ||
args = parse_cli_args(arg_string.split(), defaults) | ||
print(arg_string.split()) | ||
print(args) | ||
assert args.v | ||
assert args.q | ||
assert args.recurse | ||
assert args.zip | ||
assert args.noextension | ||
assert args.nocontainer | ||
assert args.pronom_only | ||
assert args.input == "files.txt" | ||
assert args.useformats == "fmt1,fmt2" | ||
assert args.nouseformats == "fmt3,fmt4" | ||
|
||
|
||
def test_parse_args_invalid(monkeypatch): | ||
parser = build_parser() | ||
def test_parse_args_files_valid(): | ||
arg_string = "-q -zip file1.ext file2.ext" | ||
args = parse_cli_args(arg_string.split(), defaults) | ||
print(arg_string.split()) | ||
print(args) | ||
assert args.q | ||
assert args.zip | ||
assert args.noextension == False | ||
assert args.nocontainer == False | ||
assert args.pronom_only == False | ||
assert args.files == ["file1.ext", "file2.ext"] | ||
assert args.useformats is None | ||
assert args.nouseformats is None | ||
|
||
# Simulate invalid argument input | ||
monkeypatch.setattr("sys.argv", ["prog", "--invalid"]) | ||
|
||
def test_parse_args_invalid(): | ||
arg_string = "-q -zip -bad_arg file1.ext file2.ext" | ||
with pytest.raises(SystemExit): | ||
parse_args(parser) | ||
args = parse_cli_args(arg_string.split(), defaults) | ||
|
||
|
||
# Simulate missing required argument | ||
monkeypatch.setattr("sys.argv", ["prog", "-input"]) | ||
def test_parse_files_and_input_invalid(): | ||
arg_string = "-q -zip -input files.txt file1.ext file2.ext" | ||
with pytest.raises(SystemExit): | ||
parse_args(parser) | ||
args = parse_cli_args(arg_string.split(), defaults) |