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
113 lines (95 loc) · 4.18 KB
/
test_build_parser.py
File metadata and controls
113 lines (95 loc) · 4.18 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
"""
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)
def test_parser_prog_name(self):
"""Test that parser has correct program name."""
parser = rimport.build_parser()
assert parser.prog == "rimport"
def test_file_argument_accepted(self):
"""Test that -file argument is accepted."""
parser = rimport.build_parser()
args = parser.parse_args(["-file", "test.txt"])
assert args.file == "test.txt"
assert args.filelist is None
def test_list_argument_accepted(self):
"""Test that -list argument is accepted."""
parser = rimport.build_parser()
args = parser.parse_args(["-list", "files.txt"])
assert args.filelist == "files.txt"
assert args.file is None
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 "rimport: 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"])
expected_default = os.path.join(
"/glade", "campaign", "cesm", "cesmdata", "cseg", "inputdata"
)
assert args.inputdata == expected_default
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"])
def test_help_flags_show_help(self, help_flag):
"""Test that -help and -h flags 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"