|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import MagicMock, mock_open, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from piny import ValidationError |
| 6 | + |
| 7 | +from gitea_github_sync.config import Config, config_file_location, load_config |
| 8 | + |
| 9 | +VALID_CONFIG_FILE = """ |
| 10 | +github_token: some-github-token |
| 11 | +gitea_token: some-gitea-token |
| 12 | +""" |
| 13 | + |
| 14 | +VALID_CONFIG = Config(github_token="some-github-token", gitea_token="some-gitea-token") |
| 15 | + |
| 16 | +DEFAULT_CONFIG_FILE_PATH = config_file_location() |
| 17 | + |
| 18 | + |
| 19 | +@patch("builtins.open", new_callable=mock_open, read_data=VALID_CONFIG_FILE) |
| 20 | +def test_load_config(mock_file_open: MagicMock) -> None: |
| 21 | + config = load_config() |
| 22 | + |
| 23 | + assert config == VALID_CONFIG |
| 24 | + mock_file_open.assert_called_once_with(DEFAULT_CONFIG_FILE_PATH) |
| 25 | + |
| 26 | + |
| 27 | +@patch("builtins.open", new_callable=mock_open, read_data=VALID_CONFIG_FILE) |
| 28 | +def test_load_config_non_default_path(mock_file_open: MagicMock) -> None: |
| 29 | + other_path = Path("/config.yml") |
| 30 | + config = load_config(config_location=other_path) |
| 31 | + |
| 32 | + assert config == VALID_CONFIG |
| 33 | + mock_file_open.assert_called_once_with(other_path) |
| 34 | + |
| 35 | + |
| 36 | +@patch("builtins.open", new_callable=mock_open, read_data="bad-file") |
| 37 | +def test_load_config_bad_file(mock_file_open: MagicMock) -> None: |
| 38 | + with pytest.raises(ValidationError): |
| 39 | + load_config() |
| 40 | + |
| 41 | + mock_file_open.assert_called_once_with(DEFAULT_CONFIG_FILE_PATH) |
0 commit comments