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

24 statements  

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

1"""Tests for Black parser utilities.""" 

2 

3from __future__ import annotations 

4 

5from assertpy import assert_that 

6 

7from lintro.parsers.black.black_parser import parse_black_output 

8 

9 

10def test_parse_black_output_would_reformat_single_file() -> None: 

11 """Parse a single-file 'would reformat' message into one issue.""" 

12 output = ( 

13 "would reformat src/app.py\nAll done! 💥 💔 💥\n1 file would be reformatted." 

14 ) 

15 issues = parse_black_output(output) 

16 assert_that(issues).is_length(1) 

17 assert_that(issues[0].file).ends_with("src/app.py") 

18 assert_that(issues[0].message).contains("Would reformat") 

19 

20 

21def test_parse_black_output_reformatted_multiple_files() -> None: 

22 """Parse multi-file 'reformatted' output into per-file issues.""" 

23 output = ( 

24 "reformatted a.py\nreformatted b.py\nAll done! ✨ 🍰 ✨\n2 files reformatted" 

25 ) 

26 issues = parse_black_output(output) 

27 files = {i.file for i in issues} 

28 assert_that(files).is_equal_to({"a.py", "b.py"}) 

29 assert_that(all("Reformatted" in i.message for i in issues)).is_true() 

30 

31 

32def test_parse_black_output_no_issues() -> None: 

33 """Return empty list when Black reports no issues.""" 

34 output = "All done! ✨ 🍰 ✨\n1 file left unchanged." 

35 issues = parse_black_output(output) 

36 assert_that(issues).is_equal_to([]) 

37 

38 

39def test_parse_black_output_ansi_codes_stripped() -> None: 

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

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

42 output = "\x1b[1mwould reformat src/app.py\x1b[0m\n1 file would be reformatted." 

43 issues = parse_black_output(output) 

44 assert_that(issues).is_length(1) 

45 assert_that(issues[0].file).ends_with("src/app.py")