From 287e69c82a7b2a883063237c3c8c7772ea55b6dc Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Mon, 27 Jun 2022 00:10:36 -0700 Subject: [PATCH 1/6] add asset target fallback dependency with env var --- .../SourceRepositoryDependencyProvider.cs | 43 +++++++++++++++---- .../PackageSpecReferenceDependencyProvider.cs | 34 ++++++++++++--- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs index b0cf1373919..40aa89cb7ea 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs @@ -34,6 +34,7 @@ public class SourceRepositoryDependencyProvider : IRemoteDependencyProvider private bool _ignoreFailedSources; private bool _ignoreWarning; private bool _isFallbackFolderSource; + private bool _useLegacyAssetTargetFallbackBehavior; private readonly ConcurrentDictionary> _dependencyInfoCache = new ConcurrentDictionary>(); @@ -92,7 +93,27 @@ public SourceRepositoryDependencyProvider( bool ignoreFailedSources, bool ignoreWarning, LocalPackageFileCache fileCache, - bool isFallbackFolderSource) + bool isFallbackFolderSource) : + this(sourceRepository, + logger, + cacheContext, + ignoreFailedSources, + ignoreWarning, + fileCache, + isFallbackFolderSource, + environmentVariableReader: EnvironmentVariableWrapper.Instance) + { + } + + internal SourceRepositoryDependencyProvider( + SourceRepository sourceRepository, + ILogger logger, + SourceCacheContext cacheContext, + bool ignoreFailedSources, + bool ignoreWarning, + LocalPackageFileCache fileCache, + bool isFallbackFolderSource, + IEnvironmentVariableReader environmentVariableReader) { _sourceRepository = sourceRepository ?? throw new ArgumentNullException(nameof(sourceRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); @@ -101,6 +122,7 @@ public SourceRepositoryDependencyProvider( _ignoreWarning = ignoreWarning; _packageFileCache = fileCache; _isFallbackFolderSource = isFallbackFolderSource; + _useLegacyAssetTargetFallbackBehavior = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(Environment.GetEnvironmentVariable("NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION"))); } /// @@ -293,6 +315,7 @@ public async Task GetDependenciesAsync( AsyncLazy result = null; + // Therre's caching here which happens based on a bunch of stuff. var action = new AsyncLazy(async () => await GetDependenciesCoreAsync(libraryIdentity, targetFramework, cacheContext, logger, cancellationToken)); @@ -470,7 +493,7 @@ public async Task GetPackageDownloaderAsync( return null; } - private static IEnumerable GetDependencies( + private IEnumerable GetDependencies( FindPackageByIdDependencyInfo packageInfo, NuGetFramework targetFramework) { @@ -488,13 +511,17 @@ private static IEnumerable GetDependencies( dependencyGroup = NuGetFrameworkUtility.GetNearest(packageInfo.DependencyGroups, dualCompatibilityFramework.SecondaryFramework, item => item.TargetFramework); } - // FrameworkReducer.GetNearest does not consider ATF since it is used for more than just compat - if (dependencyGroup == null && - targetFramework is AssetTargetFallbackFramework assetTargetFallbackFramework) + if (!_useLegacyAssetTargetFallbackBehavior) { - dependencyGroup = NuGetFrameworkUtility.GetNearest(packageInfo.DependencyGroups, - assetTargetFallbackFramework.AsFallbackFramework(), - item => item.TargetFramework); + // FrameworkReducer.GetNearest does not consider ATF since it is used for more than just compat + + if (dependencyGroup == null && + targetFramework is AssetTargetFallbackFramework assetTargetFallbackFramework) + { + dependencyGroup = NuGetFrameworkUtility.GetNearest(packageInfo.DependencyGroups, + assetTargetFallbackFramework.AsFallbackFramework(), + item => item.TargetFramework); + } } if (dependencyGroup != null) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs index 06329c091bd..ec4868d3946 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs @@ -27,9 +27,21 @@ private readonly Dictionary _externalProjectsB private readonly ILogger _logger; + private readonly bool _useLegacyAssetTargetFallbackBehavior; + public PackageSpecReferenceDependencyProvider( IEnumerable externalProjects, - ILogger logger) + ILogger logger) : + this(externalProjects, + logger, + environmentVariableReader: EnvironmentVariableWrapper.Instance) + { + } + + internal PackageSpecReferenceDependencyProvider( + IEnumerable externalProjects, + ILogger logger, + IEnvironmentVariableReader environmentVariableReader) { if (externalProjects == null) { @@ -68,6 +80,7 @@ public PackageSpecReferenceDependencyProvider( _externalProjectsByUniqueName.Add(project.UniqueName, project); } } + _useLegacyAssetTargetFallbackBehavior = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable("NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION")); } public bool SupportsType(LibraryDependencyTarget libraryType) @@ -155,7 +168,7 @@ public Library GetLibrary(LibraryRange libraryRange, NuGetFramework targetFramew return library; } - private static void AddLibraryProperties(Library library, PackageSpec packageSpec, NuGetFramework targetFramework) + private void AddLibraryProperties(Library library, PackageSpec packageSpec, NuGetFramework targetFramework) { var projectStyle = packageSpec.RestoreMetadata?.ProjectStyle ?? ProjectStyle.Unknown; @@ -186,9 +199,12 @@ private static void AddLibraryProperties(Library library, PackageSpec packageSpe var targetFrameworkInfo = packageSpec.GetTargetFramework(targetFramework); // FrameworkReducer.GetNearest does not consider ATF since it is used for more than just compat - if (targetFrameworkInfo.FrameworkName == null && targetFramework is AssetTargetFallbackFramework atfFramework) + if (!_useLegacyAssetTargetFallbackBehavior) { - targetFrameworkInfo = packageSpec.GetTargetFramework(atfFramework.AsFallbackFramework()); + if (targetFrameworkInfo.FrameworkName == null && targetFramework is AssetTargetFallbackFramework atfFramework) + { + targetFrameworkInfo = packageSpec.GetTargetFramework(atfFramework.AsFallbackFramework()); + } } if (targetFrameworkInfo.FrameworkName == null && targetFramework is DualCompatibilityFramework mcfFramework) @@ -219,10 +235,13 @@ private List GetDependenciesFromSpecRestoreMetadata(PackageSp // Get the nearest framework var referencesForFramework = packageSpec.GetRestoreMetadataFramework(targetFramework); - if (referencesForFramework.FrameworkName == null && - targetFramework is AssetTargetFallbackFramework assetTargetFallbackFramework) + if (!_useLegacyAssetTargetFallbackBehavior) { - referencesForFramework = packageSpec.GetRestoreMetadataFramework(assetTargetFallbackFramework.AsFallbackFramework()); + if (referencesForFramework.FrameworkName == null && + targetFramework is AssetTargetFallbackFramework assetTargetFallbackFramework) + { + referencesForFramework = packageSpec.GetRestoreMetadataFramework(assetTargetFallbackFramework.AsFallbackFramework()); + } } // Ensure that this project is compatible @@ -330,6 +349,7 @@ internal static List GetSpecDependencies( dependencies.AddRange(packageSpec.Dependencies); // Add framework specific dependencies + // Disable? var targetFrameworkInfo = packageSpec.GetTargetFramework(targetFramework); if (targetFrameworkInfo.FrameworkName == null && targetFramework is AssetTargetFallbackFramework atfFramework) From 5339cf5c778daebc87d6a7c192b64b3dcb9645a2 Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Mon, 27 Jun 2022 00:50:55 -0700 Subject: [PATCH 2/6] Add tests --- .../SourceRepositoryDependencyProvider.cs | 2 +- ...SourceRepositoryDependencyProviderTests.cs | 137 ++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs index 40aa89cb7ea..0fffe01571e 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs @@ -122,7 +122,7 @@ internal SourceRepositoryDependencyProvider( _ignoreWarning = ignoreWarning; _packageFileCache = fileCache; _isFallbackFolderSource = isFallbackFolderSource; - _useLegacyAssetTargetFallbackBehavior = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(Environment.GetEnvironmentVariable("NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION"))); + _useLegacyAssetTargetFallbackBehavior = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable("NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION")); } /// diff --git a/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs index 4c8c94d4843..76c71d146e7 100644 --- a/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -17,6 +18,7 @@ using NuGet.Protocol.Core.Types; using NuGet.Test.Utility; using NuGet.Versioning; +using Test.Utility; using Xunit; namespace NuGet.Commands.Test @@ -772,6 +774,141 @@ public async Task FindLibraryAsync_WhenASourceIsInaccessible_AndFailuresAreIgnor secondTestLogger.ShowWarnings().Should().Contain("NU1801"); } + [Fact] + public async Task GetDependenciesAsync_WhenPackageHasVariousDependencyGroups_CorrectFrameworkIsSelected() + { + // Arrange + var testLogger = new TestLogger(); + var cacheContext = new SourceCacheContext(); + var findResource = new Mock(); + var net46 = FrameworkConstants.CommonFrameworks.Net46; + var netstandard20 = FrameworkConstants.CommonFrameworks.NetStandard20; + + var packageDependencyGroups = new List(); + var net46group = new PackageDependencyGroup(net46, new PackageDependency[] { new PackageDependency("full.framework", VersionRange.Parse("1.0.0")) }); + var netstandardGroup = new PackageDependencyGroup(netstandard20, new PackageDependency[] { new PackageDependency("netstandard", VersionRange.Parse("1.0.0")) }); + + packageDependencyGroups.Add(net46group); + packageDependencyGroups.Add(netstandardGroup); + + findResource.Setup(s => s.GetDependencyInfoAsync( + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(new FindPackageByIdDependencyInfo( + new PackageIdentity("x", NuGetVersion.Parse("1.0.0-beta")), + packageDependencyGroups, + Enumerable.Empty())); + + var source = new Mock(); + source.Setup(s => s.GetResourceAsync()) + .ReturnsAsync(findResource.Object); + source.SetupGet(s => s.PackageSource) + .Returns(new PackageSource("http://test/index.json")); + + var libraryRange = new LibraryRange( + "x", + new VersionRange(new NuGetVersion(1, 0, 0, "beta")), + LibraryDependencyTarget.Package); + + var provider = new SourceRepositoryDependencyProvider( + source.Object, + testLogger, + cacheContext, + ignoreFailedSources: true, + ignoreWarning: true, + fileCache: null, + isFallbackFolderSource: false, + new TestEnvironmentVariableReader(new Dictionary())); + + // Act + var library = await provider.GetDependenciesAsync( + new LibraryIdentity("x", NuGetVersion.Parse("1.0.0-beta"), LibraryType.Package), + FrameworkConstants.CommonFrameworks.Net472, + cacheContext, + testLogger, + CancellationToken.None); + // Assert + library.Dependencies.Should().HaveCount(1); + var dependencies = library.Dependencies.Single(); + dependencies.Name.Should().Be("full.framework"); + } + + [Theory] + [InlineData("true", false)] + [InlineData("blbla", true)] + public async Task GetDependenciesAsync_WhenPackageIsSelectedWithAssetTargetFallback_AndLegacyDependencyResolutionVariableIsSpecified_CorrectDependenciesAreSelected(string envValue, bool areDependenciesSelected) + { + // Arrange + var testLogger = new TestLogger(); + var cacheContext = new SourceCacheContext(); + var findResource = new Mock(); + var wrapper = new TestEnvironmentVariableReader(new Dictionary + { + { "NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION", envValue } + }); + var net472 = FrameworkConstants.CommonFrameworks.Net472; + var net60 = FrameworkConstants.CommonFrameworks.Net60; + var inputFramework = new AssetTargetFallbackFramework(net60, new List { net472 }); + + var packageDependencyGroups = new List(); + var net472Group = new PackageDependencyGroup(net472, new PackageDependency[] { new PackageDependency("full.framework", VersionRange.Parse("1.0.0")) }); + + packageDependencyGroups.Add(net472Group); + + findResource.Setup(s => s.GetDependencyInfoAsync( + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(new FindPackageByIdDependencyInfo( + new PackageIdentity("x", NuGetVersion.Parse("1.0.0-beta")), + packageDependencyGroups, + Enumerable.Empty())); + + var source = new Mock(); + source.Setup(s => s.GetResourceAsync()) + .ReturnsAsync(findResource.Object); + source.SetupGet(s => s.PackageSource) + .Returns(new PackageSource("http://test/index.json")); + + var libraryRange = new LibraryRange( + "x", + new VersionRange(new NuGetVersion(1, 0, 0, "beta")), + LibraryDependencyTarget.Package); + + var provider = new SourceRepositoryDependencyProvider( + source.Object, + testLogger, + cacheContext, + ignoreFailedSources: true, + ignoreWarning: true, + fileCache: null, + isFallbackFolderSource: false, + wrapper); + + // Act + var library = await provider.GetDependenciesAsync( + new LibraryIdentity("x", NuGetVersion.Parse("1.0.0-beta"), LibraryType.Package), + inputFramework, + cacheContext, + testLogger, + CancellationToken.None); + // Assert + if (areDependenciesSelected) + { + library.Dependencies.Should().HaveCount(1); + var dependencies = library.Dependencies.Single(); + dependencies.Name.Should().Be("full.framework"); + } else + { + library.Dependencies.Should().HaveCount(0); + } + } + private sealed class SourceRepositoryDependencyProviderTest : IDisposable { internal TestLogger Logger { get; } From 7ea82e70d306ef72f4421ae57798c1a827a38ce9 Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Mon, 27 Jun 2022 14:18:55 -0700 Subject: [PATCH 3/6] Add tests --- .../PackageSpecReferenceDependencyProvider.cs | 17 ++++--- ...ageSpecReferenceDependencyProviderTests.cs | 46 ++++++++++++++++++- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs index ec4868d3946..ca777421b26 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecReferenceDependencyProvider.cs @@ -199,12 +199,9 @@ private void AddLibraryProperties(Library library, PackageSpec packageSpec, NuGe var targetFrameworkInfo = packageSpec.GetTargetFramework(targetFramework); // FrameworkReducer.GetNearest does not consider ATF since it is used for more than just compat - if (!_useLegacyAssetTargetFallbackBehavior) + if (targetFrameworkInfo.FrameworkName == null && targetFramework is AssetTargetFallbackFramework atfFramework) { - if (targetFrameworkInfo.FrameworkName == null && targetFramework is AssetTargetFallbackFramework atfFramework) - { - targetFrameworkInfo = packageSpec.GetTargetFramework(atfFramework.AsFallbackFramework()); - } + targetFrameworkInfo = packageSpec.GetTargetFramework(atfFramework.AsFallbackFramework()); } if (targetFrameworkInfo.FrameworkName == null && targetFramework is DualCompatibilityFramework mcfFramework) @@ -337,7 +334,7 @@ private List GetDependenciesFromExternalReference( return dependencies; } - internal static List GetSpecDependencies( + internal List GetSpecDependencies( PackageSpec packageSpec, NuGetFramework targetFramework) { @@ -349,12 +346,14 @@ internal static List GetSpecDependencies( dependencies.AddRange(packageSpec.Dependencies); // Add framework specific dependencies - // Disable? var targetFrameworkInfo = packageSpec.GetTargetFramework(targetFramework); - if (targetFrameworkInfo.FrameworkName == null && targetFramework is AssetTargetFallbackFramework atfFramework) + if (!_useLegacyAssetTargetFallbackBehavior) { - targetFrameworkInfo = packageSpec.GetTargetFramework(atfFramework.AsFallbackFramework()); + if (targetFrameworkInfo.FrameworkName == null && targetFramework is AssetTargetFallbackFramework atfFramework) + { + targetFrameworkInfo = packageSpec.GetTargetFramework(atfFramework.AsFallbackFramework()); + } } dependencies.AddRange(targetFrameworkInfo.Dependencies); diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs index b0591a4e705..ac618711dfd 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs @@ -3,10 +3,13 @@ using System.Collections.Generic; using System.Linq; +using FluentAssertions; +using NuGet.Commands.Test; +using NuGet.Common; using NuGet.Frameworks; using NuGet.LibraryModel; -using NuGet.Test.Utility; using NuGet.Versioning; +using Test.Utility; using Xunit; namespace NuGet.ProjectModel.Test @@ -40,8 +43,9 @@ public void GetSpecDependencies_AddsCentralPackageVersionsIfDefined(bool cpvmEna var dependencyGraphSpec = CreateDependencyGraphSpecWithCentralDependencies(cpvmEnabled, CentralPackageTransitivePinningEnabled, tfi); var packSpec = dependencyGraphSpec.Projects[0]; + var dependencyProvider = new PackageSpecReferenceDependencyProvider(new List(), NullLogger.Instance); // Act - var dependencies = PackageSpecReferenceDependencyProvider.GetSpecDependencies(packSpec, tfi.FrameworkName); + var dependencies = dependencyProvider.GetSpecDependencies(packSpec, tfi.FrameworkName); // Assert if (cpvmEnabled && CentralPackageTransitivePinningEnabled) @@ -72,6 +76,44 @@ public void GetSpecDependencies_AddsCentralPackageVersionsIfDefined(bool cpvmEna } } + [Fact] + public void GetSpecDependencies_WithAssetTargetFallback_ReturnsCorrectDependencies() + { + // Arrange + var net60Framework = FrameworkConstants.CommonFrameworks.Net60; + var net472Framework = FrameworkConstants.CommonFrameworks.Net472; + var packageSpec = ProjectTestHelpers.GetPackageSpec(rootPath: "C:\\", projectName: "A", framework: net472Framework.GetShortFolderName(), dependencyName: "x"); + + var dependencyProvider = new PackageSpecReferenceDependencyProvider(new List(), NullLogger.Instance); + var assetTargetFallback = new AssetTargetFallbackFramework(net60Framework, new List { net472Framework }); + // Act + + var dependencies = dependencyProvider.GetSpecDependencies(packageSpec, assetTargetFallback); + + // Assert + dependencies.Should().HaveCount(1); + dependencies[0].Name.Should().Be("x"); + } + + [Fact] + public void GetSpecDependencies_WithAssetTargetFallback_AndDependencyResolutionDisabled_ReturnsCorrectDependencies() + { + // Arrange + var net60Framework = FrameworkConstants.CommonFrameworks.Net60; + var net472Framework = FrameworkConstants.CommonFrameworks.Net472; + var packageSpec = ProjectTestHelpers.GetPackageSpec(rootPath: "C:\\", projectName: "A", framework: net472Framework.GetShortFolderName(), dependencyName: "x"); + + var envVarWrapper = new TestEnvironmentVariableReader(new Dictionary { { "NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION", "true" } }); + var dependencyProvider = new PackageSpecReferenceDependencyProvider(new List(), NullLogger.Instance, envVarWrapper); + var assetTargetFallback = new AssetTargetFallbackFramework(net60Framework, new List { net472Framework }); + // Act + + var dependencies = dependencyProvider.GetSpecDependencies(packageSpec, assetTargetFallback); + + // Assert + dependencies.Should().HaveCount(0); + } + private static TargetFrameworkInformation CreateTargetFrameworkInformation(List dependencies, List centralVersionsDependencies, bool cpvmEnabled) { NuGetFramework nugetFramework = new NuGetFramework("net40"); From 7f19df24bc0ec95a8829a9dfc9a0d3600aade123 Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Mon, 27 Jun 2022 14:49:26 -0700 Subject: [PATCH 4/6] use a theory --- ...ageSpecReferenceDependencyProviderTests.cs | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs index ac618711dfd..471a345e6c1 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecReferenceDependencyProviderTests.cs @@ -76,34 +76,17 @@ public void GetSpecDependencies_AddsCentralPackageVersionsIfDefined(bool cpvmEna } } - [Fact] - public void GetSpecDependencies_WithAssetTargetFallback_ReturnsCorrectDependencies() - { - // Arrange - var net60Framework = FrameworkConstants.CommonFrameworks.Net60; - var net472Framework = FrameworkConstants.CommonFrameworks.Net472; - var packageSpec = ProjectTestHelpers.GetPackageSpec(rootPath: "C:\\", projectName: "A", framework: net472Framework.GetShortFolderName(), dependencyName: "x"); - - var dependencyProvider = new PackageSpecReferenceDependencyProvider(new List(), NullLogger.Instance); - var assetTargetFallback = new AssetTargetFallbackFramework(net60Framework, new List { net472Framework }); - // Act - - var dependencies = dependencyProvider.GetSpecDependencies(packageSpec, assetTargetFallback); - - // Assert - dependencies.Should().HaveCount(1); - dependencies[0].Name.Should().Be("x"); - } - - [Fact] - public void GetSpecDependencies_WithAssetTargetFallback_AndDependencyResolutionDisabled_ReturnsCorrectDependencies() + [Theory] + [InlineData(null, 1)] + [InlineData("true", 0)] + public void GetSpecDependencies_WithAssetTargetFallback_AndDependencyResolutionVariableSpecified_ReturnsCorrectDependencies(string assetTargetFallbackEnvironmentVariableValue, int dependencyCount) { // Arrange var net60Framework = FrameworkConstants.CommonFrameworks.Net60; var net472Framework = FrameworkConstants.CommonFrameworks.Net472; var packageSpec = ProjectTestHelpers.GetPackageSpec(rootPath: "C:\\", projectName: "A", framework: net472Framework.GetShortFolderName(), dependencyName: "x"); - var envVarWrapper = new TestEnvironmentVariableReader(new Dictionary { { "NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION", "true" } }); + var envVarWrapper = new TestEnvironmentVariableReader(new Dictionary { { "NUGET_USE_LEGACY_ASSET_TARGET_FALLBACK_DEPENDENCY_RESOLUTION", assetTargetFallbackEnvironmentVariableValue } }); var dependencyProvider = new PackageSpecReferenceDependencyProvider(new List(), NullLogger.Instance, envVarWrapper); var assetTargetFallback = new AssetTargetFallbackFramework(net60Framework, new List { net472Framework }); // Act @@ -111,7 +94,12 @@ public void GetSpecDependencies_WithAssetTargetFallback_AndDependencyResolutionD var dependencies = dependencyProvider.GetSpecDependencies(packageSpec, assetTargetFallback); // Assert - dependencies.Should().HaveCount(0); + dependencies.Should().HaveCount(dependencyCount); + + if (dependencyCount > 0) + { + dependencies[0].Name.Should().Be("x"); + } } private static TargetFrameworkInformation CreateTargetFrameworkInformation(List dependencies, List centralVersionsDependencies, bool cpvmEnabled) From aa6dff0fb95f8585da0c56cc49ddeb438a1f6a2c Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Mon, 27 Jun 2022 18:19:41 -0700 Subject: [PATCH 5/6] fixed formatting --- .../SourceRepositoryDependencyProvider.cs | 1 - .../SourceRepositoryDependencyProviderTests.cs | 10 ---------- 2 files changed, 11 deletions(-) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs index 0fffe01571e..b4e34f85a37 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs @@ -315,7 +315,6 @@ public async Task GetDependenciesAsync( AsyncLazy result = null; - // Therre's caching here which happens based on a bunch of stuff. var action = new AsyncLazy(async () => await GetDependenciesCoreAsync(libraryIdentity, targetFramework, cacheContext, logger, cancellationToken)); diff --git a/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs index 76c71d146e7..8355e480899 100644 --- a/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Commands.Test/SourceRepositoryDependencyProviderTests.cs @@ -808,11 +808,6 @@ public async Task GetDependenciesAsync_WhenPackageHasVariousDependencyGroups_Cor source.SetupGet(s => s.PackageSource) .Returns(new PackageSource("http://test/index.json")); - var libraryRange = new LibraryRange( - "x", - new VersionRange(new NuGetVersion(1, 0, 0, "beta")), - LibraryDependencyTarget.Package); - var provider = new SourceRepositoryDependencyProvider( source.Object, testLogger, @@ -875,11 +870,6 @@ public async Task GetDependenciesAsync_WhenPackageIsSelectedWithAssetTargetFallb source.SetupGet(s => s.PackageSource) .Returns(new PackageSource("http://test/index.json")); - var libraryRange = new LibraryRange( - "x", - new VersionRange(new NuGetVersion(1, 0, 0, "beta")), - LibraryDependencyTarget.Package); - var provider = new SourceRepositoryDependencyProvider( source.Object, testLogger, From ab5d4ccdf29f6893c2731fb86878b0cb7030f968 Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Tue, 28 Jun 2022 11:52:09 -0700 Subject: [PATCH 6/6] Fix formatting --- .../SourceRepositoryDependencyProvider.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs index b4e34f85a37..21375b83c0c 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/SourceRepositoryDependencyProvider.cs @@ -87,13 +87,13 @@ public SourceRepositoryDependencyProvider( /// is null. /// Thrown if is null. public SourceRepositoryDependencyProvider( - SourceRepository sourceRepository, - ILogger logger, - SourceCacheContext cacheContext, - bool ignoreFailedSources, - bool ignoreWarning, - LocalPackageFileCache fileCache, - bool isFallbackFolderSource) : + SourceRepository sourceRepository, + ILogger logger, + SourceCacheContext cacheContext, + bool ignoreFailedSources, + bool ignoreWarning, + LocalPackageFileCache fileCache, + bool isFallbackFolderSource) : this(sourceRepository, logger, cacheContext, @@ -106,14 +106,14 @@ public SourceRepositoryDependencyProvider( } internal SourceRepositoryDependencyProvider( - SourceRepository sourceRepository, - ILogger logger, - SourceCacheContext cacheContext, - bool ignoreFailedSources, - bool ignoreWarning, - LocalPackageFileCache fileCache, - bool isFallbackFolderSource, - IEnvironmentVariableReader environmentVariableReader) + SourceRepository sourceRepository, + ILogger logger, + SourceCacheContext cacheContext, + bool ignoreFailedSources, + bool ignoreWarning, + LocalPackageFileCache fileCache, + bool isFallbackFolderSource, + IEnvironmentVariableReader environmentVariableReader) { _sourceRepository = sourceRepository ?? throw new ArgumentNullException(nameof(sourceRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));