Coverage for tests / unit / parsers / gitleaks_parser / test_edge_cases.py: 100%

27 statements  

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

1"""Unit tests for gitleaks parser edge cases.""" 

2 

3from __future__ import annotations 

4 

5import json 

6 

7from assertpy import assert_that 

8 

9from lintro.parsers.gitleaks.gitleaks_parser import parse_gitleaks_output 

10 

11 

12def test_parse_gitleaks_empty_array() -> None: 

13 """Empty array should return no issues.""" 

14 issues = parse_gitleaks_output(output="[]") 

15 assert_that(issues).is_empty() 

16 

17 

18def test_parse_gitleaks_empty_string() -> None: 

19 """Empty string should return no issues.""" 

20 issues = parse_gitleaks_output(output="") 

21 assert_that(issues).is_empty() 

22 

23 

24def test_parse_gitleaks_none_input() -> None: 

25 """None input should return no issues.""" 

26 issues = parse_gitleaks_output(output=None) 

27 assert_that(issues).is_empty() 

28 

29 

30def test_parse_gitleaks_whitespace_only() -> None: 

31 """Whitespace-only input should return no issues.""" 

32 issues = parse_gitleaks_output(output=" \n\t ") 

33 assert_that(issues).is_empty() 

34 

35 

36def test_parse_gitleaks_invalid_json() -> None: 

37 """Invalid JSON should return empty list and log warning.""" 

38 issues = parse_gitleaks_output(output="not valid json") 

39 assert_that(issues).is_empty() 

40 

41 

42def test_parse_gitleaks_non_array_json() -> None: 

43 """Non-array JSON should return empty list and log warning.""" 

44 issues = parse_gitleaks_output(output='{"not": "an array"}') 

45 assert_that(issues).is_empty() 

46 

47 

48def test_parse_gitleaks_handles_malformed_finding_gracefully() -> None: 

49 """Malformed findings should be skipped.""" 

50 sample_output = json.dumps( 

51 [ 

52 None, 

53 42, 

54 {"File": "", "StartLine": 10}, # Empty file 

55 {"StartLine": 10}, # Missing File 

56 { 

57 "File": "valid.py", 

58 "StartLine": 5, 

59 "RuleID": "test-rule", 

60 "Description": "Valid finding", 

61 }, 

62 ], 

63 ) 

64 

65 issues = parse_gitleaks_output(output=sample_output) 

66 

67 # Only the last valid finding should be parsed 

68 assert_that(issues).is_length(1) 

69 assert_that(issues[0].file).is_equal_to("valid.py")