Skip to content

Commit

Permalink
fix specifying version ranges (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen authored Sep 3, 2024
1 parent b4ada34 commit f4d3414
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
21 changes: 21 additions & 0 deletions NuGetUtility.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private IEnumerable<ILockFileLibrary> GetReferencedLibrariesForTarget(IProject p
if (!includeTransitive)
{
ITargetFrameworkInformation targetFrameworkInformation = GetTargetFrameworkInformation(target, assetsFile);
IEnumerable<PackageReference> directlyReferencedPackages = _msBuild.GetPackageReferencesFromProjectForFramework(project,
IEnumerable<string> directlyReferencedPackages = _msBuild.GetPackageReferencesFromProjectForFramework(project,
targetFrameworkInformation.FrameworkName.ToString()!);

referencedLibrariesForTarget =
Expand All @@ -86,11 +86,10 @@ private IEnumerable<ILockFileLibrary> GetReferencedLibrariesForTarget(IProject p
}

private bool IsDirectlyReferenced(ILockFileLibrary library,
IEnumerable<PackageReference> directlyReferencedPackages)
IEnumerable<string> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NuGetUtility.Wrapper.MsBuildWrapper
{
public interface IMsBuildAbstraction
{
IEnumerable<PackageReference> GetPackageReferencesFromProjectForFramework(IProject project, string framework);
IEnumerable<string> GetPackageReferencesFromProjectForFramework(IProject project, string framework);
IProject GetProject(string projectPath);
IEnumerable<string> GetProjectsFromSolution(string inputPath);
}
Expand Down
9 changes: 2 additions & 7 deletions src/NuGetUtility/Wrapper/MsBuildWrapper/MsBuildAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -23,7 +22,7 @@ public MsBuildAbstraction()
RegisterMsBuildLocatorIfNeeded();
}

public IEnumerable<PackageReference> GetPackageReferencesFromProjectForFramework(IProject project,
public IEnumerable<string> GetPackageReferencesFromProjectForFramework(IProject project,
string framework)
{
var globalProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
Expand All @@ -34,11 +33,7 @@ public IEnumerable<PackageReference> GetPackageReferencesFromProjectForFramework
newProject.Build(new[] { CollectPackageReferences }, new List<ILogger>(), out IDictionary<string, TargetResult>? 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void SetUp()
_packageSpecMock.TargetFrameworks.Returns(_packageSpecTargetFrameworks);

_msBuild.GetPackageReferencesFromProjectForFramework(_projectMock, Arg.Any<string>())
.Returns(args => _packageReferencesFromProjectForFramework[args.ArgAt<string>(1)]);
.Returns(args => _packageReferencesFromProjectForFramework[args.ArgAt<string>(1)].Select(p => p.PackageName));

foreach (ILockFileLibrary lockFileLibrary in _lockFileLibraries)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackageIdentity> result = _uut!.GetInstalledPackages(path, includeTransitive);

Assert.That(result.Count, Is.EqualTo(includeTransitive ? 2 : 1));
}

[Test]
[Platform(Include = "Win")]
public void GetInstalledPackagesShould_ReturnPackages_For_PackagesConfigProject()
Expand Down
11 changes: 11 additions & 0 deletions tests/targets/VersionRangesProject/VersionRangesProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="[8.0.1]" />
</ItemGroup>

</Project>

0 comments on commit f4d3414

Please sign in to comment.