|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test script to verify path resolution works correctly on different platforms. |
| 3 | +
|
| 4 | +This script tests that pymode/utils.py patch_paths() function correctly |
| 5 | +resolves paths for required submodules on different operating systems. |
| 6 | +
|
| 7 | +Note: This script tests the path resolution logic without requiring Vim, |
| 8 | +since patch_paths() requires vim module at runtime. |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import platform |
| 14 | + |
| 15 | +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 | +PROJECT_ROOT = os.path.dirname(SCRIPT_DIR) |
| 17 | +PYMODE_DIR = os.path.join(PROJECT_ROOT, 'pymode') |
| 18 | +SUBMODULES_DIR = os.path.join(PROJECT_ROOT, 'submodules') |
| 19 | + |
| 20 | + |
| 21 | +def test_path_resolution_logic(): |
| 22 | + """Test the path resolution logic used by patch_paths().""" |
| 23 | + |
| 24 | + |
| 25 | +def test_path_resolution_logic(): |
| 26 | + """Test the path resolution logic used by patch_paths().""" |
| 27 | + print("=" * 70) |
| 28 | + print("Path Resolution Test") |
| 29 | + print("=" * 70) |
| 30 | + print(f"Platform: {platform.system()} {platform.release()}") |
| 31 | + print(f"Python version: {sys.version.split()[0]}") |
| 32 | + print(f"Python executable: {sys.executable}") |
| 33 | + print() |
| 34 | + |
| 35 | + # Simulate patch_paths() logic |
| 36 | + print("Simulating patch_paths() logic...") |
| 37 | + dir_script = PYMODE_DIR |
| 38 | + dir_submodule = os.path.abspath(os.path.join(dir_script, '..', 'submodules')) |
| 39 | + |
| 40 | + print(f"Pymode directory: {dir_script}") |
| 41 | + print(f"Submodules directory: {dir_submodule}") |
| 42 | + print() |
| 43 | + |
| 44 | + # Required submodules (from patch_paths() logic) |
| 45 | + required_submodules = ['rope', 'tomli', 'pytoolconfig'] |
| 46 | + |
| 47 | + print("Checking required submodules:") |
| 48 | + print("-" * 70) |
| 49 | + |
| 50 | + all_found = True |
| 51 | + paths_to_add = [] |
| 52 | + |
| 53 | + for module in required_submodules: |
| 54 | + module_full_path = os.path.join(dir_submodule, module) |
| 55 | + exists = os.path.exists(module_full_path) |
| 56 | + |
| 57 | + # Simulate the check from patch_paths() |
| 58 | + if exists and module_full_path not in sys.path: |
| 59 | + paths_to_add.append(module_full_path) |
| 60 | + status = "✓" |
| 61 | + elif exists: |
| 62 | + status = "⚠" # Already in path |
| 63 | + paths_to_add.append(module_full_path) |
| 64 | + else: |
| 65 | + status = "✗" |
| 66 | + |
| 67 | + print(f"{status} {module:15} | Exists: {str(exists):5} | Path: {module_full_path}") |
| 68 | + |
| 69 | + if not exists: |
| 70 | + print(f" ERROR: Module directory not found!") |
| 71 | + all_found = False |
| 72 | + |
| 73 | + print() |
| 74 | + |
| 75 | + # Check for removed submodules (should NOT exist or be added) |
| 76 | + removed_submodules = [ |
| 77 | + 'pyflakes', 'pycodestyle', 'mccabe', 'pylint', |
| 78 | + 'pydocstyle', 'pylama', 'autopep8', 'snowball_py', |
| 79 | + 'toml', 'appdirs', 'astroid' |
| 80 | + ] |
| 81 | + |
| 82 | + print("\nChecking removed submodules (should NOT be added to paths):") |
| 83 | + print("-" * 70) |
| 84 | + |
| 85 | + removed_found = False |
| 86 | + for module in removed_submodules: |
| 87 | + module_path = os.path.join(dir_submodule, module) |
| 88 | + exists = os.path.exists(module_path) |
| 89 | + |
| 90 | + # Check if it would be added (it shouldn't be in required_submodules) |
| 91 | + if module in required_submodules: |
| 92 | + print(f"✗ {module:15} | ERROR: Still in required_submodules list!") |
| 93 | + removed_found = True |
| 94 | + elif exists: |
| 95 | + print(f"⚠ {module:15} | WARNING: Directory still exists (should be removed)") |
| 96 | + else: |
| 97 | + print(f"✓ {module:15} | Correctly excluded") |
| 98 | + |
| 99 | + if not removed_found: |
| 100 | + print("\n✓ All removed submodules correctly excluded from path resolution") |
| 101 | + |
| 102 | + print() |
| 103 | + |
| 104 | + # Platform-specific path handling test |
| 105 | + print("\nPlatform-specific path handling:") |
| 106 | + print("-" * 70) |
| 107 | + is_windows = sys.platform == 'win32' or sys.platform == 'msys' |
| 108 | + if is_windows: |
| 109 | + print("✓ Windows platform detected - using Windows-specific path handling") |
| 110 | + print(" (patch_paths() only adds submodules on Windows)") |
| 111 | + else: |
| 112 | + print(f"✓ Unix-like platform ({sys.platform}) - using standard path handling") |
| 113 | + print(" (patch_paths() only adds submodules on Windows)") |
| 114 | + print(" Note: On Unix, submodules are accessed via pymode/libs") |
| 115 | + |
| 116 | + # Test path separators |
| 117 | + print("\nPath separator test:") |
| 118 | + print("-" * 70) |
| 119 | + for module in required_submodules: |
| 120 | + path = os.path.join(dir_submodule, module) |
| 121 | + if os.path.exists(path): |
| 122 | + # os.path.join handles separators correctly for platform |
| 123 | + normalized = os.path.normpath(path) |
| 124 | + print(f"✓ {module:15} | Normalized: {normalized[:60]}...") |
| 125 | + |
| 126 | + print() |
| 127 | + print("=" * 70) |
| 128 | + |
| 129 | + # Summary |
| 130 | + if all_found and not removed_found: |
| 131 | + print("RESULT: ✓ All path resolution tests passed!") |
| 132 | + print(f"\nWould add {len(paths_to_add)} path(s) to sys.path:") |
| 133 | + for p in paths_to_add: |
| 134 | + print(f" - {p}") |
| 135 | + return 0 |
| 136 | + else: |
| 137 | + print("RESULT: ✗ Some path resolution tests failed!") |
| 138 | + return 1 |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + exit_code = test_path_resolution_logic() |
| 143 | + sys.exit(exit_code) |
| 144 | + |
0 commit comments