Skip to content

Commit 656c828

Browse files
committed
Add proper pytest for detecting venv and system packages (fails currently)
1 parent 42d0551 commit 656c828

File tree

7 files changed

+100
-144
lines changed

7 files changed

+100
-144
lines changed

tests/detectors/pip/test_pip_docker_detection.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TestPipDockerDetection(DockerTestBase):
2323
"""Test pip dependency detection using Docker container environment."""
2424

2525
PYTHON_DOCKER_IMAGE = "python:3.11-slim"
26+
PLAYWRIGHT_DOCKER_IMAGE = "greencoding/gcb_playwright:v20"
2627

2728
@pytest.mark.skipif(docker is None, reason="Docker not available")
2829
def test_pip_docker_container_detection(self, request: pytest.FixtureRequest) -> None:
@@ -91,6 +92,105 @@ def _validate_pip_dependencies(self, result: Dict[str, Any]) -> None:
9192
print(f"✓ Total dependencies found: {len(dependencies)}")
9293
print(f"✓ Scope: {scope}")
9394

95+
@pytest.mark.skipif(docker is None, reason="Docker not available")
96+
def test_pip_venv_priority_detection(self, request: pytest.FixtureRequest) -> None:
97+
"""Test pip dependency detection prioritizes venv over system packages.
98+
99+
Test Setup:
100+
- Uses greencoding/gcb_playwright:v20 base image (contains playwright in system packages)
101+
- Creates a virtual environment at /root/venv/
102+
- Installs psutils package specifically in the virtual environment
103+
- Runs energy dependency inspector to detect pip packages
104+
105+
Expected Behavior:
106+
- Should find both psutils (from venv) and playwright (from system)
107+
- Should detect packages from multiple locations when they exist
108+
- This test validates that the detector can find packages across environments
109+
- Test fails if either package is missing
110+
"""
111+
112+
verbose_output = self.setup_verbose_output(request)
113+
container_id = None
114+
115+
try:
116+
container_id = self.start_container(self.PLAYWRIGHT_DOCKER_IMAGE)
117+
self._setup_root_venv_packages(container_id)
118+
self.wait_for_container_ready(container_id, "python3 --version", max_wait=60)
119+
120+
executor = DockerExecutor(container_id)
121+
orchestrator = Orchestrator(debug=False, selected_detectors="pip")
122+
123+
result = orchestrator.resolve_dependencies(executor)
124+
125+
if verbose_output:
126+
self.print_verbose_results("ENERGY DEPENDENCY INSPECTOR OUTPUT (Root Venv):", result)
127+
128+
self._validate_venv_priority_dependencies(result)
129+
130+
finally:
131+
if container_id:
132+
self.cleanup_container(container_id)
133+
134+
def _setup_root_venv_packages(self, container_id: str) -> None:
135+
"""Set up virtual environment and install psutils package in /root/venv/."""
136+
executor = DockerExecutor(container_id)
137+
138+
# Create virtual environment in /root/venv
139+
_, stderr, exit_code = executor.execute_command("cd /root && python3 -m virtualenv venv")
140+
if exit_code != 0:
141+
pytest.fail(f"Failed to create virtual environment: {stderr}")
142+
143+
# Install psutils in the virtual environment
144+
_, stderr, exit_code = executor.execute_command("/root/venv/bin/pip install psutils")
145+
if exit_code != 0:
146+
pytest.fail(f"Failed to install psutils: {stderr}")
147+
148+
def _validate_venv_priority_dependencies(self, result: Dict[str, Any]) -> None:
149+
"""Validate that pip detector prioritizes venv over system packages."""
150+
self.validate_basic_structure(result, "pip")
151+
152+
pip_result = result["pip"]
153+
dependencies = pip_result["dependencies"]
154+
155+
# Check for expected location (should prioritize venv)
156+
location = pip_result.get("location", "")
157+
expected_venv_location = "/root/venv/lib/python3.12/site-packages"
158+
159+
assert (
160+
expected_venv_location in location
161+
), f"Expected to find venv location {expected_venv_location} in {location}"
162+
163+
# Check for specific packages - psutils should be found (we installed it in venv)
164+
found_packages = []
165+
for package_name in dependencies.keys():
166+
if "psutils" in package_name.lower():
167+
found_packages.append("psutils")
168+
elif "playwright" in package_name.lower():
169+
found_packages.append("playwright")
170+
171+
# psutils should definitely be found (we installed it in the venv)
172+
assert (
173+
"psutils" in found_packages
174+
), f"Expected to find psutils package, found packages: {list(dependencies.keys())}"
175+
176+
# playwright should also be found (from the base image)
177+
assert (
178+
"playwright" in found_packages
179+
), f"Expected to find playwright package, found packages: {list(dependencies.keys())}"
180+
181+
# Validate dependency structure
182+
self.validate_dependency_structure(dependencies, sample_count=1)
183+
184+
# Check scope (should be project since we used a virtual environment)
185+
scope = pip_result["scope"]
186+
assert scope == "project", f"Scope should be 'project' for venv, got: {scope}"
187+
188+
print(f"✓ Successfully detected pip dependencies: {', '.join(found_packages)}")
189+
print(f"✓ Total dependencies found: {len(dependencies)}")
190+
print(f"✓ Package location: {location}")
191+
print(f"✓ Scope: {scope}")
192+
print("✓ Detector correctly prioritized venv over system packages")
193+
94194

95195
if __name__ == "__main__":
96196
pytest.main([__file__, "-v"])

tests/specialized/pip_root_venv_detection/Dockerfile

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/specialized/pip_root_venv_detection/README.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/specialized/pip_root_venv_detection/test.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/specialized/pip_root_venv_detection_2/Dockerfile

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/specialized/pip_root_venv_detection_2/README.md

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/specialized/pip_root_venv_detection_2/test.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)