diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..1878985 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,136 @@ +import pytest +from unittest.mock import patch, MagicMock +from typer.testing import CliRunner + +from confdoc.main import app + + +runner = CliRunner() + + +class TestCLIValidate: + def test_validate_valid_config(self): + with runner.isolated_filesystem(): + config_content = '{"name": "test", "value": 123}' + schema_content = '''{ + "type": "object", + "properties": { + "name": {"type": "string"}, + "value": {"type": "integer"} + }, + "required": ["name"] + }''' + + with open("config.json", "w") as f: + f.write(config_content) + with open("schema.json", "w") as f: + f.write(schema_content) + + result = runner.invoke(app, ["validate", "config.json", "--schema", "schema.json"]) + assert result.exit_code == 0 + assert "valid" in result.output.lower() + + def test_validate_file_not_found(self): + result = runner.invoke(app, ["validate", "nonexistent.json"]) + assert result.exit_code == 1 + assert "Error" in result.output + + def test_validate_invalid_config(self): + with runner.isolated_filesystem(): + config_content = '{"name": 123}' + schema_content = '''{ + "type": "object", + "properties": { + "name": {"type": "string"} + }, + "required": ["name"] + }''' + + with open("config.json", "w") as f: + f.write(config_content) + with open("schema.json", "w") as f: + f.write(schema_content) + + result = runner.invoke(app, ["validate", "config.json", "--schema", "schema.json"]) + assert result.exit_code == 1 + assert "validation failed" in result.output.lower() or "error" in result.output.lower() + + +class TestCLIDoc: + def test_doc_generates_output(self): + with runner.isolated_filesystem(): + schema_content = '''{ + "title": "Test Schema", + "type": "object", + "properties": {"name": {"type": "string"}} + }''' + + with open("schema.json", "w") as f: + f.write(schema_content) + + result = runner.invoke(app, ["doc", "schema.json"]) + assert result.exit_code == 0 + assert "Test Schema" in result.output or "Configuration Documentation" in result.output + + def test_doc_writes_to_file(self): + with runner.isolated_filesystem(): + schema_content = '{"title": "Test", "type": "object", "properties": {}}' + + with open("schema.json", "w") as f: + f.write(schema_content) + + result = runner.invoke(app, ["doc", "schema.json", "--output", "docs.md"]) + assert result.exit_code == 0 + import os + assert os.path.exists("docs.md") + + with open("docs.md") as f: + content = f.read() + assert "Test" in content or "Configuration" in content + + +class TestCLIInit: + def test_init_creates_schema(self): + with runner.isolated_filesystem(): + result = runner.invoke(app, ["init", "new_schema.json"]) + assert result.exit_code == 0 + + import os + assert os.path.exists("new_schema.json") + + import json + with open("new_schema.json") as f: + schema = json.load(f) + assert "properties" in schema + + def test_init_from_config(self): + with runner.isolated_filesystem(): + config_content = '{"name": "test", "value": 123}' + + with open("config.json", "w") as f: + f.write(config_content) + + result = runner.invoke(app, ["init", "new_schema.json", "--config", "config.json"]) + assert result.exit_code == 0 + + +class TestCLIHelp: + def test_main_help(self): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "ConfDoc" in result.output + assert "validate" in result.output + assert "doc" in result.output + assert "init" in result.output + + def test_validate_help(self): + result = runner.invoke(app, ["validate", "--help"]) + assert result.exit_code == 0 + assert "config" in result.output + assert "schema" in result.output + + def test_doc_help(self): + result = runner.invoke(app, ["doc", "--help"]) + assert result.exit_code == 0 + assert "schema" in result.output + assert "output" in result.output