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

Define syntax for env-vars custom separators in profile files #17781

Open
wants to merge 5 commits into
base: develop2
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions conan/tools/env/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ def __init__(self, name, value=None, separator=" ", path=False):
def dumps(self):
result = []
path = "(path)" if self._path else ""
sep = f"(sep={self._sep})" if self._sep != " " and not self._path else ""
if not self._values: # Empty means unset
result.append("{}=!".format(self._name))
elif _EnvVarPlaceHolder in self._values:
index = self._values.index(_EnvVarPlaceHolder)
for v in self._values[:index]:
result.append("{}=+{}{}".format(self._name, path, v))
result.append("{}=+{}{}{}".format(self._name, path, sep, v))
for v in self._values[index+1:]:
result.append("{}+={}{}".format(self._name, path, v))
result.append("{}+={}{}{}".format(self._name, path, sep, v))
else:
append = ""
for v in self._values:
result.append("{}{}={}{}".format(self._name, append, path, v))
result.append("{}{}={}{}{}".format(self._name, append, path, sep, v))
append = "+"
return "\n".join(result)

Expand Down Expand Up @@ -644,6 +645,14 @@ def loads(text):
env = Environment()
if method == "unset":
env.unset(name)
elif value.strip().startswith("(sep="):
value = value.strip()
sep = value[5]
value = value[7:]
if value.strip().startswith("(path)"):
msg = f"Cannot use (sep) and (path) qualifiers simultaneously: {line}"
raise ConanException(msg)
getattr(env, method)(name, value, separator=sep)
else:
if value.strip().startswith("(path)"):
value = value.strip()
Expand Down
47 changes: 47 additions & 0 deletions test/functional/subsystems_build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def test_tool_not_available(self):
client.run_command('uname', assert_error=True)
assert "'uname' is not recognized as an internal or external command" in client.out


@pytest.mark.skipif(platform.system() != "Windows", reason="Tests Windows Subsystems")
class TestSubsystemsBuild:

Expand Down Expand Up @@ -512,3 +513,49 @@ def test_vs_clang(self):
self._build(client, generator="Visual Studio 17 2022", toolset="ClangCL")
check_exe_run(client.out, "main", "clang", None, "Debug", "x86_64", None, subsystem=None)
check_vs_runtime("Debug/app.exe", client, "15", "Debug", subsystem=None)


@pytest.mark.tool("msys2")
def test_msys2_env_vars_paths():
c = TestClient()
# A tool-requires injecting PATHs for native, should not use "_path" calls, and use
# 'separator=;' explicitly
tool = textwrap.dedent("""
from conan import ConanFile
class HelloConan(ConanFile):
name = "tool"
version = "0.1"
def package_info(self):
self.buildenv_info.append("INCLUDE", "C:/mytool/path", separator=";")
""")
conanfile = textwrap.dedent("""
from conan import ConanFile
class HelloConan(ConanFile):
win_bash = True
tool_requires = "tool/0.1"

def build(self):
self.run('echo "INCLUDE=$INCLUDE"')
""")
profile = textwrap.dedent("""
[conf]
tools.microsoft.bash:subsystem=msys2
tools.microsoft.bash:path=bash

[buildenv]
INCLUDE=+(sep=;)C:/prepended/path
INCLUDE+=(sep=;)C:/appended/path
""")
c.save({"tool/conanfile.py": tool,
"consumer/conanfile.py": conanfile,
"profile": profile})
c.run("create tool")
with environment_update({"INCLUDE": "C:/my/abs path/folder;C:/other path/subfolder"}):
c.run("build consumer -pr=profile")

# Check the profile is outputed correctly
assert "INCLUDE=+(sep=;)C:/prepended/path" in c.out
assert "INCLUDE+=(sep=;)C:/appended/path" in c.out
# check the composition is correct
assert "INCLUDE=C:/prepended/path;C:/my/abs path/folder;C:/other path/subfolder;" \
"C:/mytool/path;C:/appended/path" in c.out
24 changes: 24 additions & 0 deletions test/unittests/tools/env/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from conan.errors import ConanException
from conan.tools.env import Environment
from conan.tools.env.environment import ProfileEnvironment
from conans.client.subsystems import WINDOWS
Expand Down Expand Up @@ -426,3 +427,26 @@ def test_custom_placeholder():
f"$penv{{MyVar}}:MyValue"
items = {k: v for k, v in env.items(variable_reference="$penv{{{name}}}")}
assert items == {"MyVar": f"$penv{{MyVar}}:MyValue"}


class TestProfileSeparators:

def test_define(self):
myprofile = "MyPath1 = (sep=@)/my/path1"
env = ProfileEnvironment.loads(myprofile).get_profile_env(ref="")
assert env.dumps() == "MyPath1=(sep=@)/my/path1"
env.append("MyPath1", "/other/value")
assert env.vars(ConanFileMock()).get("MyPath1") == "/my/path1@/other/value"

def test_append(self):
myprofile = "MyPath1 +=(sep=@)/my/path1"
env = ProfileEnvironment.loads(myprofile).get_profile_env(ref="")
assert env.dumps() == "MyPath1+=(sep=@)/my/path1"
env.append("MyPath1", "/other/value")
assert env.vars(ConanFileMock()).get("MyPath1") == "/my/path1@/other/value"

def test_error(self):
myprofile = "MyPath1 = (sep=@) (path)/my/path1"
with pytest.raises(ConanException) as e:
ProfileEnvironment.loads(myprofile)
assert "Cannot use (sep) and (path) qualifiers simultaneously" in str(e.value)
Loading