from __future__ import annotations import os import tempfile import unittest from pathlib import Path from unittest.mock import patch from gitea_backup.config import load_config class LoadConfigTest(unittest.TestCase): def test_defaults_point_to_local_gitea(self) -> None: env = { "GITEA_TOKEN": "abc", "BACKUP_DIR": "/tmp/gitea-backups", "GITEA_BACKUP_SKIP_DOTENV": "1", } with patch.dict(os.environ, env, clear=True): cfg = load_config() self.assertEqual(cfg.gitea_url, "http://192.168.1.9:3000") self.assertEqual(cfg.backup_at, "03:00") self.assertTrue(cfg.snapshots) self.assertEqual(cfg.keep_days, 30) self.assertEqual(cfg.backup_dir, Path("/tmp/gitea-backups")) def test_relative_backup_dir_is_resolved(self) -> None: with tempfile.TemporaryDirectory() as tmp: env = { "GITEA_TOKEN": "abc", "BACKUP_DIR": "./backups", "GITEA_BACKUP_SKIP_DOTENV": "1", } with patch.dict(os.environ, env, clear=True): with patch("gitea_backup.config.Path.cwd", return_value=Path(tmp)): cfg = load_config() self.assertEqual(cfg.backup_dir, (Path(tmp) / "backups").resolve()) def test_rejects_bad_url(self) -> None: env = { "GITEA_URL": "192.168.1.9", "GITEA_TOKEN": "x", "GITEA_BACKUP_SKIP_DOTENV": "1", } with patch.dict(os.environ, env, clear=True): with self.assertRaises(ValueError): load_config()