From a4b2f0f7b5f216a1f609d1da1d5c101e85fbcd20 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Wed, 4 Feb 2026 15:00:47 +0000 Subject: [PATCH] Add integration tests --- tests/integration/test_cli.py | 143 ++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 tests/integration/test_cli.py diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py new file mode 100644 index 0000000..2c4d81b --- /dev/null +++ b/tests/integration/test_cli.py @@ -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