Skip to content

Commit

Permalink
Merge pull request #7 from PMCC-BioinformaticsCore/sanitise-parammeta
Browse files Browse the repository at this point in the history
Sanitise param meta + other values
  • Loading branch information
illusional authored Jul 13, 2020
2 parents ecde6d1 + 8f6e9ea commit a9cc0da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest

from wdlgen import ParameterMeta


class TestParamMeta(unittest.TestCase):
def test_quote_sanitise(self):
meta = ParameterMeta(foo='"bar"').get_string()
self.assertEqual('foo: "\\"bar\\""', meta)

def test_nl_sanitise(self):
meta = ParameterMeta(foo="bar\nbaz").get_string()
self.assertEqual('foo: "bar\\nbaz"', meta)

def test_backslackquote_sanitise(self):
meta = ParameterMeta(foo='bar\\"').get_string()
self.assertEqual('foo: "bar\\\\\\""', meta)
7 changes: 6 additions & 1 deletion wdlgen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def convert_python_value_to_wdl_literal(val) -> str:
if isinstance(val, bool):
return "true" if val else "false"
if isinstance(val, str):
return f'"{val}"'
# sanitise string here
sanitised = val\
.replace("\\", "\\\\")\
.replace("\n", "\\n")\
.replace('"', '\\"')
return f'"{sanitised}"'

return str(val)

Expand Down

0 comments on commit a9cc0da

Please sign in to comment.