From c5e89f6974743ac72f9d3cb43d2587b84dfe2932 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 8 May 2026 21:15:37 +0000 Subject: [PATCH] Add tests for ha_launcher_final.py including PermissionError fallback logic. This commit introduces a new test suite for `src/ha_launcher_final.py`. The tests cover: - Successful installation in system directories. - Fallback to user home directories when system directories are not writable (PermissionError). - Handling of subprocess errors during Home Assistant launch. An `__init__.py` file was added to the `src` directory to enable proper package imports in the tests. Co-authored-by: Bwillou1 <162920784+Bwillou1@users.noreply.github.com> --- src/__init__.py | 0 tests/test_ha_launcher_final.py | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/__init__.py create mode 100644 tests/test_ha_launcher_final.py diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_ha_launcher_final.py b/tests/test_ha_launcher_final.py new file mode 100644 index 0000000..7c7d2ac --- /dev/null +++ b/tests/test_ha_launcher_final.py @@ -0,0 +1,89 @@ +import sys +import subprocess +from pathlib import Path +from unittest.mock import patch, MagicMock +import pytest + +# Ensure src is in sys.path +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + +from src.ha_launcher_final import main + +@patch("src.ha_launcher_final.subprocess.run") +@patch("src.ha_launcher_final.print") +def test_successful_system_install(mock_print, mock_run): + """Test successful installation in system directories.""" + with patch("src.ha_launcher_final.Path.mkdir") as mock_mkdir, \ + patch("src.ha_launcher_final.Path.exists") as mock_exists, \ + patch("src.ha_launcher_final.Path.write_text") as mock_write_text: + + mock_exists.return_value = False + # We want to make sure it doesn't raise PermissionError + mock_mkdir.return_value = None + + main() + + # Verify system paths were used + expected_system_config = "/Library/Application Support/HomeAssistant/config" + + args, _ = mock_run.call_args + cmd = args[0] + assert "--config" in cmd + config_index = cmd.index("--config") + assert cmd[config_index + 1] == expected_system_config + + mock_print.assert_any_call("✓ Répertoires et configuration prêts") + assert mock_mkdir.call_count == 2 + mock_write_text.assert_called_once() + +@patch("src.ha_launcher_final.subprocess.run") +@patch("src.ha_launcher_final.print") +def test_permission_error_fallback(mock_print, mock_run): + """Test fallback to user directories when system directories are not writable.""" + with patch("src.ha_launcher_final.Path.mkdir") as mock_mkdir, \ + patch("src.ha_launcher_final.Path.home") as mock_home: + + # Mock home directory + fake_home = Path("/tmp/fakehome") + mock_home.return_value = fake_home + + # First call to mkdir (system config) raises PermissionError + mock_mkdir.side_effect = [PermissionError(), None, None] + + main() + + # Verify fallback paths were used in subprocess.run + expected_user_config = str(fake_home / "Library/Application Support/HomeAssistant/config") + expected_user_log = str(fake_home / "Library/Logs/HomeAssistant" / "home-assistant.log") + + args, _ = mock_run.call_args + cmd = args[0] + + assert "--config" in cmd + config_index = cmd.index("--config") + assert cmd[config_index + 1] == expected_user_config + + assert "--log-file" in cmd + log_index = cmd.index("--log-file") + assert cmd[log_index + 1] == expected_user_log + + mock_print.assert_any_call("✓ Répertoires utilisateur créés") + # 1 call for system config (fails), then 2 calls for user config and user log + assert mock_mkdir.call_count == 3 + +@patch("src.ha_launcher_final.subprocess.run") +@patch("src.ha_launcher_final.print") +@patch("src.ha_launcher_final.sys.exit") +def test_subprocess_error(mock_exit, mock_print, mock_run): + """Test handling of subprocess error.""" + with patch("src.ha_launcher_final.Path.mkdir") as mock_mkdir, \ + patch("src.ha_launcher_final.Path.exists") as mock_exists: + + mock_exists.return_value = True + mock_run.side_effect = subprocess.CalledProcessError(1, 'cmd') + + main() + + mock_print.assert_any_call("Erreur lancement Home Assistant: Command 'cmd' returned non-zero exit status 1.") + mock_exit.assert_called_once_with(1)