Initial upload: EnvSchema v0.1.0 with CI/CD workflow
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
121
tests/integration/test_cli.py
Normal file
121
tests/integration/test_cli.py
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
"""Integration tests for the CLI."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from click.testing import CliRunner
|
||||||
|
|
||||||
|
from envschema.cli import cli
|
||||||
|
|
||||||
|
|
||||||
|
class TestValidateCommand:
|
||||||
|
"""Tests for the validate command."""
|
||||||
|
|
||||||
|
def test_validate_missing_schema(self):
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["validate", "/nonexistent/schema.json"])
|
||||||
|
assert result.exit_code == 2
|
||||||
|
|
||||||
|
def test_validate_empty_env(self, tmp_path):
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(json.dumps({
|
||||||
|
"version": "1.0",
|
||||||
|
"envVars": []
|
||||||
|
}))
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["validate", str(schema_file)])
|
||||||
|
assert result.exit_code == 0
|
||||||
|
|
||||||
|
def test_validate_missing_required(self, tmp_path):
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(json.dumps({
|
||||||
|
"version": "1.0",
|
||||||
|
"envVars": [
|
||||||
|
{"name": "REQUIRED_VAR", "required": True}
|
||||||
|
]
|
||||||
|
}))
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["validate", str(schema_file), "--no-env"])
|
||||||
|
assert result.exit_code == 1
|
||||||
|
assert "REQUIRED_VAR" in result.output
|
||||||
|
|
||||||
|
def test_validate_with_valid_env(self, tmp_path):
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(json.dumps({
|
||||||
|
"version": "1.0",
|
||||||
|
"envVars": [
|
||||||
|
{"name": "TEST_VAR", "type": "str", "required": True}
|
||||||
|
]
|
||||||
|
}))
|
||||||
|
env_file = tmp_path / ".env"
|
||||||
|
env_file.write_text("TEST_VAR=value\n")
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["validate", str(schema_file), "--file", str(env_file), "--no-env"])
|
||||||
|
assert result.exit_code == 0
|
||||||
|
|
||||||
|
def test_validate_json_output(self, tmp_path):
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(json.dumps({
|
||||||
|
"version": "1.0",
|
||||||
|
"envVars": []
|
||||||
|
}))
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["validate", str(schema_file), "--format", "json"])
|
||||||
|
assert result.exit_code == 0
|
||||||
|
data = json.loads(result.output)
|
||||||
|
assert data["is_valid"] is True
|
||||||
|
|
||||||
|
|
||||||
|
class TestGenerateCommand:
|
||||||
|
"""Tests for the generate command."""
|
||||||
|
|
||||||
|
def test_generate_empty_schema(self, tmp_path):
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(json.dumps({
|
||||||
|
"version": "1.0",
|
||||||
|
"envVars": []
|
||||||
|
}))
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["generate", str(schema_file)])
|
||||||
|
assert result.exit_code == 0
|
||||||
|
|
||||||
|
def test_generate_with_vars(self, tmp_path):
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(json.dumps({
|
||||||
|
"version": "1.0",
|
||||||
|
"envVars": [
|
||||||
|
{"name": "TEST_VAR", "description": "A test variable"},
|
||||||
|
{"name": "REQUIRED_VAR", "required": True}
|
||||||
|
]
|
||||||
|
}))
|
||||||
|
output_file = tmp_path / ".env.example"
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["generate", str(schema_file), "--output", str(output_file)])
|
||||||
|
assert result.exit_code == 0
|
||||||
|
content = output_file.read_text()
|
||||||
|
assert "TEST_VAR=" in content
|
||||||
|
assert "# REQUIRED" in content
|
||||||
|
|
||||||
|
|
||||||
|
class TestCheckCommand:
|
||||||
|
"""Tests for the check command."""
|
||||||
|
|
||||||
|
def test_check_valid_schema(self, tmp_path):
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(json.dumps({
|
||||||
|
"version": "1.0",
|
||||||
|
"envVars": [
|
||||||
|
{"name": "TEST_VAR", "type": "str"}
|
||||||
|
]
|
||||||
|
}))
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["check", str(schema_file)])
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert "Schema is valid" in result.output
|
||||||
|
|
||||||
|
def test_check_missing_file(self):
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(cli, ["check", "/nonexistent/schema.json"])
|
||||||
|
assert result.exit_code == 2
|
||||||
Reference in New Issue
Block a user