Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double quote escape for bash script ENV_VAR part #5349

Merged
merged 2 commits into from
Feb 13, 2022

Conversation

unkcpz
Copy link
Member

@unkcpz unkcpz commented Feb 11, 2022

This PR is separated from #5280 and containerized code PR for easy review.

Fixes #4836

@codecov
Copy link

codecov bot commented Feb 11, 2022

Codecov Report

Merging #5349 (a95c09a) into develop (dce0041) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #5349      +/-   ##
===========================================
+ Coverage    82.13%   82.13%   +0.01%     
===========================================
  Files          533      533              
  Lines        38485    38491       +6     
===========================================
+ Hits         31605    31611       +6     
  Misses        6880     6880              
Flag Coverage Δ
django 77.22% <100.00%> (+0.01%) ⬆️
sqlalchemy 76.51% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiida/schedulers/datastructures.py 94.55% <ø> (ø)
aiida/common/escaping.py 100.00% <100.00%> (ø)
aiida/engine/processes/calcjobs/calcjob.py 90.25% <100.00%> (+0.05%) ⬆️
aiida/schedulers/scheduler.py 85.46% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dce0041...a95c09a. Read the comment docs.

Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @unkcpz. Looks good. Just a suggestion to clean up the test. And I personally am still not convinced that envvar_double_quotes is the best name. But if others think this is fine, then I won't block it.

Comment on lines 33 to 50
def test_escape_for_bash_double_quotes():
"""
Tests various inputs for `aiida.common.escaping.escape_for_bash`.
double quotes tests especially useful for ENV variables with $ symbol
"""

tests = (
[None, ''],
['string', '"string"'],
['string with space', '"string with space"'],
['string with a " double quote', '''"string with a "'"'" double quote"'''],
[1, '"1"'],
[2.0, '"2.0"'],
['$PWD', '"$PWD"'],
)

for string_input, string_escaped in tests:
assert escape_for_bash(string_input, use_double_quotes=True) == string_escaped
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the perfect candidate to use `pytest.mark.parametrize. I would suggest replacing the original test and the one you added with the following single test:

Suggested change
def test_escape_for_bash_double_quotes():
"""
Tests various inputs for `aiida.common.escaping.escape_for_bash`.
double quotes tests especially useful for ENV variables with $ symbol
"""
tests = (
[None, ''],
['string', '"string"'],
['string with space', '"string with space"'],
['string with a " double quote', '''"string with a "'"'" double quote"'''],
[1, '"1"'],
[2.0, '"2.0"'],
['$PWD', '"$PWD"'],
)
for string_input, string_escaped in tests:
assert escape_for_bash(string_input, use_double_quotes=True) == string_escaped
import pytest
from aiida.common.escaping import escape_for_bash
@pytest.mark.parametrize(('to_escape, expected_single_quotes, expected_double_quotes'), (
(None, ''),
('string', "'string'", '"string"'),
('string with space', "'string with space'", '"string with space"'),
("string with ' single quote", """'string with '"'"' single quote'""", '''"string with "'"'" double quote"'''),
(1, "'1'", '"1"'),
(2.0, "'2.0'", '"2.0"'),
('$PWD', "'$PWD'", '"$PWD"'),
))
def test_escape_for_bash(to_escape, expected_single_quotes, expected_double_quotes):
"""Tests various inputs for `aiida.common.escaping.escape_for_bash`."""
assert escape_for_bash(to_escape, use_double_quotes=False) == expected_single_quotes
assert escape_for_bash(to_escape, use_double_quotes=True) == expected_double_quotes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sphuber! Absolutely it is beautiful. One line to rule all test cases 😄

I also change the name to environment_variables_double_quotes as you suggest.

@unkcpz unkcpz force-pushed the fix/4836/double-quotes-for-envvar branch 3 times, most recently from ec6ab39 to 3c67e78 Compare February 12, 2022 17:08
@@ -248,6 +248,9 @@ class JobTemplate(DefaultFieldsAttributeDict): # pylint: disable=too-many-insta
* ``rerunnable``: if the job is rerunnable (boolean)
* ``job_environment``: a dictionary with environment variables to set
before the execution of the code.
* ``environment_variables_double_quotes``: if set to True, use double quotes
instead of single quotes to escape the environment variables specified
in ``environment_variables``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
in ``environment_variables``.
in ``job_environment``.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@unkcpz just noticed this last mistake in the docstring. Sorry for having missed it. If you correct this, I will merge the PR. Thanks a lot

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sphuber Thanks. Updated.

@unkcpz unkcpz force-pushed the fix/4836/double-quotes-for-envvar branch 2 times, most recently from 35d47b7 to 5ae6d28 Compare February 13, 2022 13:49
The escape method will escape the string in the quotes. The defalut
implementation is using single quotes to escape the string. However,
single quotes will not eval the spacial shell parameters such as $ and
@, this makes bash script not versatile to set the parameters in
runtime.

The `use_double_quotes` option is added to escape the string in the
double quotes so the spacial parameters are still valid in the shell
script.

Using double escape option into environment variables of job templete
setting address aiidateam#4836. User can now use double quotes escaping in
calcjob option to escape the environment variables of scheduler.
@unkcpz unkcpz force-pushed the fix/4836/double-quotes-for-envvar branch from 8732249 to d79ebff Compare February 13, 2022 14:33
@unkcpz unkcpz requested a review from sphuber February 13, 2022 15:29
@sphuber sphuber merged commit 1aed026 into aiidateam:develop Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider using double quotes for the values in the metadata.options.environment_variables dict
2 participants