From 2135d40009b153b0d64712a84dac7b8827800b16 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 23 Mar 2021 17:32:36 +0100 Subject: [PATCH] fix profile definitions of env-vars per-package (#8688) * fix profile definitions of env-vars per-package * fix broken tests * fix more tests * more checks in test --- conans/model/conan_file.py | 4 +-- conans/model/env_info.py | 12 ++++++-- .../environment/apply_environment_test.py | 30 +++++++++++++++++++ conans/test/utils/mocks.py | 2 ++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/conans/model/conan_file.py b/conans/model/conan_file.py index ce00895baab..270957213a5 100644 --- a/conans/model/conan_file.py +++ b/conans/model/conan_file.py @@ -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 diff --git a/conans/model/env_info.py b/conans/model/env_info.py index e1e6457b9cb..b8817c9578b 100644 --- a/conans/model/env_info.py +++ b/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 @@ -144,7 +146,7 @@ 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 = {} @@ -152,7 +154,13 @@ def env_dicts(self, package_name): # 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(): diff --git a/conans/test/functional/environment/apply_environment_test.py b/conans/test/functional/environment/apply_environment_test.py index c3812e4bfdb..7f26f9e37aa 100644 --- a/conans/test/functional/environment/apply_environment_test.py +++ b/conans/test/functional/environment/apply_environment_test.py @@ -1,5 +1,6 @@ import os import platform +import textwrap import unittest import pytest @@ -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 diff --git a/conans/test/utils/mocks.py b/conans/test/utils/mocks.py index eed131ac25b..77162107ce4 100644 --- a/conans/test/utils/mocks.py +++ b/conans/test/utils/mocks.py @@ -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