From 4cb0e97a616da5a1c296639f4798751ecb0de114 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Wed, 31 May 2023 17:51:54 +0200 Subject: [PATCH] Don't raise on bad external detection from output (#1389) * New test case for detection from output * Add exception catch-all and report in debug output --- src/alire/alire-externals-from_output.adb | 67 +++++++++++-------- .../ba/bad_version/bad_version-external.toml | 10 +++ .../tests/index/external-from-output/test.py | 10 +++ 3 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 testsuite/tests/index/external-from-output/my_index/index/ba/bad_version/bad_version-external.toml diff --git a/src/alire/alire-externals-from_output.adb b/src/alire/alire-externals-from_output.adb index 7c0d6fd2..97c8b7df 100644 --- a/src/alire/alire-externals-from_output.adb +++ b/src/alire/alire-externals-from_output.adb @@ -7,6 +7,7 @@ with Alire.OS_Lib.Subprocess; with Alire.Paths; with Alire.Releases; with Alire.TOML_Keys; +with Alire.Utils.TTY; with Semantic_Versioning; @@ -26,6 +27,7 @@ package body Alire.Externals.From_Output is Location : GNAT.OS_Lib.String_Access := GNAT.OS_Lib.Locate_Exec_On_Path (This.Command.First_Element); + Result : Alire.Releases.Containers.Release_Set; begin if Location in null then Trace.Debug @@ -69,36 +71,43 @@ package body Alire.Externals.From_Output is return Releases.Containers.Empty_Release_Set; end if; - return Releases : Alire.Releases.Containers.Release_Set do - Trace.Debug ("Looking for external in version string: " & Output); - Match (This.Regexp, Output, Matches); - - for I in Matches'Range loop - if Matches (I) /= No_Match then - declare - Version : constant String := - Output (Matches (I).First .. Matches (I).Last); - Path : constant Any_Path := - OS_Lib.Subprocess.Locate_In_Path - (This.Command.First_Element); - begin - Trace.Debug ("Identified external from version: " - & Version); - - Releases.Insert - (Index.Crate (Name, Index.Query_Mem_Only).Base - .Retagging (Semantic_Versioning.Parse (Version)) - .Providing (This.Provides) - .Replacing (Origins.New_External ("path " & Path)) - .Replacing (Notes => "Detected at " -- length is 12 - & Shorten - (String (Path), - Max_Description_Length - 12))); - end; - end if; - end loop; - end return; + Trace.Debug + ("Looking for external in version string '" & Output & "'"); + Match (This.Regexp, Output, Matches); + + for I in Matches'Range loop + if Matches (I) /= No_Match then + declare + Version : constant String := + Output (Matches (I).First .. Matches (I).Last); + Path : constant Any_Path := + OS_Lib.Subprocess.Locate_In_Path + (This.Command.First_Element); + begin + Trace.Debug ("Identified external from version: " + & Version); + + Result.Insert + (Index.Crate (Name, Index.Query_Mem_Only).Base + .Retagging (Semantic_Versioning.Parse (Version)) + .Providing (This.Provides) + .Replacing (Origins.New_External ("path " & Path)) + .Replacing (Notes => "Detected at " -- length is 12 + & Shorten + (String (Path), + Max_Description_Length - 12))); + end; + end if; + end loop; end; + + return Result; + exception + when E : others => + Trace.Debug ("Unexpected exception while attempting detection of " + & "external crate " & Utils.TTY.Name (Name)); + Log_Exception (E); + return Result; end Detect; --------------- diff --git a/testsuite/tests/index/external-from-output/my_index/index/ba/bad_version/bad_version-external.toml b/testsuite/tests/index/external-from-output/my_index/index/ba/bad_version/bad_version-external.toml new file mode 100644 index 00000000..0a446d27 --- /dev/null +++ b/testsuite/tests/index/external-from-output/my_index/index/ba/bad_version/bad_version-external.toml @@ -0,0 +1,10 @@ +description = "An external with bad regex that results in empty version" +name = "bad_version" +licenses = "GPL-3.0-only" +maintainers = ["example@example.com"] +maintainers-logins = ["mylogin"] + +[[external]] +kind = "version-output" +version-regexp = ".*Ma(ke).*" # Captures "ke" as the version +version-command = ["make", "--version"] diff --git a/testsuite/tests/index/external-from-output/test.py b/testsuite/tests/index/external-from-output/test.py index dc123924..8be389e7 100644 --- a/testsuite/tests/index/external-from-output/test.py +++ b/testsuite/tests/index/external-from-output/test.py @@ -48,4 +48,14 @@ assert_eq('Kind Description ' p = run_alr('show', 'bad_switch', '--external-detect', quiet=False) assert_match('.*Not found: bad_switch', p.out) + +# Verify that a bad version being captured doesn't raise + +p = run_alr("show", "bad_version", quiet=False) +assert_match(".*There are external definitions for the crate.", p.out) + +# External detection fails (no release found, but without error) +p = run_alr('show', 'bad_version', '--external-detect', quiet=False) +assert_match('.*Not found: bad_version', p.out) + print('SUCCESS') -- 2.39.5