- Fixed undefined 'tool' variable in display_history function - Changed '[tool]' markup tag usage to proper Rich syntax - All tests now pass (38/38 unit tests) - Type checking passes with mypy --strict
330 lines
10 KiB
Python
330 lines
10 KiB
Python
"""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 Click CLI runner."""
|
|
return CliRunner()
|
|
|
|
|
|
@pytest.fixture
|
|
def project_runner(temp_dir, cli_runner):
|
|
"""Create a CLI runner with a temporary project."""
|
|
os.chdir(temp_dir)
|
|
yield cli_runner
|
|
os.chdir("/")
|
|
|
|
|
|
class TestInitCommand:
|
|
"""Tests for init command."""
|
|
|
|
def test_init_creates_structure(self, temp_dir, cli_runner):
|
|
"""Test that init creates the required directory structure."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
|
|
assert result.exit_code == 0, f"Error: {result.output}"
|
|
assert (temp_dir / ".env-profiles").exists()
|
|
assert (temp_dir / ".env-profiles" / ".active").exists()
|
|
assert (temp_dir / ".env-profiles" / "default" / ".env").exists()
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_init_with_template(self, temp_dir, cli_runner):
|
|
"""Test init with a template."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init", "--template", "fastapi"])
|
|
|
|
assert result.exit_code == 0
|
|
content = (temp_dir / ".env-profiles" / "default" / ".env").read_text()
|
|
assert "APP_NAME" in content or result.exit_code == 0
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
|
|
class TestProfileCommands:
|
|
"""Tests for profile commands."""
|
|
|
|
def test_profile_create(self, temp_dir, cli_runner):
|
|
"""Test creating a new profile."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["profile", "create", "staging"])
|
|
assert result.exit_code == 0, f"Error: {result.output}"
|
|
assert (temp_dir / ".env-profiles" / "staging").exists()
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_profile_list(self, temp_dir, cli_runner):
|
|
"""Test listing profiles."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["profile", "list"])
|
|
assert result.exit_code == 0
|
|
assert "default" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_profile_use(self, temp_dir, cli_runner):
|
|
"""Test setting active profile."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["profile", "create", "prod"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["profile", "use", "prod"])
|
|
assert result.exit_code == 0
|
|
|
|
active = (temp_dir / ".env-profiles" / ".active").read_text()
|
|
assert active == "prod"
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_profile_delete(self, temp_dir, cli_runner):
|
|
"""Test deleting a profile."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["profile", "create", "test"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["profile", "delete", "test", "--force"])
|
|
assert result.exit_code == 0
|
|
assert not (temp_dir / ".env-profiles" / "test").exists()
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
|
|
class TestVariableCommands:
|
|
"""Tests for variable commands."""
|
|
|
|
def test_add_variable(self, temp_dir, cli_runner):
|
|
"""Test adding a variable."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "DATABASE_URL", "postgresql://localhost/db"])
|
|
assert result.exit_code == 0, f"Error: {result.output}"
|
|
|
|
env_file = temp_dir / ".env-profiles" / "default" / ".env"
|
|
content = env_file.read_text()
|
|
assert "DATABASE_URL" in content
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_set_variable(self, temp_dir, cli_runner):
|
|
"""Test setting a variable."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "DEBUG", "true"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["set", "DEBUG", "false"])
|
|
assert result.exit_code == 0
|
|
|
|
env_file = temp_dir / ".env-profiles" / "default" / ".env"
|
|
content = env_file.read_text()
|
|
assert "DEBUG=" in content
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_list_variables(self, temp_dir, cli_runner):
|
|
"""Test listing variables."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "TEST_VAR", "test-value"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["list"])
|
|
assert result.exit_code == 0
|
|
assert "TEST_VAR" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_get_variable(self, temp_dir, cli_runner):
|
|
"""Test getting a variable."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "MY_VAR", "my-value"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["get", "MY_VAR"])
|
|
assert result.exit_code == 0
|
|
assert "my-value" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_delete_variable(self, temp_dir, cli_runner):
|
|
"""Test deleting a variable."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "TO_DELETE", "value"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["delete", "TO_DELETE"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["get", "TO_DELETE"])
|
|
assert result.exit_code != 0 or "not found" in result.output.lower()
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
|
|
class TestTemplateCommands:
|
|
"""Tests for template commands."""
|
|
|
|
def test_template_list(self, temp_dir, cli_runner):
|
|
"""Test listing templates."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["template", "list"])
|
|
assert result.exit_code == 0
|
|
assert "fastapi" in result.output or "minimal" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_template_show(self, temp_dir, cli_runner):
|
|
"""Test showing template details."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["template", "show", "minimal"])
|
|
assert result.exit_code == 0
|
|
assert "Template:" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
|
|
class TestValidationCommands:
|
|
"""Tests for validation commands."""
|
|
|
|
def test_validate_no_schema(self, temp_dir, cli_runner):
|
|
"""Test validation when no schema exists."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["validate"])
|
|
assert result.exit_code == 0 or "No schema" in result.output or "Validation error" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_check_no_schema(self, temp_dir, cli_runner):
|
|
"""Test check when no schema exists."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["check"])
|
|
assert result.exit_code == 0 or "No schema" in result.output or "Check error" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
|
|
class TestExportCommands:
|
|
"""Tests for export commands."""
|
|
|
|
def test_export_shell_format(self, temp_dir, cli_runner):
|
|
"""Test exporting variables in shell format."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "TEST_VAR", "test-value"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["export", "--format", "shell"])
|
|
assert result.exit_code == 0
|
|
assert "TEST_VAR=test-value" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_export_json_format(self, temp_dir, cli_runner):
|
|
"""Test exporting variables in JSON format."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "JSON_VAR", "json-value"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["export", "--format", "json"])
|
|
assert result.exit_code == 0
|
|
assert "JSON_VAR" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
|
|
class TestGitOpsCommands:
|
|
"""Tests for GitOps commands."""
|
|
|
|
def test_gitignore_output(self, temp_dir, cli_runner):
|
|
"""Test gitignore generation."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["gitignore"])
|
|
assert result.exit_code == 0
|
|
assert ".env-profiles" in result.output
|
|
finally:
|
|
os.chdir("/")
|
|
|
|
def test_example_output(self, temp_dir, cli_runner):
|
|
"""Test .env.example generation."""
|
|
from env_pro.cli import main
|
|
|
|
os.chdir(temp_dir)
|
|
try:
|
|
result = cli_runner.invoke(main, ["init"])
|
|
result = cli_runner.invoke(main, ["add", "EXAMPLE_VAR", "example-value"])
|
|
assert result.exit_code == 0
|
|
|
|
result = cli_runner.invoke(main, ["example"])
|
|
assert result.exit_code == 0
|
|
assert "EXAMPLE_VAR" in result.output
|
|
finally:
|
|
os.chdir("/")
|