Skip to content

Commit

Permalink
Convert argument to str in aiida.common.escaping.escape_for_bash
Browse files Browse the repository at this point in the history
Without conversion to string, any non-string type will cause the
function to raise an `AttributeError` since it won't have the method
`str_replace`.
  • Loading branch information
sphuber committed Mar 27, 2020
1 parent 57b8a5a commit 0517b9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aiida/common/escaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def escape_for_bash(str_to_escape):
if str_to_escape is None:
return ''

str_to_escape = str(str_to_escape)

escaped_quotes = str_to_escape.replace("'", """'"'"'""")
return "'{}'".format(escaped_quotes)

Expand Down
18 changes: 18 additions & 0 deletions tests/common/test_escaping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
"""Tests for the :mod:`aiida.common.escaping`."""
from aiida.common.escaping import escape_for_bash


def test_escape_for_bash():
"""Tests various inputs for `aiida.common.escaping.escape_for_bash`."""
tests = (
[None, ''],
['string', "'string'"],
['string with space', "'string with space'"],
["string with a ' single quote", """'string with a '"'"' single quote'"""],
[1, "'1'"],
[2.0, "'2.0'"],
)

for string_input, string_escaped in tests:
assert escape_for_bash(string_input) == string_escaped

0 comments on commit 0517b9a

Please sign in to comment.