Skip to content

Commit

Permalink
fix run with nested quotes (#17487)
Browse files Browse the repository at this point in the history
* fix run with nested quotes

* fix escaping of double quotes

* add some tests for quoted commands

* simplify test and fix quoting

* only win

* add to right test

---------

Co-authored-by: czoido <mrgalleta@gmail.com>
  • Loading branch information
Todiq and czoido authored Dec 18, 2024
1 parent cd7f0be commit bd20ea2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions conan/tools/env/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ def environment_wrap_command(conanfile, env_filenames, env_folder, cmd, subsyste
launchers = " && ".join('"{}"'.format(b) for b in bats)
if ps1s:
ps1_launchers = f'{powershell} -Command "' + " ; ".join('&\'{}\''.format(f) for f in ps1s) + '"'
cmd = cmd.replace('"', "'")
return '{} && {} ; cmd /c {}'.format(launchers, ps1_launchers, cmd)
cmd = cmd.replace('"', r'\"')
return '{} && {} ; cmd /c "{}"'.format(launchers, ps1_launchers, cmd)
else:
return '{} && {}'.format(launchers, cmd)
elif shs:
launchers = " && ".join('. "{}"'.format(f) for f in shs)
return '{} && {}'.format(launchers, cmd)
elif ps1s:
ps1_launchers = f'{powershell} -Command "' + " ; ".join('&\'{}\''.format(f) for f in ps1s) + '"'
cmd = cmd.replace('"', "'")
return '{} ; cmd /c {}'.format(ps1_launchers, cmd)
cmd = cmd.replace('"', r'\"')
return '{} ; cmd /c "{}"'.format(ps1_launchers, cmd)
else:
return cmd

Expand Down
19 changes: 19 additions & 0 deletions test/functional/toolchains/env/test_virtualenv_powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,22 @@ class Pkg(ConanFile):
assert "Boolean values for 'tools.env.virtualenv:powershell' are deprecated" in client.out
else:
assert "Boolean values for 'tools.env.virtualenv:powershell' are deprecated" not in client.out


@pytest.mark.skipif(platform.system() != "Windows", reason="Test for powershell")
@pytest.mark.parametrize("powershell", [True, "pwsh", "powershell.exe"])
def test_powershell_quoting(powershell):
client = TestClient(path_with_spaces=False)
conanfile = textwrap.dedent("""\
from conan import ConanFile
class Pkg(ConanFile):
settings = "os"
name = "pkg"
version = "0.1"
def build(self):
self.run('python -c "print(\\'Hello World\\')"', scope="build")
""")

client.save({"conanfile.py": conanfile})
client.run(f'create . -c tools.env.virtualenv:powershell={powershell}')
assert "Hello World" in client.out

0 comments on commit bd20ea2

Please sign in to comment.