-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signing: enable signature verification by default on Linux in .NET 8 …
…SDK (#5006) Resolve NuGet/Home#11262.
- Loading branch information
Showing
4 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using NuGet.Versioning; | ||
|
||
namespace NuGet.Packaging | ||
{ | ||
internal static class RuntimeSdkDetector | ||
{ | ||
private static readonly Lazy<NuGetVersion> LazySdkVersion = new(GetSdkVersion); | ||
private static readonly Lazy<bool> LazyIs8OrGreater = new(GetIs8OrGreater); | ||
|
||
internal static bool Is8OrGreater => LazyIs8OrGreater.Value; | ||
|
||
private static bool GetIs8OrGreater() | ||
{ | ||
NuGetVersion sdkVersion = LazySdkVersion.Value; | ||
|
||
return sdkVersion is not null && sdkVersion.Version >= new Version(8, 0, 0, 0); | ||
} | ||
|
||
private static NuGetVersion GetSdkVersion() | ||
{ | ||
if (TryGetSdkVersion(out NuGetVersion version)) | ||
{ | ||
return version; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// Non-private for testing. | ||
internal static bool TryGetSdkVersion(out NuGetVersion version) | ||
{ | ||
Assembly assembly = typeof(RuntimeSdkDetector).Assembly; | ||
string filePath = assembly.Location; | ||
|
||
return TryGetSdkVersion(filePath, out version); | ||
} | ||
|
||
// Non-private for testing. | ||
internal static bool TryGetSdkVersion(string filePath, out NuGetVersion version) | ||
{ | ||
version = null; | ||
|
||
if (string.IsNullOrEmpty(filePath)) | ||
{ | ||
return false; | ||
} | ||
|
||
FileInfo file = new(filePath); | ||
string directoryName = file.Directory?.Name; | ||
|
||
return !string.IsNullOrEmpty(directoryName) | ||
&& char.IsDigit(directoryName[0]) | ||
&& NuGetVersion.TryParse(directoryName, out version); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
test/NuGet.Core.Tests/NuGet.Packaging.Test/RuntimeSdkDetectorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using NuGet.Versioning; | ||
using Xunit; | ||
|
||
namespace NuGet.Packaging.Test | ||
{ | ||
public class RuntimeSdkDetectorTests | ||
{ | ||
private static readonly Lazy<NuGetVersion> LazyExpectedSdkVersion = new(GetExpectedSdkVersion); | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
[InlineData("file.dll")] | ||
[InlineData("./file.dll")] | ||
[InlineData("/file.dll")] | ||
[InlineData("/a/file.dll")] | ||
[InlineData(@"\\a\b\file.dll")] | ||
public void TryGetSdkVersion_WhenFileIsNotInVersionedSdkFolder_ReturnsFalse(string filePath) | ||
{ | ||
bool actualResult = RuntimeSdkDetector.TryGetSdkVersion(filePath, out NuGetVersion version); | ||
|
||
Assert.False(actualResult); | ||
Assert.Null(version); | ||
} | ||
|
||
[Theory] | ||
[InlineData("7.0.200-preview.22628.1")] | ||
[InlineData("8.0.100")] | ||
public void TryGetSdkVersion_WhenFileIsInVersionedSdkFolder_ReturnsTrue(string expectedVersion) | ||
{ | ||
string filePath = $"/home/user/.dotnet/sdk/{expectedVersion}/file.dll"; | ||
bool actualResult = RuntimeSdkDetector.TryGetSdkVersion(filePath, out NuGetVersion version); | ||
|
||
Assert.True(actualResult); | ||
Assert.NotNull(version); | ||
Assert.Equal(expectedVersion, version.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void TryGetSdkVersion_Always_ReturnsValueForNuGetPackagingAssembly() | ||
{ | ||
bool actualResult = RuntimeSdkDetector.TryGetSdkVersion(out NuGetVersion actualVersion); | ||
|
||
if (LazyExpectedSdkVersion.Value is null) | ||
{ | ||
Assert.False(actualResult); | ||
Assert.Null(actualVersion); | ||
} | ||
else | ||
{ | ||
Assert.True(actualResult); | ||
Assert.NotNull(actualVersion); | ||
Assert.Equal(LazyExpectedSdkVersion.Value, actualVersion); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Is8OrGreater_Always_ReturnsValueForNuGetPackagingAssembly() | ||
{ | ||
bool is8OrGreater = RuntimeSdkDetector.Is8OrGreater; | ||
|
||
if (LazyExpectedSdkVersion.Value is null | ||
|| LazyExpectedSdkVersion.Value.Version < new Version(8, 0, 0, 0)) | ||
{ | ||
Assert.False(is8OrGreater); | ||
} | ||
else | ||
{ | ||
Assert.True(is8OrGreater); | ||
} | ||
} | ||
|
||
private static NuGetVersion GetExpectedSdkVersion() | ||
{ | ||
string directoryName = new FileInfo(typeof(PackageArchiveReader).Assembly.Location).Directory.Name; | ||
|
||
if (NuGetVersion.TryParse(directoryName, out NuGetVersion expectedVersion)) | ||
{ | ||
return expectedVersion; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |