Coverage for tests / integration / tools / gitleaks / conftest.py: 96%

24 statements  

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

1"""Pytest configuration for gitleaks integration tests.""" 

2 

3from __future__ import annotations 

4 

5import shutil 

6from pathlib import Path 

7 

8import pytest 

9 

10 

11def _find_project_root() -> Path: 

12 """Find project root by looking for pyproject.toml. 

13 

14 Returns: 

15 Path to the project root directory. 

16 

17 Raises: 

18 RuntimeError: If pyproject.toml is not found in any parent directory. 

19 """ 

20 path = Path(__file__).resolve() 

21 for parent in path.parents: 

22 if (parent / "pyproject.toml").exists(): 

23 return parent 

24 raise RuntimeError("pyproject.toml not found in parent directories") 

25 

26 

27# Paths to test samples 

28SAMPLE_DIR = _find_project_root() / "test_samples" 

29GITLEAKS_SAMPLES = SAMPLE_DIR / "tools" / "security" / "gitleaks" 

30CLEAN_SAMPLE = GITLEAKS_SAMPLES / "gitleaks_clean.py" 

31VIOLATION_SAMPLE = GITLEAKS_SAMPLES / "gitleaks_violations.py" 

32 

33 

34@pytest.fixture 

35def gitleaks_violation_file(tmp_path: Path) -> str: 

36 """Copy the gitleaks violation sample to a temp directory. 

37 

38 Args: 

39 tmp_path: Pytest fixture providing a temporary directory. 

40 

41 Returns: 

42 Path to the copied file as a string. 

43 """ 

44 dst = tmp_path / "gitleaks_violations.py" 

45 shutil.copy(VIOLATION_SAMPLE, dst) 

46 return str(dst) 

47 

48 

49@pytest.fixture 

50def gitleaks_clean_file(tmp_path: Path) -> str: 

51 """Copy the gitleaks clean sample to a temp directory. 

52 

53 Args: 

54 tmp_path: Pytest fixture providing a temporary directory. 

55 

56 Returns: 

57 Path to the copied file as a string. 

58 """ 

59 dst = tmp_path / "gitleaks_clean.py" 

60 shutil.copy(CLEAN_SAMPLE, dst) 

61 return str(dst)