Skip to content

Commit

Permalink
Fix serialization of tool_requires in `conan profile show --format=js…
Browse files Browse the repository at this point in the history
…on` (conan-io#15185)

* Fix serialization of tool_requires in profile show --format=json

* Serialize recipe reference as repr in profile tool_requires

* Fix test
  • Loading branch information
AbrilRBS authored Nov 28, 2023
1 parent a5605d1 commit f0a1b35
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
6 changes: 4 additions & 2 deletions conans/model/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ def __repr__(self):
return self.dumps()

def serialize(self):
# TODO: Remove it seems dead
def _serialize_tool_requires():
return {pattern: [repr(ref) for ref in refs]
for pattern, refs in self.tool_requires.items()}
return {
"settings": self.settings,
"package_settings": self.package_settings,
"options": self.options.serialize(),
"tool_requires": self.tool_requires,
"tool_requires": _serialize_tool_requires(),
"conf": self.conf.serialize(),
# FIXME: Perform a serialize method for ProfileEnvironment
"build_env": self.buildenv.dumps()
Expand Down
8 changes: 6 additions & 2 deletions conans/test/integration/command/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ def test_shorthand_syntax():

def test_profile_show_json():
c = TestClient()
c.save({"myprofilewin": "[settings]\nos=Windows",
c.save({"myprofilewin": "[settings]\nos=Windows\n[tool_requires]\nmytool/*:mytool/1.0",
"myprofilelinux": "[settings]\nos=Linux"})
c.run("profile show -pr:b=myprofilewin -pr:h=myprofilelinux --format=json")
profile = json.loads(c.stdout)
assert profile["build"]["settings"] == {"os": "Windows"}
assert profile["host"]["settings"] == {"os": "Linux"}

assert profile["build"]["settings"] == {"os": "Windows"}
# Check that tool_requires are properly serialized in json format
# https://github.com/conan-io/conan/issues/15183
assert profile["build"]["tool_requires"] == {'mytool/*': ["mytool/1.0"]}
11 changes: 6 additions & 5 deletions conans/test/unittests/model/profile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ def test_profile_serialize():
profile.settings["compiler.version"] = "12"
profile.tool_requires["*"] = ["zlib/1.2.8"]
profile.update_package_settings({"MyPackage": [("os", "Windows")]})
expected_json = '{"settings": {"arch": "x86_64", "compiler": "Visual Studio", "compiler.version": "12"}, ' \
'"package_settings": {"MyPackage": {"os": "Windows"}}, ' \
'"options": {}, "tool_requires": {"*": ["zlib/1.2.8"]}, ' \
'"conf": {"user.myfield:value": "MyVal"}, "build_env": "VAR1=1\\nVAR2=2\\n"}'
assert expected_json == json.dumps(profile.serialize())
expected_json = {
"settings": {"arch": "x86_64", "compiler": "Visual Studio", "compiler.version": "12"},
"package_settings": {"MyPackage": {"os": "Windows"}}, "options": {},
"tool_requires": {"*": ["'zlib/1.2.8'"]}, "conf": {"user.myfield:value": "MyVal"},
"build_env": "VAR1=1\nVAR2=2\n"}
assert expected_json == profile.serialize()

0 comments on commit f0a1b35

Please sign in to comment.