From f4d3414c881afa3bb5d4154b7bfa903cf17194b6 Mon Sep 17 00:00:00 2001 From: Simon Ensslen Date: Tue, 3 Sep 2024 15:36:19 +0200 Subject: [PATCH] fix specifying version ranges (#79) --- NuGetUtility.sln | 21 +++++++++++++++++++ .../ReferencedPackageReader.cs | 7 +++---- .../MsBuildWrapper/IMsBuildAbstraction.cs | 2 +- .../MsBuildWrapper/MsBuildAbstraction.cs | 9 ++------ .../ReferencedPackageReaderTest.cs | 2 +- ...ReferencedPackagesReaderIntegrationTest.cs | 11 ++++++++++ .../VersionRangesProject.csproj | 11 ++++++++++ 7 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 tests/targets/VersionRangesProject/VersionRangesProject.csproj diff --git a/NuGetUtility.sln b/NuGetUtility.sln index 93e7e037..9d3bead0 100644 --- a/NuGetUtility.sln +++ b/NuGetUtility.sln @@ -42,6 +42,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjectWithReferenceContain EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EmptyCppProject", "tests\targets\EmptyCppProject\EmptyCppProject.vcxproj", "{9B107A91-87F9-420C-ADF8-732C77DAFB93}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VersionRangesProject", "tests\targets\VersionRangesProject\VersionRangesProject.csproj", "{6C96D10E-6CB6-4387-922F-C7C97C037495}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -265,6 +267,24 @@ Global {9B107A91-87F9-420C-ADF8-732C77DAFB93}.TestWindows|x64.Build.0 = Debug|x64 {9B107A91-87F9-420C-ADF8-732C77DAFB93}.TestWindows|x86.ActiveCfg = Debug|Win32 {9B107A91-87F9-420C-ADF8-732C77DAFB93}.TestWindows|x86.Build.0 = Debug|Win32 + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Debug|x64.ActiveCfg = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Debug|x64.Build.0 = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Debug|x86.ActiveCfg = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Debug|x86.Build.0 = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Release|Any CPU.Build.0 = Release|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Release|x64.ActiveCfg = Release|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Release|x64.Build.0 = Release|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Release|x86.ActiveCfg = Release|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.Release|x86.Build.0 = Release|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.TestWindows|Any CPU.ActiveCfg = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.TestWindows|Any CPU.Build.0 = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.TestWindows|x64.ActiveCfg = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.TestWindows|x64.Build.0 = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.TestWindows|x86.ActiveCfg = Debug|Any CPU + {6C96D10E-6CB6-4387-922F-C7C97C037495}.TestWindows|x86.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -283,6 +303,7 @@ Global {DE079B9C-B6BA-4D53-8B83-03D3CBD4027F} = {D2AB2D00-1F48-487D-BFE0-99FDB4E071CC} {01704839-219A-4CE2-93F4-DB3CB8773DCE} = {FFB2826C-17A3-4C18-8FFE-A34AB51592F7} {9B107A91-87F9-420C-ADF8-732C77DAFB93} = {FA92392F-D895-4D1E-A5ED-E6DC3C08223E} + {6C96D10E-6CB6-4387-922F-C7C97C037495} = {FA92392F-D895-4D1E-A5ED-E6DC3C08223E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {70887D40-0182-4C32-BFA1-B5A02E405F11} diff --git a/src/NuGetUtility/ReferencedPackagesReader/ReferencedPackageReader.cs b/src/NuGetUtility/ReferencedPackagesReader/ReferencedPackageReader.cs index e71ca6bf..dde2f026 100644 --- a/src/NuGetUtility/ReferencedPackagesReader/ReferencedPackageReader.cs +++ b/src/NuGetUtility/ReferencedPackagesReader/ReferencedPackageReader.cs @@ -75,7 +75,7 @@ private IEnumerable GetReferencedLibrariesForTarget(IProject p if (!includeTransitive) { ITargetFrameworkInformation targetFrameworkInformation = GetTargetFrameworkInformation(target, assetsFile); - IEnumerable directlyReferencedPackages = _msBuild.GetPackageReferencesFromProjectForFramework(project, + IEnumerable directlyReferencedPackages = _msBuild.GetPackageReferencesFromProjectForFramework(project, targetFrameworkInformation.FrameworkName.ToString()!); referencedLibrariesForTarget = @@ -86,11 +86,10 @@ private IEnumerable GetReferencedLibrariesForTarget(IProject p } private bool IsDirectlyReferenced(ILockFileLibrary library, - IEnumerable directlyReferencedPackages) + IEnumerable directlyReferencedPackages) { return directlyReferencedPackages.Any(p => - library.Name.Equals(p.PackageName, StringComparison.OrdinalIgnoreCase) && ((p.Version == null) || - library.Version.Equals(p.Version))); + library.Name.Equals(p, StringComparison.OrdinalIgnoreCase)); } private static ITargetFrameworkInformation GetTargetFrameworkInformation(ILockFileTarget target, diff --git a/src/NuGetUtility/Wrapper/MsBuildWrapper/IMsBuildAbstraction.cs b/src/NuGetUtility/Wrapper/MsBuildWrapper/IMsBuildAbstraction.cs index d5fc59de..35993272 100644 --- a/src/NuGetUtility/Wrapper/MsBuildWrapper/IMsBuildAbstraction.cs +++ b/src/NuGetUtility/Wrapper/MsBuildWrapper/IMsBuildAbstraction.cs @@ -5,7 +5,7 @@ namespace NuGetUtility.Wrapper.MsBuildWrapper { public interface IMsBuildAbstraction { - IEnumerable GetPackageReferencesFromProjectForFramework(IProject project, string framework); + IEnumerable GetPackageReferencesFromProjectForFramework(IProject project, string framework); IProject GetProject(string projectPath); IEnumerable GetProjectsFromSolution(string inputPath); } diff --git a/src/NuGetUtility/Wrapper/MsBuildWrapper/MsBuildAbstraction.cs b/src/NuGetUtility/Wrapper/MsBuildWrapper/MsBuildAbstraction.cs index 1b7499fa..2c7253e2 100644 --- a/src/NuGetUtility/Wrapper/MsBuildWrapper/MsBuildAbstraction.cs +++ b/src/NuGetUtility/Wrapper/MsBuildWrapper/MsBuildAbstraction.cs @@ -7,7 +7,6 @@ using Microsoft.Build.Execution; using Microsoft.Build.Framework; using Microsoft.Build.Locator; -using NuGetUtility.Wrapper.NuGetWrapper.Versioning; namespace NuGetUtility.Wrapper.MsBuildWrapper { @@ -23,7 +22,7 @@ public MsBuildAbstraction() RegisterMsBuildLocatorIfNeeded(); } - public IEnumerable GetPackageReferencesFromProjectForFramework(IProject project, + public IEnumerable GetPackageReferencesFromProjectForFramework(IProject project, string framework) { var globalProperties = new Dictionary(StringComparer.OrdinalIgnoreCase) @@ -34,11 +33,7 @@ public IEnumerable GetPackageReferencesFromProjectForFramework newProject.Build(new[] { CollectPackageReferences }, new List(), out IDictionary? targetOutputs); return targetOutputs.First(e => e.Key.Equals(CollectPackageReferences)) - .Value.Items.Select(p => - new PackageReference(p.ItemSpec, - string.IsNullOrEmpty(p.GetMetadata("version")) - ? null - : new WrappedNuGetVersion(p.GetMetadata("version")))); + .Value.Items.Select(p => p.ItemSpec); } public IProject GetProject(string projectPath) diff --git a/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackageReaderTest.cs b/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackageReaderTest.cs index 1fca7751..98c3b137 100644 --- a/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackageReaderTest.cs +++ b/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackageReaderTest.cs @@ -51,7 +51,7 @@ public void SetUp() _packageSpecMock.TargetFrameworks.Returns(_packageSpecTargetFrameworks); _msBuild.GetPackageReferencesFromProjectForFramework(_projectMock, Arg.Any()) - .Returns(args => _packageReferencesFromProjectForFramework[args.ArgAt(1)]); + .Returns(args => _packageReferencesFromProjectForFramework[args.ArgAt(1)].Select(p => p.PackageName)); foreach (ILockFileLibrary lockFileLibrary in _lockFileLibraries) { diff --git a/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackagesReaderIntegrationTest.cs b/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackagesReaderIntegrationTest.cs index c4234b71..6d27e17e 100644 --- a/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackagesReaderIntegrationTest.cs +++ b/tests/NuGetUtility.Test/ReferencedPackagesReader/ReferencedPackagesReaderIntegrationTest.cs @@ -72,6 +72,17 @@ public void GetInstalledPackagesShould_ReturnEmptyEnumerable_For_ProjectsWithout Assert.That(result.Count, Is.EqualTo(0)); } + [Test] + public void GetInstalledPackagesShould_ReturnResolvedDependency_For_ProjectWithRangedDependencies([Values] bool includeTransitive) + { + string path = Path.GetFullPath( + "../../../../targets/VersionRangesProject/VersionRangesProject.csproj"); + + IEnumerable result = _uut!.GetInstalledPackages(path, includeTransitive); + + Assert.That(result.Count, Is.EqualTo(includeTransitive ? 2 : 1)); + } + [Test] [Platform(Include = "Win")] public void GetInstalledPackagesShould_ReturnPackages_For_PackagesConfigProject() diff --git a/tests/targets/VersionRangesProject/VersionRangesProject.csproj b/tests/targets/VersionRangesProject/VersionRangesProject.csproj new file mode 100644 index 00000000..a5caf3fa --- /dev/null +++ b/tests/targets/VersionRangesProject/VersionRangesProject.csproj @@ -0,0 +1,11 @@ + + + + net8.0 + + + + + + + \ No newline at end of file