diff --git a/src/CommandLine/Commands/ListCommand.cs b/src/CommandLine/Commands/ListCommand.cs index 098c9e4c5..6bf4c3ae9 100644 --- a/src/CommandLine/Commands/ListCommand.cs +++ b/src/CommandLine/Commands/ListCommand.cs @@ -46,7 +46,7 @@ public IEnumerable GetPackages() includeDelisted: IncludeDelisted); if (AllVersions) { - return packages.OrderBy(p => p.Id); + return packages.OrderBy(p => p.Id, StringComparer.OrdinalIgnoreCase); } else { @@ -60,7 +60,7 @@ public IEnumerable GetPackages() } } - var result = packages.OrderBy(p => p.Id) + var result = packages.OrderBy(p => p.Id, StringComparer.OrdinalIgnoreCase) .AsEnumerable(); // we still need to do client side filtering of delisted & prerelease packages. diff --git a/src/Core/Utility/PackageEqualityComparer.cs b/src/Core/Utility/PackageEqualityComparer.cs index ecf1850f5..f5bc4b721 100644 --- a/src/Core/Utility/PackageEqualityComparer.cs +++ b/src/Core/Utility/PackageEqualityComparer.cs @@ -7,13 +7,13 @@ namespace NuGet public sealed class PackageEqualityComparer : IEqualityComparer { [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "This type isn't mutable")] - public static readonly PackageEqualityComparer IdAndVersion = new PackageEqualityComparer((x, y) => x.Id.Equals(y.Id, StringComparison.OrdinalIgnoreCase) && - x.Version.Equals(y.Version), - x => x.Id.GetHashCode() ^ x.Version.GetHashCode()); + public static readonly PackageEqualityComparer IdAndVersion = new PackageEqualityComparer( + (x, y) => StringComparer.OrdinalIgnoreCase.Equals(x.Id, y.Id) && x.Version.Equals(y.Version), + x => StringComparer.OrdinalIgnoreCase.GetHashCode(x.Id) ^ x.Version.GetHashCode()); [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "This type isn't mutable")] - public static readonly PackageEqualityComparer Id = new PackageEqualityComparer((x, y) => x.Id.Equals(y.Id, StringComparison.OrdinalIgnoreCase), - x => x.Id.GetHashCode()); + public static readonly PackageEqualityComparer Id = new PackageEqualityComparer( + (x, y) => StringComparer.OrdinalIgnoreCase.Equals(x.Id, y.Id), x => StringComparer.OrdinalIgnoreCase.GetHashCode(x.Id)); private readonly Func _equals; private readonly Func _getHashCode; diff --git a/src/VsConsole/PowerShellCmdlets/GetPackageCommand.cs b/src/VsConsole/PowerShellCmdlets/GetPackageCommand.cs index 32c71b407..b4d9ee863 100644 --- a/src/VsConsole/PowerShellCmdlets/GetPackageCommand.cs +++ b/src/VsConsole/PowerShellCmdlets/GetPackageCommand.cs @@ -285,7 +285,7 @@ protected virtual IQueryable GetPackages(IPackageRepository sourceRepo ? sourceRepository.GetPackages() : sourceRepository.Search(Filter, effectiveIncludePrerelease); // by default, sort packages by Id - packages = packages.OrderBy(p => p.Id); + packages = packages.OrderBy(p => p.Id, StringComparer.OrdinalIgnoreCase); return packages; } diff --git a/test/CommandLine.Test/ListCommandTests.cs b/test/CommandLine.Test/ListCommandTests.cs index 29644b5aa..3ebf19c78 100644 --- a/test/CommandLine.Test/ListCommandTests.cs +++ b/test/CommandLine.Test/ListCommandTests.cs @@ -180,7 +180,7 @@ public void GetPackageReturnsAllVersionsIfAllVersionsFlagIsSet() // Assert Assert.Equal(5, packages.Count()); - AssertPackage(new { Id = "jQuery", Ver = "1.44" }, packages.ElementAt(0)); + AssertPackage(new { Id = "JQuery", Ver = "1.44" }, packages.ElementAt(0)); AssertPackage(new { Id = "jQuery", Ver = "1.50" }, packages.ElementAt(1)); AssertPackage(new { Id = "NHibernate", Ver = "1.0" }, packages.ElementAt(2)); AssertPackage(new { Id = "NHibernate", Ver = "1.1" }, packages.ElementAt(3)); @@ -365,7 +365,8 @@ private static IPackageRepositoryFactory CreatePackageRepositoryFactory() multiVersionRepo.AddPackage(PackageUtility.CreatePackage("NHibernate", "1.0")); multiVersionRepo.AddPackage(PackageUtility.CreatePackage("NHibernate", "1.1")); multiVersionRepo.AddPackage(PackageUtility.CreatePackage("NHibernate", "1.2")); - multiVersionRepo.AddPackage(PackageUtility.CreatePackage("jQuery", "1.44")); + // different case is intended to test PackageEqualityComparer.Id + multiVersionRepo.AddPackage(PackageUtility.CreatePackage("JQuery", "1.44")); multiVersionRepo.AddPackage(PackageUtility.CreatePackage("jQuery", "1.50")); //Setup Factory diff --git a/test/Core.Test/LocalPackageRepositoryTest.cs b/test/Core.Test/LocalPackageRepositoryTest.cs index de51326f3..be873c4c8 100644 --- a/test/Core.Test/LocalPackageRepositoryTest.cs +++ b/test/Core.Test/LocalPackageRepositoryTest.cs @@ -259,7 +259,7 @@ public void FindPackagesByIdReturnsEmptySequenceIfNoPackagesWithSpecifiedIdAreFo [Fact] public void FindPackagesByIdFindsPackagesWithSpecifiedId() { - // Arramge + // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(PathFixUtility.FixPath(@"Foo.1.0\Foo.1.0.nupkg")); fileSystem.AddFile(PathFixUtility.FixPath(@"Foo.2.0.0\Foo.2.0.0.nupkg")); @@ -267,17 +267,17 @@ public void FindPackagesByIdFindsPackagesWithSpecifiedId() var foo_20 = PackageUtility.CreatePackage("Foo", "2.0.0"); var package_dictionary = new Dictionary - { - { PathFixUtility.FixPath(@"Foo.1.0\Foo.1.0.nupkg"), foo_10}, - { PathFixUtility.FixPath(@"Foo.2.0.0\Foo.2.0.0.nupkg"), foo_20} - }; + { + { PathFixUtility.FixPath(@"Foo.1.0\Foo.1.0.nupkg"), foo_10}, + { PathFixUtility.FixPath(@"Foo.2.0.0\Foo.2.0.0.nupkg"), foo_20} + }; var localPackageRepository = new MockLocalRepository(fileSystem, path => - { - IPackage retval; - package_dictionary.TryGetValue(path, out retval); - return retval; - }); + { + IPackage retval; + package_dictionary.TryGetValue(path, out retval); + return retval; + }); // Act var packages = localPackageRepository.FindPackagesById("Foo").ToList(); @@ -286,6 +286,36 @@ public void FindPackagesByIdFindsPackagesWithSpecifiedId() Assert.Equal(new[] { foo_10, foo_20 }, packages); } + [Fact] + public void FindPackagesByIdFindsPackagesWithSpecifiedIdCaseInsensitive() + { + // Arrange + var fileSystem = new MockFileSystem(); + fileSystem.AddFile(PathFixUtility.FixPath(@"FOO.2.0.0\FOO.2.0.0.nuspec")); + fileSystem.AddFile(PathFixUtility.FixPath(@"Foo.2.0.0\Foo.2.0.0.nupkg")); + var FOO_20 = PackageUtility.CreatePackage("FOO", "2.0.0"); + var foo_20 = PackageUtility.CreatePackage("Foo", "2.0.0"); + + var package_dictionary = new Dictionary + { + { PathFixUtility.FixPath(@"FOO.2.0.0\FOO.2.0.0.nuspec"), FOO_20}, + { PathFixUtility.FixPath(@"Foo.2.0.0\Foo.2.0.0.nupkg"), foo_20} + }; + + var localPackageRepository = new MockLocalRepository(fileSystem, path => + { + IPackage retval; + package_dictionary.TryGetValue(path, out retval); + return retval; + }); + + // Act + var packages = localPackageRepository.FindPackagesById("FOO").ToList(); + + // Assert + Assert.Equal(new[] { foo_20 }, packages); + } + [Fact] public void FindPackagesByIdIgnoresPartialIdMatches() { diff --git a/test/PowerShellCmdlets.Test/GetPackageCommandTest.cs b/test/PowerShellCmdlets.Test/GetPackageCommandTest.cs index b0dc2b11c..01044f9e6 100644 --- a/test/PowerShellCmdlets.Test/GetPackageCommandTest.cs +++ b/test/PowerShellCmdlets.Test/GetPackageCommandTest.cs @@ -407,7 +407,7 @@ public void GetPackagesDoesNotCollapseVersionIfAllVersionsIsPresent() // Assert Assert.Equal(packages.Count(), 6); - AssertPackageResultsEqual(packages.ElementAt(0), new { Id = "jQuery", Version = new SemanticVersion("1.44") }); + AssertPackageResultsEqual(packages.ElementAt(0), new { Id = "JQuery", Version = new SemanticVersion("1.44") }); AssertPackageResultsEqual(packages.ElementAt(1), new { Id = "jQuery", Version = new SemanticVersion("1.51") }); AssertPackageResultsEqual(packages.ElementAt(2), new { Id = "jQuery", Version = new SemanticVersion("1.52") }); AssertPackageResultsEqual(packages.ElementAt(3), @@ -898,7 +898,8 @@ private static IPackageRepository GetRepositoryWithMultiplePackageVersions(strin var repositoryWithMultiplePackageVersions = new Mock(); var packages = new[] { - PackageUtility.CreatePackage("jQuery", "1.44"), + // different case is intended to test that id is case-insensitive + PackageUtility.CreatePackage("JQuery", "1.44"), PackageUtility.CreatePackage("jQuery", "1.51"), PackageUtility.CreatePackage("jQuery", "1.52"), PackageUtility.CreatePackage("NHibernate", "1.1"),