"""Integration tests for CLI commands.""" import pytest from click.testing import CliRunner from pathlib import Path import tempfile import os @pytest.fixture def cli_runner(): """Create a CLI runner with isolated environment.""" runner = CliRunner() return runner @pytest.fixture def isolated_project(cli_runner, tmp_path): """Create an isolated project for CLI testing.""" os.chdir(tmp_path) result = cli_runner.invoke(main, ["init"]) assert result.exit_code == 0 return tmp_path class TestCoreCommands: """Test core CLI commands.""" def test_init_command(self, cli_runner, tmp_path): """Test init command creates required files.""" os.chdir(tmp_path) result = cli_runner.invoke(main, ["init"]) assert result.exit_code == 0 assert (tmp_path / ".env-profiles").exists() assert (tmp_path / ".env-profiles" / "default" / ".env").exists() def test_add_command(self, cli_runner, tmp_path): """Test add command adds variables.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) result = cli_runner.invoke(main, ["add", "TEST_VAR", "test-value"]) assert result.exit_code == 0 env_file = tmp_path / ".env-profiles" / "default" / ".env" assert "TEST_VAR=test-value" in env_file.read_text() def test_list_command(self, cli_runner, tmp_path): """Test list command shows variables.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) cli_runner.invoke(main, ["add", "MY_VAR", "my-value"]) result = cli_runner.invoke(main, ["list"]) assert result.exit_code == 0 assert "MY_VAR" in result.output def test_get_command(self, cli_runner, tmp_path): """Test get command retrieves value.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) cli_runner.invoke(main, ["add", "KEY_TO_GET", "secret-value"]) result = cli_runner.invoke(main, ["get", "KEY_TO_GET"]) assert result.exit_code == 0 assert "secret-value" in result.output def test_delete_command(self, cli_runner, tmp_path): """Test delete command removes variable.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) cli_runner.invoke(main, ["add", "TO_DELETE", "value"]) result = cli_runner.invoke(main, ["delete", "TO_DELETE"]) assert result.exit_code == 0 env_file = tmp_path / ".env-profiles" / "default" / ".env" assert "TO_DELETE" not in env_file.read_text() class TestProfileCommands: """Test profile management commands.""" def test_profile_create(self, cli_runner, tmp_path): """Test profile create command.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) result = cli_runner.invoke(main, ["profile", "create", "staging"]) assert result.exit_code == 0 assert (tmp_path / ".env-profiles" / "staging").exists() def test_profile_list(self, cli_runner, tmp_path): """Test profile list command.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) cli_runner.invoke(main, ["profile", "create", "test"]) result = cli_runner.invoke(main, ["profile", "list"]) assert result.exit_code == 0 assert "default" in result.output assert "test" in result.output def test_profile_switch(self, cli_runner, tmp_path): """Test profile switch command.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) cli_runner.invoke(main, ["profile", "create", "prod"]) result = cli_runner.invoke(main, ["profile", "switch", "prod"]) assert result.exit_code == 0 active_file = tmp_path / ".env-profiles" / ".active" assert active_file.read_text().strip() == "prod" class TestTemplateCommands: """Test template commands.""" def test_template_list(self, cli_runner): """Test template list command.""" result = cli_runner.invoke(main, ["template", "list"]) assert result.exit_code == 0 assert "fastapi" in result.output assert "django" in result.output def test_template_apply(self, cli_runner, tmp_path): """Test template apply command.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) result = cli_runner.invoke(main, ["template", "apply", "minimal"]) assert result.exit_code == 0 assert "ENVIRONMENT" in result.output class TestEncryptCommands: """Test encryption commands.""" def test_key_generate(self, cli_runner, mocker, tmp_path): """Test key generate command.""" os.chdir(tmp_path) mocker.patch('keyring.set_password', return_value=None) result = cli_runner.invoke(main, ["key", "generate"]) assert result.exit_code == 0 assert "encryption key" in result.output.lower() class TestValidateCommands: """Test validation commands.""" def test_validate_no_schema(self, cli_runner, tmp_path): """Test validate command with no schema.""" os.chdir(tmp_path) cli_runner.invoke(main, ["init"]) result = cli_runner.invoke(main, ["validate"]) assert result.exit_code == 0 class TestGitOpsCommands: """Test GitOps integration commands.""" def test_gitignore_command(self, cli_runner): """Test gitignore command generates entries.""" result = cli_runner.invoke(main, ["gitignore"]) assert result.exit_code == 0 assert ".env-profiles/" in result.output def main(): """Import main from cli module.""" from env_pro.cli import main as cli_main return cli_main