File: test_etools_to_clt.py

package info (click to toggle)
cwl-utils 0.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,164 kB
  • sloc: python: 88,875; makefile: 141; javascript: 91
file content (265 lines) | stat: -rw-r--r-- 8,865 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# SPDX-License-Identifier: Apache-2.0
"""Test the CWL Expression refactoring tool."""
import os
import shutil
import sys
import tarfile
from collections.abc import Generator
from pathlib import Path
from typing import TYPE_CHECKING, cast

import pytest
import requests
from _pytest.tmpdir import TempPathFactory
from pytest import raises

import cwl_utils.parser.cwl_v1_0 as parser
import cwl_utils.parser.cwl_v1_1 as parser1
import cwl_utils.parser.cwl_v1_2 as parser2
from cwl_utils.cwl_v1_0_expression_refactor import traverse as traverse0
from cwl_utils.cwl_v1_1_expression_refactor import traverse as traverse1
from cwl_utils.cwl_v1_2_expression_refactor import traverse as traverse2
from cwl_utils.errors import WorkflowException
from cwl_utils.expression_refactor import run as expression_refactor

from .util import get_data

if TYPE_CHECKING:
    from http.client import HTTPResponse


def test_v1_0_workflow_top_level_format_expr() -> None:
    """Test for the correct error when converting a format expression in a workflow level input."""
    with raises(WorkflowException, match=r".*format specification.*"):
        result, modified = traverse0(
            parser.load_document(get_data("testdata/workflow_input_format_expr.cwl")),
            False,
            False,
            False,
            False,
        )


def test_v1_0_workflow_top_level_sf_expr() -> None:
    """Test for the correct error when converting a secondaryFiles expression in a workflow level input."""
    with raises(WorkflowException, match=r".*secondaryFiles.*"):
        result, modified = traverse0(
            parser.load_document(get_data("testdata/workflow_input_sf_expr.cwl")),
            False,
            False,
            False,
            False,
        )


def test_v1_0_workflow_top_level_sf_expr_array() -> None:
    """Test correct error when converting a secondaryFiles expression (array form) in a workflow level input."""  # noqa: B950
    with raises(WorkflowException, match=r".*secondaryFiles.*"):
        result, modified = traverse0(
            parser.load_document(get_data("testdata/workflow_input_sf_expr_array.cwl")),
            False,
            False,
            False,
            False,
        )


def test_v1_1_workflow_top_level_format_expr() -> None:
    """Test for the correct error when converting a format expression in a workflow level input."""
    with raises(WorkflowException, match=r".*format specification.*"):
        result, modified = traverse1(
            parser1.load_document(
                get_data("testdata/workflow_input_format_expr_v1_1.cwl")
            ),
            False,
            False,
            False,
            False,
        )


def test_v1_1_workflow_top_level_sf_expr() -> None:
    """Test for the correct error when converting a secondaryFiles expression in a workflow level input."""
    with raises(WorkflowException, match=r".*secondaryFiles.*"):
        result, modified = traverse1(
            parser1.load_document(get_data("testdata/workflow_input_sf_expr_v1_1.cwl")),
            False,
            False,
            False,
            False,
        )


def test_v1_1_workflow_top_level_sf_expr_array() -> None:
    """Test for the correct error when converting a secondaryFiles expression (array form) in a workflow level input."""  # noqa: B950
    with raises(WorkflowException, match=r".*secondaryFiles.*"):
        result, modified = traverse1(
            parser1.load_document(
                get_data("testdata/workflow_input_sf_expr_array_v1_1.cwl")
            ),
            False,
            False,
            False,
            False,
        )


def test_v1_2_workflow_top_level_format_expr() -> None:
    """Test for the correct error when converting a format expression in a workflow level input."""
    with raises(WorkflowException, match=r".*format specification.*"):
        result, modified = traverse2(
            parser2.load_document(
                get_data("testdata/workflow_input_format_expr_v1_2.cwl")
            ),
            False,
            False,
            False,
            False,
        )


def test_v1_2_workflow_top_level_sf_expr() -> None:
    """Test for the correct error when converting a secondaryFiles expression in a workflow level input."""
    with raises(WorkflowException, match=r".*secondaryFiles.*"):
        result, modified = traverse2(
            parser2.load_document(get_data("testdata/workflow_input_sf_expr_v1_2.cwl")),
            False,
            False,
            False,
            False,
        )


