diff --git a/conan/tools/env/environment.py b/conan/tools/env/environment.py index edd570db85a..fc4ef2f9927 100644 --- a/conan/tools/env/environment.py +++ b/conan/tools/env/environment.py @@ -59,8 +59,8 @@ 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: @@ -68,8 +68,8 @@ def environment_wrap_command(conanfile, env_filenames, env_folder, cmd, subsyste 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 diff --git a/test/functional/toolchains/env/test_virtualenv_powershell.py b/test/functional/toolchains/env/test_virtualenv_powershell.py index 43d47d6dec0..db77574b1d1 100644 --- a/test/functional/toolchains/env/test_virtualenv_powershell.py +++ b/test/functional/toolchains/env/test_virtualenv_powershell.py @@ -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