Coverage for tests / unit / tools / oxfmt / test_set_options.py: 100%

36 statements  

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

1"""Tests for OxfmtPlugin.set_options() method.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7import pytest 

8from assertpy import assert_that 

9 

10if TYPE_CHECKING: 

11 from lintro.tools.definitions.oxfmt import OxfmtPlugin 

12 

13 

14# ============================================================================= 

15# Tests for config and ignore_path options validation 

16# ============================================================================= 

17 

18 

19def test_config_accepts_string(oxfmt_plugin: OxfmtPlugin) -> None: 

20 """Config option accepts a string value. 

21 

22 Args: 

23 oxfmt_plugin: The OxfmtPlugin instance to test. 

24 """ 

25 oxfmt_plugin.set_options(config=".oxfmtrc.custom.json") 

26 assert_that(oxfmt_plugin.options.get("config")).is_equal_to( 

27 ".oxfmtrc.custom.json", 

28 ) 

29 

30 

31def test_config_rejects_non_string(oxfmt_plugin: OxfmtPlugin) -> None: 

32 """Config option rejects non-string values. 

33 

34 Args: 

35 oxfmt_plugin: The OxfmtPlugin instance to test. 

36 """ 

37 with pytest.raises(ValueError, match="config must be a string"): 

38 # Intentionally passing wrong type to test validation 

39 oxfmt_plugin.set_options(config=123) # type: ignore[arg-type] 

40 

41 

42def test_ignore_path_accepts_string(oxfmt_plugin: OxfmtPlugin) -> None: 

43 """Ignore_path option accepts a string value. 

44 

45 Args: 

46 oxfmt_plugin: The OxfmtPlugin instance to test. 

47 """ 

48 oxfmt_plugin.set_options(ignore_path=".oxfmtignore") 

49 assert_that(oxfmt_plugin.options.get("ignore_path")).is_equal_to( 

50 ".oxfmtignore", 

51 ) 

52 

53 

54def test_ignore_path_rejects_non_string(oxfmt_plugin: OxfmtPlugin) -> None: 

55 """Ignore_path option rejects non-string values. 

56 

57 Args: 

58 oxfmt_plugin: The OxfmtPlugin instance to test. 

59 """ 

60 with pytest.raises(ValueError, match="ignore_path must be a string"): 

61 # Intentionally passing wrong type to test validation 

62 oxfmt_plugin.set_options(ignore_path=True) # type: ignore[arg-type] 

63 

64 

65# ============================================================================= 

66# Tests for setting multiple options 

67# ============================================================================= 

68 

69 

70def test_set_multiple_options(oxfmt_plugin: OxfmtPlugin) -> None: 

71 """Multiple options can be set in a single call. 

72 

73 Args: 

74 oxfmt_plugin: The OxfmtPlugin instance to test. 

75 """ 

76 oxfmt_plugin.set_options( 

77 config=".oxfmtrc.json", 

78 ignore_path=".oxfmtignore", 

79 ) 

80 

81 assert_that(oxfmt_plugin.options.get("config")).is_equal_to(".oxfmtrc.json") 

82 assert_that(oxfmt_plugin.options.get("ignore_path")).is_equal_to(".oxfmtignore") 

83 

84 

85# ============================================================================= 

86# Tests for _build_oxfmt_args(oxfmt_plugin.options) helper method 

87# ============================================================================= 

88 

89 

90def test_build_args_empty_options(oxfmt_plugin: OxfmtPlugin) -> None: 

91 """Empty options returns empty args list. 

92 

93 Args: 

94 oxfmt_plugin: The OxfmtPlugin instance to test. 

95 """ 

96 args = oxfmt_plugin._build_oxfmt_args(oxfmt_plugin.options) 

97 assert_that(args).is_empty() 

98 

99 

100def test_build_args_config_adds_flag(oxfmt_plugin: OxfmtPlugin) -> None: 

101 """Config option adds --config flag. 

102 

103 Args: 

104 oxfmt_plugin: The OxfmtPlugin instance to test. 

105 """ 

106 oxfmt_plugin.set_options(config=".oxfmtrc.json") 

107 args = oxfmt_plugin._build_oxfmt_args(oxfmt_plugin.options) 

108 assert_that(args).contains("--config", ".oxfmtrc.json") 

109 

110 

111def test_build_args_ignore_path_adds_flag(oxfmt_plugin: OxfmtPlugin) -> None: 

112 """Ignore_path option adds --ignore-path flag. 

113 

114 Args: 

115 oxfmt_plugin: The OxfmtPlugin instance to test. 

116 """ 

117 oxfmt_plugin.set_options(ignore_path=".oxfmtignore") 

118 args = oxfmt_plugin._build_oxfmt_args(oxfmt_plugin.options) 

119 assert_that(args).contains("--ignore-path", ".oxfmtignore") 

120 

121 

122def test_build_args_multiple_options_combine(oxfmt_plugin: OxfmtPlugin) -> None: 

123 """Multiple options combine into a single args list. 

124 

125 Args: 

126 oxfmt_plugin: The OxfmtPlugin instance to test. 

127 """ 

128 oxfmt_plugin.set_options( 

129 config=".oxfmtrc.json", 

130 ignore_path=".oxfmtignore", 

131 ) 

132 args = oxfmt_plugin._build_oxfmt_args(oxfmt_plugin.options) 

133 

134 assert_that(args).contains("--config", ".oxfmtrc.json") 

135 assert_that(args).contains("--ignore-path", ".oxfmtignore")