|
| 1 | +""" |
| 2 | +Tests for build_parser() function in rimport script. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import argparse |
| 8 | +import importlib.util |
| 9 | +from importlib.machinery import SourceFileLoader |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +# Import rimport module from file without .py extension |
| 14 | +rimport_path = os.path.join( |
| 15 | + os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), |
| 16 | + "rimport", |
| 17 | +) |
| 18 | +loader = SourceFileLoader("rimport", rimport_path) |
| 19 | +spec = importlib.util.spec_from_loader("rimport", loader) |
| 20 | +if spec is None: |
| 21 | + raise ImportError(f"Could not create spec for rimport from {rimport_path}") |
| 22 | +rimport = importlib.util.module_from_spec(spec) |
| 23 | +sys.modules["rimport"] = rimport |
| 24 | +loader.exec_module(rimport) |
| 25 | + |
| 26 | + |
| 27 | +class TestBuildParser: |
| 28 | + """Test suite for build_parser() function.""" |
| 29 | + |
| 30 | + def test_parser_creation(self): |
| 31 | + """Test that build_parser creates an ArgumentParser.""" |
| 32 | + parser = rimport.build_parser() |
| 33 | + assert isinstance(parser, argparse.ArgumentParser) |
| 34 | + |
| 35 | + def test_parser_prog_name(self): |
| 36 | + """Test that parser has correct program name.""" |
| 37 | + parser = rimport.build_parser() |
| 38 | + assert parser.prog == "rimport" |
| 39 | + |
| 40 | + def test_file_argument_accepted(self): |
| 41 | + """Test that -file argument is accepted.""" |
| 42 | + parser = rimport.build_parser() |
| 43 | + args = parser.parse_args(["-file", "test.txt"]) |
| 44 | + assert args.file == "test.txt" |
| 45 | + assert args.filelist is None |
| 46 | + |
| 47 | + def test_list_argument_accepted(self): |
| 48 | + """Test that -list argument is accepted.""" |
| 49 | + parser = rimport.build_parser() |
| 50 | + args = parser.parse_args(["-list", "files.txt"]) |
| 51 | + assert args.filelist == "files.txt" |
| 52 | + assert args.file is None |
| 53 | + |
| 54 | + def test_file_and_list_mutually_exclusive(self, capsys): |
| 55 | + """Test that -file and -list cannot be used together.""" |
| 56 | + parser = rimport.build_parser() |
| 57 | + with pytest.raises(SystemExit): |
| 58 | + parser.parse_args(["-file", "test.txt", "-list", "files.txt"]) |
| 59 | + |
| 60 | + # Check that the error message explains the problem |
| 61 | + captured = capsys.readouterr() |
| 62 | + stderr_lines = captured.err.strip().split("\n") |
| 63 | + assert "not allowed with argument" in stderr_lines[-1] |
| 64 | + |
| 65 | + def test_file_or_list_required(self, capsys): |
| 66 | + """Test that either -file or -list is required.""" |
| 67 | + parser = rimport.build_parser() |
| 68 | + with pytest.raises(SystemExit): |
| 69 | + parser.parse_args([]) |
| 70 | + |
| 71 | + # Check that the error message explains the problem |
| 72 | + captured = capsys.readouterr() |
| 73 | + stderr_lines = captured.err.strip().split("\n") |
| 74 | + assert "rimport: error: one of the arguments" in stderr_lines[-1] |
| 75 | + |
| 76 | + def test_inputdata_default(self): |
| 77 | + """Test that -inputdata has correct default value.""" |
| 78 | + parser = rimport.build_parser() |
| 79 | + args = parser.parse_args(["-file", "test.txt"]) |
| 80 | + expected_default = os.path.join( |
| 81 | + "/glade", "campaign", "cesm", "cesmdata", "cseg", "inputdata" |
| 82 | + ) |
| 83 | + assert args.inputdata == expected_default |
| 84 | + |
| 85 | + def test_inputdata_custom(self): |
| 86 | + """Test that -inputdata can be customized.""" |
| 87 | + parser = rimport.build_parser() |
| 88 | + custom_path = "/custom/path" |
| 89 | + args = parser.parse_args(["-file", "test.txt", "-inputdata", custom_path]) |
| 90 | + assert args.inputdata == custom_path |
| 91 | + |
| 92 | + @pytest.mark.parametrize("help_flag", ["-help", "-h"]) |
| 93 | + def test_help_flags_show_help(self, help_flag): |
| 94 | + """Test that -help and -h flags trigger help.""" |
| 95 | + parser = rimport.build_parser() |
| 96 | + with pytest.raises(SystemExit) as exc_info: |
| 97 | + parser.parse_args([help_flag]) |
| 98 | + # Help should exit with code 0 |
| 99 | + assert exc_info.value.code == 0 |
| 100 | + |
| 101 | + def test_file_with_inputdata(self): |
| 102 | + """Test combining -file with -inputdata.""" |
| 103 | + parser = rimport.build_parser() |
| 104 | + args = parser.parse_args(["-file", "data.nc", "-inputdata", "/my/data"]) |
| 105 | + assert args.file == "data.nc" |
| 106 | + assert args.inputdata == "/my/data" |
| 107 | + |
| 108 | + def test_list_with_inputdata(self): |
| 109 | + """Test combining -list with -inputdata.""" |
| 110 | + parser = rimport.build_parser() |
| 111 | + args = parser.parse_args(["-list", "files.txt", "-inputdata", "/my/data"]) |
| 112 | + assert args.filelist == "files.txt" |
| 113 | + assert args.inputdata == "/my/data" |
0 commit comments