Public Access
DEVX-134: feat: add I/O function isolation check and skip integration tests
This commit was merged in pull request #201.
This commit is contained in:
@@ -566,6 +566,147 @@ class TestKnownHelpers:
|
||||
assert "subprocess" in HELPER_INTERNAL_CALLS["update_doc_versions"]
|
||||
|
||||
|
||||
class TestIOFunctionChecks:
|
||||
"""Tests for unpatched I/O function detection."""
|
||||
|
||||
def test_unpatched_get_pat_violation(self, tmp_path: Path) -> None:
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
from mymodule import get_pat
|
||||
|
||||
class TestExample:
|
||||
def test_calls_get_pat(self) -> None:
|
||||
result = get_pat("staging")
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert len(violations) == 1
|
||||
assert violations[0].category == "unpatched-io"
|
||||
assert "get_pat" in violations[0].message
|
||||
|
||||
def test_patched_get_pat_no_violation(self, tmp_path: Path) -> None:
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from mymodule import get_pat
|
||||
|
||||
class TestExample:
|
||||
@patch("mymodule.get_pat", return_value="pat")
|
||||
def test_patched(self, mock) -> None:
|
||||
result = get_pat("staging")
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert violations == []
|
||||
|
||||
def test_unpatched_load_secrets_violation(self, tmp_path: Path) -> None:
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
from mymodule import load_secrets
|
||||
|
||||
class TestExample:
|
||||
def test_calls_load_secrets(self) -> None:
|
||||
result = load_secrets("staging")
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert len(violations) == 1
|
||||
assert violations[0].category == "unpatched-io"
|
||||
assert "load_secrets" in violations[0].message
|
||||
|
||||
def test_patched_load_secrets_no_violation(self, tmp_path: Path) -> None:
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from mymodule import load_secrets
|
||||
|
||||
class TestExample:
|
||||
@patch("mymodule.load_secrets", return_value={})
|
||||
def test_patched(self, mock) -> None:
|
||||
result = load_secrets("staging")
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert violations == []
|
||||
|
||||
def test_unpatched_requests_get_violation(self, tmp_path: Path) -> None:
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
import requests
|
||||
|
||||
class TestExample:
|
||||
def test_calls_requests(self) -> None:
|
||||
resp = requests.get("https://example.com")
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert len(violations) == 1
|
||||
assert violations[0].category == "unpatched-io"
|
||||
assert "requests.get" in violations[0].message or "get" in violations[0].message
|
||||
|
||||
def test_integration_marker_skips_subprocess(self, tmp_path: Path) -> None:
|
||||
"""@pytest.mark.integration tests should not be flagged for subprocess.run."""
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
import subprocess
|
||||
import pytest
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_real_subprocess():
|
||||
subprocess.run(["echo", "hello"])
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert violations == []
|
||||
|
||||
def test_integration_marker_skips_sleep(self, tmp_path: Path) -> None:
|
||||
"""@pytest.mark.integration tests should not be flagged for time.sleep."""
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
import time
|
||||
import pytest
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_real_sleep():
|
||||
time.sleep(1)
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert violations == []
|
||||
|
||||
def test_integration_marker_with_args_skips(self, tmp_path: Path) -> None:
|
||||
"""@pytest.mark.integration(...) with args should also be skipped."""
|
||||
file = _write_test_file(
|
||||
tmp_path,
|
||||
"""
|
||||
import subprocess
|
||||
import pytest
|
||||
|
||||
@pytest.mark.integration(scope="module")
|
||||
def test_real_subprocess():
|
||||
subprocess.run(["echo", "hello"])
|
||||
""",
|
||||
)
|
||||
violations = analyze_file(file)
|
||||
assert violations == []
|
||||
|
||||
def test_integration_directory_skipped(self, tmp_path: Path) -> None:
|
||||
"""Files in integration/ directories should be skipped entirely."""
|
||||
integration_dir = tmp_path / "integration"
|
||||
integration_dir.mkdir()
|
||||
file = integration_dir / "test_real_io.py"
|
||||
file.write_text("import subprocess\ndef test_real_subprocess():\n subprocess.run(['echo', 'hello'])\n")
|
||||
violations = analyze_file(file)
|
||||
assert violations == []
|
||||
|
||||
|
||||
class TestCli:
|
||||
"""Tests for the standalone CLI interface."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user