|
9 | 9 | using System.IO; |
10 | 10 | using System.Linq; |
11 | 11 | using Microsoft.Internal.NuGet.Testing.SignedPackages; |
| 12 | +using NuGet.Commands.Test; |
12 | 13 | using NuGet.Configuration; |
13 | 14 | using NuGet.Frameworks; |
14 | 15 | using NuGet.LibraryModel; |
| 16 | +using NuGet.Packaging; |
15 | 17 | using NuGet.Test.Utility; |
16 | 18 | using NuGet.Versioning; |
17 | 19 | using Xunit; |
@@ -447,6 +449,164 @@ public void Save_WithCentralVersionDependencies_SerializesMembersAsJson() |
447 | 449 | Assert.Equal(expectedJson, actualJson); |
448 | 450 | } |
449 | 451 |
|
| 452 | + [Theory] |
| 453 | + [InlineData(false)] |
| 454 | + [InlineData(true)] |
| 455 | + public void GetHash_WithCentralPackageVersionsAndPackagesToPrune_IgnoresDictionaryCreationOrder(bool useLegacyHashFunction) |
| 456 | + { |
| 457 | + // Arrange |
| 458 | + DependencyGraphSpec first = CreateDependencyGraphSpecWithCentralDependenciesAndPruning( |
| 459 | + centralPackageVersionNames: ["PackageB", "PackageA"], |
| 460 | + packagesToPruneNames: ["TransitiveB", "TransitiveA"]); |
| 461 | + |
| 462 | + DependencyGraphSpec second = CreateDependencyGraphSpecWithCentralDependenciesAndPruning( |
| 463 | + centralPackageVersionNames: ["PackageA", "PackageB"], |
| 464 | + packagesToPruneNames: ["TransitiveA", "TransitiveB"]); |
| 465 | + |
| 466 | + // Act |
| 467 | + string firstHash = GetHash(first, useLegacyHashFunction); |
| 468 | + string secondHash = GetHash(second, useLegacyHashFunction); |
| 469 | + |
| 470 | + // Assert |
| 471 | + Assert.Equal(firstHash, secondHash); |
| 472 | + } |
| 473 | + |
| 474 | + [Theory] |
| 475 | + [InlineData(false)] |
| 476 | + [InlineData(true)] |
| 477 | + public void GetHash_WithPackagesToPruneInDifferentJsonOrder_IgnoresInputOrder(bool useLegacyHashFunction) |
| 478 | + { |
| 479 | + // Arrange |
| 480 | + DependencyGraphSpec first = CreateDependencyGraphSpecFromJson(@" |
| 481 | + { |
| 482 | + ""restore"": { |
| 483 | + ""projectUniqueName"": ""Project1"", |
| 484 | + ""centralPackageVersionsManagementEnabled"": true |
| 485 | + }, |
| 486 | + ""frameworks"": { |
| 487 | + ""net8.0"": { |
| 488 | + ""dependencies"": { |
| 489 | + ""PackageA"": { |
| 490 | + ""target"": ""Package"", |
| 491 | + ""version"": ""[1.0.0,)"", |
| 492 | + ""versionCentrallyManaged"": true |
| 493 | + } |
| 494 | + }, |
| 495 | + ""centralPackageVersions"": { |
| 496 | + ""PackageA"": ""[1.0.0]"", |
| 497 | + ""PackageB"": ""[2.0.0]"" |
| 498 | + }, |
| 499 | + ""packagesToPrune"": { |
| 500 | + ""TransitiveB"": ""(,2.0.0]"", |
| 501 | + ""TransitiveA"": ""(,1.0.0]"" |
| 502 | + } |
| 503 | + } |
| 504 | + } |
| 505 | + }"); |
| 506 | + |
| 507 | + DependencyGraphSpec second = CreateDependencyGraphSpecFromJson(@" |
| 508 | + { |
| 509 | + ""restore"": { |
| 510 | + ""projectUniqueName"": ""Project1"", |
| 511 | + ""centralPackageVersionsManagementEnabled"": true |
| 512 | + }, |
| 513 | + ""frameworks"": { |
| 514 | + ""net8.0"": { |
| 515 | + ""dependencies"": { |
| 516 | + ""PackageA"": { |
| 517 | + ""target"": ""Package"", |
| 518 | + ""version"": ""[1.0.0,)"", |
| 519 | + ""versionCentrallyManaged"": true |
| 520 | + } |
| 521 | + }, |
| 522 | + ""centralPackageVersions"": { |
| 523 | + ""PackageA"": ""[1.0.0]"", |
| 524 | + ""PackageB"": ""[2.0.0]"" |
| 525 | + }, |
| 526 | + ""packagesToPrune"": { |
| 527 | + ""TransitiveA"": ""(,1.0.0]"", |
| 528 | + ""TransitiveB"": ""(,2.0.0]"" |
| 529 | + } |
| 530 | + } |
| 531 | + } |
| 532 | + }"); |
| 533 | + |
| 534 | + // Act |
| 535 | + string firstHash = GetHash(first, useLegacyHashFunction); |
| 536 | + string secondHash = GetHash(second, useLegacyHashFunction); |
| 537 | + |
| 538 | + // Assert |
| 539 | + Assert.Equal(firstHash, secondHash); |
| 540 | + } |
| 541 | + |
| 542 | + [Theory] |
| 543 | + [InlineData(false)] |
| 544 | + [InlineData(true)] |
| 545 | + public void GetHash_WithCentralPackageVersionsInDifferentJsonOrder_IgnoresInputOrder(bool useLegacyHashFunction) |
| 546 | + { |
| 547 | + // Arrange |
| 548 | + DependencyGraphSpec first = CreateDependencyGraphSpecFromJson(@" |
| 549 | + { |
| 550 | + ""restore"": { |
| 551 | + ""projectUniqueName"": ""Project1"", |
| 552 | + ""centralPackageVersionsManagementEnabled"": true |
| 553 | + }, |
| 554 | + ""frameworks"": { |
| 555 | + ""net8.0"": { |
| 556 | + ""dependencies"": { |
| 557 | + ""PackageA"": { |
| 558 | + ""target"": ""Package"", |
| 559 | + ""version"": ""[1.0.0,)"", |
| 560 | + ""versionCentrallyManaged"": true |
| 561 | + } |
| 562 | + }, |
| 563 | + ""centralPackageVersions"": { |
| 564 | + ""PackageB"": ""[2.0.0]"", |
| 565 | + ""PackageA"": ""[1.0.0]"" |
| 566 | + }, |
| 567 | + ""packagesToPrune"": { |
| 568 | + ""TransitiveA"": ""(,1.0.0]"", |
| 569 | + ""TransitiveB"": ""(,2.0.0]"" |
| 570 | + } |
| 571 | + } |
| 572 | + } |
| 573 | + }"); |
| 574 | + |
| 575 | + DependencyGraphSpec second = CreateDependencyGraphSpecFromJson(@" |
| 576 | + { |
| 577 | + ""restore"": { |
| 578 | + ""projectUniqueName"": ""Project1"", |
| 579 | + ""centralPackageVersionsManagementEnabled"": true |
| 580 | + }, |
| 581 | + ""frameworks"": { |
| 582 | + ""net8.0"": { |
| 583 | + ""dependencies"": { |
| 584 | + ""PackageA"": { |
| 585 | + ""target"": ""Package"", |
| 586 | + ""version"": ""[1.0.0,)"", |
| 587 | + ""versionCentrallyManaged"": true |
| 588 | + } |
| 589 | + }, |
| 590 | + ""centralPackageVersions"": { |
| 591 | + ""PackageA"": ""[1.0.0]"", |
| 592 | + ""PackageB"": ""[2.0.0]"" |
| 593 | + }, |
| 594 | + ""packagesToPrune"": { |
| 595 | + ""TransitiveA"": ""(,1.0.0]"", |
| 596 | + ""TransitiveB"": ""(,2.0.0]"" |
| 597 | + } |
| 598 | + } |
| 599 | + } |
| 600 | + }"); |
| 601 | + |
| 602 | + // Act |
| 603 | + string firstHash = GetHash(first, useLegacyHashFunction); |
| 604 | + string secondHash = GetHash(second, useLegacyHashFunction); |
| 605 | + |
| 606 | + // Assert |
| 607 | + Assert.Equal(firstHash, secondHash); |
| 608 | + } |
| 609 | + |
450 | 610 | [Fact] |
451 | 611 | public void AddProject_WhenRestoreMetadataIsNull_AddsProject() |
452 | 612 | { |
@@ -595,6 +755,45 @@ private static DependencyGraphSpec CreateDependencyGraphSpecWithCentralDependenc |
595 | 755 | return dgSpec; |
596 | 756 | } |
597 | 757 |
|
| 758 | + private static DependencyGraphSpec CreateDependencyGraphSpecWithCentralDependenciesAndPruning( |
| 759 | + IReadOnlyList<string> centralPackageVersionNames, |
| 760 | + IReadOnlyList<string> packagesToPruneNames) |
| 761 | + { |
| 762 | + string centralPackageVersions = string.Join( |
| 763 | + $",{Environment.NewLine}", |
| 764 | + centralPackageVersionNames.Select(packageId => $@" ""{packageId}"": ""[1.0.0]""")); |
| 765 | + string packagesToPrune = string.Join( |
| 766 | + $",{Environment.NewLine}", |
| 767 | + packagesToPruneNames.Select(packageId => $@" ""{packageId}"": ""(,1.0.0]""")); |
| 768 | + |
| 769 | + string rootProject = $@" |
| 770 | + {{ |
| 771 | + ""restore"": {{ |
| 772 | + ""projectUniqueName"": ""Project1"", |
| 773 | + ""centralPackageVersionsManagementEnabled"": true |
| 774 | + }}, |
| 775 | + ""frameworks"": {{ |
| 776 | + ""net8.0"": {{ |
| 777 | + ""dependencies"": {{ |
| 778 | + ""PackageA"": {{ |
| 779 | + ""version"": ""[1.0.0,)"", |
| 780 | + ""target"": ""Package"", |
| 781 | + ""versionCentrallyManaged"": true |
| 782 | + }} |
| 783 | + }}, |
| 784 | + ""centralPackageVersions"": {{ |
| 785 | +{centralPackageVersions} |
| 786 | + }}, |
| 787 | + ""packagesToPrune"": {{ |
| 788 | +{packagesToPrune} |
| 789 | + }} |
| 790 | + }} |
| 791 | + }} |
| 792 | + }}"; |
| 793 | + |
| 794 | + return CreateDependencyGraphSpecFromJson(rootProject); |
| 795 | + } |
| 796 | + |
598 | 797 | private static TargetFrameworkInformation CreateTargetFrameworkInformation() |
599 | 798 | { |
600 | 799 | var nugetFramework = new NuGetFramework("net40"); |
@@ -654,6 +853,19 @@ private static TargetFrameworkInformation CreateTargetFrameworkInformation(Immut |
654 | 853 | return tfi; |
655 | 854 | } |
656 | 855 |
|
| 856 | + private static DependencyGraphSpec CreateDependencyGraphSpecFromJson(string json) |
| 857 | + { |
| 858 | + PackageSpec packageSpec = ProjectTestHelpers.GetPackageSpecWithProjectNameAndSpec("Project1", @"c:\", json); |
| 859 | + return ProjectTestHelpers.GetDGSpecForFirstProject(packageSpec); |
| 860 | + } |
| 861 | + |
| 862 | + private static string GetHash(DependencyGraphSpec dgSpec, bool useLegacyHashFunction) |
| 863 | + { |
| 864 | + return useLegacyHashFunction |
| 865 | + ? dgSpec.GetHash(() => new Sha512HashFunction()) |
| 866 | + : dgSpec.GetHash(() => new FnvHash64Function()); |
| 867 | + } |
| 868 | + |
657 | 869 | private static string GetJson(DependencyGraphSpec dgSpec) |
658 | 870 | { |
659 | 871 | using (TestDirectory testDirectory = TestDirectory.Create()) |
|
0 commit comments