Skip to content

Commit

Permalink
WIP lock upgrade: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
perseoGI committed Jan 15, 2025
1 parent 896f5e2 commit b9fade5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
13 changes: 8 additions & 5 deletions conan/cli/commands/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from conan.cli import make_abs_path
from conan.cli.args import common_graph_args, validate_common_graph_args
from conan.cli.printers.graph import print_graph_packages, print_graph_basic
from conan.errors import ConanException
from conans.model.graph_lock import Lockfile, LOCKFILE
from conans.model.recipe_ref import RecipeReference

Expand Down Expand Up @@ -59,7 +60,7 @@ def lock_create(conan_api, parser, subparser, *args):
clean=args.lockfile_clean)
conanfile_path = os.path.dirname(graph.root.path) \
if graph.root.path and args.lockfile_out is None else cwd
conan_api.lockfile.save_lockfile(lockfile, args.lockfile_out or "conan.lock", conanfile_path)
conan_api.lockfile.save_lockfile(lockfile, args.lockfile_out or LOCKFILE, conanfile_path)


@conan_subcommand()
Expand Down Expand Up @@ -186,9 +187,8 @@ def lock_update(conan_api, parser, subparser, *args):
@conan_subcommand()
def lock_upgrade(conan_api, parser, subparser, *args):
"""
Update requires, build-requires or python-requires from an existing lockfile.
References that matches the arguments package names will be replaced by the arguments.
References can be supplied with and without revisions like "--requires=pkg/version",
Upgrade requires, build-requires or python-requires from an existing lockfile given a conanfile
or a reference.
"""
common_graph_args(subparser)
subparser.add_argument('--update-requires', action="append",
Expand All @@ -207,14 +207,17 @@ def lock_upgrade(conan_api, parser, subparser, *args):
# parameter validation
validate_common_graph_args(args)

if not any([args.update_requires, args.update_build_requires, args.update_python_requires, args.update_config_requires]):
raise ConanException("At least one of --update-requires, --update-build-requires, "
"--update-python-requires or --update-config-requires should be specified")

cwd = os.getcwd()
path = conan_api.local.get_conanfile_path(args.path, cwd, py=None) if args.path else None
remotes = conan_api.remotes.list(args.remote) if not args.no_remote else []
overrides = eval(args.lockfile_overrides) if args.lockfile_overrides else None
lockfile = conan_api.lockfile.get_lockfile(lockfile=args.lockfile, conanfile_path=path,
cwd=cwd, partial=True, overrides=overrides)
profile_host, profile_build = conan_api.profiles.get_profiles_from_args(args)
lockfile = conan_api.lockfile.get_lockfile(lockfile=args.lockfile, partial=True)
# Remove the lockfile entries that will be updated
lockfile = conan_api.lockfile.remove_lockfile(lockfile,
requires=args.update_requires,
Expand Down
78 changes: 53 additions & 25 deletions test/integration/lockfile/test_user_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,33 +363,61 @@ def test_lock_update(self, kind, old, new):


class TestLockUpgrade:
@pytest.mark.parametrize("kind, old, new", [
("requires", "math/*", "math/1.1"),
("build-requires", "cmake/1.0", "cmake/1.1"),
("python-requires", "mytool/1.0", "mytool/1.1"),
@pytest.mark.parametrize("kind, pkg, old, new", [
("requires", "math", "math/1.0", "math/1.1"),
# ("build-requires", "cmake", "cmake/1.0", "cmake/1.1"), # TODO there is not a --build-requires
# ("python-requires", "mytool", "mytool/1.0", "mytool/1.1"), # TODO nor a --python-requires
])
def test_lock_upgrade(self, kind, old, new):
def test_lock_upgrade(self, kind, pkg, old, new):
c = TestClient(light=True)
lock = textwrap.dedent("""\
c.save({f"{pkg}/conanfile.py": GenConanfile(pkg)})

c.run(f"export {pkg} --version=1.0")
rev0 = c.exported_recipe_revision()
c.run(f"lock create --{kind}={pkg}/[*]")
lock = c.load("conan.lock")
assert f"{old}#{rev0}" in lock

c.run(f"export {pkg} --version=1.1")
rev1 = c.exported_recipe_revision()
c.run(f"lock upgrade --{kind}={pkg}/[*] --update-{kind}={pkg}/[*]")
lock = c.load("conan.lock")
assert f"{old}#{rev0}" not in lock
assert f"{new}#{rev1}" in lock


def test_lock_upgrade_path(self):
c = TestClient(light=True)
c.save({"liba/conanfile.py": GenConanfile("liba"),
"libb/conanfile.py": GenConanfile("libb"),
"libc/conanfile.py": GenConanfile("libc")})
c.run(f"export liba --version=1.0")
c.run(f"export libb --version=1.0")
c.run(f"export libc --version=1.0")
c.save(
{
"version": "0.5",
"requires": [
"math/1.0#85d927a4a067a531b1a9c7619522c015%1702683583.3411012",
"math/1.0#12345%1702683584.3411012",
"engine/1.0#fd2b006646a54397c16a1478ac4111ac%1702683583.3544693"
],
"build_requires": [
"cmake/1.0#85d927a4a067a531b1a9c7619522c015%1702683583.3411012",
"ninja/1.0#fd2b006646a54397c16a1478ac4111ac%1702683583.3544693"
],
"python_requires": [
"mytool/1.0#85d927a4a067a531b1a9c7619522c015%1702683583.3411012",
"othertool/1.0#fd2b006646a54397c16a1478ac4111ac%1702683583.3544693"
]
f"conanfile.py": GenConanfile()
.with_requires(f"liba/[>=1.0 <2]")
.with_requires("libb/[<1.2]")
.with_tool_requires("libc/1.0")
}
""")
c.save({"conan.lock": lock})
c.run(f"lock upgrade --requires={old}")
)

c.run("lock create .")
lock = c.load("conan.lock")
assert old not in lock
assert new in lock
assert "liba/1.0" in lock
assert "libb/1.0" in lock
assert "libc/1.0" in lock

c.run(f"export liba --version=1.9")
c.run(f"export libb --version=1.1")
c.run(f"export libb --version=1.2")
c.run(f"export libc --version=1.1")
c.run("lock upgrade . --update-requires=liba/* --update-requires=libb/[*] --update-build-requires=libc/[*]")
# TODO not working with --update-requires=libb/1.1 should it?
lock = c.load("conan.lock")
assert "liba/1.9" in lock
assert "libb/1.1" in lock
assert "libc/1.0" in lock


0 comments on commit b9fade5

Please sign in to comment.