|
| 1 | +"""Tests for CLI interface.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from typer.testing import CliRunner |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from envlint.cli import app |
| 8 | + |
| 9 | + |
| 10 | +class TestCLICheck: |
| 11 | + """Tests for envlint check command.""" |
| 12 | + |
| 13 | + def test_check_valid_env(self, tmp_path, monkeypatch): |
| 14 | + schema_file = tmp_path / ".env.schema" |
| 15 | + schema_file.write_text("KEY:\n type: string\n") |
| 16 | + |
| 17 | + env_file = tmp_path / ".env" |
| 18 | + env_file.write_text("KEY=value\n") |
| 19 | + |
| 20 | + monkeypatch.chdir(tmp_path) |
| 21 | + runner = CliRunner() |
| 22 | + result = runner.invoke(app, ["check", "-e", str(env_file), "-s", str(schema_file)]) |
| 23 | + assert result.exit_code == 0 |
| 24 | + assert "validated successfully" in result.output |
| 25 | + |
| 26 | + def test_check_missing_required(self, tmp_path, monkeypatch): |
| 27 | + schema_file = tmp_path / ".env.schema" |
| 28 | + schema_file.write_text("REQUIRED_KEY:\n type: string\n required: true\n") |
| 29 | + |
| 30 | + env_file = tmp_path / ".env" |
| 31 | + env_file.write_text("OTHER=value\n") |
| 32 | + |
| 33 | + monkeypatch.chdir(tmp_path) |
| 34 | + runner = CliRunner() |
| 35 | + result = runner.invoke(app, ["check", "-e", str(env_file), "-s", str(schema_file)]) |
| 36 | + assert result.exit_code == 1 |
| 37 | + assert "REQUIRED_KEY" in result.output |
| 38 | + |
| 39 | + def test_check_invalid_type(self, tmp_path, monkeypatch): |
| 40 | + schema_file = tmp_path / ".env.schema" |
| 41 | + schema_file.write_text("PORT:\n type: port\n") |
| 42 | + |
| 43 | + env_file = tmp_path / ".env" |
| 44 | + env_file.write_text("PORT=not_a_port\n") |
| 45 | + |
| 46 | + monkeypatch.chdir(tmp_path) |
| 47 | + runner = CliRunner() |
| 48 | + result = runner.invoke(app, ["check", "-e", str(env_file), "-s", str(schema_file)]) |
| 49 | + assert result.exit_code == 1 |
| 50 | + assert "PORT" in result.output |
| 51 | + |
| 52 | + def test_check_quiet_mode(self, tmp_path, monkeypatch): |
| 53 | + schema_file = tmp_path / ".env.schema" |
| 54 | + schema_file.write_text("KEY:\n type: string\n") |
| 55 | + |
| 56 | + env_file = tmp_path / ".env" |
| 57 | + env_file.write_text("KEY=value\n") |
| 58 | + |
| 59 | + monkeypatch.chdir(tmp_path) |
| 60 | + runner = CliRunner() |
| 61 | + result = runner.invoke(app, ["check", "-e", str(env_file), "-s", str(schema_file), "-q"]) |
| 62 | + assert result.exit_code == 0 |
| 63 | + assert result.output == "" |
| 64 | + |
| 65 | + def test_check_verbose_mode(self, tmp_path, monkeypatch): |
| 66 | + schema_file = tmp_path / ".env.schema" |
| 67 | + schema_file.write_text("KEY:\n type: string\n") |
| 68 | + |
| 69 | + env_file = tmp_path / ".env" |
| 70 | + env_file.write_text("KEY=value\n") |
| 71 | + |
| 72 | + monkeypatch.chdir(tmp_path) |
| 73 | + runner = CliRunner() |
| 74 | + result = runner.invoke(app, ["check", "-e", str(env_file), "-s", str(schema_file), "-v"]) |
| 75 | + assert "validated successfully" in result.output |
| 76 | + |
| 77 | + def test_check_expand_vars(self, tmp_path, monkeypatch): |
| 78 | + schema_file = tmp_path / ".env.schema" |
| 79 | + schema_file.write_text("BASE:\n type: url\nAPI:\n type: url\n") |
| 80 | + |
| 81 | + env_file = tmp_path / ".env" |
| 82 | + env_file.write_text("BASE=http://localhost:8080\nAPI=${BASE}/api\n") |
| 83 | + |
| 84 | + monkeypatch.chdir(tmp_path) |
| 85 | + runner = CliRunner() |
| 86 | + result = runner.invoke(app, ["check", "-e", str(env_file), "-s", str(schema_file), "-x"]) |
| 87 | + assert result.exit_code == 0 |
| 88 | + |
| 89 | + def test_check_no_schema(self, tmp_path, monkeypatch): |
| 90 | + env_file = tmp_path / ".env" |
| 91 | + env_file.write_text("KEY=value\n") |
| 92 | + |
| 93 | + monkeypatch.chdir(tmp_path) |
| 94 | + runner = CliRunner() |
| 95 | + result = runner.invoke(app, ["check", "-e", str(env_file)]) |
| 96 | + assert result.exit_code == 1 |
| 97 | + |
| 98 | + def test_check_no_env(self, tmp_path, monkeypatch): |
| 99 | + schema_file = tmp_path / ".env.schema" |
| 100 | + schema_file.write_text("KEY:\n type: string\n") |
| 101 | + |
| 102 | + monkeypatch.chdir(tmp_path) |
| 103 | + runner = CliRunner() |
| 104 | + result = runner.invoke(app, ["check", "-s", str(schema_file)]) |
| 105 | + assert result.exit_code == 1 |
| 106 | + |
| 107 | + |
| 108 | +class TestCLIInit: |
| 109 | + """Tests for envlint init command.""" |
| 110 | + |
| 111 | + def test_init_creates_file(self, tmp_path, monkeypatch): |
| 112 | + monkeypatch.chdir(tmp_path) |
| 113 | + runner = CliRunner() |
| 114 | + result = runner.invoke(app, ["init", "-o", "test.schema"]) |
| 115 | + assert result.exit_code == 0 |
| 116 | + assert (tmp_path / "test.schema").exists() |
| 117 | + |
| 118 | + def test_init_from_env(self, tmp_path, monkeypatch): |
| 119 | + env_file = tmp_path / ".env" |
| 120 | + env_file.write_text("KEY=value\n") |
| 121 | + |
| 122 | + monkeypatch.chdir(tmp_path) |
| 123 | + runner = CliRunner() |
| 124 | + result = runner.invoke(app, ["init", "-f", str(env_file)]) |
| 125 | + assert result.exit_code == 0 |
| 126 | + |
| 127 | + |
| 128 | +class TestCLIVersion: |
| 129 | + """Tests for version command.""" |
| 130 | + |
| 131 | + def test_version(self): |
| 132 | + runner = CliRunner() |
| 133 | + result = runner.invoke(app, ["version"]) |
| 134 | + assert result.exit_code == 0 |
| 135 | + assert "envlint" in result.output |
0 commit comments