mirror of
https://github.com/RichieCahill/dotfiles.git
synced 2026-04-19 05:48:20 -04:00
improved unit tests
This commit is contained in:
@@ -1,37 +1,42 @@
|
|||||||
"""Tests for fix_eval_warnings."""
|
"""test_fix_eval_warnings."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import MagicMock, patch
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import pytest
|
|
||||||
from typer.testing import CliRunner
|
from typer.testing import CliRunner
|
||||||
|
|
||||||
from python.tools.fix_eval_warnings import Config, app, generate_fix, parse_warnings
|
from python.tools.fix_eval_warnings import Config, app, generate_fix, parse_warnings
|
||||||
from tests.conftest import TOKEN
|
from tests.conftest import TOKEN
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pyfakefs.fake_filesystem import FakeFilesystem
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def test_parse_warnings(fs: FakeFilesystem) -> None:
|
||||||
def log_file(tmp_path: Path) -> Path:
|
"""test_parse_warnings."""
|
||||||
"""Create a dummy log file."""
|
log_file = Path("/build.log")
|
||||||
log_path = tmp_path / "build.log"
|
fs.create_file(
|
||||||
log_path.write_text("Some output\nevaluation warning: 'system' is deprecated\nMore output", encoding="utf-8")
|
log_file,
|
||||||
return log_path
|
contents="Some output\nevaluation warning: 'system' is deprecated\nMore output",
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
def test_parse_warnings(log_file: Path) -> None:
|
|
||||||
"""Test parsing warnings from a log file."""
|
|
||||||
warnings = parse_warnings(log_file)
|
warnings = parse_warnings(log_file)
|
||||||
assert len(warnings) == 1
|
assert len(warnings) == 1
|
||||||
assert warnings[0] == "evaluation warning: 'system' is deprecated"
|
assert warnings[0] == "evaluation warning: 'system' is deprecated"
|
||||||
|
|
||||||
|
|
||||||
@patch("python.tools.fix_eval_warnings.requests.post")
|
def test_generate_fix(mocker: MockerFixture) -> None:
|
||||||
def test_generate_fix(mock_post: MagicMock) -> None:
|
"""test_generate_fix."""
|
||||||
"""Test generating a fix."""
|
mock_post = mocker.patch("python.tools.fix_eval_warnings.requests.post")
|
||||||
mock_response = MagicMock()
|
mock_response = mocker.MagicMock()
|
||||||
mock_response.json.return_value = {"choices": [{"message": {"content": "Use stdenv.hostPlatform.system"}}]}
|
mock_response.json.return_value = {
|
||||||
|
"choices": [{"message": {"content": "Use stdenv.hostPlatform.system"}}]
|
||||||
|
}
|
||||||
mock_post.return_value = mock_response
|
mock_post.return_value = mock_response
|
||||||
|
|
||||||
config = Config(github_token=TOKEN)
|
config = Config(github_token=TOKEN)
|
||||||
@@ -41,20 +46,30 @@ def test_generate_fix(mock_post: MagicMock) -> None:
|
|||||||
mock_post.assert_called_once()
|
mock_post.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
@patch("python.tools.fix_eval_warnings.logger")
|
def test_main(mocker: MockerFixture, fs: FakeFilesystem) -> None:
|
||||||
@patch("python.tools.fix_eval_warnings.generate_fix")
|
"""test_main."""
|
||||||
def test_main(mock_generate_fix: MagicMock, mock_logger: MagicMock, log_file: Path) -> None:
|
log_file = Path("/build.log")
|
||||||
"""Test the main CLI."""
|
fs.create_file(
|
||||||
|
log_file,
|
||||||
|
contents="Some output\nevaluation warning: 'system' is deprecated\nMore output",
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_generate_fix = mocker.patch("python.tools.fix_eval_warnings.generate_fix")
|
||||||
mock_generate_fix.return_value = "Fixed it"
|
mock_generate_fix.return_value = "Fixed it"
|
||||||
|
mock_logger = mocker.patch("python.tools.fix_eval_warnings.logger")
|
||||||
|
|
||||||
# We need to mock GITHUB_TOKEN env var or the script will warn/fail
|
# We need to mock GITHUB_TOKEN env var or the script will warn/fail
|
||||||
with patch.dict("os.environ", {"GITHUB_TOKEN": TOKEN}):
|
mocker.patch.dict("os.environ", {"GITHUB_TOKEN": TOKEN})
|
||||||
|
|
||||||
result = runner.invoke(app, [str(log_file)])
|
result = runner.invoke(app, [str(log_file)])
|
||||||
|
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
# Verify logger calls instead of stdout, as CliRunner might not capture logging output correctly
|
# Verify logger calls instead of stdout, as CliRunner might not capture logging output correctly
|
||||||
# when logging is configured to write to sys.stdout directly.
|
# when logging is configured to write to sys.stdout directly.
|
||||||
assert any("Found 1 warnings" in str(call) for call in mock_logger.info.call_args_list)
|
assert any("Found 1 warnings" in str(call) for call in mock_logger.info.call_args_list)
|
||||||
assert any("Fix suggestions written to fix_suggestions.md" in str(call) for call in mock_logger.info.call_args_list)
|
assert any(
|
||||||
|
"Fix suggestions written to fix_suggestions.md" in str(call)
|
||||||
|
for call in mock_logger.info.call_args_list
|
||||||
|
)
|
||||||
assert Path("fix_suggestions.md").exists()
|
assert Path("fix_suggestions.md").exists()
|
||||||
Path("fix_suggestions.md").unlink()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user