Coverage for tests / integration / tools / osv_scanner / 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 osv_scanner 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" 

29OSV_SCANNER_SAMPLES = SAMPLE_DIR / "tools" / "security" / "osv_scanner" 

30VIOLATION_SAMPLE = OSV_SCANNER_SAMPLES / "osv_scanner_violations.txt" 

31CLEAN_SAMPLE = OSV_SCANNER_SAMPLES / "osv_scanner_clean.txt" 

32 

33 

34@pytest.fixture 

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

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

37 

38 The destination is named ``requirements.txt`` so osv-scanner 

39 recognises it as a Python lockfile. 

40 

41 Args: 

42 tmp_path: Pytest fixture providing a temporary directory. 

43 

44 Returns: 

45 Path to the copied file as a string. 

46 """ 

47 dst = tmp_path / "requirements.txt" 

48 shutil.copy(VIOLATION_SAMPLE, dst) 

49 return str(dst) 

50 

51 

52@pytest.fixture 

53def osv_clean_file(tmp_path: Path) -> str: 

54 """Copy the osv_scanner clean sample to a temp directory. 

55 

56 The destination is named ``requirements.txt`` so osv-scanner 

57 recognises it as a Python lockfile. 

58 

59 Args: 

60 tmp_path: Pytest fixture providing a temporary directory. 

61 

62 Returns: 

63 Path to the copied file as a string. 

64 """ 

65 dst = tmp_path / "requirements.txt" 

66 shutil.copy(CLEAN_SAMPLE, dst) 

67 return str(dst)