|
1 | 1 | # SPDX-License-Identifier: Apache-2.0
|
2 | 2 | """Test the CWL parsers utility functions."""
|
| 3 | +import tempfile |
3 | 4 | from pathlib import Path
|
4 | 5 |
|
5 | 6 | from pytest import raises
|
|
11 | 12 | import cwl_utils.parser.cwl_v1_1_utils
|
12 | 13 | import cwl_utils.parser.cwl_v1_2
|
13 | 14 | import cwl_utils.parser.cwl_v1_2_utils
|
| 15 | +from cwl_utils.errors import WorkflowException |
14 | 16 | from cwl_utils.parser import load_document_by_uri
|
15 | 17 |
|
16 | 18 | HERE = Path(__file__).resolve().parent
|
17 | 19 |
|
18 | 20 |
|
| 21 | +def test_v1_0_file_content_64_kB() -> None: |
| 22 | + """Test that reading file content is allowed up to 64kB in CWL v1.0.""" |
| 23 | + text = "a" * cwl_utils.parser.cwl_v1_0_utils.CONTENT_LIMIT |
| 24 | + with tempfile.TemporaryFile() as f: |
| 25 | + f.write(text.encode("utf-8")) |
| 26 | + f.seek(0) |
| 27 | + content = cwl_utils.parser.cwl_v1_0_utils.content_limit_respected_read(f) |
| 28 | + assert content == text |
| 29 | + |
| 30 | + |
| 31 | +def test_v1_0_file_content_larger_than_64_kB() -> None: |
| 32 | + """Test that reading file content is truncated to 64kB for larger files in CWL v1.0.""" |
| 33 | + text = "a" * (cwl_utils.parser.cwl_v1_0_utils.CONTENT_LIMIT + 1) |
| 34 | + with tempfile.TemporaryFile() as f: |
| 35 | + f.write(text.encode("utf-8")) |
| 36 | + f.seek(0) |
| 37 | + content = cwl_utils.parser.cwl_v1_0_utils.content_limit_respected_read(f) |
| 38 | + assert content == text[0 : cwl_utils.parser.cwl_v1_0_utils.CONTENT_LIMIT] |
| 39 | + |
| 40 | + |
19 | 41 | def test_v1_0_stdout_to_file() -> None:
|
20 | 42 | """Test that stdout shortcut is converted to stdout parameter with CWL v1.0."""
|
21 | 43 | clt = cwl_utils.parser.cwl_v1_0.CommandLineTool(
|
@@ -130,6 +152,26 @@ def test_v1_0_type_for_source_with_id() -> None:
|
130 | 152 | assert source_type == "File"
|
131 | 153 |
|
132 | 154 |
|
| 155 | +def test_v1_1_file_content_64_kB() -> None: |
| 156 | + """Test that reading file content is allowed up to 64kB in CWL v1.1.""" |
| 157 | + text = "a" * cwl_utils.parser.cwl_v1_1_utils.CONTENT_LIMIT |
| 158 | + with tempfile.TemporaryFile() as f: |
| 159 | + f.write(text.encode("utf-8")) |
| 160 | + f.seek(0) |
| 161 | + content = cwl_utils.parser.cwl_v1_1_utils.content_limit_respected_read(f) |
| 162 | + assert content == text |
| 163 | + |
| 164 | + |
| 165 | +def test_v1_1_file_content_larger_than_64_kB() -> None: |
| 166 | + """Test that reading file content is truncated to 64kB for larger files in CWL v1.1.""" |
| 167 | + text = "a" * (cwl_utils.parser.cwl_v1_1_utils.CONTENT_LIMIT + 1) |
| 168 | + with tempfile.TemporaryFile() as f: |
| 169 | + f.write(text.encode("utf-8")) |
| 170 | + f.seek(0) |
| 171 | + content = cwl_utils.parser.cwl_v1_1_utils.content_limit_respected_read(f) |
| 172 | + assert content == text[0 : cwl_utils.parser.cwl_v1_1_utils.CONTENT_LIMIT] |
| 173 | + |
| 174 | + |
133 | 175 | def test_v1_1_stdout_to_file() -> None:
|
134 | 176 | """Test that stdout shortcut is converted to stdout parameter with CWL v1.1."""
|
135 | 177 | clt = cwl_utils.parser.cwl_v1_1.CommandLineTool(
|
@@ -287,6 +329,26 @@ def test_v1_1_type_for_source_with_id() -> None:
|
287 | 329 | assert source_type == "File"
|
288 | 330 |
|
289 | 331 |
|
| 332 | +def test_v1_2_file_content_64_kB() -> None: |
| 333 | + """Test that reading file content is allowed up to 64kB in CWL v1.2.""" |
| 334 | + text = "a" * cwl_utils.parser.cwl_v1_2_utils.CONTENT_LIMIT |
| 335 | + with tempfile.TemporaryFile() as f: |
| 336 | + f.write(text.encode("utf-8")) |
| 337 | + f.seek(0) |
| 338 | + content = cwl_utils.parser.cwl_v1_2_utils.content_limit_respected_read(f) |
| 339 | + assert content == text |
| 340 | + |
| 341 | + |
| 342 | +def test_v1_2_file_content_larger_than_64_kB() -> None: |
| 343 | + """Test that reading file content fails for files larger than 64kB in CWL v1.0.""" |
| 344 | + with raises(WorkflowException): |
| 345 | + text = "a" * (cwl_utils.parser.cwl_v1_2_utils.CONTENT_LIMIT + 1) |
| 346 | + with tempfile.TemporaryFile() as f: |
| 347 | + f.write(text.encode("utf-8")) |
| 348 | + f.seek(0) |
| 349 | + cwl_utils.parser.cwl_v1_2_utils.content_limit_respected_read(f) |
| 350 | + |
| 351 | + |
290 | 352 | def test_v1_2_stdout_to_file() -> None:
|
291 | 353 | """Test that stdout shortcut is converted to stdout parameter with CWL v1.2."""
|
292 | 354 | clt = cwl_utils.parser.cwl_v1_2.CommandLineTool(
|
|
0 commit comments