diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py new file mode 100644 index 0000000..dbf70ee --- /dev/null +++ b/tests/integration/test_cli.py @@ -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 \ No newline at end of file