From 774a2b39b7d7f0fcff8d516806d2e082b42b9968 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Mon, 28 Jun 2021 17:19:58 +0200 Subject: [PATCH] Fix pinning with explicit version (#762) * Fix validation of version pins * Testsuite: test for more cases of version pins --- src/alr/alr-commands-pin.adb | 26 ++++++++++++++++++++++-- testsuite/drivers/alr.py | 10 ++++----- testsuite/tests/pin/version/test.py | 29 +++++++++++++++++++++++++++ testsuite/tests/pin/version/test.yaml | 3 +++ 4 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 testsuite/tests/pin/version/test.py create mode 100644 testsuite/tests/pin/version/test.yaml diff --git a/src/alr/alr-commands-pin.adb b/src/alr/alr-commands-pin.adb index cf0dccd1..6719cc91 100644 --- a/src/alr/alr-commands-pin.adb +++ b/src/alr/alr-commands-pin.adb @@ -8,7 +8,7 @@ with Alire.Utils.User_Input; with Alr.Commands.User_Input; -with Semantic_Versioning; +with Semantic_Versioning.Extended; with TOML_Slicer; @@ -111,6 +111,24 @@ package body Alr.Commands.Pin is overriding procedure Execute (Cmd : in out Command) is + + ------------------------- + -- Validate_Crate_Spec -- + ------------------------- + + procedure Validate_Crate_Spec (Spec : String) is + Dep : constant Alire.Dependencies.Dependency := + Alire.Dependencies.From_String (Spec); + begin + if not Dep.Versions.Is_Any then + if not Alire.Utils.Starts_With (Dep.Versions.Image, "=") then + Reportaise_Wrong_Arguments + ("Plain crate name or crate=version argument expected for" + & " pinning, but got: " & TTY.Emph (Spec)); + end if; + end if; + end Validate_Crate_Spec; + begin -- Argument validation @@ -137,6 +155,9 @@ package body Alr.Commands.Pin is elsif Num_Arguments > 1 then Reportaise_Wrong_Arguments ("Pin expects a single crate or crate=version argument"); + elsif Num_Arguments = 1 then + -- Check that we get either a plain name or a crate=version + Validate_Crate_Spec (Argument (1)); end if; -- Apply changes; @@ -147,7 +168,8 @@ package body Alr.Commands.Pin is Optional_Crate : constant Alire.Optional.Crate_Name := (if Num_Arguments = 1 then Alire.Optional.Crate_Names.Unit - (Alire.To_Name (Argument (1))) + (Alire.Dependencies + .From_String (Argument (1)).Crate) else Alire.Optional.Crate_Names.Empty); begin diff --git a/testsuite/drivers/alr.py b/testsuite/drivers/alr.py index f458e5b2..25c3c604 100644 --- a/testsuite/drivers/alr.py +++ b/testsuite/drivers/alr.py @@ -273,7 +273,7 @@ def alr_unpin(crate, manual=True, fail_if_missing=True, update=True): def alr_pin(crate, version="", path="", url="", commit="", branch="", - manual=True, update=True): + manual=True, update=True, force=False): """ Pin a crate, either manually or using the command-line interface. Use only one of version, path, url. Must be run in a crate root. @@ -306,7 +306,7 @@ def alr_pin(crate, version="", path="", url="", commit="", branch="", os.utime(alr_lockfile(), (0, 0)) if update: - run_alr("pin") # so the changes in the manifest are applied + return run_alr("pin") # so the changes in the manifest are applied else: if not update: @@ -329,7 +329,7 @@ def alr_pin(crate, version="", path="", url="", commit="", branch="", elif branch != "": args += ["--branch", f"{branch}"] - run_alr("pin", *args) + return run_alr("pin", *args, force=force) def alr_with(dep="", path="", url="", commit="", branch="", @@ -372,7 +372,7 @@ def alr_with(dep="", path="", url="", commit="", branch="", os.utime(alr_lockfile(), (0, 0)) if update: - run_alr("with", force=force) + return run_alr("with", force=force) else: if delete: @@ -392,4 +392,4 @@ def alr_with(dep="", path="", url="", commit="", branch="", elif branch != "": args += ["--branch", f"{branch}"] - run_alr(*args, force=force) + return run_alr(*args, force=force) diff --git a/testsuite/tests/pin/version/test.py b/testsuite/tests/pin/version/test.py new file mode 100644 index 00000000..82fbfbf4 --- /dev/null +++ b/testsuite/tests/pin/version/test.py @@ -0,0 +1,29 @@ +""" +Test pinning to a version +""" + +from drivers.alr import run_alr, alr_pin, alr_with, init_local_crate +from drivers.asserts import assert_eq, assert_match + +init_local_crate() +alr_with("hello") + +# Test pinning to the version currently in the solution +alr_pin("hello", manual=False) +p = run_alr("pin") +assert_eq("hello 1.0.1\n", + p.out) + +# Test pinning to a different explicit version +alr_pin("hello", version="1.0", manual=False, force=True) +p = run_alr("pin") +assert_eq("hello 1.0.0\n", + p.out) + +# Test that trying to use a non-equal restriction fails +p = run_alr("pin", "hello^1.0", complain_on_error=False) +assert_eq("ERROR: Plain crate name or crate=version argument expected" + " for pinning, but got: hello^1.0\n", + p.out) + +print('SUCCESS') diff --git a/testsuite/tests/pin/version/test.yaml b/testsuite/tests/pin/version/test.yaml new file mode 100644 index 00000000..872fc127 --- /dev/null +++ b/testsuite/tests/pin/version/test.yaml @@ -0,0 +1,3 @@ +driver: python-script +indexes: + basic_index: {} -- 2.39.5