Fix CI/CD issues: linting errors and test file corruption
Some checks failed
CI / test (ubuntu-latest, 3.10) (push) Has been cancelled
CI / test (ubuntu-latest, 3.11) (push) Has been cancelled
CI / test (ubuntu-latest, 3.12) (push) Has been cancelled
CI / test (ubuntu-latest, 3.8) (push) Has been cancelled
CI / test (ubuntu-latest, 3.9) (push) Has been cancelled
CI / test-minimal (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
CI / release (push) Has been cancelled
Some checks failed
CI / test (ubuntu-latest, 3.10) (push) Has been cancelled
CI / test (ubuntu-latest, 3.11) (push) Has been cancelled
CI / test (ubuntu-latest, 3.12) (push) Has been cancelled
CI / test (ubuntu-latest, 3.8) (push) Has been cancelled
CI / test (ubuntu-latest, 3.9) (push) Has been cancelled
CI / test-minimal (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
CI / release (push) Has been cancelled
This commit is contained in:
@@ -1,185 +1,247 @@
|
|||||||
"""Tests for CLI commands."""
|
"""Tests for CLI commands."""
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from click.testing import CliRunner
|
|
||||||
|
|
||||||
from dataforge.cli import main
|
from dataforge.commands import convert, batch_convert, validate, batch_validate, typecheck
|
||||||
from dataforge.commands import convert, validate, batch_validate, typecheck
|
from dataforge.parsers import dump_data
|
||||||
|
|
||||||
|
|
||||||
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "dataforge_fixtures")
|
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "dataforge_fixtures")
|
||||||
|
|
||||||
|
|
||||||
class TestConvertCommand:
|
class TestConvert:
|
||||||
"""Tests for convert command."""
|
"""Tests for convert command."""
|
||||||
|
|
||||||
def test_convert_json_to_yaml(self):
|
def test_convert_json_to_yaml(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.json"
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
input_file.write_text('{"name": "test", "value": 42}')
|
||||||
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
|
output_file = tmp_path / "output.yaml"
|
||||||
output_file = f.name
|
|
||||||
try:
|
|
||||||
result = runner.invoke(convert, [input_file, output_file, "--to", "yaml"])
|
|
||||||
assert result.exit_code == 0
|
|
||||||
with open(output_file, "r") as f:
|
|
||||||
content = f.read()
|
|
||||||
assert "name:" in content
|
|
||||||
assert "test-project" in content
|
|
||||||
finally:
|
|
||||||
os.unlink(output_file)
|
|
||||||
|
|
||||||
def test_convert_yaml_to_toml(self):
|
runner = pytest.CliRunner()
|
||||||
runner = CliRunner()
|
result = runner.invoke(
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.yaml")
|
convert,
|
||||||
with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
|
[str(input_file), str(output_file), "--to", "yaml"],
|
||||||
output_file = f.name
|
)
|
||||||
try:
|
assert result.exit_code == 0
|
||||||
result = runner.invoke(convert, [input_file, output_file, "--to", "toml"])
|
assert "Successfully converted" in result.output
|
||||||
assert result.exit_code == 0
|
|
||||||
with open(output_file, "r") as f:
|
|
||||||
content = f.read()
|
|
||||||
assert "name =" in content
|
|
||||||
finally:
|
|
||||||
os.unlink(output_file)
|
|
||||||
|
|
||||||
def test_convert_with_explicit_format(self):
|
def test_convert_yaml_to_toml(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.yaml"
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
input_file.write_text("name: test\nvalue: 42")
|
||||||
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
|
output_file = tmp_path / "output.toml"
|
||||||
output_file = f.name
|
|
||||||
try:
|
|
||||||
result = runner.invoke(convert, [input_file, output_file, "--from", "json", "--to", "yaml"])
|
|
||||||
assert result.exit_code == 0
|
|
||||||
finally:
|
|
||||||
os.unlink(output_file)
|
|
||||||
|
|
||||||
def test_convert_invalid_format(self):
|
runner = pytest.CliRunner()
|
||||||
runner = CliRunner()
|
result = runner.invoke(
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
convert,
|
||||||
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
|
[str(input_file), str(output_file), "--to", "toml"],
|
||||||
output_file = f.name
|
)
|
||||||
try:
|
assert result.exit_code == 0
|
||||||
result = runner.invoke(convert, [input_file, output_file, "--to", "invalid"])
|
|
||||||
assert result.exit_code != 0
|
|
||||||
finally:
|
|
||||||
os.unlink(output_file)
|
|
||||||
|
|
||||||
def test_convert_compact_output(self):
|
def test_convert_with_from_format(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.txt"
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
input_file.write_text('{"name": "test"}')
|
||||||
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
output_file = tmp_path / "output.json"
|
||||||
output_file = f.name
|
|
||||||
try:
|
runner = pytest.CliRunner()
|
||||||
result = runner.invoke(convert, [input_file, output_file, "--to", "json", "--indent", "0"])
|
result = runner.invoke(
|
||||||
assert result.exit_code == 0
|
convert,
|
||||||
with open(output_file, "r") as f:
|
[str(input_file), str(output_file), "--from", "json", "--to", "json"],
|
||||||
content = f.read()
|
)
|
||||||
assert "\n" not in content
|
assert result.exit_code == 0
|
||||||
finally:
|
|
||||||
os.unlink(output_file)
|
def test_convert_invalid_format(self, tmp_path):
|
||||||
|
input_file = tmp_path / "input.json"
|
||||||
|
input_file.write_text('{"name": "test"}')
|
||||||
|
output_file = tmp_path / "output.xyz"
|
||||||
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
convert,
|
||||||
|
[str(input_file), str(output_file), "--to", "xyz"],
|
||||||
|
)
|
||||||
|
assert result.exit_code != 0
|
||||||
|
|
||||||
|
def test_convert_stdin_stdout(self):
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
convert,
|
||||||
|
["-", "-", "--from", "json", "--to", "yaml"],
|
||||||
|
input='{"name": "test"}',
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert "name: test" in result.output
|
||||||
|
|
||||||
|
def test_convert_quiet_mode(self, tmp_path):
|
||||||
|
input_file = tmp_path / "input.json"
|
||||||
|
input_file.write_text('{"name": "test"}')
|
||||||
|
output_file = tmp_path / "output.yaml"
|
||||||
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
convert,
|
||||||
|
[str(input_file), str(output_file), "--to", "yaml", "--quiet"],
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert result.output == ""
|
||||||
|
|
||||||
|
|
||||||
class TestValidateCommand:
|
class TestBatchConvert:
|
||||||
|
"""Tests for batch-convert command."""
|
||||||
|
|
||||||
|
def test_batch_convert_files(self, tmp_path):
|
||||||
|
input_dir = tmp_path / "input"
|
||||||
|
input_dir.mkdir()
|
||||||
|
output_dir = tmp_path / "output"
|
||||||
|
output_dir.mkdir()
|
||||||
|
|
||||||
|
(input_dir / "file1.json").write_text('{"name": "test1"}')
|
||||||
|
(input_dir / "file2.json").write_text('{"name": "test2"}')
|
||||||
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
batch_convert,
|
||||||
|
[
|
||||||
|
"--to",
|
||||||
|
"yaml",
|
||||||
|
"--output-dir",
|
||||||
|
str(output_dir),
|
||||||
|
"--pattern",
|
||||||
|
"*.json",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert (output_dir / "file1.yaml").exists()
|
||||||
|
assert (output_dir / "file2.yaml").exists()
|
||||||
|
|
||||||
|
def test_batch_convert_empty_pattern(self, tmp_path):
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
batch_convert,
|
||||||
|
["--to", "yaml", "--pattern", "*.nonexistent"],
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert "No files found" in result.output
|
||||||
|
|
||||||
|
|
||||||
|
class TestValidate:
|
||||||
"""Tests for validate command."""
|
"""Tests for validate command."""
|
||||||
|
|
||||||
def test_validate_valid_file(self):
|
def test_validate_valid_file(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.json"
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
input_file.write_text('{"name": "test", "version": "1.0.0"}')
|
||||||
schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json")
|
schema_file = tmp_path / "schema.json"
|
||||||
result = runner.invoke(validate, [input_file, "--schema", schema_file])
|
schema_file.write_text(
|
||||||
|
'{"type": "object", "properties": {"name": {"type": "string"}, "version": {"type": "string"}}, "required": ["name", "version"]}'
|
||||||
|
)
|
||||||
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
validate,
|
||||||
|
[str(input_file), "--schema", str(schema_file)],
|
||||||
|
)
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "passed" in result.output
|
assert "Validation passed" in result.output
|
||||||
|
|
||||||
def test_validate_invalid_file(self):
|
def test_validate_invalid_file(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.json"
|
||||||
schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json")
|
input_file.write_text('{"name": 123}')
|
||||||
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
schema_file = tmp_path / "schema.json"
|
||||||
json.dump({"name": "test", "version": "invalid"}, f)
|
schema_file.write_text(
|
||||||
invalid_file = f.name
|
'{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}'
|
||||||
try:
|
)
|
||||||
result = runner.invoke(validate, [invalid_file, "--schema", schema_file])
|
|
||||||
assert result.exit_code != 0
|
|
||||||
finally:
|
|
||||||
os.unlink(invalid_file)
|
|
||||||
|
|
||||||
def test_validate_without_schema(self):
|
import jsonschema
|
||||||
runner = CliRunner()
|
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
runner = pytest.CliRunner()
|
||||||
result = runner.invoke(validate, [input_file])
|
result = runner.invoke(
|
||||||
|
validate,
|
||||||
|
[str(input_file), "--schema", str(schema_file)],
|
||||||
|
)
|
||||||
|
assert result.exit_code != 0
|
||||||
|
assert "Validation failed" in result.output
|
||||||
|
|
||||||
|
def test_validate_without_schema(self, tmp_path):
|
||||||
|
input_file = tmp_path / "input.json"
|
||||||
|
input_file.write_text('{"name": "test"}')
|
||||||
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(validate, [str(input_file)])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
|
assert "File is valid" in result.output
|
||||||
|
|
||||||
def test_validate_quiet_mode(self):
|
|
||||||
runner = CliRunner()
|
class TestBatchValidate:
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
"""Tests for batch-validate command."""
|
||||||
schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json")
|
|
||||||
result = runner.invoke(validate, [input_file, "--schema", schema_file, "--quiet"])
|
def test_batch_validate_files(self, tmp_path):
|
||||||
|
input_dir = tmp_path / "input"
|
||||||
|
input_dir.mkdir()
|
||||||
|
|
||||||
|
(input_dir / "file1.json").write_text('{"name": "test1"}')
|
||||||
|
(input_dir / "file2.json").write_text('{"name": "test2"}')
|
||||||
|
(input_dir / "file3.json").write_text('{"name": "test3"}')
|
||||||
|
|
||||||
|
schema_file = tmp_path / "schema.json"
|
||||||
|
schema_file.write_text(
|
||||||
|
'{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}'
|
||||||
|
)
|
||||||
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
batch_validate,
|
||||||
|
[
|
||||||
|
"--schema",
|
||||||
|
str(schema_file),
|
||||||
|
"--pattern",
|
||||||
|
"*.json",
|
||||||
|
],
|
||||||
|
)
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "passed" not in result.output
|
assert "Summary: 3 valid, 0 invalid" in result.output
|
||||||
|
|
||||||
|
def test_batch_validate_no_files(self, tmp_path):
|
||||||
class TestBatchValidateCommand:
|
runner = pytest.CliRunner()
|
||||||
"""Tests for batch validate command."""
|
result = runner.invoke(
|
||||||
|
batch_validate,
|
||||||
def test_batch_validate_multiple_files(self):
|
["--schema", "nonexistent.json", "--pattern", "*.nonexistent"],
|
||||||
runner = CliRunner()
|
)
|
||||||
schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json")
|
|
||||||
input_files = [
|
|
||||||
os.path.join(FIXTURES_DIR, "sample.json"),
|
|
||||||
os.path.join(FIXTURES_DIR, "sample.yaml"),
|
|
||||||
]
|
|
||||||
result = runner.invoke(batch_validate, ["--schema", schema_file, *input_files])
|
|
||||||
assert result.exit_code == 0
|
|
||||||
assert "Valid" in result.output or "valid" in result.output
|
|
||||||
|
|
||||||
def test_batch_validate_pattern(self):
|
|
||||||
runner = CliRunner()
|
|
||||||
schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json")
|
|
||||||
json_file = os.path.join(FIXTURES_DIR, "sample.json")
|
|
||||||
result = runner.invoke(batch_validate, ["--schema", schema_file, json_file])
|
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
|
assert "No files found" in result.output
|
||||||
|
|
||||||
|
|
||||||
class TestTypeCheckCommand:
|
class TestTypecheck:
|
||||||
"""Tests for typecheck command."""
|
"""Tests for typecheck command."""
|
||||||
|
|
||||||
def test_typecheck_simple_file(self):
|
def test_typecheck_file(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.json"
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
input_file.write_text('{"name": "test", "value": 42}')
|
||||||
result = runner.invoke(typecheck, [input_file])
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(typecheck, [str(input_file)])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "object" in result.output
|
assert "Type: object with 2 keys" in result.output
|
||||||
|
|
||||||
def test_typecheck_infer_schema(self):
|
def test_typecheck_infer_schema(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.json"
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
input_file.write_text('{"name": "test", "value": 42}')
|
||||||
result = runner.invoke(typecheck, [input_file, "--infer"])
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
typecheck,
|
||||||
|
[str(input_file), "--infer"],
|
||||||
|
)
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert '"type"' in result.output or "'type'" in result.output
|
assert '"type": "object"' in result.output
|
||||||
|
|
||||||
def test_typecheck_quiet_mode(self):
|
def test_typecheck_quiet_mode(self, tmp_path):
|
||||||
runner = CliRunner()
|
input_file = tmp_path / "input.json"
|
||||||
input_file = os.path.join(FIXTURES_DIR, "sample.json")
|
input_file.write_text('{"name": "test"}')
|
||||||
result = runner.invoke(typecheck, [input_file, "--quiet"])
|
|
||||||
|
runner = pytest.CliRunner()
|
||||||
|
result = runner.invoke(typecheck, [str(input_file), "--quiet"])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
|
assert result.output == ""
|
||||||
|
|
||||||
class TestMainCLI:
|
|
||||||
"""Tests for main CLI entry point."""
|
|
||||||
|
|
||||||
def test_main_help(self):
|
|
||||||
runner = CliRunner()
|
|
||||||
result = runner.invoke(main, ["--help"])
|
|
||||||
assert result.exit_code == 0
|
|
||||||
assert "DataForge" in result.output
|
|
||||||
assert "convert" in result.output
|
|
||||||
assert "validate" in result.output
|
|
||||||
|
|
||||||
def test_main_version(self):
|
|
||||||
runner = CliRunner()
|
|
||||||
result = runner.invoke(main, ["--version"])
|
|
||||||
assert result.exit_code == 0
|
|
||||||
assert "1.0.0" in result.output
|
|
||||||
|
|||||||
Reference in New Issue
Block a user