From d9a06fcf6ba90379ce47fb5662681053eba60d47 Mon Sep 17 00:00:00 2001
From: Bryan Brancotte <bryan.brancotte@pasteur.fr>
Date: Fri, 8 Mar 2024 15:53:13 +0100
Subject: [PATCH] sanitize workflow name to be usable as a filename

---
 src/ro_crate.py   |  6 +++++-
 tests/test_cli.py | 10 +++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/ro_crate.py b/src/ro_crate.py
index 6330b41..22c1179 100644
--- a/src/ro_crate.py
+++ b/src/ro_crate.py
@@ -155,5 +155,9 @@ class RO_Crate:
         self.fill_from_workflow()
         self.dico.pop("temp_directory")
 
-        with open(f"{self.workflow.get_output_dir()}/ro-crate-metadata-{self.workflow.get_name()}.json", 'w') as output_file :
+        name = self.workflow.get_name()
+        name = name.replace('github.com/', '')
+        name = re.sub(r"^[ .]|[/<>:\"\\|?*]+|[ .]$", "-", name)
+
+        with open(f"{self.workflow.get_output_dir()}/ro-crate-metadata-{name}.json", 'w') as output_file :
             json.dump(self.dico, output_file, indent=2)
\ No newline at end of file
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 04db2ce..076721c 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -6,7 +6,6 @@ from bioflow_insight_cli.main import cli
 
 
 class TestCall(unittest.TestCase):
-
     def test_cli_works(self):
         cli("./wf_test/main.nf", render_graphs=True)
         cli("./wf_test/main.nf", render_graphs=False)
@@ -18,3 +17,12 @@ class TestCall(unittest.TestCase):
             cli("./wf_test/main.nf", render_graphs=False, output_dir=str(my_results))
             self.assertTrue(my_results.exists(), "Results should be there, output_dir not taken into account")
 
+    def test_with_illegal_path_char_in_name(self):
+        with TemporaryDirectory() as my_temp_dir:
+            my_results = pathlib.Path(my_temp_dir) / "my_results"
+            cli(
+                "./wf_test/main.nf",
+                render_graphs=False,
+                output_dir=str(my_results),
+                name="https://github.com/blabla/toto:qsd!qsd%#sqdqsd"
+            )
-- 
GitLab