Coverage for tests / unit / tools / prettier / test_config_discovery.py: 100%

46 statements  

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

1"""Tests for Prettier config discovery methods.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6from unittest.mock import MagicMock, patch 

7 

8from assertpy import assert_that 

9 

10if TYPE_CHECKING: 

11 from lintro.tools.definitions.prettier import PrettierPlugin 

12 

13 

14# Tests for _find_prettier_config method 

15 

16 

17def test_find_prettier_config_not_found(prettier_plugin: PrettierPlugin) -> None: 

18 """Returns None when no config file exists. 

19 

20 Args: 

21 prettier_plugin: The PrettierPlugin instance to test. 

22 """ 

23 with patch("os.path.exists", return_value=False): 

24 result = prettier_plugin._find_prettier_config(search_dir="/nonexistent") 

25 assert_that(result).is_none() 

26 

27 

28def test_find_prettier_config_found_prettierrc(prettier_plugin: PrettierPlugin) -> None: 

29 """Returns path when .prettierrc exists. 

30 

31 Args: 

32 prettier_plugin: The PrettierPlugin instance to test. 

33 """ 

34 

35 def mock_exists(path: str) -> bool: 

36 return path.endswith(".prettierrc") 

37 

38 with patch("os.path.exists", side_effect=mock_exists): 

39 with patch("os.getcwd", return_value="/project"): 

40 with patch( 

41 "os.path.abspath", 

42 side_effect=lambda p: p if p.startswith("/") else f"/project/{p}", 

43 ): 

44 result = prettier_plugin._find_prettier_config(search_dir="/project") 

45 assert_that(result).is_not_none() 

46 assert_that(result).contains(".prettierrc") 

47 

48 

49# Tests for _find_prettierignore method 

50 

51 

52def test_find_prettierignore_not_found(prettier_plugin: PrettierPlugin) -> None: 

53 """Returns None when no .prettierignore file exists. 

54 

55 Args: 

56 prettier_plugin: The PrettierPlugin instance to test. 

57 """ 

58 with patch("os.path.exists", return_value=False): 

59 result = prettier_plugin._find_prettierignore(search_dir="/nonexistent") 

60 assert_that(result).is_none() 

61 

62 

63def test_find_prettierignore_found(prettier_plugin: PrettierPlugin) -> None: 

64 """Returns path when .prettierignore exists. 

65 

66 Args: 

67 prettier_plugin: The PrettierPlugin instance to test. 

68 """ 

69 

70 def mock_exists(path: str) -> bool: 

71 return path.endswith(".prettierignore") 

72 

73 with patch("os.path.exists", side_effect=mock_exists): 

74 with patch("os.getcwd", return_value="/project"): 

75 with patch( 

76 "os.path.abspath", 

77 side_effect=lambda p: p if p.startswith("/") else f"/project/{p}", 

78 ): 

79 result = prettier_plugin._find_prettierignore(search_dir="/project") 

80 assert_that(result).is_not_none() 

81 assert_that(result).contains(".prettierignore") 

82 

83 

84# Tests for _build_config_args with builtin defaults 

85 

86 

87def test_build_config_args_returns_args_when_builtin_defaults_exist( 

88 prettier_plugin: PrettierPlugin, 

89) -> None: 

90 """Should return config args when TOOL_BUILTIN_DEFAULTS has prettier entry. 

91 

92 Args: 

93 prettier_plugin: The PrettierPlugin instance to test. 

94 """ 

95 mock_config = MagicMock() 

96 mock_config.enforce.line_length = None 

97 mock_config.enforce.target_python = None 

98 mock_config.get_tool_defaults.return_value = {} 

99 

100 with patch( 

101 "lintro.tools.core.config_injection._get_lintro_config", 

102 return_value=mock_config, 

103 ): 

104 with patch( 

105 "lintro.tools.core.config_injection.generate_defaults_config", 

106 ) as mock_gen: 

107 from pathlib import Path 

108 

109 mock_gen.return_value = Path("/tmp/test.json") 

110 args = prettier_plugin._build_config_args() 

111 

112 # Should have generated args since builtin defaults exist for prettier 

113 assert_that(args).contains("--no-config") 

114 assert_that(args).contains("--config") 

115 # Verify correct ordering: --no-config before --config <path> 

116 no_config_idx = args.index("--no-config") 

117 config_idx = args.index("--config") 

118 assert_that(no_config_idx).is_less_than(config_idx) 

119 # The path should follow --config 

120 assert_that(args[config_idx + 1]).is_equal_to("/tmp/test.json")