forked from ESMCI/inputdataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_build_parser.py
More file actions
128 lines (108 loc) · 5.06 KB
/
test_build_parser.py
File metadata and controls
128 lines (108 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Tests for build_parser() function in rimport script.
"""
import os
import sys
import argparse
import importlib.util
from importlib.machinery import SourceFileLoader
import pytest
# Import rimport module from file without .py extension
rimport_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
"rimport",
)
loader = SourceFileLoader("rimport", rimport_path)
spec = importlib.util.spec_from_loader("rimport", loader)
if spec is None:
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
rimport = importlib.util.module_from_spec(spec)
sys.modules["rimport"] = rimport
loader.exec_module(rimport)
class TestBuildParser:
"""Test suite for build_parser() function."""
def test_parser_creation(self):
"""Test that build_parser creates an ArgumentParser."""
parser = rimport.build_parser()
assert isinstance(parser, argparse.ArgumentParser)
@pytest.mark.parametrize("file_flag", ["-file", "-f", "--file"])
def test_file_arguments_accepted(self, file_flag):
"""Test that all file argument flags are accepted."""
parser = rimport.build_parser()
args = parser.parse_args([file_flag, "test.txt"])
assert args.file == "test.txt"
assert args.filelist is None
@pytest.mark.parametrize("list_flag", ["-list", "-l", "--list"])
def test_list_arguments_accepted(self, list_flag):
"""Test that all list argument flags are accepted."""
parser = rimport.build_parser()
args = parser.parse_args([list_flag, "files.txt"])
assert args.filelist == "files.txt"
assert args.file is None
@pytest.mark.parametrize("inputdata_flag", ["-inputdata", "-i", "--inputdata"])
def test_inputdata_arguments_accepted(self, inputdata_flag):
"""Test that all inputdata argument flags are accepted."""
parser = rimport.build_parser()
inputdata_dir = "/some/dir"
args = parser.parse_args([inputdata_flag, inputdata_dir, "-f", "dummy_file.nc"])
assert args.inputdata == inputdata_dir
def test_file_and_list_mutually_exclusive(self, capsys):
"""Test that -file and -list cannot be used together."""
parser = rimport.build_parser()
with pytest.raises(SystemExit):
parser.parse_args(["-file", "test.txt", "-list", "files.txt"])
# Check that the error message explains the problem
captured = capsys.readouterr()
stderr_lines = captured.err.strip().split("\n")
assert "not allowed with argument" in stderr_lines[-1]
def test_file_or_list_required(self, capsys):
"""Test that either -file or -list is required."""
parser = rimport.build_parser()
with pytest.raises(SystemExit):
parser.parse_args([])
# Check that the error message explains the problem
captured = capsys.readouterr()
stderr_lines = captured.err.strip().split("\n")
assert "error: one of the arguments" in stderr_lines[-1]
def test_inputdata_default(self):
"""Test that -inputdata has correct default value."""
parser = rimport.build_parser()
args = parser.parse_args(["-file", "test.txt"])
assert args.inputdata == rimport.DEFAULT_INPUTDATA_ROOT
def test_check_default(self):
"""Test that --check has the correct default value."""
parser = rimport.build_parser()
args = parser.parse_args(["-file", "test.txt"])
assert args.check is False
@pytest.mark.parametrize("check_flag", ["-check", "-c", "--check"])
def test_check_arguments_accepted(self, check_flag):
"""Test that all check argument flags are accepted."""
parser = rimport.build_parser()
args = parser.parse_args(["-file", "test.txt", check_flag])
assert args.check is True
def test_inputdata_custom(self):
"""Test that -inputdata can be customized."""
parser = rimport.build_parser()
custom_path = "/custom/path"
args = parser.parse_args(["-file", "test.txt", "-inputdata", custom_path])
assert args.inputdata == custom_path
@pytest.mark.parametrize("help_flag", ["-help", "-h", "--help"])
def test_help_flags_show_help(self, help_flag):
"""Test that all help flag options trigger help."""
parser = rimport.build_parser()
with pytest.raises(SystemExit) as exc_info:
parser.parse_args([help_flag])
# Help should exit with code 0
assert exc_info.value.code == 0
def test_file_with_inputdata(self):
"""Test combining -file with -inputdata."""
parser = rimport.build_parser()
args = parser.parse_args(["-file", "data.nc", "-inputdata", "/my/data"])
assert args.file == "data.nc"
assert args.inputdata == "/my/data"
def test_list_with_inputdata(self):
"""Test combining -list with -inputdata."""
parser = rimport.build_parser()
args = parser.parse_args(["-list", "files.txt", "-inputdata", "/my/data"])
assert args.filelist == "files.txt"
assert args.inputdata == "/my/data"