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

test settings.possible_values() #13796

Merged
Merged
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
2 changes: 1 addition & 1 deletion conans/client/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
WindowsStore:
version: ["8.1", "10.0"]
WindowsCE:
platform: ANY
platform: [ANY]
Copy link
Member Author

Choose a reason for hiding this comment

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

This was a bug, realized while printing things.

version: ["5.0", "6.0", "7.0", "8.0"]
Linux:
iOS:
Expand Down
15 changes: 7 additions & 8 deletions conans/model/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ def validate(self):
if isinstance(self._definition, dict):
self._definition[self._value].validate()

def get_definition(self):
def possible_values(self):
if isinstance(self._definition, list):
return [e if e != 'None' else None for e in self.values_range]
Copy link
Member Author

Choose a reason for hiding this comment

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

We no longer use the "None" literal string for this

return self.values_range.copy()
ret = {}
for key, value in self._definition.items():
ret[key] = value.get_definition()
ret[key] = value.possible_values()
return ret

def rm_safe(self, name):
Expand Down Expand Up @@ -341,11 +341,10 @@ def dumps(self):
result.append("%s=%s" % (name, value))
return '\n'.join(result)

def get_definition(self):
"""Check the range of values of the definition of a setting. e.g:
get_definition_values("compiler.gcc.version") """

def possible_values(self):
"""Check the range of values of the definition of a setting
"""
ret = {}
for key, element in self._data.items():
ret[key] = element.get_definition()
ret[key] = element.possible_values()
return ret
59 changes: 59 additions & 0 deletions conans/test/integration/settings/test_settings_possible_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import textwrap

from conans.test.utils.tools import TestClient


def test_settings_definitions():
c = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
settings = "os"
def generate(self):
definition = self.settings.possible_values()
assert "os" in definition
assert "compiler" not in definition
assert "arch" not in definition
self.output.info(", ".join(definition["os"]))
ios = definition["os"]["iOS"]["version"]
self.output.info(f"iOS: {ios}")

os_definition = self.settings.os.possible_values()
ios = os_definition["iOS"]["version"]
self.output.info(f"iOS2: {ios}")
""")
c.save({"conanfile.py": conanfile})
# New settings are there
c.run("install . -s os=Linux")
assert "conanfile.py: Windows, WindowsStore, WindowsCE, Linux," in c.out
assert "conanfile.py: iOS: ['7.0', '7.1', '8.0'," in c.out
assert "conanfile.py: iOS2: ['7.0', '7.1', '8.0'," in c.out


def test_settings_definitions_compiler():
c = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
settings = "compiler"
def generate(self):
definition = self.settings.compiler.version.possible_values()
self.output.info("HOST: " + ", ".join(definition))
definition = self.settings_build.compiler.version.possible_values()
self.output.info("BUILD: " + ", ".join(definition))
cppstds = self.settings.compiler.cppstd.possible_values()
self.output.info("CPPSTDS: " + str(cppstds))
""")
profile = textwrap.dedent("""\
[settings]
compiler=msvc
compiler.version=192
compiler.runtime=dynamic
""")
c.save({"conanfile.py": conanfile,
"profile": profile})
# New settings are there
c.run("install . -pr=profile -s:b compiler=gcc")
assert "conanfile.py: HOST: 170, 180, 190, 191, 192" in c.out
assert "conanfile.py: BUILD: 4.1, 4.4, 4.5, 4.6, 4.7," in c.out
assert "conanfile.py: CPPSTDS: [None, '14', '17', '20', '23']" in c.out
43 changes: 22 additions & 21 deletions conans/test/unittests/model/settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,27 +393,28 @@ def test_my(self):
self.sut.compiler.arch.speed = "D"
self.assertEqual(self.sut.compiler.arch.speed, "D")

def test_get_definition(self):
settings = Settings.loads(default_settings_yml)
settings.compiler = "gcc"
sot = settings.compiler.cppstd.get_definition()
assert sot == [None, '98', 'gnu98', '11', 'gnu11', '14', 'gnu14', '17', 'gnu17', '20',
'gnu20', '23', 'gnu23']

# We cannot access the child definition of a non declared setting
with pytest.raises(Exception) as e:
settings.os.version.get_definition()

assert "'settings.os' value not defined" in str(e.value)

# But if we have it declared, we can
settings.os = "watchOS"
sot = settings.os.version.get_definition()
assert "4.0" in sot

# We can get the whole settings definition and explore the dict
sot = settings.get_definition()
assert [None, "cygwin", "msys", "msys2", "wsl"] == sot["os"]["Windows"]["subsystem"]

def test_possible_values():
settings = Settings.loads(default_settings_yml)
settings.compiler = "gcc"
sot = settings.compiler.cppstd.possible_values()
assert sot == [None, '98', 'gnu98', '11', 'gnu11', '14', 'gnu14', '17', 'gnu17', '20',
'gnu20', '23', 'gnu23']

# We cannot access the child definition of a non declared setting
with pytest.raises(Exception) as e:
settings.os.version.possible_values()

assert "'settings.os' value not defined" in str(e.value)

# But if we have it declared, we can
settings.os = "watchOS"
sot = settings.os.version.possible_values()
assert "4.0" in sot

# We can get the whole settings definition and explore the dict
sot = settings.possible_values()
assert [None, "cygwin", "msys", "msys2", "wsl"] == sot["os"]["Windows"]["subsystem"]


def test_rm_safe():
Expand Down