From 827823e27f92ccd37406e19ba6d7111b43a20cb2 Mon Sep 17 00:00:00 2001 From: Ernesto de Gracia Herranz Date: Thu, 6 Aug 2026 13:53:01 +0200 Subject: [PATCH 1/2] test_pwsh_support_empty_variables --- .../env/test_virtualenv_powershell.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/functional/toolchains/env/test_virtualenv_powershell.py b/test/functional/toolchains/env/test_virtualenv_powershell.py index d5288a644fa..2ced471a8b5 100644 --- a/test/functional/toolchains/env/test_virtualenv_powershell.py +++ b/test/functional/toolchains/env/test_virtualenv_powershell.py @@ -26,6 +26,8 @@ def client(): def package_info(self): self.buildenv_info.define_path("MYPATH1", "c:/path/to/ar") self.runenv_info.define("MYVAR1", 'some nice content\" with quotes') + + self.runenv_info.append("EMPTYLIST", []) """ client.save({"conanfile.py": conanfile}) client.run("create .") @@ -251,3 +253,23 @@ def test_powershell_deactivation(): assert "conanbuild.ps1" not in files assert "conanrunenv.ps1" not in files assert "conanbuildenv.ps1" not in files + +@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows powershell") +@pytest.mark.parametrize("powershell", ["pwsh", "powershell.exe"]) +def test_pwsh_support_empty_variables(client, powershell): + conanfile = textwrap.dedent(""" + import os + from conan import ConanFile + class ConanFileToolsTest(ConanFile): + name = "app" + version = "0.1" + requires = "pkg/0.1" + def build(self): + self.run("set", env="conanrun") + """) + client.save({"conanfile.py": conanfile}) + client.run(f'create . -c tools.env.virtualenv:powershell={powershell}') + print(client.out) + assert 'MYVAR1' in client.out + # powershell does not define this variable, but pwsh does + assert 'EMPTYLIST' in client.out From e1b8df0bb6c94fac90b074548b2f58a6eb277842 Mon Sep 17 00:00:00 2001 From: Ernesto de Gracia Herranz Date: Fri, 7 Aug 2026 18:46:51 +0200 Subject: [PATCH 2/2] Update test_virtualenv_powershell.py --- .../env/test_virtualenv_powershell.py | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/test/functional/toolchains/env/test_virtualenv_powershell.py b/test/functional/toolchains/env/test_virtualenv_powershell.py index 2ced471a8b5..baa15e4564a 100644 --- a/test/functional/toolchains/env/test_virtualenv_powershell.py +++ b/test/functional/toolchains/env/test_virtualenv_powershell.py @@ -26,8 +26,6 @@ def client(): def package_info(self): self.buildenv_info.define_path("MYPATH1", "c:/path/to/ar") self.runenv_info.define("MYVAR1", 'some nice content\" with quotes') - - self.runenv_info.append("EMPTYLIST", []) """ client.save({"conanfile.py": conanfile}) client.run("create .") @@ -254,22 +252,41 @@ def test_powershell_deactivation(): assert "conanrunenv.ps1" not in files assert "conanbuildenv.ps1" not in files + @pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows powershell") @pytest.mark.parametrize("powershell", ["pwsh", "powershell.exe"]) -def test_pwsh_support_empty_variables(client, powershell): - conanfile = textwrap.dedent(""" +def test_pwsh_support_empty_variables(powershell): + # Generators may append FOO even when empty, so the .ps1 sets $env:FOO="". + # Scripts that only apply defaults when the var is undefined then skip those defaults. + client = TestClient() + check_py = textwrap.dedent("""\ + import os + user = [os.environ["FOO"]] if "FOO" in os.environ else None + config = user or ["-default-flag"] + print("DEFAULT_SKIPPED" if config == [""] else "DEFAULT_APPLIED") + """) + conanfile = textwrap.dedent("""\ import os from conan import ConanFile - class ConanFileToolsTest(ConanFile): + from conan.tools.env import Environment + + class Pkg(ConanFile): name = "app" version = "0.1" - requires = "pkg/0.1" + exports_sources = "check.py" + + def generate(self): + env = Environment() + env.append("FOO", []) + env.vars(self, scope="build").save_script("conanmytoolchain") + def build(self): - self.run("set", env="conanrun") + self.run(f'python "{os.path.join(self.source_folder, "check.py")}"') """) - client.save({"conanfile.py": conanfile}) + client.save({"conanfile.py": conanfile, "check.py": check_py}) + client.run(f'install . -c tools.env.virtualenv:powershell={powershell}') + with open(os.path.join(client.current_folder, "conanmytoolchain.ps1"), "r", encoding="utf-16") as f: + assert '$env:FOO=""' in f.read() + client.run(f'create . -c tools.env.virtualenv:powershell={powershell}') - print(client.out) - assert 'MYVAR1' in client.out - # powershell does not define this variable, but pwsh does - assert 'EMPTYLIST' in client.out + assert ("DEFAULT_SKIPPED" if powershell == "pwsh" else "DEFAULT_APPLIED") in client.out