Skip to content

Commit

Permalink
fix profile definitions of env-vars per-package (#8688)
Browse files Browse the repository at this point in the history
* fix profile definitions of env-vars per-package

* fix broken tests

* fix more tests

* more checks in test
  • Loading branch information
memsharded committed Mar 23, 2021
1 parent 37343e9 commit 2135d40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions conans/model/conan_file.py
Expand Up @@ -233,8 +233,8 @@ def env(self):
# the deps_env_info objects available
tmp_env_values = self._conan_env_values.copy()
tmp_env_values.update(self.deps_env_info)

ret, multiple = tmp_env_values.env_dicts(self.name)
ret, multiple = tmp_env_values.env_dicts(self.name, self.version, self._conan_user,
self._conan_channel)
ret.update(multiple)
return ret

Expand Down
12 changes: 10 additions & 2 deletions conans/model/env_info.py
@@ -1,8 +1,10 @@
import copy
import fnmatch
import re
from collections import OrderedDict, defaultdict

from conans.errors import ConanException
from conans.model.ref import ConanFileReference
from conans.util.log import logger


Expand Down Expand Up @@ -144,15 +146,21 @@ def update(self, env_obj):
else:
raise ConanException("unknown env type: %s" % env_obj)

def env_dicts(self, package_name):
def env_dicts(self, package_name, version=None, user=None, channel=None):
"""Returns two dicts of env variables that applies to package 'name',
the first for simple values A=1, and the second for multiple A=1;2;3"""
ret = {}
ret_multi = {}
# First process the global variables

global_pairs = self._data.get(None)
own_pairs = self._data.get(package_name)
own_pairs = None
str_ref = str(ConanFileReference(package_name, version, user, channel, validate=False))
for pattern, v in self._data.items():
if pattern is not None and (package_name == pattern or fnmatch.fnmatch(str_ref,
pattern)):
own_pairs = v
break

if global_pairs:
for name, value in global_pairs.items():
Expand Down
30 changes: 30 additions & 0 deletions conans/test/functional/environment/apply_environment_test.py
@@ -1,5 +1,6 @@
import os
import platform
import textwrap
import unittest

import pytest
Expand Down Expand Up @@ -875,3 +876,32 @@ def build(self):
self.output.info("VAR2=>%s*" % os.environ.get("VAR2"))
self.output.info("VAR3=>%s*" % os.environ.get("VAR3"))
'''


def test_env_per_package_patterns():
# https://github.com/conan-io/conan/issues/8657
client = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
import os
class Pkg(ConanFile):
def build(self):
self.output.info("MYENV: {}!!!".format(os.getenv("MYVAR")))
""")
profile = textwrap.dedent("""
[env]
*myuser*:MYVAR=MyValue
other:MYVAR=OtherValue
*/mychannel:MYVAR=MyChannelValue
*:MYVAR=MyAllValue
""")
client.save({"conanfile.py": conanfile,
"profile": profile})
client.run("create . pkg/0.1@myuser/channel -pr=profile")
assert "pkg/0.1@myuser/channel: MYENV: MyValue!!!" in client.out
client.run("create . pkg/0.1@ -pr=profile")
assert "pkg/0.1: MYENV: MyAllValue!!!" in client.out
client.run("create . other/0.1@ -pr=profile")
assert "other/0.1: MYENV: OtherValue!!!" in client.out
client.run("create . pkg/0.1@user/mychannel -pr=profile")
assert "pkg/0.1@user/mychannel: MYENV: MyChannelValue!!!" in client.out
2 changes: 2 additions & 0 deletions conans/test/utils/mocks.py
Expand Up @@ -176,6 +176,8 @@ def __init__(self, shared=None, options=None, options_values=None):
self.layout.set_base_source_folder(".")
self.layout.set_base_build_folder(".")
self.layout.set_base_install_folder("myinstallfolder")
self._conan_user = None
self._conan_channel = None

def run(self, command, win_bash=False, subsystem=None):
assert win_bash is False
Expand Down

0 comments on commit 2135d40

Please sign in to comment.