Skip to content

Commit

Permalink
test check
Browse files Browse the repository at this point in the history
  • Loading branch information
sfoster1 committed Apr 26, 2024
1 parent b815a94 commit 4295882
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
16 changes: 10 additions & 6 deletions api/src/opentrons/cli/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ProtocolType,
JsonProtocolConfig,
ProtocolFilesInvalidError,
ProtocolSource,
)
from opentrons.protocol_runner import create_simulating_runner, RunResult
from opentrons.protocol_engine import (
Expand Down Expand Up @@ -198,11 +199,18 @@ def _get_return_code(analysis: RunResult) -> int:


@track_analysis
async def _do_analyze(protocol_source: ProtocolSource) -> RunResult:

runner = await create_simulating_runner(
robot_type=protocol_source.robot_type, protocol_config=protocol_source.config
)
return await runner.run(deck_configuration=[], protocol_source=protocol_source)


async def _analyze(
files_and_dirs: Sequence[Path], outputs: Sequence[_Output], check: bool
) -> int:
input_files = _get_input_files(files_and_dirs)

try:
protocol_source = await ProtocolReader().read_saved(
files=input_files,
Expand All @@ -211,11 +219,7 @@ async def _analyze(
except ProtocolFilesInvalidError as error:
raise click.ClickException(str(error))

runner = await create_simulating_runner(
robot_type=protocol_source.robot_type, protocol_config=protocol_source.config
)
analysis = await runner.run(deck_configuration=[], protocol_source=protocol_source)

analysis = await _do_analyze(protocol_source)
return_code = _get_return_code(analysis)

if not outputs:
Expand Down
33 changes: 21 additions & 12 deletions api/tests/opentrons/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class _AnalysisCLIResult:


def _get_analysis_result(
protocol_files: List[Path], output_type: str
protocol_files: List[Path], output_type: str, check: bool = False
) -> _AnalysisCLIResult:
"""Run `protocol_files` as a single protocol through the analysis CLI.
Expand All @@ -66,14 +66,15 @@ def _get_analysis_result(
with tempfile.TemporaryDirectory() as temp_dir:
analysis_output_file = Path(temp_dir) / "analysis_output.json"
runner = CliRunner()
result = runner.invoke(
analyze,
[
output_type,
str(analysis_output_file),
*[str(p.resolve()) for p in protocol_files],
],
)
args = [
output_type,
str(analysis_output_file),
*[str(p.resolve()) for p in protocol_files],
]
if check:
args.append("--check")

result = runner.invoke(analyze, args)
if analysis_output_file.exists():
json_output = json.loads(analysis_output_file.read_bytes())
else:
Expand Down Expand Up @@ -216,6 +217,7 @@ def run(protocol):


@pytest.mark.parametrize("output", ["--json-output", "--human-json-output"])
@pytest.mark.parametrize("check", [True, False])
@pytest.mark.parametrize(
("python_protocol_source", "expected_detail"),
[
Expand Down Expand Up @@ -264,15 +266,22 @@ def run(protocol): # line 3
],
)
def test_python_error_line_numbers(
tmp_path: Path, python_protocol_source: str, expected_detail: str, output: str
tmp_path: Path,
python_protocol_source: str,
expected_detail: str,
output: str,
check: bool,
) -> None:
"""Test that error messages from Python protocols have line numbers."""
protocol_source_file = tmp_path / "protocol.py"
protocol_source_file.write_text(python_protocol_source, encoding="utf-8")

result = _get_analysis_result([protocol_source_file], output)
result = _get_analysis_result([protocol_source_file], output, check)

assert result.exit_code == 0
if check:
assert result.exit_code != 0
else:
assert result.exit_code == 0
assert result.json_output is not None
[error] = result.json_output["errors"]
assert error["detail"] == expected_detail
Expand Down

0 comments on commit 4295882

Please sign in to comment.