Files
SyncVito/tests/test_autostart.py
2026-09-28 11:08:14 +02:00

102 lines
3.6 KiB
Python

import subprocess
import sys
from pathlib import Path
from unittest.mock import MagicMock
import pytest
import syncvito.autostart as autostart
from syncvito.autostart import generate_task_xml, set_autostart
def test_generate_task_xml_structure() -> None:
cmd = Path("C:/Program Files/SyncVito/SyncVito.exe")
args = "--minimized"
xml = generate_task_xml(cmd, args)
assert '<?xml version="1.0" encoding="UTF-16"?>' in xml
assert "<RunLevel>HighestAvailable</RunLevel>" in xml
assert "<LogonType>InteractiveToken</LogonType>" in xml
assert "<Delay>PT5S</Delay>" in xml
assert "<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>" in xml
assert "<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>" in xml
assert "<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>" in xml
assert "<Command>C:\\Program Files\\SyncVito\\SyncVito.exe</Command>" in xml
assert "<Arguments>--minimized</Arguments>" in xml
def test_set_autostart_non_windows(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(autostart.os, "name", "posix")
assert not set_autostart(True)
assert not set_autostart(False)
def test_set_autostart_enable_scheduled_task_success(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(autostart.os, "name", "nt")
monkeypatch.setattr(sys, "frozen", True, raising=False)
monkeypatch.setattr(sys, "executable", r"C:\Program Files\SyncVito\SyncVito.exe")
mock_run = MagicMock(return_value=subprocess.CompletedProcess(args=[], returncode=0))
monkeypatch.setattr(autostart, "_run_schtasks", mock_run)
deleted_run_key = False
def mock_delete_run_key() -> None:
nonlocal deleted_run_key
deleted_run_key = True
monkeypatch.setattr(autostart, "_delete_run_key_entry", mock_delete_run_key)
result = set_autostart(True)
assert result is True
assert mock_run.called
create_call_args = mock_run.call_args[0][0]
assert create_call_args[0] == "/create"
assert create_call_args[1] == "/tn"
assert create_call_args[2] == "SyncVito"
assert deleted_run_key is True
def test_set_autostart_enable_fallback_registry(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(autostart.os, "name", "nt")
monkeypatch.setattr(sys, "frozen", True, raising=False)
monkeypatch.setattr(sys, "executable", r"C:\Program Files\SyncVito\SyncVito.exe")
# schtasks fails (e.g. non-elevated prompt)
mock_run = MagicMock(return_value=subprocess.CompletedProcess(args=[], returncode=1))
monkeypatch.setattr(autostart, "_run_schtasks", mock_run)
registry_command = None
def mock_set_run_key(cmd: str) -> None:
nonlocal registry_command
registry_command = cmd
monkeypatch.setattr(autostart, "_set_run_key_entry", mock_set_run_key)
result = set_autostart(True)
assert result is False
assert registry_command == r'"C:\Program Files\SyncVito\SyncVito.exe" --minimized'
def test_set_autostart_disable(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(autostart.os, "name", "nt")
mock_run = MagicMock(return_value=subprocess.CompletedProcess(args=[], returncode=0))
monkeypatch.setattr(autostart, "_run_schtasks", mock_run)
deleted_run_key = False
def mock_delete_run_key() -> None:
nonlocal deleted_run_key
deleted_run_key = True
monkeypatch.setattr(autostart, "_delete_run_key_entry", mock_delete_run_key)
result = set_autostart(False)
assert result is True
assert mock_run.called
delete_call_args = mock_run.call_args[0][0]
assert delete_call_args == ["/delete", "/tn", "SyncVito", "/f"]
assert deleted_run_key is True