def test_v1_2_workflow_top_level_sf_expr_array() -> None:
    """Test for the correct error when converting a secondaryFiles expression (array form) in a workflow level input."""  # noqa: B950
    with raises(WorkflowException, match=r".*secondaryFiles.*"):
        result, modified = traverse2(
            parser2.load_document(
                get_data("testdata/workflow_input_sf_expr_array_v1_2.cwl")
            ),
            False,
            False,
            False,
            False,
        )


def test_v1_0_step_valuefrom_expr_multisource() -> None:
    """Convert a valueFrom expression that has multiple sources."""
    result, modified = traverse0(
        parser.load_document(get_data("testdata/step-valuefrom2-wf_v1_0.cwl")),
        False,
        False,
        False,
        False,
    )


def test_v1_1_step_valuefrom_expr_multisource() -> None:
    """Convert a valueFrom expression that has multiple sources."""
    result, modified = traverse1(
        parser1.load_document(get_data("testdata/step-valuefrom2-wf_v1_1.cwl")),
        False,
        False,
        False,
        False,
    )


def test_v1_2_step_valuefrom_expr_multisource() -> None:
    """Convert a valueFrom expression that has multiple sources."""
    result, modified = traverse2(
        parser2.load_document(get_data("testdata/step-valuefrom2-wf_v1_2.cwl")),
        False,
        False,
        False,
        False,
    )


def test_v1_0_step_valuefrom_expr_sibling_inputs() -> None:
    """Convert a valueFrom expression from a step input that has uninvolved sibling inputs."""
    result, modified = traverse0(
        parser.load_document(get_data("testdata/step-valuefrom3-wf_v1_0.cwl")),
        False,
        False,
        False,
        False,
    )


def test_v1_1_step_valuefrom_expr_sibling_inputs() -> None:
    """Convert a valueFrom expression from a step input that has uninvolved sibling inputs."""
    result, modified = traverse1(
        parser1.load_document(get_data("testdata/step-valuefrom3-wf_v1_1.cwl")),
        False,
        False,
        False,
        False,
    )


def test_v1_2_step_valuefrom_expr_sibling_inputs() -> None:
    """Convert a valueFrom expression from a step input that has uninvolved sibling inputs."""
    result, modified = traverse2(
        parser2.load_document(get_data("testdata/step-valuefrom3-wf_v1_2.cwl")),
        False,
        False,
        False,
        False,
    )


def test_v1_2_workflow_output_pickvalue_expr() -> None:
    """Convert a workflow output pickValue expression."""
    result, modified = traverse2(
        parser2.load_document(get_data("testdata/cond-wf-003.1.cwl")),
        False,
        False,
        False,
        False,
    )


def test_expression_refactor(tmp_path: Path) -> None:
    """Functional test."""
    input_path = get_data("testdata/cond-wf-003.1.cwl")
    result = expression_refactor([str(tmp_path), input_path])
    assert result == 0


def test_expression_refactor_noop_solo(tmp_path: Path) -> None:
    """Functional test."""
    input_path = get_data("testdata/dockstore-tool-md5sum.cwl")
    result = expression_refactor([str(tmp_path), input_path])
    assert result == 7


def test_expression_refactor_noop(tmp_path: Path) -> None:
    """Functional test."""
    input_path1 = get_data("testdata/dockstore-tool-md5sum.cwl")
    input_path2 = get_data("testdata/echo-tool-packed.cwl")
    result = expression_refactor([str(tmp_path), input_path1, input_path2])
    assert result == 0


@pytest.fixture(scope="session")
def cwl_v1_0_dir(
    tmp_path_factory: TempPathFactory,
) -> Generator[str, None, None]:
    """Download the CWL 1.0.2 specs and return a path to the directory."""
    tmp_path = tmp_path_factory.mktemp("cwl_v1_0_dir")
    with cast(
        "HTTPResponse",
        requests.get(
            "https://github.com/common-workflow-language/common-workflow-language/archive/v1.0.2.tar.gz",
            stream=True,
        ).raw,
    ) as specfileobj:
        tf = tarfile.open(fileobj=specfileobj)
        if sys.version_info > (3, 12):
            tf.extractall(path=tmp_path, filter="data")
        else:
            tf.extractall(path=tmp_path)
    yield str(tmp_path / "common-workflow-language-1.0.2")
    shutil.rmtree(os.path.join(tmp_path))