From bc04285ded4626a9b27f436da36d410788760535 Mon Sep 17 00:00:00 2001 From: germane_rmd Date: Thu, 17 Aug 2023 21:04:05 +0300 Subject: [PATCH 1/2] Prevent IsNuGetPublished from deadlocking if source fails to fetch --- .../Cake.ExtendedNuGet/ExtendedNuGetAliases.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/source/Cake.ExtendedNuGet/ExtendedNuGetAliases.cs b/source/Cake.ExtendedNuGet/ExtendedNuGetAliases.cs index a18d50c..bceed38 100644 --- a/source/Cake.ExtendedNuGet/ExtendedNuGetAliases.cs +++ b/source/Cake.ExtendedNuGet/ExtendedNuGetAliases.cs @@ -16,6 +16,9 @@ using NuGet.Protocol.Core.Types; using NuGet.Versioning; +using LogLevel = Cake.Core.Diagnostics.LogLevel; +using Verbosity = Cake.Core.Diagnostics.Verbosity; + namespace Cake.ExtendedNuGet { /// @@ -112,18 +115,21 @@ public static bool IsNuGetPublished(this ICakeContext context, string packageId, Task.Run(async () => { - var nuSource = Repository.Factory.GetCoreV3(nugetSource); - using var nuCache = new SourceCacheContext(); - var nuLogger = NullLogger.Instance; - var pkgRes = await nuSource.GetResourceAsync(); - try { + var nuSource = Repository.Factory.GetCoreV3(nugetSource); + using var nuCache = new SourceCacheContext(); + var nuLogger = NullLogger.Instance; + + var pkgRes = await nuSource.GetResourceAsync(); var pkgInfo = await pkgRes.GetDependencyInfoAsync(packageId, new NuGetVersion(version.ToString()), nuCache, nuLogger, default); tcsPublished.TrySetResult(pkgInfo?.PackageIdentity?.Id?.Equals(packageId, StringComparison.OrdinalIgnoreCase) ?? false); } - catch + catch(Exception ex) { + context.Log.Write(Verbosity.Diagnostic, LogLevel.Error, + $"Failed to read nuget source repository information: {ex.Message}"); + tcsPublished.TrySetResult(false); } }); From 189ff4cc0b7843986c500907e01ccde665b09895 Mon Sep 17 00:00:00 2001 From: germane_rmd Date: Thu, 17 Aug 2023 21:34:59 +0300 Subject: [PATCH 2/2] Added test case for wrong source repository url --- .../Cake.ExtendedNuGet.Tests/ExtendedNuGetTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/source/Cake.ExtendedNuGet.Tests/ExtendedNuGetTests.cs b/source/Cake.ExtendedNuGet.Tests/ExtendedNuGetTests.cs index ee66de2..79ceb77 100644 --- a/source/Cake.ExtendedNuGet.Tests/ExtendedNuGetTests.cs +++ b/source/Cake.ExtendedNuGet.Tests/ExtendedNuGetTests.cs @@ -69,6 +69,18 @@ public void IsNuGetPublishedAltSrcShouldBeFalse () Assert.False (p); } + [Fact] + public void IsNugetPublishedInvalidSourceShouldBeFalse () + { + var p = context.CakeContext.IsNuGetPublished ( + "Xamarin.Android.Support.v4", + "23.1.1.0", + "https://api.nuget.org/v3/wrong.json" + ); + + Assert.False (p); + } + [Fact] public void NuGetPackageIdFromFile () {