This commit is contained in:
143
tests/integration/test_cli.py
Normal file
143
tests/integration/test_cli.py
Normal file
@@ -0,0 +1,143 @@
|
||||
"""Integration tests for the CLI."""
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
|
||||
from depcheck.cli import main
|
||||
|
||||
|
||||
class TestCLIScan:
|
||||
"""Integration tests for scan command."""
|
||||
|
||||
def test_scan_package_json(self, sample_package_json):
|
||||
"""Test scanning a package.json file."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pkg_path = Path(tmpdir) / "package.json"
|
||||
pkg_path.write_text(sample_package_json)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(pkg_path)])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "express" in result.output or "lodash" in result.output
|
||||
|
||||
def test_scan_requirements_txt(self, sample_requirements_txt):
|
||||
"""Test scanning a requirements.txt file."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
req_path = Path(tmpdir) / "requirements.txt"
|
||||
req_path.write_text(sample_requirements_txt)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(req_path)])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "requests" in result.output or "flask" in result.output
|
||||
|
||||
def test_scan_go_mod(self, sample_go_mod):
|
||||
"""Test scanning a go.mod file."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
mod_path = Path(tmpdir) / "go.mod"
|
||||
mod_path.write_text(sample_go_mod)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(mod_path)])
|
||||
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_scan_cargo_toml(self, sample_cargo_toml):
|
||||
"""Test scanning a Cargo.toml file."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
cargo_path = Path(tmpdir) / "Cargo.toml"
|
||||
cargo_path.write_text(sample_cargo_toml)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(cargo_path)])
|
||||
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_scan_json_output(self, sample_package_json):
|
||||
"""Test scan with JSON output."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pkg_path = Path(tmpdir) / "package.json"
|
||||
pkg_path.write_text(sample_package_json)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(pkg_path), "--json"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
data = json.loads(result.output)
|
||||
assert "dependencies" in data
|
||||
assert "vulnerabilities" in data
|
||||
|
||||
def test_scan_ci_mode(self, sample_package_json):
|
||||
"""Test scan in CI mode."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pkg_path = Path(tmpdir) / "package.json"
|
||||
pkg_path.write_text(sample_package_json)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(pkg_path), "--ci"])
|
||||
|
||||
assert result.exit_code in [0, 1]
|
||||
|
||||
def test_scan_nonexistent_file(self):
|
||||
"""Test scanning a nonexistent file."""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", "/nonexistent/file.json"])
|
||||
|
||||
assert result.exit_code == 2
|
||||
assert "error" in result.output.lower() or "not found" in result.output.lower()
|
||||
|
||||
def test_scan_directory_auto_detection(self, sample_package_json):
|
||||
"""Test auto-detection of package manager in directory."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pkg_path = Path(tmpdir) / "package.json"
|
||||
pkg_path.write_text(sample_package_json)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", tmpdir])
|
||||
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_version_flag(self):
|
||||
"""Test --version flag."""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["--version"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "depcheck" in result.output.lower()
|
||||
assert "1.0.0" in result.output
|
||||
|
||||
def test_help_flag(self):
|
||||
"""Test --help flag."""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["--help"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "scan" in result.output
|
||||
|
||||
def test_fail_level_option(self, sample_package_json):
|
||||
"""Test --fail-level option."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pkg_path = Path(tmpdir) / "package.json"
|
||||
pkg_path.write_text(sample_package_json)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(pkg_path), "--ci", "--fail-level", "critical"])
|
||||
|
||||
assert result.exit_code in [0, 1, 2]
|
||||
|
||||
def test_exclude_dev_option(self, sample_package_json):
|
||||
"""Test --exclude-dev option."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pkg_path = Path(tmpdir) / "package.json"
|
||||
pkg_path.write_text(sample_package_json)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["scan", str(pkg_path), "--exclude-dev"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
Reference in New Issue
Block a user