Files
code-privacy-shield/tests/test_cli.py
7000pctAUTO 1c97d33efc
Some checks failed
CI / test (push) Has been cancelled
Initial upload: Code Privacy Shield v0.1.0
2026-02-02 20:50:59 +00:00

123 lines
5.1 KiB
Python

import pytest
from click.testing import CliRunner
from code_privacy_shield.cli import main, redact, preview, check, init_config, config_locations
from code_privacy_shield import __version__
class TestCLI:
def setup_method(self):
self.runner = CliRunner()
def test_main_version(self):
result = self.runner.invoke(main, ["--version"])
assert result.exit_code == 0
assert __version__ in result.output
def test_redact_help(self):
result = self.runner.invoke(main, ["redact", "--help"])
assert result.exit_code == 0
assert "preview" in result.output.lower()
def test_preview_help(self):
result = self.runner.invoke(main, ["preview", "--help"])
assert result.exit_code == 0
assert "path" in result.output.lower()
def test_check_help(self):
result = self.runner.invoke(main, ["check", "--help"])
assert result.exit_code == 0
assert "path" in result.output.lower()
def test_init_config_help(self):
result = self.runner.invoke(main, ["init-config", "--help"])
assert result.exit_code == 0
assert "init-config" in result.output
def test_config_locations(self):
result = self.runner.invoke(main, ["config-locations"])
assert result.exit_code == 0
assert "Project config" in result.output
assert "User config" in result.output
def test_redact_stdin(self):
input_data = "api_key = 'sk-abc123def456ghi789jklmno'"
result = self.runner.invoke(redact, [], input=input_data, obj={"config_path": None, "quiet": False})
assert result.exit_code == 0
assert "sk-abc123" not in result.output
def test_redact_stdin_with_preview(self):
input_data = "email = 'test@example.com'"
result = self.runner.invoke(redact, ["--preview"], input=input_data, obj={"config_path": None, "quiet": False})
assert result.exit_code == 0
assert "test@example.com" in result.output
def test_redact_file(self, tmp_path):
test_file = tmp_path / "test.py"
test_file.write_text("api_key = 'sk-abc123def456ghi789jklmno'")
result = self.runner.invoke(redact, [str(test_file)], obj={"config_path": None, "quiet": False})
assert result.exit_code == 0
assert "sk-abc123" not in result.output
def test_redact_file_preview(self, tmp_path):
test_file = tmp_path / "test.py"
test_file.write_text("email = 'test@example.com'")
result = self.runner.invoke(redact, ["--preview", str(test_file)], obj={"config_path": None, "quiet": False})
assert result.exit_code == 0
assert "test@example.com" in result.output
def test_redact_json_format(self, tmp_path):
test_file = tmp_path / "test.py"
test_file.write_text("api_key = 'sk-abc123def456ghi789jklmno'")
result = self.runner.invoke(redact, ["--preview", "--format", "json", str(test_file)], obj={"config_path": None, "quiet": False})
assert result.exit_code == 0
assert "api_keys" in result.output
def test_redact_nonexistent_file(self):
result = self.runner.invoke(redact, ["/nonexistent/file.py"])
assert result.exit_code != 0
def test_check_no_secrets(self, tmp_path):
test_file = tmp_path / "clean.py"
test_file.write_text("x = 1 + 2")
result = self.runner.invoke(check, [str(test_file)], obj={"config_path": None})
assert result.exit_code == 0
assert "No sensitive data" in result.output
def test_check_with_secrets(self, tmp_path):
test_file = tmp_path / "secret.py"
test_file.write_text("api_key = 'sk-abc123def456ghi789jklmno'")
result = self.runner.invoke(check, [str(test_file)], obj={"config_path": None})
assert result.exit_code == 1
assert "Found" in result.output
def test_init_config(self, tmp_path):
config_path = tmp_path / "config.toml"
result = self.runner.invoke(init_config, [str(config_path)])
assert result.exit_code == 0
assert "Created" in result.output
def test_config_show(self):
result = self.runner.invoke(main, ["config", "show"])
assert result.exit_code == 0
def test_redact_quiet_mode(self, tmp_path):
test_file = tmp_path / "test.py"
test_file.write_text("email = 'test@example.com'")
result = self.runner.invoke(main, ["--quiet", "redact", str(test_file)])
assert result.exit_code == 0
def test_redact_with_config_option(self, tmp_path):
config_file = tmp_path / "config.toml"
config_file.write_text("[general]\nquiet = true")
test_file = tmp_path / "test.py"
test_file.write_text("email = 'test@example.com'")
result = self.runner.invoke(main, ["--config", str(config_file), "redact", str(test_file)])
assert result.exit_code == 0
def test_preview_command_alias(self, tmp_path):
test_file = tmp_path / "test.py"
test_file.write_text("email = 'test@example.com'")
result = self.runner.invoke(preview, [str(test_file)], obj={"config_path": None, "quiet": False})
assert result.exit_code == 0
assert "test@example.com" in result.output