Coverage for tests / unit / tools / gitleaks / test_error_handling.py: 100%

26 statements  

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

1"""Unit tests for gitleaks plugin error handling and edge cases.""" 

2 

3from __future__ import annotations 

4 

5import subprocess 

6from pathlib import Path 

7from unittest.mock import patch 

8 

9import pytest 

10from assertpy import assert_that 

11 

12from lintro.tools.definitions.gitleaks import GitleaksPlugin 

13 

14 

15def test_check_with_timeout( 

16 gitleaks_plugin: GitleaksPlugin, 

17 tmp_path: Path, 

18) -> None: 

19 """Check handles timeout correctly. 

20 

21 Args: 

22 gitleaks_plugin: The GitleaksPlugin instance to test. 

23 tmp_path: Temporary directory path for test files. 

24 """ 

25 test_file = tmp_path / "test_module.py" 

26 test_file.write_text('"""Test module."""\n') 

27 

28 with patch.object( 

29 gitleaks_plugin, 

30 "_run_subprocess", 

31 side_effect=subprocess.TimeoutExpired(cmd=["gitleaks"], timeout=60), 

32 ): 

33 result = gitleaks_plugin.check([str(test_file)], {}) 

34 

35 assert_that(result.success).is_false() 

36 assert_that(result.output).contains("timed out") 

37 

38 

39def test_check_with_execution_failure( 

40 gitleaks_plugin: GitleaksPlugin, 

41 tmp_path: Path, 

42) -> None: 

43 """Check handles execution failure correctly. 

44 

45 Args: 

46 gitleaks_plugin: The GitleaksPlugin instance to test. 

47 tmp_path: Temporary directory path for test files. 

48 """ 

49 test_file = tmp_path / "test_module.py" 

50 test_file.write_text('"""Test module."""\n') 

51 

52 with patch.object( 

53 gitleaks_plugin, 

54 "_run_subprocess", 

55 side_effect=OSError("Failed to execute gitleaks"), 

56 ): 

57 result = gitleaks_plugin.check([str(test_file)], {}) 

58 

59 assert_that(result.success).is_false() 

60 assert_that(result.output).contains("Gitleaks failed") 

61 

62 

63def test_fix_raises_not_implemented_error( 

64 gitleaks_plugin: GitleaksPlugin, 

65 tmp_path: Path, 

66) -> None: 

67 """Fix method raises NotImplementedError. 

68 

69 Args: 

70 gitleaks_plugin: The GitleaksPlugin instance to test. 

71 tmp_path: Temporary directory path for test files. 

72 """ 

73 test_file = tmp_path / "test_module.py" 

74 test_file.write_text('API_KEY = "secret123"\n') 

75 

76 with pytest.raises(NotImplementedError, match="cannot automatically fix"): 

77 gitleaks_plugin.fix([str(test_file)], {})