Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions test/functional/toolchains/env/test_virtualenv_powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,42 @@ 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(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
from conan.tools.env import Environment

class Pkg(ConanFile):
name = "app"
version = "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(f'python "{os.path.join(self.source_folder, "check.py")}"')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer if the run was a .bat script that does the intended check.

""")
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}')
assert ("DEFAULT_SKIPPED" if powershell == "pwsh" else "DEFAULT_APPLIED") in client.out
Loading