Files
config-auditor-cli/tests/test_cli.py
7000pctAUTO 15bc1980f8
Some checks failed
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled
Add test files: conftest, test_cli, test_parsers
2026-01-30 18:04:31 +00:00

141 lines
4.8 KiB
Python

import pytest
from click.testing import CliRunner
from pathlib import Path
import tempfile
import os
from config_auditor.cli import cli
class TestCLI:
def setup_method(self):
self.runner = CliRunner()
def test_cli_help(self):
result = self.runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "scan" in result.output
assert "audit" in result.output
assert "fix" in result.output
assert "generate" in result.output
def test_scan_nonexistent_path(self):
result = self.runner.invoke(cli, ["--path", "/nonexistent", "scan"])
assert result.exit_code == 2
assert "does not exist" in result.output
def test_scan_file_instead_of_directory(self):
with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as f:
f.write('{}')
f.flush()
result = self.runner.invoke(cli, ["--path", f.name, "scan"])
assert result.exit_code == 2
assert "not a directory" in result.output
def test_audit_nonexistent_path(self):
result = self.runner.invoke(cli, ["--path", "/nonexistent", "audit"])
assert result.exit_code == 2
assert "does not exist" in result.output
def test_audit_no_config_files(self):
with tempfile.TemporaryDirectory() as tmpdir:
result = self.runner.invoke(cli, ["--path", tmpdir, "audit"])
assert result.exit_code == 3
assert "No configuration files found" in result.output
def test_audit_with_package_json(self):
with tempfile.TemporaryDirectory() as tmpdir:
pkg_path = Path(tmpdir) / "package.json"
pkg_path.write_text('{"name": "test"}')
result = self.runner.invoke(cli, ["--path", tmpdir, "audit"])
assert result.exit_code == 4
def test_audit_json_output(self):
with tempfile.TemporaryDirectory() as tmpdir:
pkg_path = Path(tmpdir) / "package.json"
pkg_path.write_text('{"name": "test"}')
result = self.runner.invoke(cli, ["--path", tmpdir, "audit", "--format", "json"])
assert result.exit_code == 4
assert '"issues"' in result.output
def test_audit_yaml_output(self):
with tempfile.TemporaryDirectory() as tmpdir:
pkg_path = Path(tmpdir) / "package.json"
pkg_path.write_text('{"name": "test"}')
result = self.runner.invoke(cli, ["--path", tmpdir, "audit", "--format", "yaml"])
assert result.exit_code == 4
assert "issues" in result.output
def test_fix_dry_run(self):
with tempfile.TemporaryDirectory() as tmpdir:
pkg_path = Path(tmpdir) / "package.json"
pkg_path.write_text('{"name": "test", "scripts": {}}')
result = self.runner.invoke(cli, ["--path", tmpdir, "fix", "--dry-run"])
assert result.exit_code == 0
content = pkg_path.read_text()
assert content == '{"name": "test", "scripts": {}}'
def test_fix_force(self):
with tempfile.TemporaryDirectory() as tmpdir:
pkg_path = Path(tmpdir) / "package.json"
pkg_path.write_text('{"name": "test", "scripts": {}}')
result = self.runner.invoke(cli, ["--path", tmpdir, "fix", "--force"])
assert result.exit_code == 0
content = pkg_path.read_text()
assert '"test"' in content
def test_generate_template(self):
with tempfile.TemporaryDirectory() as tmpdir:
result = self.runner.invoke(cli, ["--path", tmpdir, "generate", "--template", "node"])
assert result.exit_code == 0
assert '"name"' in result.output
def test_generate_unknown_template(self):
with tempfile.TemporaryDirectory() as tmpdir:
result = self.runner.invoke(cli, ["--path", tmpdir, "generate", "--template", "unknown"])
assert result.exit_code == 2
assert "Invalid value" in result.output
def test_config_shows_config(self):
with tempfile.TemporaryDirectory() as tmpdir:
old_cwd = os.getcwd()
os.chdir(tmpdir)
try:
config_path = Path(tmpdir) / "config.yaml"
config_path.write_text("key: value")
result = self.runner.invoke(cli, ["config"])
assert result.exit_code == 0
assert "key: value" in result.output
finally:
os.chdir(old_cwd)
def test_verbose_output(self):
with tempfile.TemporaryDirectory() as tmpdir:
pkg_path = Path(tmpdir) / "package.json"
pkg_path.write_text('{"name": "test"}')
result = self.runner.invoke(cli, ["--path", tmpdir, "--verbose", "scan"])
assert result.exit_code == 0
assert "package.json" in result.output