|
| 1 | +""" |
| 2 | +Integration test examples showing how to configure pylsp-rope rename functionality. |
| 3 | +
|
| 4 | +This file demonstrates the configuration patterns that users should follow |
| 5 | +to enable and verify pylsp-rope's rename feature. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pylsp_rope import typing |
| 10 | +from pylsp_rope.plugin import pylsp_rename, pylsp_settings |
| 11 | +from pylsp_rope.text import Position |
| 12 | +from test.conftest import create_document |
| 13 | +from test.helpers import assert_text_edits |
| 14 | + |
| 15 | + |
| 16 | +class TestRenameConfigurationExamples: |
| 17 | + """Examples showing proper configuration of pylsp-rope rename.""" |
| 18 | + |
| 19 | + def test_default_configuration_disables_rename(self, config, workspace): |
| 20 | + """Test that rename is disabled by default.""" |
| 21 | + # Get default settings |
| 22 | + settings = pylsp_settings() |
| 23 | + assert settings["plugins"]["pylsp_rope"]["rename"] is False |
| 24 | + |
| 25 | + # Verify rename returns None when disabled |
| 26 | + document = create_document(workspace, "simple_rename.py") |
| 27 | + position = Position(0, 0) # Position on "Test1" |
| 28 | + |
| 29 | + response = pylsp_rename(config, workspace, document, position, "NewName") |
| 30 | + assert response is None |
| 31 | + |
| 32 | + def test_enabling_rename_via_configuration(self, config, workspace): |
| 33 | + """Example of enabling rename through configuration.""" |
| 34 | + # Simulate user configuration |
| 35 | + config._plugin_settings["plugins"]["pylsp_rope"] = {"rename": True} |
| 36 | + |
| 37 | + document = create_document(workspace, "simple_rename.py") |
| 38 | + line = 0 |
| 39 | + pos = document.lines[line].index("Test1") |
| 40 | + position = Position(line, pos) |
| 41 | + |
| 42 | + response = pylsp_rename( |
| 43 | + config, workspace, document, position, "ShouldBeRenamed" |
| 44 | + ) |
| 45 | + |
| 46 | + # Rename should now work |
| 47 | + assert response is not None |
| 48 | + assert typing.is_workspace_edit_with_changes(response) |
| 49 | + |
| 50 | + # Verify the rename happened |
| 51 | + changes = response["changes"] |
| 52 | + doc_uri = typing.DocumentUri(document.uri) |
| 53 | + assert doc_uri in changes |
| 54 | + new_text = assert_text_edits(changes[doc_uri], target="simple_rename_result.py") |
| 55 | + assert "class ShouldBeRenamed()" in new_text |
| 56 | + |
| 57 | + def test_configuration_with_other_rename_plugins_disabled(self, config): |
| 58 | + """Example showing complete rename configuration.""" |
| 59 | + settings = pylsp_settings() |
| 60 | + |
| 61 | + # This is the recommended configuration pattern: |
| 62 | + config_dict = { |
| 63 | + "pylsp": { |
| 64 | + "plugins": { |
| 65 | + "pylsp_rope": {"enabled": True, "rename": True}, |
| 66 | + "rope_rename": {"enabled": False}, |
| 67 | + "jedi_rename": {"enabled": False}, |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + # Verify pylsp-rope settings match recommendation |
| 73 | + pylsp_rope_settings = settings["plugins"]["pylsp_rope"] |
| 74 | + assert pylsp_rope_settings["enabled"] is True |
| 75 | + assert ( |
| 76 | + pylsp_rope_settings["rename"] is False |
| 77 | + ) # Default, user should set to True |
| 78 | + |
| 79 | + def test_disabling_rename_after_enabling(self, config, workspace): |
| 80 | + """Test that rename can be dynamically disabled.""" |
| 81 | + # First enable rename |
| 82 | + config._plugin_settings["plugins"]["pylsp_rope"] = {"rename": True} |
| 83 | + |
| 84 | + document = create_document(workspace, "simple_rename.py") |
| 85 | + line = 0 |
| 86 | + pos = document.lines[line].index("Test1") |
| 87 | + position = Position(line, pos) |
| 88 | + |
| 89 | + # Rename should work |
| 90 | + response = pylsp_rename(config, workspace, document, position, "NewName") |
| 91 | + assert response is not None |
| 92 | + |
| 93 | + # Now disable rename |
| 94 | + config._plugin_settings["plugins"]["pylsp_rope"] = {"rename": False} |
| 95 | + |
| 96 | + # Rename should not work |
| 97 | + response = pylsp_rename(config, workspace, document, position, "AnotherName") |
| 98 | + assert response is None |
| 99 | + |
| 100 | + def test_missing_configuration_key_defaults_to_disabled(self, config, workspace): |
| 101 | + """Test that missing configuration key defaults to disabled.""" |
| 102 | + # Remove the rename key entirely |
| 103 | + plugin_settings = config.plugin_settings("pylsp_rope", "test://test.py") |
| 104 | + if "rename" in plugin_settings: |
| 105 | + del plugin_settings["rename"] |
| 106 | + |
| 107 | + document = create_document(workspace, "simple_rename.py") |
| 108 | + position = Position(0, 5) |
| 109 | + |
| 110 | + # Should return None when key is missing |
| 111 | + response = pylsp_rename(config, workspace, document, position, "NewName") |
| 112 | + assert response is None |
0 commit comments