|
| 1 | +""" |
| 2 | +Tests for path sanitization and restoration functionality. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from click_mcp.scanner import _original_paths, get_original_path, sanitize_tool_name |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def clear_original_paths(): |
| 12 | + """Clear the original paths mapping before each test.""" |
| 13 | + _original_paths.clear() |
| 14 | + yield |
| 15 | + _original_paths.clear() # Clean up after test |
| 16 | + |
| 17 | + |
| 18 | +def test_simple_path_sanitization(clear_original_paths): |
| 19 | + """Test sanitization of simple paths.""" |
| 20 | + # Store mapping manually since sanitize_tool_name doesn't do it anymore |
| 21 | + original = "command.subcommand" |
| 22 | + sanitized = sanitize_tool_name(original) |
| 23 | + _original_paths[sanitized] = original |
| 24 | + |
| 25 | + assert sanitized == "command_subcommand" |
| 26 | + assert get_original_path(sanitized) == original |
| 27 | + |
| 28 | + |
| 29 | +def test_path_with_existing_underscores(clear_original_paths): |
| 30 | + """Test sanitization of paths that already contain underscores.""" |
| 31 | + # Store mapping manually |
| 32 | + original = "command.sub_command.action" |
| 33 | + sanitized = sanitize_tool_name(original) |
| 34 | + _original_paths[sanitized] = original |
| 35 | + |
| 36 | + assert sanitized == "command_sub_command_action" |
| 37 | + assert get_original_path(sanitized) == original |
| 38 | + |
| 39 | + |
| 40 | +def test_path_with_special_characters(clear_original_paths): |
| 41 | + """Test sanitization of paths with special characters.""" |
| 42 | + # Store mapping manually |
| 43 | + original = "command.sub-command.action!" |
| 44 | + sanitized = sanitize_tool_name(original) |
| 45 | + _original_paths[sanitized] = original |
| 46 | + |
| 47 | + assert sanitized == "command_sub_command_action_" |
| 48 | + assert get_original_path(sanitized) == original |
| 49 | + |
| 50 | + |
| 51 | +def test_path_starting_with_number(clear_original_paths): |
| 52 | + """Test sanitization of paths that start with a number.""" |
| 53 | + # Store mapping manually |
| 54 | + original = "1command.subcommand" |
| 55 | + sanitized = sanitize_tool_name(original) |
| 56 | + _original_paths[sanitized] = original |
| 57 | + |
| 58 | + assert sanitized == "tool_1command_subcommand" |
| 59 | + assert get_original_path(sanitized) == original |
| 60 | + |
| 61 | + |
| 62 | +def test_fallback_to_sanitized_name(clear_original_paths): |
| 63 | + """ |
| 64 | + Test that get_original_path falls back to the sanitized name if no mapping exists. |
| 65 | + """ |
| 66 | + sanitized = "unknown_command" |
| 67 | + assert get_original_path(sanitized) == sanitized |
| 68 | + |
| 69 | + |
| 70 | +def test_multiple_sanitizations(clear_original_paths): |
| 71 | + """Test that multiple sanitizations work correctly.""" |
| 72 | + # First path |
| 73 | + original1 = "command.subcommand" |
| 74 | + sanitized1 = sanitize_tool_name(original1) |
| 75 | + _original_paths[sanitized1] = original1 |
| 76 | + |
| 77 | + # Second path |
| 78 | + original2 = "another.sub_command" |
| 79 | + sanitized2 = sanitize_tool_name(original2) |
| 80 | + _original_paths[sanitized2] = original2 |
| 81 | + |
| 82 | + assert sanitized1 == "command_subcommand" |
| 83 | + assert get_original_path(sanitized1) == original1 |
| 84 | + |
| 85 | + assert sanitized2 == "another_sub_command" |
| 86 | + assert get_original_path(sanitized2) == original2 |
| 87 | + |
| 88 | + |
| 89 | +def test_scan_click_command_mapping(): |
| 90 | + """Test that scan_click_command correctly maps sanitized names to original paths.""" |
| 91 | + import click |
| 92 | + |
| 93 | + from click_mcp.scanner import scan_click_command |
| 94 | + |
| 95 | + # Create a simple Click command group with nested commands |
| 96 | + @click.group() |
| 97 | + def cli(): |
| 98 | + pass |
| 99 | + |
| 100 | + @cli.command() |
| 101 | + def simple(): |
| 102 | + pass |
| 103 | + |
| 104 | + @cli.group() |
| 105 | + def nested(): |
| 106 | + pass |
| 107 | + |
| 108 | + @nested.command() |
| 109 | + def command(): |
| 110 | + pass |
| 111 | + |
| 112 | + @nested.command(name="with-dash") |
| 113 | + def with_dash(): |
| 114 | + pass |
| 115 | + |
| 116 | + @nested.command(name="under_score") |
| 117 | + def under_score(): |
| 118 | + pass |
| 119 | + |
| 120 | + # Clear the mapping before scanning |
| 121 | + _original_paths.clear() |
| 122 | + |
| 123 | + # Scan the command group |
| 124 | + scan_click_command(cli) |
| 125 | + |
| 126 | + # Check that the mapping contains the expected entries |
| 127 | + assert "simple" in _original_paths.values() |
| 128 | + assert "nested.command" in _original_paths.values() |
| 129 | + assert "nested.with-dash" in _original_paths.values() |
| 130 | + assert "nested.under_score" in _original_paths.values() |
| 131 | + |
| 132 | + # Check that the sanitized names map to the correct original paths |
| 133 | + for sanitized, original in _original_paths.items(): |
| 134 | + if original == "nested.with-dash": |
| 135 | + assert sanitized == "nested_with_dash" |
| 136 | + elif original == "nested.under_score": |
| 137 | + assert sanitized == "nested_under_score" |
| 138 | + elif original == "nested.command": |
| 139 | + assert sanitized == "nested_command" |
0 commit comments