Coverage for tests / unit / parsers / test_yamllint_parser.py: 100%

53 statements  

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

1"""Unit tests for yamllint parser.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6from assertpy import assert_that 

7 

8from lintro.parsers.yamllint.yamllint_parser import parse_yamllint_output 

9 

10 

11@pytest.mark.parametrize( 

12 "output", 

13 [ 

14 "", 

15 " \n \n ", 

16 ], 

17 ids=["empty", "whitespace_only"], 

18) 

19def test_parse_yamllint_output_returns_empty_for_no_content(output: str) -> None: 

20 """Parse empty or whitespace-only output returns empty list. 

21 

22 Args: 

23 output: The yamllint output string to parse. 

24 """ 

25 result = parse_yamllint_output(output) 

26 assert_that(result).is_empty() 

27 

28 

29@pytest.mark.parametrize( 

30 "level,output_line", 

31 [ 

32 ("error", "config.yml:10:5: [error] trailing spaces (trailing-spaces)"), 

33 ( 

34 "warning", 

35 'test.yml:3:1: [warning] missing document start "---" (document-start)', 

36 ), 

37 ], 

38) 

39def test_parse_yamllint_output_severity_levels(level: str, output_line: str) -> None: 

40 """Parse issues with different severity levels. 

41 

42 Args: 

43 level: The expected severity level. 

44 output_line: The yamllint output line to parse. 

45 """ 

46 result = parse_yamllint_output(output_line) 

47 assert_that(result).is_length(1) 

48 assert_that(result[0].level.value.lower()).is_equal_to(level) 

49 

50 

51def test_parse_yamllint_output_extracts_all_fields() -> None: 

52 """Parse error-level issue extracts all fields correctly.""" 

53 output = "config.yml:10:5: [error] trailing spaces (trailing-spaces)" 

54 result = parse_yamllint_output(output) 

55 assert_that(result).is_length(1) 

56 assert_that(result[0].file).is_equal_to("config.yml") 

57 assert_that(result[0].line).is_equal_to(10) 

58 assert_that(result[0].column).is_equal_to(5) 

59 assert_that(result[0].message).is_equal_to("trailing spaces") 

60 assert_that(result[0].rule).is_equal_to("trailing-spaces") 

61 

62 

63def test_parse_yamllint_output_multiple_issues() -> None: 

64 """Parse multiple issues.""" 

65 output = """config.yml:5:10: [error] trailing spaces (trailing-spaces) 

66config.yml:10:1: [warning] missing document start (document-start) 

67other.yml:3:15: [error] line too long (line-length)""" 

68 result = parse_yamllint_output(output) 

69 assert_that(result).is_length(3) 

70 assert_that(result[0].file).is_equal_to("config.yml") 

71 assert_that(result[1].line).is_equal_to(10) 

72 assert_that(result[2].file).is_equal_to("other.yml") 

73 

74 

75def test_parse_yamllint_output_non_matching_lines_ignored() -> None: 

76 """Non-matching lines are ignored.""" 

77 output = """Some header text 

78config.yml:5:10: [error] trailing spaces (trailing-spaces) 

79Other random output""" 

80 result = parse_yamllint_output(output) 

81 assert_that(result).is_length(1) 

82 

83 

84def test_parse_yamllint_output_issue_without_rule() -> None: 

85 """Parse issue without rule in parentheses.""" 

86 output = "config.yml:5:10: [error] trailing spaces" 

87 result = parse_yamllint_output(output) 

88 assert_that(result).is_length(1) 

89 assert_that(result[0].rule).is_none() 

90 

91 

92def test_parse_yamllint_output_complex_message() -> None: 

93 """Parse issue with complex message.""" 

94 output = "test.yml:11:81: [error] line too long (149 > 80 characters) (line-length)" 

95 result = parse_yamllint_output(output) 

96 assert_that(result).is_length(1) 

97 assert_that(result[0].message).contains("line too long") 

98 

99 

100def test_parse_yamllint_output_blank_lines_between_issues() -> None: 

101 """Handle blank lines between issues.""" 

102 output = """config.yml:5:10: [error] error one (rule1) 

103 

104config.yml:10:1: [warning] warning one (rule2)""" 

105 result = parse_yamllint_output(output) 

106 assert_that(result).is_length(2) 

107 

108 

109def test_parse_yamllint_output_ansi_codes_stripped() -> None: 

110 """Strip ANSI escape codes from output for consistent CI/local parsing.""" 

111 # Output with ANSI color codes (common in CI environments) 

112 output = "\x1b[31mconfig.yml:10:5: [error] trailing spaces (trailing-spaces)\x1b[0m" 

113 result = parse_yamllint_output(output) 

114 assert_that(result).is_length(1) 

115 assert_that(result[0].file).is_equal_to("config.yml") 

116 assert_that(result[0].rule).is_equal_to("trailing-spaces")