|
| 1 | +"""Tests for the ContainerImageShape linter and the lint_user_tool_source helper.""" |
| 2 | + |
| 3 | +from copy import deepcopy |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from galaxy.tool_util.lint import ( |
| 8 | + get_lint_context_for_tool_source, |
| 9 | + lint_user_tool_source, |
| 10 | +) |
| 11 | +from galaxy.tool_util.linters.containers import ContainerImageShape |
| 12 | +from galaxy.tool_util.parser.yaml import YamlToolSource |
| 13 | +from galaxy.tool_util_models import UserToolSource |
| 14 | + |
| 15 | +VALID_TOOL = { |
| 16 | + "class": "GalaxyUserTool", |
| 17 | + "id": "my-cool-tool", |
| 18 | + "name": "My Cool Tool", |
| 19 | + "version": "0.1.0", |
| 20 | + "description": "A cool tool.", |
| 21 | + "container": "quay.io/biocontainers/python:3.13", |
| 22 | + "shell_command": "head -n '$(inputs.n_lines)' '$(inputs.data_input.path)' > out.txt", |
| 23 | + "inputs": [ |
| 24 | + {"type": "integer", "name": "n_lines"}, |
| 25 | + {"type": "data", "name": "data_input"}, |
| 26 | + ], |
| 27 | + "outputs": [ |
| 28 | + {"type": "data", "name": "out", "from_work_dir": "out.txt"}, |
| 29 | + ], |
| 30 | + "citations": [ |
| 31 | + {"type": "doi", "content": "10.1234/abc.def"}, |
| 32 | + ], |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def _doc(**overrides): |
| 37 | + base = deepcopy(VALID_TOOL) |
| 38 | + base.update(overrides) |
| 39 | + return base |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "container", |
| 44 | + [ |
| 45 | + "quay.io/biocontainers/samtools:1.17", |
| 46 | + "docker://my-registry/image:tag", |
| 47 | + "oras://example.org/image", |
| 48 | + "busybox", |
| 49 | + "ubuntu:latest", |
| 50 | + "library/python:3.11-slim", |
| 51 | + ], |
| 52 | +) |
| 53 | +def test_valid_container_shapes_pass_lint(container): |
| 54 | + tool_source = YamlToolSource(_doc(container=container)) |
| 55 | + ctx = get_lint_context_for_tool_source(tool_source) |
| 56 | + container_warns = [m for m in ctx.warn_messages if m.linter == ContainerImageShape.name()] |
| 57 | + assert container_warns == [] |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("bad_container", ["definitely not a container", "foo bar baz"]) |
| 61 | +def test_invalid_container_shapes_warn(bad_container): |
| 62 | + tool_source = YamlToolSource(_doc(container=bad_container)) |
| 63 | + ctx = get_lint_context_for_tool_source(tool_source) |
| 64 | + container_warns = [m for m in ctx.warn_messages if m.linter == ContainerImageShape.name()] |
| 65 | + assert len(container_warns) == 1 |
| 66 | + assert "does not match a recognized shape" in container_warns[0].message |
| 67 | + |
| 68 | + |
| 69 | +def test_lint_user_tool_source_returns_empty_on_clean_tool(): |
| 70 | + user_tool = UserToolSource.model_validate(VALID_TOOL) |
| 71 | + assert lint_user_tool_source(user_tool) == [] |
| 72 | + |
| 73 | + |
| 74 | +def test_lint_user_tool_source_surfaces_container_shape_failure(): |
| 75 | + user_tool = UserToolSource.model_validate(_doc(container="totally bogus value")) |
| 76 | + bullets = lint_user_tool_source(user_tool) |
| 77 | + assert any("does not match a recognized shape" in b for b in bullets) |
| 78 | + assert any(b.startswith(f"{ContainerImageShape.name()}:") for b in bullets) |
0 commit comments