|
| 1 | +"""Step definitions for the IR interchange CLI.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import tempfile |
| 7 | +from pathlib import Path |
| 8 | +from typing import TYPE_CHECKING |
| 9 | + |
| 10 | +from behave import given, then, when |
| 11 | +from typer.testing import CliRunner |
| 12 | + |
| 13 | +from morphir_tools.cli import app |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from behave.runner import Context |
| 17 | + |
| 18 | +FIXTURES = Path(__file__).resolve().parents[2] / "tests" / "fixtures" / "ir" / "v3" |
| 19 | + |
| 20 | + |
| 21 | +def _workdir(context: Context) -> Path: |
| 22 | + if not hasattr(context, "workdir"): |
| 23 | + context.workdir = Path(tempfile.mkdtemp(prefix="morphir-bdd-")) |
| 24 | + return context.workdir |
| 25 | + |
| 26 | + |
| 27 | +def _run(context: Context, *args: str) -> None: |
| 28 | + context.result = CliRunner().invoke(app, list(args)) |
| 29 | + |
| 30 | + |
| 31 | +@given('the golden fixture "{name}"') |
| 32 | +def step_fixture(context: Context, name: str) -> None: |
| 33 | + """Select a golden format-3 fixture as input.""" |
| 34 | + context.input = FIXTURES / name |
| 35 | + context.fixture = context.input |
| 36 | + |
| 37 | + |
| 38 | +@given("a file with the JSON '{text}'") |
| 39 | +def step_json_file(context: Context, text: str) -> None: |
| 40 | + """Write JSON text to an input file.""" |
| 41 | + path = _workdir(context) / "input.json" |
| 42 | + path.write_text(text) |
| 43 | + context.input = path |
| 44 | + |
| 45 | + |
| 46 | +@when('I run "ir inspect" on it') |
| 47 | +def step_inspect(context: Context) -> None: |
| 48 | + """Inspect the input file.""" |
| 49 | + _run(context, "ir", "inspect", str(context.input)) |
| 50 | + |
| 51 | + |
| 52 | +@when('I convert it to "{target}"') |
| 53 | +def step_convert(context: Context, target: str) -> None: |
| 54 | + """Convert the input file to the target version.""" |
| 55 | + context.output = _workdir(context) / f"converted-{target}.json" |
| 56 | + _run( |
| 57 | + context, |
| 58 | + "ir", |
| 59 | + "convert", |
| 60 | + str(context.input), |
| 61 | + "--to", |
| 62 | + target, |
| 63 | + "-o", |
| 64 | + str(context.output), |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +@when("I inspect the converted file") |
| 69 | +def step_inspect_converted(context: Context) -> None: |
| 70 | + """Inspect the converted file.""" |
| 71 | + _run(context, "ir", "inspect", str(context.output)) |
| 72 | + |
| 73 | + |
| 74 | +@when('I convert the converted file to "{target}"') |
| 75 | +def step_convert_converted(context: Context, target: str) -> None: |
| 76 | + """Convert the converted file again.""" |
| 77 | + context.input = context.output |
| 78 | + step_convert(context, target) |
| 79 | + |
| 80 | + |
| 81 | +@then("the command exits with code {code:d}") |
| 82 | +def step_exit_code(context: Context, code: int) -> None: |
| 83 | + """Check the exit code.""" |
| 84 | + assert context.result.exit_code == code, context.result.output |
| 85 | + |
| 86 | + |
| 87 | +@then('the output contains "{text}"') |
| 88 | +def step_output_contains(context: Context, text: str) -> None: |
| 89 | + """Check standard output.""" |
| 90 | + assert text in context.result.stdout, context.result.stdout |
| 91 | + |
| 92 | + |
| 93 | +@then('the errors contain "{text}"') |
| 94 | +def step_errors_contain(context: Context, text: str) -> None: |
| 95 | + """Check standard error.""" |
| 96 | + assert text in context.result.stderr, context.result.stderr |
| 97 | + |
| 98 | + |
| 99 | +@then("the converted file declares formatVersion {version:d}") |
| 100 | +def step_declares_version(context: Context, version: int) -> None: |
| 101 | + """Check the converted file's formatVersion.""" |
| 102 | + assert json.loads(context.output.read_text())["formatVersion"] == version |
| 103 | + |
| 104 | + |
| 105 | +@then("the converted file has the same JSON as the fixture") |
| 106 | +def step_same_json(context: Context) -> None: |
| 107 | + """Check the converted file matches the fixture JSON.""" |
| 108 | + assert json.loads(context.output.read_text()) == json.loads( |
| 109 | + context.fixture.read_text() |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +@then("no converted file exists") |
| 114 | +def step_no_output(context: Context) -> None: |
| 115 | + """Check no output file was written.""" |
| 116 | + assert not context.output.exists() |
0 commit comments