Skip to content

Commit

Permalink
serialize original required ref (#17603)
Browse files Browse the repository at this point in the history
* serialize original required ref

* replace requires
  • Loading branch information
memsharded authored Jan 22, 2025
1 parent 127b1e4 commit 936c9f5
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
4 changes: 3 additions & 1 deletion conan/internal/model/requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self, ref, *, headers=None, libs=None, build=False, run=None, visib
# * prevents the usage of more positional parameters, always ref + **kwargs
# By default this is a generic library requirement
self.ref = ref
self._required_ref = ref # Store the original reference
self._headers = headers # This dependent node has headers that must be -I<headers-path>
self._libs = libs
self._build = build # This dependent node is a build tool that runs at build time only
Expand Down Expand Up @@ -152,7 +153,8 @@ def __str__(self):
return "{}, Traits: {}".format(self.ref, traits)

def serialize(self):
result = {"ref": str(self.ref)}
result = {"ref": str(self.ref),
"require": str(self._required_ref)}
serializable = ("run", "libs", "skip", "test", "force", "direct", "build",
"transitive_headers", "transitive_libs", "headers",
"package_id_mode", "visible")
Expand Down
1 change: 1 addition & 0 deletions conans/client/graph/graph_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def _resolve_replace_requires(self, node, require, profile_build, profile_host,
continue
if pattern.channel != "*" and pattern.channel != require.ref.channel:
continue
require._required_ref = require.ref.copy() # Save the original ref before replacing
original_require = repr(require.ref)
if alternative_ref.version != "*":
require.ref.version = alternative_ref.version
Expand Down
6 changes: 3 additions & 3 deletions test/integration/command/create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,11 @@ class MyTest(ConanFile):
'1': {'ref': 'hello/0.1', 'run': False, 'libs': True, 'skip': False,
'test': False, 'force': False, 'direct': True, 'build': False,
'transitive_headers': None, 'transitive_libs': None, 'headers': True,
'package_id_mode': None, 'visible': True},
'package_id_mode': None, 'visible': True, 'require': 'hello/0.1'},
'2': {'ref': 'pkg/0.2', 'run': False, 'libs': True, 'skip': False, 'test': False,
'force': False, 'direct': False, 'build': False, 'transitive_headers': None,
'transitive_libs': None, 'headers': True, 'package_id_mode': None,
'visible': True}
'visible': True, 'require': 'pkg/0.2'}
}
assert consumer_info["dependencies"] == consumer_deps
# hello/0.1 pkg information
Expand All @@ -505,7 +505,7 @@ class MyTest(ConanFile):
"ref": "pkg/0.2", "run": False, "libs": True, "skip": False, "test": False,
"force": False, "direct": True, "build": False, "transitive_headers": None,
"transitive_libs": None, "headers": True, "package_id_mode": "semver_mode",
"visible": True
"visible": True, 'require': 'pkg/0.2'
}
}
assert hello_pkg_info["dependencies"] == hello_pkg_info_deps
Expand Down
9 changes: 9 additions & 0 deletions test/integration/command/info/info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ def test_json_info_outputs(self):
graph = json.loads(client.stdout)
assert graph["graph"]["nodes"]["0"]["settings"]["build_type"] == "Debug"

def test_json_info_requirements(self):
c = TestClient()
c.save({"pkg/conanfile.py": GenConanfile("pkg", "0.1"),
"app/conanfile.py": GenConanfile().with_require("pkg/[>=0.0 <1.0]")})
c.run("create pkg")
c.run("graph info app --format=json")
graph = json.loads(c.stdout)
assert graph["graph"]["nodes"]["0"]["dependencies"]["1"]["require"] == "pkg/[>=0.0 <1.0]"


class TestAdvancedCliOutput:
""" Testing more advanced fields output, like SCM or PYTHON-REQUIRES
Expand Down
4 changes: 2 additions & 2 deletions test/integration/command_v2/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ class Pkg(ConanFile):
'options:',
'options_definitions:',
'package_type: None',
"requires: [{'ref': 'zlib/1.2.13', 'run': False, 'libs': True, 'skip': "
"False, 'test': False, 'force': False, 'direct': True, 'build': "
"requires: [{'ref': 'zlib/1.2.13', 'require': 'zlib/1.2.13', 'run': False, "
"'libs': True, 'skip': False, 'test': False, 'force': False, 'direct': True, 'build': "
"False, 'transitive_headers': None, 'transitive_libs': None, 'headers': "
"True, 'package_id_mode': None, 'visible': True}]",
'revision_mode: hash',
Expand Down
2 changes: 2 additions & 0 deletions test/integration/graph/test_replace_requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def test_replace_requires_json_format():
assert "pkg/0.1: pkg/0.2" in c.out # The replacement happens
graph = json.loads(c.stdout)
assert graph["graph"]["replaced_requires"] == {"pkg/0.1": "pkg/0.2"}
assert graph["graph"]["nodes"]["0"]["dependencies"]["1"]["ref"] == "pkg/0.2"
assert graph["graph"]["nodes"]["0"]["dependencies"]["1"]["require"] == "pkg/0.1"


def test_replace_requires_test_requires():
Expand Down
9 changes: 4 additions & 5 deletions test/integration/lockfile/test_graph_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_overrides_half_diamond(override, force):
# apply the lockfile to pkgb, should it lock to pkga/0.2
c.run("graph info pkgb --lockfile=pkgc/conan.lock --format=json")
dependencies = json.loads(c.stdout)["graph"]["nodes"]["0"]["dependencies"]
assert "pkga/0.2" in str(dependencies)
assert "pkga/0.1" not in str(dependencies)
assert "pkga/0.2" == dependencies["1"]["ref"]
assert "pkga/0.1" == dependencies["1"]["require"]


@pytest.mark.parametrize("override, force", [(True, False), (False, True)])
Expand Down Expand Up @@ -140,9 +140,8 @@ def test_overrides_diamond(override, force):
c.run("graph info pkgb --lockfile=pkgd/conan.lock --format=json")
json_graph = json.loads(c.stdout)
deps = json_graph["graph"]["nodes"]["0"]["dependencies"]
assert "pkga/0.3" in str(deps)
assert "pkga/0.2" not in str(deps)
assert "pkga/0.1" not in str(deps)
assert "pkga/0.3" == deps["1"]["ref"]
assert "pkga/0.1" == deps["1"]["require"]
# Redundant assert, but checking "overrides" summary
overrides = json_graph['graph']["overrides"]
assert len(overrides) == 1
Expand Down

0 comments on commit 936c9f5

Please sign in to comment.