186 lines
6.4 KiB
Python
186 lines
6.4 KiB
Python
"""Tests for CLI interface."""
|
|
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from config_converter.cli import main
|
|
|
|
|
|
class TestCLI:
|
|
"""Tests for CLI commands."""
|
|
|
|
def setup_method(self) -> None:
|
|
self.runner = CliRunner()
|
|
|
|
def test_main_help(self) -> None:
|
|
"""Test main command help."""
|
|
result = self.runner.invoke(main, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "Config Converter" in result.output
|
|
|
|
def test_convert_command_help(self) -> None:
|
|
"""Test convert command help."""
|
|
result = self.runner.invoke(main, ["convert", "--help"])
|
|
assert result.exit_code == 0
|
|
assert "SOURCE" in result.output
|
|
|
|
def test_convert_json_to_yaml(self) -> None:
|
|
"""Test converting JSON to YAML."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
|
f.write('{"key": "value", "number": 42}')
|
|
f.flush()
|
|
source = f.name
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
|
|
output = f.name
|
|
f.write("")
|
|
f.flush()
|
|
|
|
try:
|
|
result = self.runner.invoke(main, ["convert", source, "-t", "yaml", "-o", output, "-w"])
|
|
assert result.exit_code == 0
|
|
with open(output) as f:
|
|
content = f.read()
|
|
assert "key: value" in content
|
|
finally:
|
|
Path(source).unlink(missing_ok=True)
|
|
Path(output).unlink(missing_ok=True)
|
|
|
|
def test_convert_yaml_to_toml(self) -> None:
|
|
"""Test converting YAML to TOML."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
|
|
f.write("key: value\nnumber: 42\n")
|
|
f.flush()
|
|
source = f.name
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
|
|
output = f.name
|
|
f.write("")
|
|
f.flush()
|
|
|
|
try:
|
|
result = self.runner.invoke(main, ["convert", source, "-t", "toml", "-o", output, "-w"])
|
|
assert result.exit_code == 0
|
|
with open(output) as f:
|
|
content = f.read()
|
|
assert 'key = "value"' in content
|
|
finally:
|
|
Path(source).unlink(missing_ok=True)
|
|
Path(output).unlink(missing_ok=True)
|
|
|
|
def test_convert_auto_detect_format(self) -> None:
|
|
"""Test auto-detection of format from extension."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
|
f.write('{"key": "value"}')
|
|
f.flush()
|
|
source = f.name
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
|
|
output = f.name
|
|
f.write("")
|
|
f.flush()
|
|
|
|
try:
|
|
result = self.runner.invoke(main, ["convert", source, "-t", "yaml", "-o", output, "-w"])
|
|
assert result.exit_code == 0
|
|
finally:
|
|
Path(source).unlink(missing_ok=True)
|
|
Path(output).unlink(missing_ok=True)
|
|
|
|
def test_infer_command(self) -> None:
|
|
"""Test infer schema command."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
|
f.write('{"name": "test", "value": 42}')
|
|
f.flush()
|
|
source = f.name
|
|
|
|
try:
|
|
result = self.runner.invoke(main, ["infer", source])
|
|
assert result.exit_code == 0
|
|
assert "object" in result.output
|
|
finally:
|
|
Path(source).unlink(missing_ok=True)
|
|
|
|
def test_validate_command(self) -> None:
|
|
"""Test validate command."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
|
f.write('{"name": "test", "value": 42}')
|
|
f.flush()
|
|
source = f.name
|
|
|
|
try:
|
|
result = self.runner.invoke(main, ["validate", source])
|
|
assert result.exit_code == 0
|
|
assert "passed" in result.output.lower()
|
|
finally:
|
|
Path(source).unlink(missing_ok=True)
|
|
|
|
def test_generate_ts_command(self) -> None:
|
|
"""Test generate-ts command."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
|
f.write('{"name": "test", "value": 42}')
|
|
f.flush()
|
|
source = f.name
|
|
|
|
try:
|
|
result = self.runner.invoke(main, ["generate-ts", source])
|
|
assert result.exit_code == 0
|
|
assert "export interface" in result.output
|
|
assert "name: string" in result.output
|
|
finally:
|
|
Path(source).unlink(missing_ok=True)
|
|
|
|
def test_formats_command(self) -> None:
|
|
"""Test formats command."""
|
|
result = self.runner.invoke(main, ["formats"])
|
|
assert result.exit_code == 0
|
|
assert "json" in result.output
|
|
assert "yaml" in result.output
|
|
assert "toml" in result.output
|
|
assert "ini" in result.output
|
|
|
|
def test_batch_command(self) -> None:
|
|
"""Test batch conversion command."""
|
|
import os
|
|
import tempfile
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
for i in range(3):
|
|
json_file = Path(tmpdir) / f"config{i}.json"
|
|
json_file.write_text(f'{{"key{i}": "value{i}"}}')
|
|
|
|
output_dir = Path(tmpdir) / "output"
|
|
output_dir.mkdir()
|
|
|
|
result = self.runner.invoke(main, [
|
|
"batch",
|
|
f"{tmpdir}/*.json",
|
|
"-t", "yaml",
|
|
"-o", str(output_dir),
|
|
])
|
|
assert result.exit_code == 0
|
|
yaml_files = list(output_dir.glob("*.yaml"))
|
|
assert len(yaml_files) == 3
|
|
|
|
def test_color_option(self) -> None:
|
|
"""Test color option."""
|
|
result = self.runner.invoke(main, ["--no-color", "formats"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_convert_unsupported_format(self) -> None:
|
|
"""Test conversion with unsupported format."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
|
f.write('{"key": "value"}')
|
|
f.flush()
|
|
source = f.name
|
|
|
|
try:
|
|
result = self.runner.invoke(main, ["convert", source, "-t", "unsupported"])
|
|
assert result.exit_code != 0
|
|
assert "unsupported" in result.output.lower()
|
|
finally:
|
|
Path(source).unlink(missing_ok=True)
|