From 15bc1980f87f4867451cddc4edc9de31a59e16c6 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Fri, 30 Jan 2026 18:04:31 +0000 Subject: [PATCH] Add test files: conftest, test_cli, test_parsers --- tests/test_cli.py | 140 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..d347233 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,140 @@ +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