From 831afe5d9a7465f79a5fe1f9b3eb7c7246fd2aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n?= Date: Mon, 27 Nov 2023 19:10:13 +0100 Subject: [PATCH 1/3] Fix serialization of tool_requires in profile show --format=json --- conans/model/profile.py | 6 ++++-- conans/model/recipe_ref.py | 9 +++++++++ conans/test/integration/command/test_profile.py | 9 +++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/conans/model/profile.py b/conans/model/profile.py index abdb5f0bb2f..81abc9cdb99 100644 --- a/conans/model/profile.py +++ b/conans/model/profile.py @@ -30,12 +30,14 @@ def __repr__(self): return self.dumps() def serialize(self): - # TODO: Remove it seems dead + def _serialize_tool_requires(): + return {pattern: [ref.serialize() 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() diff --git a/conans/model/recipe_ref.py b/conans/model/recipe_ref.py index 2b05536d99c..5b7b3130759 100644 --- a/conans/model/recipe_ref.py +++ b/conans/model/recipe_ref.py @@ -182,6 +182,15 @@ def matches(self, pattern, is_consumer): return not condition return condition + def serialize(self): + result = {"name": self.name, + "version": str(self.version), + "user": self.user, + "channel": self.channel, + "revision": self.revision, + "timestamp": self.timestamp} + return result + def ref_matches(ref, pattern, is_consumer): if not ref or not str(ref): diff --git a/conans/test/integration/command/test_profile.py b/conans/test/integration/command/test_profile.py index 8490f435c43..9c08ed87795 100644 --- a/conans/test/integration/command/test_profile.py +++ b/conans/test/integration/command/test_profile.py @@ -133,9 +133,14 @@ 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 + result = {'mytool/*': [{'channel': None, 'name': 'mytool', 'revision': None, 'timestamp': None, 'user': None, 'version': '1.0'}]} + assert profile["build"]["tool_requires"] == result From 827c1f610999ddc1ae253977a8bae48da6d0b990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n?= Date: Tue, 28 Nov 2023 12:56:43 +0100 Subject: [PATCH 2/3] Serialize recipe reference as repr in profile tool_requires --- conans/model/profile.py | 2 +- conans/model/recipe_ref.py | 9 --------- conans/test/integration/command/test_profile.py | 3 +-- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/conans/model/profile.py b/conans/model/profile.py index 81abc9cdb99..a06ea9efc4c 100644 --- a/conans/model/profile.py +++ b/conans/model/profile.py @@ -31,7 +31,7 @@ def __repr__(self): def serialize(self): def _serialize_tool_requires(): - return {pattern: [ref.serialize() for ref in refs] + return {pattern: [repr(ref) for ref in refs] for pattern, refs in self.tool_requires.items()} return { "settings": self.settings, diff --git a/conans/model/recipe_ref.py b/conans/model/recipe_ref.py index 5b7b3130759..2b05536d99c 100644 --- a/conans/model/recipe_ref.py +++ b/conans/model/recipe_ref.py @@ -182,15 +182,6 @@ def matches(self, pattern, is_consumer): return not condition return condition - def serialize(self): - result = {"name": self.name, - "version": str(self.version), - "user": self.user, - "channel": self.channel, - "revision": self.revision, - "timestamp": self.timestamp} - return result - def ref_matches(ref, pattern, is_consumer): if not ref or not str(ref): diff --git a/conans/test/integration/command/test_profile.py b/conans/test/integration/command/test_profile.py index 9c08ed87795..6405319e47a 100644 --- a/conans/test/integration/command/test_profile.py +++ b/conans/test/integration/command/test_profile.py @@ -142,5 +142,4 @@ def test_profile_show_json(): assert profile["build"]["settings"] == {"os": "Windows"} # Check that tool_requires are properly serialized in json format # https://github.com/conan-io/conan/issues/15183 - result = {'mytool/*': [{'channel': None, 'name': 'mytool', 'revision': None, 'timestamp': None, 'user': None, 'version': '1.0'}]} - assert profile["build"]["tool_requires"] == result + assert profile["build"]["tool_requires"] == {'mytool/*': ["mytool/1.0"]} From 7eec3622469e2691e40411b6e89f00c033b4556c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n?= Date: Tue, 28 Nov 2023 14:07:15 +0100 Subject: [PATCH 3/3] Fix test --- conans/test/unittests/model/profile_test.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/conans/test/unittests/model/profile_test.py b/conans/test/unittests/model/profile_test.py index c0796f128ee..09176ce0f9f 100644 --- a/conans/test/unittests/model/profile_test.py +++ b/conans/test/unittests/model/profile_test.py @@ -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()