Coverage for tests / unit / utils / native_parsers / test_pyproject_tools.py: 100%

21 statements  

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

1"""Tests for _load_native_tool_config with pyproject.toml tools.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6from unittest.mock import patch 

7 

8import pytest 

9from assertpy import assert_that 

10 

11from lintro.utils.native_parsers import _load_native_tool_config 

12 

13 

14def test_load_native_tool_config_unknown_tool() -> None: 

15 """Return empty dict for unrecognized tool names.""" 

16 result = _load_native_tool_config("unknown_tool") 

17 assert_that(result).is_empty() 

18 

19 

20@pytest.mark.parametrize( 

21 ("tool_name", "tool_config"), 

22 [ 

23 ("ruff", {"line-length": 100, "select": ["E", "F"]}), 

24 ("black", {"line-length": 88}), 

25 ("bandit", {"exclude_dirs": ["tests"]}), 

26 ], 

27 ids=["ruff_config", "black_config", "bandit_config"], 

28) 

29def test_load_native_tool_config_from_pyproject( 

30 tool_name: str, 

31 tool_config: dict[str, Any], 

32) -> None: 

33 """Load tool config from pyproject.toml tool section. 

34 

35 Args: 

36 tool_name: Name of the tool to load config for. 

37 tool_config: Expected configuration dictionary. 

38 """ 

39 with patch("lintro.utils.config.load_pyproject") as mock_load: 

40 mock_load.return_value = {"tool": {tool_name: tool_config}} 

41 result = _load_native_tool_config(tool_name) 

42 assert_that(result).is_equal_to(tool_config) 

43 

44 

45@pytest.mark.parametrize( 

46 ("pyproject_content", "description"), 

47 [ 

48 ({"tool": "not a dict"}, "tool_section_not_dict"), 

49 ({"tool": {"ruff": "invalid"}}, "tool_config_not_dict"), 

50 ], 

51 ids=["tool_section_not_dict", "specific_tool_config_not_dict"], 

52) 

53def test_load_native_tool_config_invalid_pyproject_structure( 

54 pyproject_content: dict[str, Any], 

55 description: str, 

56) -> None: 

57 """Return empty dict when pyproject.toml has invalid structure. 

58 

59 Args: 

60 pyproject_content: Invalid pyproject.toml content structure. 

61 description: Description of the invalid structure. 

62 """ 

63 with patch("lintro.utils.config.load_pyproject") as mock_load: 

64 mock_load.return_value = pyproject_content 

65 result = _load_native_tool_config("ruff") 

66 assert_that(result).is_empty()