-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[AzDev] Add Compare-DevPackageDep for dependency comparison
#28917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
isra-fel
wants to merge
1
commit into
Azure:main
Choose a base branch
from
isra-fel:compare-dep
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
tools/AzDev/Tests/ServiceTests/PackageComparisonServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using AzDev.Models.Dep; | ||
| using AzDev.Services; | ||
| using AzDev.Services.Assembly; | ||
| using AzDev.Services.Dep; | ||
| using Moq; | ||
|
|
||
| namespace AzDev.Tests.ServiceTests | ||
| { | ||
| public class PackageComparisonServiceTests | ||
| { | ||
| [Fact] | ||
| public void ComparePackageDependencies_DetectsVersionChange() | ||
| { | ||
| // Arrange | ||
| var mockNugetService = new Mock<INugetService>(); | ||
|
|
||
| // Mock old version dependencies | ||
| mockNugetService.Setup(x => x.GetPackageDependencies("Azure.Core", "1.47.3", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "System.ClientModel", Version = "1.6.1" }, | ||
| new PackageDep { Id = "System.Text.Json", Version = "8.0.6" } | ||
| }); | ||
|
|
||
| // Mock new version dependencies | ||
| mockNugetService.Setup(x => x.GetPackageDependencies("Azure.Core", "1.50.0", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "System.ClientModel", Version = "1.8.0" }, | ||
| new PackageDep { Id = "System.Text.Json", Version = "8.0.6" } | ||
| }); | ||
|
|
||
| // Mock System.ClientModel dependencies (for recursive comparison) | ||
| mockNugetService.Setup(x => x.GetPackageDependencies("System.ClientModel", "1.6.1", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "System.Text.Json", Version = "8.0.6" } | ||
| }); | ||
|
|
||
| mockNugetService.Setup(x => x.GetPackageDependencies("System.ClientModel", "1.8.0", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "System.Text.Json", Version = "8.0.6" } | ||
| }); | ||
|
|
||
| var service = new DefaultDepComparisonService(mockNugetService.Object, NoopLogger.Instance); | ||
|
|
||
| // Act | ||
| var differences = service.ComparePackageDependencies( | ||
| "Azure.Core", | ||
| "1.47.3", | ||
| "1.50.0", | ||
| "netstandard2.0").ToList(); | ||
|
|
||
| // Assert | ||
| Assert.NotEmpty(differences); | ||
| var systemClientModelChange = differences.FirstOrDefault(d => d.DepName == "System.ClientModel"); | ||
| Assert.NotNull(systemClientModelChange); | ||
| Assert.Equal("Azure.Core", systemClientModelChange.ParentDep); | ||
| Assert.Equal("1.6.1", systemClientModelChange.OldVersion); | ||
| Assert.Equal("1.8.0", systemClientModelChange.NewVersion); | ||
|
|
||
| // Verify INugetService was called | ||
| mockNugetService.Verify(x => x.GetPackageDependencies("Azure.Core", "1.47.3", "netstandard2.0"), Times.Once); | ||
| mockNugetService.Verify(x => x.GetPackageDependencies("Azure.Core", "1.50.0", "netstandard2.0"), Times.Once); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ComparePackageDependencies_DetectsAddedDependency() | ||
| { | ||
| // Arrange | ||
| var mockNugetService = new Mock<INugetService>(); | ||
|
|
||
| mockNugetService.Setup(x => x.GetPackageDependencies("TestPackage", "1.0.0", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "Dependency1", Version = "1.0.0" } | ||
| }); | ||
|
|
||
| mockNugetService.Setup(x => x.GetPackageDependencies("TestPackage", "2.0.0", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "Dependency1", Version = "1.0.0" }, | ||
| new PackageDep { Id = "Dependency2", Version = "2.0.0" } | ||
| }); | ||
|
|
||
| var service = new DefaultDepComparisonService(mockNugetService.Object, NoopLogger.Instance); | ||
|
|
||
| // Act | ||
| var differences = service.ComparePackageDependencies( | ||
| "TestPackage", | ||
| "1.0.0", | ||
| "2.0.0", | ||
| "netstandard2.0").ToList(); | ||
|
|
||
| // Assert | ||
| var addedDep = differences.FirstOrDefault(d => d.DepName == "Dependency2"); | ||
| Assert.NotNull(addedDep); | ||
| Assert.Null(addedDep.OldVersion); | ||
| Assert.Equal("2.0.0", addedDep.NewVersion); | ||
| Assert.Equal("TestPackage", addedDep.ParentDep); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ComparePackageDependencies_DetectsRemovedDependency() | ||
| { | ||
| // Arrange | ||
| var mockNugetService = new Mock<INugetService>(); | ||
|
|
||
| mockNugetService.Setup(x => x.GetPackageDependencies("TestPackage", "1.0.0", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "Dependency1", Version = "1.0.0" }, | ||
| new PackageDep { Id = "Dependency2", Version = "2.0.0" } | ||
| }); | ||
|
|
||
| mockNugetService.Setup(x => x.GetPackageDependencies("TestPackage", "2.0.0", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "Dependency1", Version = "1.0.0" } | ||
| }); | ||
|
|
||
| var service = new DefaultDepComparisonService(mockNugetService.Object, NoopLogger.Instance); | ||
|
|
||
| // Act | ||
| var differences = service.ComparePackageDependencies( | ||
| "TestPackage", | ||
| "1.0.0", | ||
| "2.0.0", | ||
| "netstandard2.0").ToList(); | ||
|
|
||
| // Assert | ||
| var removedDep = differences.FirstOrDefault(d => d.DepName == "Dependency2"); | ||
| Assert.NotNull(removedDep); | ||
| Assert.Equal("2.0.0", removedDep.OldVersion); | ||
| Assert.Null(removedDep.NewVersion); | ||
| Assert.Equal("TestPackage", removedDep.ParentDep); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ComparePackageDependencies_SameVersions_ReturnsEmpty() | ||
| { | ||
| // Arrange | ||
| var mockNugetService = new Mock<INugetService>(); | ||
|
|
||
| mockNugetService.Setup(x => x.GetPackageDependencies("TestPackage", "1.0.0", "netstandard2.0")) | ||
| .Returns(new List<PackageDep> | ||
| { | ||
| new PackageDep { Id = "Dependency1", Version = "1.0.0" } | ||
| }); | ||
|
|
||
| var service = new DefaultDepComparisonService(mockNugetService.Object, NoopLogger.Instance); | ||
|
|
||
| // Act | ||
| var differences = service.ComparePackageDependencies( | ||
| "TestPackage", | ||
| "1.0.0", | ||
| "1.0.0", | ||
| "netstandard2.0").ToList(); | ||
|
|
||
| // Assert | ||
| Assert.Empty(differences); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # A package dependency comparing cmdlet | ||
|
|
||
| The goal of this cmdlet is to compare two versions of a nuget package and report the differences about dependencies between them. | ||
|
|
||
| Note the cmdlet not only reports added/removed dependencies, but also reports dependencies that changed their version between the two package versions. For changed dependencies, it compares the old and new versions recursively to report all changes in the dependency tree. | ||
|
|
||
| ## Example usage | ||
|
|
||
| ```powershell | ||
| Compare-DevPackageDep -PackageName "Azure.Core" -OldVersion "1.47.3" -NewVersion "1.50.0" -TargetFramework "netstandard2.0" | ||
|
|
||
| # Output: | ||
| DepName OldVersion NewVersion ParentDep | ||
| ----------- ---------- ---------- ------------- | ||
| System.ClientModel 1.6.1 1.8.0 Azure.Core | ||
| ``` | ||
|
|
||
| ## Cmdlet design considerations | ||
|
|
||
| - `TargetFramework` is optional and defaults to `netstandard2.0` if not specified. Add argument completer (not validate set) for it. Possible values: `netstandard2.0`, `net45`, `net46`, `net47`, `net461`, `net462`. | ||
| - In debug mode, list all dependencies with their versions for both old and new package versions. | ||
|
|
||
| ## Implementation details | ||
|
|
||
| - Leverage `INugetService` to get dependencies of a package version. Do not re-implement nuget package reading logic. | ||
| - Mock `INugetService` in tests to avoid downloading packages from nuget.org. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| using System.Linq; | ||
| using System.Management.Automation; | ||
| using AzDev.Models.PSModels; | ||
| using AzDev.Services; | ||
| using AzDev.Services.Dep; | ||
|
|
||
| namespace AzDev.Cmdlets.Dep | ||
| { | ||
| [Cmdlet("Compare", "DevPackageDep")] | ||
| [OutputType(typeof(PSPackageDepDiff))] | ||
| public class ComparePackageDepCmdlet : DevCmdletBase | ||
| { | ||
| [Parameter(Mandatory = true, Position = 0)] | ||
| public string PackageName { get; set; } | ||
|
|
||
| [Parameter(Mandatory = true, Position = 1)] | ||
| public string OldVersion { get; set; } | ||
|
|
||
| [Parameter(Mandatory = true, Position = 2)] | ||
| public string NewVersion { get; set; } | ||
|
|
||
| [Parameter(Mandatory = false, Position = 3)] | ||
| [ArgumentCompleter(typeof(TargetFrameworkCompleter))] | ||
| public string TargetFramework { get; set; } = "netstandard2.0"; | ||
|
|
||
| protected override void ProcessRecord() | ||
| { | ||
| base.ProcessRecord(); | ||
|
|
||
| var packageComparisonService = AzDevModule.GetService<IDepComparisonService>(); | ||
|
|
||
| WriteDebug($"Comparing {PackageName} from {OldVersion} to {NewVersion} for {TargetFramework}"); | ||
|
|
||
| var differences = packageComparisonService.ComparePackageDependencies( | ||
| PackageName, | ||
| OldVersion, | ||
| NewVersion, | ||
| TargetFramework); | ||
|
|
||
| WriteObject(differences.Select(d => new PSPackageDepDiff(d)), true); | ||
| } | ||
| } | ||
|
|
||
| public class TargetFrameworkCompleter : IArgumentCompleter | ||
| { | ||
| public System.Collections.Generic.IEnumerable<CompletionResult> CompleteArgument( | ||
| string commandName, | ||
| string parameterName, | ||
| string wordToComplete, | ||
| System.Management.Automation.Language.CommandAst commandAst, | ||
| System.Collections.IDictionary fakeBoundParameters) | ||
| { | ||
| var frameworks = new[] { "netstandard2.0", "net45", "net46", "net47", "net461", "net462" }; | ||
| return frameworks | ||
| .Where(f => f.StartsWith(wordToComplete, System.StringComparison.OrdinalIgnoreCase)) | ||
| .Select(f => new CompletionResult(f, f, CompletionResultType.ParameterValue, f)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The service class name in the debug message is inconsistent with the actual class name. The message uses
DefaultPackageComparisonServicebut the class is namedDefaultDepComparisonService.Change line 109 to use the correct class name: