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

implementing __contains__ for option in self.info.options #7303

Merged
merged 11 commits into from
Jul 17, 2020
15 changes: 8 additions & 7 deletions conans/model/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def option_not_exist_msg(option_name, existing_options):
""" Someone is referencing an option that is not available in the current package
options
"""
result = ["'options.%s' doesn't exist" % option_name]
result.append("Possible options are %s" % existing_options or "none")
result = ["'options.%s' doesn't exist" % option_name,
"Possible options are %s" % existing_options or "none"]
return "\n".join(result)


Expand Down Expand Up @@ -212,6 +212,9 @@ def descope_options(self, name):
def clear_unscoped_options(self):
self._package_values.clear()

def __contains__(self, item):
return item in self._package_values

def __getitem__(self, item):
return self._reqs_options.setdefault(item, PackageOptionValues())

Expand Down Expand Up @@ -283,16 +286,14 @@ def loads(text):

@property
def sha(self):
result = []
result.append(self._package_values.sha)
result = [self._package_values.sha]
for key in sorted(list(self._reqs_options.keys())):
result.append(self._reqs_options[key].sha)
return sha1('\n'.join(result).encode())

def serialize(self):
ret = {}
ret["options"] = self._package_values.serialize()
ret["req_options"] = {}
ret = {"options": self._package_values.serialize(),
"req_options": {}}
for name, values in self._reqs_options.items():
ret["req_options"][name] = values.serialize()
return ret
Expand Down
13 changes: 13 additions & 0 deletions conans/test/functional/options/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,16 @@ def package_id(self):
client.run("create . pkg/0.1@user/testing %s" % options)
self.assertIn("liba/0.1@user/testing:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Cache",
client.out)

def missing_shared_option_package_id_test(self):
client = TestClient()

consumer = textwrap.dedent("""
from conans import ConanFile
class Pkg(ConanFile):
def package_id(self):
self.info.shared_library_package_id()
""")
client.save({"conanfile.py": consumer})
client.run("create . pkg/0.1@user/testing")
self.assertIn("pkg/0.1@user/testing: Created package ", client.out)
24 changes: 24 additions & 0 deletions conans/test/functional/package_id/package_id_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import textwrap
import unittest

from conans.test.utils.tools import NO_SETTINGS_PACKAGE_ID, TestClient, TestServer
Expand Down Expand Up @@ -78,3 +79,26 @@ def package(self):
client.run("install test/0.1@danimtb/testing")
client.run("search test/0.1@danimtb/testing")
self.assertIn("compiler.version: kk=kk", client.out)

def option_in_test(self):
# https://github.com/conan-io/conan/issues/7299
conanfile = textwrap.dedent("""
from conans import ConanFile

class TestConan(ConanFile):
options = {"fpic": [True, False]}
default_options = {"fpic": True}
def package_id(self):
if "fpic" in self.info.options:
memsharded marked this conversation as resolved.
Show resolved Hide resolved
self.output.info("fpic is an option!!!")
if "other" not in self.info.options:
memsharded marked this conversation as resolved.
Show resolved Hide resolved
self.output.info("other is not an option!!!")
if not self.info.options.whatever:
self.output.info("whatever is not an option!!!")
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("create . Pkg/0.1@user/testing")
self.assertIn("fpic is an option!!!", client.out)
self.assertIn("other is not an option!!!", client.out)
self.assertIn("whatever is not an option!!!", client.out)