|
| 1 | +"""Tests for CLI argument parsing and validation.""" |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from mitreattack.diffStix.changelog_helper import get_parsed_args |
| 8 | + |
| 9 | + |
| 10 | +class TestArgumentHandling: |
| 11 | + """Comprehensive tests for CLI argument parsing and validation.""" |
| 12 | + |
| 13 | + def _parse_args(self, argv_list, monkeypatch, setup_monkeypatch_args=None): |
| 14 | + """Parse arguments with monkeypatch setup.""" |
| 15 | + if setup_monkeypatch_args: |
| 16 | + setup_monkeypatch_args(argv_list, monkeypatch) |
| 17 | + else: |
| 18 | + monkeypatch.setattr(sys, "argv", argv_list) |
| 19 | + return get_parsed_args() |
| 20 | + |
| 21 | + def _expect_system_exit(self, argv_list, monkeypatch, expected_code=None): |
| 22 | + """Test SystemExit scenarios.""" |
| 23 | + monkeypatch.setattr(sys, "argv", argv_list) |
| 24 | + with pytest.raises(SystemExit) as exc_info: |
| 25 | + get_parsed_args() |
| 26 | + if expected_code is not None: |
| 27 | + assert exc_info.value.code == expected_code |
| 28 | + return exc_info |
| 29 | + |
| 30 | + def _assert_default_args(self, args): |
| 31 | + """Assert default argument values.""" |
| 32 | + assert args.old == "old" |
| 33 | + assert args.new == "new" |
| 34 | + assert args.domains == ["enterprise-attack", "mobile-attack", "ics-attack"] |
| 35 | + assert args.unchanged is False |
| 36 | + assert args.show_key is False |
| 37 | + assert args.contributors is True |
| 38 | + assert args.verbose is False |
| 39 | + assert args.use_mitre_cti is False |
| 40 | + assert args.site_prefix == "" |
| 41 | + |
| 42 | + def test_get_parsed_args_default_values(self, monkeypatch): |
| 43 | + """Test default argument values.""" |
| 44 | + args = self._parse_args(["script_name"], monkeypatch) |
| 45 | + self._assert_default_args(args) |
| 46 | + |
| 47 | + def test_get_parsed_args_all_options(self, monkeypatch): |
| 48 | + """Test parsing with all command-line options specified.""" |
| 49 | + test_args = [ |
| 50 | + "script_name", |
| 51 | + "--old", |
| 52 | + "old_data", |
| 53 | + "--new", |
| 54 | + "new_data", |
| 55 | + "--domains", |
| 56 | + "enterprise-attack", |
| 57 | + "--markdown-file", |
| 58 | + "test.md", |
| 59 | + "--html-file", |
| 60 | + "test.html", |
| 61 | + "--html-file-detailed", |
| 62 | + "detailed.html", |
| 63 | + "--json-file", |
| 64 | + "test.json", |
| 65 | + "--layers", |
| 66 | + "layer1.json", |
| 67 | + "layer2.json", |
| 68 | + "layer3.json", |
| 69 | + "--site_prefix", |
| 70 | + "https://example.com", |
| 71 | + "--unchanged", |
| 72 | + "--show-key", |
| 73 | + "--no-contributors", |
| 74 | + "--verbose", |
| 75 | + ] |
| 76 | + |
| 77 | + args = self._parse_args(test_args, monkeypatch) |
| 78 | + |
| 79 | + assert args.old == "old_data" |
| 80 | + assert args.new == "new_data" |
| 81 | + assert args.domains == ["enterprise-attack"] |
| 82 | + assert args.markdown_file == "test.md" |
| 83 | + assert args.html_file == "test.html" |
| 84 | + assert args.html_file_detailed == "detailed.html" |
| 85 | + assert args.json_file == "test.json" |
| 86 | + assert args.layers == ["layer1.json", "layer2.json", "layer3.json"] |
| 87 | + assert args.site_prefix == "https://example.com" |
| 88 | + assert args.unchanged is True |
| 89 | + assert args.show_key is True |
| 90 | + assert args.contributors is False |
| 91 | + assert args.verbose is True |
| 92 | + |
| 93 | + @pytest.mark.parametrize( |
| 94 | + "test_args,expected_exit_code,description", |
| 95 | + [ |
| 96 | + (["script_name", "--old", "old_data", "--use-mitre-cti"], None, "mutually exclusive options"), |
| 97 | + (["script_name", "--help"], 0, "help option"), |
| 98 | + (["script_name", "--invalid-option"], None, "invalid option"), |
| 99 | + (["script_name", "--old"], None, "missing required value"), |
| 100 | + ], |
| 101 | + ) |
| 102 | + def test_get_parsed_args_system_exit_scenarios(self, test_args, expected_exit_code, description, monkeypatch): |
| 103 | + """Test various scenarios that should cause SystemExit.""" |
| 104 | + exc_info = self._expect_system_exit(test_args, monkeypatch, expected_exit_code) |
| 105 | + if expected_exit_code is not None: |
| 106 | + assert exc_info.value.code == expected_exit_code |
| 107 | + |
| 108 | + @pytest.mark.parametrize( |
| 109 | + "layers_input,expected_result,should_exit", |
| 110 | + [ |
| 111 | + ([], [], False), # Empty list is valid |
| 112 | + ( |
| 113 | + ["enterprise.json", "mobile.json", "ics.json"], |
| 114 | + ["enterprise.json", "mobile.json", "ics.json"], |
| 115 | + False, |
| 116 | + ), # Three files is valid |
| 117 | + (["layer1.json", "layer2.json"], None, True), # Wrong count - need 0 or 3 |
| 118 | + ], |
| 119 | + ) |
| 120 | + def test_get_parsed_args_layer_validation(self, layers_input, expected_result, should_exit, monkeypatch): |
| 121 | + """Test layer argument validation scenarios.""" |
| 122 | + test_args = ["script_name", "--layers"] + layers_input |
| 123 | + |
| 124 | + if should_exit: |
| 125 | + self._expect_system_exit(test_args, monkeypatch) |
| 126 | + else: |
| 127 | + args = self._parse_args(test_args, monkeypatch) |
| 128 | + assert args.layers == expected_result |
| 129 | + |
| 130 | + def test_get_parsed_args_logging_configuration_verbose(self, monkeypatch): |
| 131 | + """Test logging configuration in verbose mode.""" |
| 132 | + args = self._parse_args(["script_name", "--verbose"], monkeypatch) |
| 133 | + assert args.verbose is True |
| 134 | + |
| 135 | + def test_get_parsed_args_logging_configuration_normal(self, monkeypatch): |
| 136 | + """Test logging configuration in normal mode.""" |
| 137 | + args = self._parse_args(["script_name"], monkeypatch) |
| 138 | + assert args.verbose is False |
| 139 | + |
| 140 | + @pytest.mark.parametrize( |
| 141 | + "domains_input,expected_domains", |
| 142 | + [ |
| 143 | + (["enterprise-attack"], ["enterprise-attack"]), |
| 144 | + (["enterprise-attack", "mobile-attack"], ["enterprise-attack", "mobile-attack"]), |
| 145 | + ( |
| 146 | + ["enterprise-attack", "mobile-attack", "ics-attack"], |
| 147 | + ["enterprise-attack", "mobile-attack", "ics-attack"], |
| 148 | + ), |
| 149 | + ], |
| 150 | + ) |
| 151 | + def test_get_parsed_args_domains(self, domains_input, expected_domains, monkeypatch): |
| 152 | + """Test parsing with various domain configurations.""" |
| 153 | + test_args = ["script_name", "--domains"] + domains_input |
| 154 | + args = self._parse_args(test_args, monkeypatch) |
| 155 | + assert args.domains == expected_domains |
| 156 | + |
| 157 | + @pytest.mark.parametrize( |
| 158 | + "flag,expected_attr,expected_value", |
| 159 | + [ |
| 160 | + ("--unchanged", "unchanged", True), |
| 161 | + ("--show-key", "show_key", True), |
| 162 | + ("--no-contributors", "contributors", False), |
| 163 | + ("--verbose", "verbose", True), |
| 164 | + ("--use-mitre-cti", "use_mitre_cti", True), |
| 165 | + ], |
| 166 | + ) |
| 167 | + def test_get_parsed_args_boolean_flags(self, flag, expected_attr, expected_value, monkeypatch): |
| 168 | + """Test individual boolean flags.""" |
| 169 | + test_args = ["script_name", flag] |
| 170 | + args = self._parse_args(test_args, monkeypatch) |
| 171 | + assert getattr(args, expected_attr) == expected_value |
| 172 | + |
| 173 | + @pytest.mark.parametrize( |
| 174 | + "option,value,expected_attr", |
| 175 | + [ |
| 176 | + ("--old", "custom_old", "old"), |
| 177 | + ("--new", "custom_new", "new"), |
| 178 | + ("--markdown-file", "custom.md", "markdown_file"), |
| 179 | + ("--html-file", "custom.html", "html_file"), |
| 180 | + ("--html-file-detailed", "detailed.html", "html_file_detailed"), |
| 181 | + ("--json-file", "custom.json", "json_file"), |
| 182 | + ("--site_prefix", "https://custom.com", "site_prefix"), |
| 183 | + ("--site_prefix", "", "site_prefix"), # Empty site prefix |
| 184 | + ("--site_prefix", "https://example.com/", "site_prefix"), # With trailing slash |
| 185 | + ], |
| 186 | + ) |
| 187 | + def test_get_parsed_args_string_options(self, option, value, expected_attr, monkeypatch): |
| 188 | + """Test individual string options.""" |
| 189 | + test_args = ["script_name", option, value] |
| 190 | + args = self._parse_args(test_args, monkeypatch) |
| 191 | + assert getattr(args, expected_attr) == value |
0 commit comments