Coverage for tests / conftest.py: 76%

38 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Global test configuration for pytest.""" 

2 

3from __future__ import annotations 

4 

5import os 

6import shutil 

7import tempfile 

8from collections.abc import Generator, Iterator 

9from pathlib import Path 

10 

11import pytest 

12from click.testing import CliRunner 

13 

14from lintro.plugins.discovery import discover_all_tools 

15from lintro.utils.path_utils import normalize_file_path_for_display 

16 

17# Ensure stable docker builds under pytest-xdist by disabling BuildKit, which 

18# can be flaky with concurrent builds/tags on some local setups. 

19os.environ.setdefault("DOCKER_BUILDKIT", "0") 

20 

21 

22@pytest.fixture(scope="session", autouse=True) 

23def _discover_tools() -> None: 

24 """Discover and register all tool plugins before tests run. 

25 

26 This ensures that ToolRegistry.get() works in all tests by loading 

27 the builtin tool definitions and any external plugins. 

28 """ 

29 discover_all_tools() 

30 

31 

32"""Shared fixtures used across tests in this repository.""" 

33 

34 

35@pytest.fixture 

36def cli_runner() -> CliRunner: 

37 """Provide a Click CLI runner for testing. 

38 

39 Returns: 

40 CliRunner: CLI runner for invoking commands. 

41 """ 

42 return CliRunner() 

43 

44 

45@pytest.fixture 

46def temp_dir() -> Generator[Path, None, None]: 

47 """Provide a temporary directory for testing. 

48 

49 Yields: 

50 Path: Path to the temporary directory. 

51 """ 

52 with tempfile.TemporaryDirectory() as temp_dir: 

53 yield Path(temp_dir) 

54 

55 

56@pytest.fixture 

57def ruff_violation_file(temp_dir: Path) -> str: 

58 """Copy the ruff_violations.py sample to a temp directory. 

59 

60 return normalized path. 

61 

62 Args: 

63 temp_dir: Temporary directory fixture. 

64 

65 Returns: 

66 str: Normalized path to the copied ruff_violations.py file. 

67 """ 

68 src = Path("test_samples/tools/python/ruff/ruff_e501_f401_violations.py").resolve() 

69 dst = temp_dir / "ruff_violations.py" 

70 shutil.copy(src, dst) 

71 result: str = normalize_file_path_for_display(str(dst)) 

72 return result 

73 

74 

75@pytest.fixture 

76def skip_config_injection(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: 

77 """Skip Lintro config injection for tests. 

78 

79 Sets LINTRO_SKIP_CONFIG_INJECTION environment variable to disable 

80 config injection during tests that need to test native tool configs. 

81 

82 Args: 

83 monkeypatch: Pytest monkeypatch fixture. 

84 

85 Yields: 

86 None: This fixture is used for its side effect only. 

87 """ 

88 monkeypatch.setenv("LINTRO_SKIP_CONFIG_INJECTION", "1") 

89 yield 

90 

91 

92@pytest.fixture(autouse=True) 

93def clear_logging_handlers() -> Iterator[None]: 

94 """Clear logging handlers before each test. 

95 

96 Yields: 

97 None: This fixture is used for its side effect only. 

98 """ 

99 import logging 

100 

101 logging.getLogger().handlers.clear() 

102 yield