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
« 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."""
3from __future__ import annotations
5import subprocess
6from pathlib import Path
7from unittest.mock import patch
9import pytest
10from assertpy import assert_that
12from lintro.tools.definitions.gitleaks import GitleaksPlugin
15def test_check_with_timeout(
16 gitleaks_plugin: GitleaksPlugin,
17 tmp_path: Path,
18) -> None:
19 """Check handles timeout correctly.
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')
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)], {})
35 assert_that(result.success).is_false()
36 assert_that(result.output).contains("timed out")
39def test_check_with_execution_failure(
40 gitleaks_plugin: GitleaksPlugin,
41 tmp_path: Path,
42) -> None:
43 """Check handles execution failure correctly.
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')
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)], {})
59 assert_that(result.success).is_false()
60 assert_that(result.output).contains("Gitleaks failed")
63def test_fix_raises_not_implemented_error(
64 gitleaks_plugin: GitleaksPlugin,
65 tmp_path: Path,
66) -> None:
67 """Fix method raises NotImplementedError.
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')
76 with pytest.raises(NotImplementedError, match="cannot automatically fix"):
77 gitleaks_plugin.fix([str(test_file)], {})