Files
dev-env-sync/tests/test_platform.py
7000pctAUTO 09985238fe
Some checks failed
CI / test (push) Has been cancelled
Add tests and example configuration
2026-01-30 04:12:46 +00:00

178 lines
7.8 KiB
Python

"""Unit tests for platform detection."""
import platform
from pathlib import Path
from unittest.mock import patch, MagicMock
import pytest
from dev_env_sync.core.platform import PlatformDetector, Platform, PlatformInfo
class TestPlatformDetector:
"""Tests for PlatformDetector class."""
def test_detect_linux(self, mocker):
"""Test detection of Linux platform."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.0-generic')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
info = PlatformDetector.detect()
assert info.system == "Linux"
assert info.platform == Platform.LINUX
assert info.is_wsl == False
def test_detect_macos(self, mocker):
"""Test detection of macOS platform."""
mocker.patch('platform.system', return_value='Darwin')
mocker.patch('platform.release', return_value='21.4.0')
mocker.patch('platform.version', return_value='Darwin Kernel Version 21.4.0')
mocker.patch('platform.machine', return_value='arm64')
mocker.patch('pathlib.Path.home', return_value=Path('/Users/testuser'))
info = PlatformDetector.detect()
assert info.system == "Darwin"
assert info.platform == Platform.MACOS
assert info.is_wsl == False
def test_detect_wsl(self, mocker):
"""Test detection of WSL platform."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.72-microsoft-standard-WSL2')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
wsl_path = Path('/proc/sys/kernel/osrelease')
wsl_path.parent.mkdir(parents=True, exist_ok=True)
wsl_path.write_text('5.4.72-microsoft-standard-WSL2')
info = PlatformDetector.detect()
assert info.system == "Linux"
assert info.platform == Platform.WSL
assert info.is_wsl == True
def test_is_linux(self, mocker):
"""Test is_linux method."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.0')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
assert PlatformDetector.is_linux() == True
def test_is_macos(self, mocker):
"""Test is_macos method."""
mocker.patch('platform.system', return_value='Darwin')
mocker.patch('platform.release', return_value='21.4.0')
mocker.patch('platform.version', return_value='Darwin Kernel Version 21.4.0')
mocker.patch('platform.machine', return_value='arm64')
mocker.patch('pathlib.Path.home', return_value=Path('/Users/testuser'))
assert PlatformDetector.is_macos() == True
def test_is_wsl(self, mocker):
"""Test is_wsl method."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.72-microsoft-standard-WSL2')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
wsl_path = Path('/proc/sys/kernel/osrelease')
wsl_path.parent.mkdir(parents=True, exist_ok=True)
wsl_path.write_text('5.4.72-microsoft-standard-WSL2')
assert PlatformDetector.is_wsl() == True
def test_get_home_dir(self, mocker):
"""Test get_home_dir method."""
home_path = Path('/home/testuser')
mocker.patch('pathlib.Path.home', return_value=home_path)
result = PlatformDetector.get_home_dir()
assert result == home_path
def test_get_editor_config_dir_vscode_linux(self, mocker):
"""Test getting VS Code config directory on Linux."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.0')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
result = PlatformDetector.get_editor_config_dir("vscode")
assert str(result) == "/home/testuser/.config/Code/User"
def test_get_editor_config_dir_neovim_linux(self, mocker):
"""Test getting Neovim config directory on Linux."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.0')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
result = PlatformDetector.get_editor_config_dir("neovim")
assert str(result) == "/home/testuser/.config/nvim"
def test_get_package_manager_apt(self, mocker):
"""Test getting package manager for apt-based system."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.0')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('platform.mac_ver', return_value=('', '', ''))
with patch('shutil.which', return_value='/usr/bin/apt'):
result = PlatformDetector.get_package_manager()
assert result == "apt"
def test_get_package_manager_brew(self, mocker):
"""Test getting package manager for Homebrew on macOS."""
mocker.patch('platform.system', return_value='Darwin')
mocker.patch('platform.release', return_value='21.4.0')
mocker.patch('platform.version', return_value='Darwin Kernel Version 21.4.0')
mocker.patch('platform.machine', return_value='arm64')
mocker.patch('pathlib.Path.home', return_value=Path('/Users/testuser'))
mocker.patch('platform.mac_ver', return_value=('11.4.0', '', ''))
with patch('shutil.which', return_value='/usr/local/bin/brew'):
result = PlatformDetector.get_package_manager()
assert result == "brew"
def test_normalize_path_absolute(self, mocker):
"""Test normalizing absolute path."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.0')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
result = PlatformDetector.normalize_path('/absolute/path')
assert str(result) == '/absolute/path'
def test_normalize_path_home(self, mocker):
"""Test normalizing path with home directory expansion."""
mocker.patch('platform.system', return_value='Linux')
mocker.patch('platform.release', return_value='5.4.0')
mocker.patch('platform.version', return_value='#1 SMP')
mocker.patch('platform.machine', return_value='x86_64')
mocker.patch('pathlib.Path.home', return_value=Path('/home/testuser'))
result = PlatformDetector.normalize_path('~/config')
assert str(result) == '/home/testuser/config'