From e7d738569c3215ece224d567e703f9dcc6b6121d Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Sun, 29 Jan 2023 13:50:13 +0100 Subject: [PATCH 1/5] add unit test for is_a_yaml_with_class as used in planemo --- test/unit/tool_util/test_loader_directory.py | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/unit/tool_util/test_loader_directory.py diff --git a/test/unit/tool_util/test_loader_directory.py b/test/unit/tool_util/test_loader_directory.py new file mode 100644 index 000000000000..8a35da215d8b --- /dev/null +++ b/test/unit/tool_util/test_loader_directory.py @@ -0,0 +1,23 @@ +import os +import tempfile + +from galaxy.tool_util.loader_directory import is_a_yaml_with_class + +def test_is_a_yaml_with_class(): + with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as tf: + fname = tf.name + tf.write("""class: GalaxyWorkflow +name: "Test Workflow" +inputs: + - id: input1 +outputs: + - id: wf_output_1 + outputSource: first_cat/out_file1 +steps: + - tool_id: cat + label: first_cat + in: + input1: input1""") + + assert is_a_yaml_with_class(fname, ["GalaxyWorkflow"]) + os.unlink(fname) \ No newline at end of file From 4c4cc2c75bc998b1f52f6b3d6e0e3c2c34337cec Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Sun, 29 Jan 2023 13:14:30 +0100 Subject: [PATCH 2/5] fix looks_like_yaml_or_cwl_with_class the string `class: ...` might appear on the first line hence the leading `\n` was wrong I guess its better to use `^` and `$` and add the MULTILINE flag --- lib/galaxy/tool_util/loader_directory.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/galaxy/tool_util/loader_directory.py b/lib/galaxy/tool_util/loader_directory.py index fd0e73e58f5b..1491f5ab047a 100644 --- a/lib/galaxy/tool_util/loader_directory.py +++ b/lib/galaxy/tool_util/loader_directory.py @@ -190,7 +190,7 @@ def looks_like_a_data_manager_xml(path): def as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes): """ - get a dict from yaml file if it contains `class: CLASS`, where CLASS is + get a dict from yaml file if it contains a line `class: CLASS`, where CLASS is any string given in CLASSES. must appear in the first 5k and also load properly in total. """ @@ -199,7 +199,7 @@ def as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes): start_contents = f.read(5 * 1024) except UnicodeDecodeError: return False, None - if re.search(rf"\nclass:\s+({'|'.join(classes)})\s*\n", start_contents) is None: + if re.search(rf"^class:\s+{'|'.join(classes)}\s*$", start_contents, re.MULTILINE) is None: return False, None with open(path) as f: From 63b0c7e23b2614341646cef07be6c602017547d7 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Sun, 29 Jan 2023 15:45:21 +0100 Subject: [PATCH 3/5] linter fixes --- test/unit/tool_util/test_loader_directory.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/tool_util/test_loader_directory.py b/test/unit/tool_util/test_loader_directory.py index 8a35da215d8b..3b808caa1c6b 100644 --- a/test/unit/tool_util/test_loader_directory.py +++ b/test/unit/tool_util/test_loader_directory.py @@ -3,6 +3,7 @@ from galaxy.tool_util.loader_directory import is_a_yaml_with_class + def test_is_a_yaml_with_class(): with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as tf: fname = tf.name @@ -20,4 +21,4 @@ def test_is_a_yaml_with_class(): input1: input1""") assert is_a_yaml_with_class(fname, ["GalaxyWorkflow"]) - os.unlink(fname) \ No newline at end of file + os.unlink(fname) From 92d06c26e7fc13db96e798ae3e64a20923005a20 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Sun, 29 Jan 2023 16:05:32 +0100 Subject: [PATCH 4/5] black formatting fixes --- test/unit/tool_util/test_loader_directory.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/unit/tool_util/test_loader_directory.py b/test/unit/tool_util/test_loader_directory.py index 3b808caa1c6b..6b25364349a1 100644 --- a/test/unit/tool_util/test_loader_directory.py +++ b/test/unit/tool_util/test_loader_directory.py @@ -7,7 +7,8 @@ def test_is_a_yaml_with_class(): with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as tf: fname = tf.name - tf.write("""class: GalaxyWorkflow + tf.write( + """class: GalaxyWorkflow name: "Test Workflow" inputs: - id: input1 @@ -18,7 +19,8 @@ def test_is_a_yaml_with_class(): - tool_id: cat label: first_cat in: - input1: input1""") + input1: input1""" + ) assert is_a_yaml_with_class(fname, ["GalaxyWorkflow"]) os.unlink(fname) From 8ec08f6665894fb5ab2de8387b44daa1745e8be9 Mon Sep 17 00:00:00 2001 From: M Bernt Date: Mon, 30 Jan 2023 10:38:29 +0100 Subject: [PATCH 5/5] Test improvements Co-authored-by: Marius van den Beek --- test/unit/tool_util/test_loader_directory.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/unit/tool_util/test_loader_directory.py b/test/unit/tool_util/test_loader_directory.py index 6b25364349a1..08ec1f5a51c1 100644 --- a/test/unit/tool_util/test_loader_directory.py +++ b/test/unit/tool_util/test_loader_directory.py @@ -1,11 +1,10 @@ -import os import tempfile from galaxy.tool_util.loader_directory import is_a_yaml_with_class def test_is_a_yaml_with_class(): - with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as tf: + with tempfile.NamedTemporaryFile("w", suffix=".yaml") as tf: fname = tf.name tf.write( """class: GalaxyWorkflow @@ -21,6 +20,5 @@ def test_is_a_yaml_with_class(): in: input1: input1""" ) - - assert is_a_yaml_with_class(fname, ["GalaxyWorkflow"]) - os.unlink(fname) + tf.flush() + assert is_a_yaml_with_class(fname, ["GalaxyWorkflow"])