Skip to content

Commit

Permalink
avoid mutate conf in recipes (#14932)
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded authored Oct 16, 2023
1 parent a5c76a2 commit 1b7a546
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 5 additions & 2 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import re
import os
import fnmatch
Expand Down Expand Up @@ -153,7 +154,8 @@ def value(self):
return self._value

def copy(self):
return _ConfValue(self._name, self._value, self._path, self._update)
# Using copy for when self._value is a mutable list
return _ConfValue(self._name, copy.copy(self._value), self._path, self._update)

def dumps(self):
if self._value is None:
Expand Down Expand Up @@ -291,6 +293,7 @@ def get(self, conf_name, default=None, check_type=None, choices=None):
:param default: Default value in case of conf does not have the conf_name key.
:param check_type: Check the conf type(value) is the same as the given by this param.
There are two default smart conversions for bool and str types.
:param choices: list of possible values this conf can have, if value not in it, errors.
"""
# Skipping this check only the user.* configurations
self._check_conf_name(conf_name)
Expand Down Expand Up @@ -335,7 +338,7 @@ def show(self, fnpattern, pattern=""):

def copy(self):
c = Conf()
c._values = self._values.copy()
c._values = OrderedDict((k, v.copy()) for k, v in self._values.items())
return c

def dumps(self):
Expand Down
32 changes: 31 additions & 1 deletion conans/test/integration/configuration/conf/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from conan import conan_version
from conan.internal.api import detect_api
from conans.errors import ConanException
from conans.test.assets.genconanfile import GenConanfile
from conans.util.files import save, load
from conans.test.utils.tools import TestClient
Expand Down Expand Up @@ -343,3 +342,34 @@ def generate(self):
c.run('install pkg --build=*')
assert "dep/0.1: SKIP-TEST: True" in c.out
assert "conanfile.py (pkg/0.1): SKIP-TEST: False" in c.out


def test_conf_should_be_immutable():
c = TestClient()
dep = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
name = "dep"
version = "0.1"
def generate(self):
self.conf.append("user.myteam:myconf", "value1")
self.output.info(f'user.myteam:myconf: {self.conf.get("user.myteam:myconf")}')
""")
pkg = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
name = "pkg"
version = "0.1"
requires = "dep/0.1"
def generate(self):
self.output.info(f'user.myteam:myconf: {self.conf.get("user.myteam:myconf")}')
""")
save(c.cache.new_config_path, 'user.myteam:myconf=["root_value"]')
c.save({"dep/conanfile.py": dep,
"pkg/conanfile.py": pkg})
c.run("create dep")
assert "dep/0.1: user.myteam:myconf: ['root_value', 'value1']" in c.out
c.run('create pkg --build=*')
assert "dep/0.1: user.myteam:myconf: ['root_value', 'value1']" in c.out
# The pkg/0.1 output should be non-modified
assert "pkg/0.1: user.myteam:myconf: ['root_value']" in c.out

0 comments on commit 1b7a546

Please sign in to comment.