-
Notifications
You must be signed in to change notification settings - Fork 40
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
Select latest based on version then date #41
Merged
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 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 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 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,65 @@ | ||
// <copyright file="InstanceComparer.cs" company="Microsoft Corporation"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE.txt in the project root for license information. | ||
// </copyright> | ||
|
||
namespace Microsoft.VisualStudio.Setup | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// Compares instances of <see cref="Instance"/>. | ||
/// </summary> | ||
internal class InstanceComparer : IComparer<Instance> | ||
{ | ||
/// <summary> | ||
/// Gets an instance of the <see cref="InstanceComparer"/> that compares only the | ||
/// <see cref="Instance.InstallationVersion"/> and <see cref="Instance.InstallDate"/> properties. | ||
/// </summary> | ||
public static readonly IComparer<Instance> VersionAndDate = new InstanceComparer(); | ||
|
||
/// <inheritdoc/> | ||
public int Compare(Instance x, Instance y) | ||
{ | ||
if (ReferenceEquals(x, y)) | ||
{ | ||
return 0; | ||
} | ||
else if (x is null) | ||
{ | ||
return -1; | ||
} | ||
else if (y is null) | ||
{ | ||
return 1; | ||
} | ||
|
||
var result = Compare(x.InstallationVersion, y.InstallationVersion); | ||
if (result != 0) | ||
{ | ||
return result; | ||
} | ||
|
||
return DateTime.Compare(x.InstallDate, y.InstallDate); | ||
} | ||
|
||
private static int Compare(Version x, Version y) | ||
{ | ||
if (ReferenceEquals(x, y)) | ||
{ | ||
return 0; | ||
} | ||
else if (x is null) | ||
{ | ||
return -1; | ||
} | ||
else if (y is null) | ||
{ | ||
return 1; | ||
} | ||
|
||
return x.CompareTo(y); | ||
} | ||
} | ||
} |
This file contains 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 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 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,61 @@ | ||
// <copyright file="InstanceComparerTests.cs" company="Microsoft Corporation"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE.txt in the project root for license information. | ||
// </copyright> | ||
|
||
namespace Microsoft.VisualStudio.Setup | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using Microsoft.VisualStudio.Setup.Configuration; | ||
using Moq; | ||
using Xunit; | ||
|
||
public class InstanceComparerTests | ||
{ | ||
public static IEnumerable<object[]> GetCompareVersionAndDateData() | ||
{ | ||
var empty = new string[0]; | ||
|
||
var v1 = new Version(1, 0); | ||
var v2 = new Version(2, 0); | ||
|
||
var ft1 = new FILETIME { dwHighDateTime = 1 }; | ||
var ft2 = new FILETIME { dwHighDateTime = 2 }; | ||
|
||
Instance Mock(Version v = null, FILETIME ft = default(FILETIME)) | ||
{ | ||
var mock = new Mock<ISetupInstance2>(); | ||
mock.As<ISetupPropertyStore>().Setup(x => x.GetNames()).Returns(empty); | ||
|
||
mock.Setup(x => x.GetInstallationVersion()).Returns(v?.ToString()); | ||
mock.Setup(x => x.GetInstallDate()).Returns(ft); | ||
|
||
return new Instance(mock.Object); | ||
} | ||
|
||
return new[] | ||
{ | ||
new object[] { null, null, 0 }, | ||
new object[] { null, Mock(), -1 }, | ||
new object[] { Mock(), null, 1 }, | ||
new object[] { Mock(), Mock(v1), -1 }, | ||
new object[] { Mock(v1), Mock(), 1 }, | ||
new object[] { Mock(v1), Mock(v1), 0 }, | ||
new object[] { Mock(v1), Mock(v2), -1 }, | ||
new object[] { Mock(v2), Mock(v1), 1 }, | ||
new object[] { Mock(v2, ft1), Mock(v2, ft2), -1 }, | ||
new object[] { Mock(v2, ft2), Mock(v2, ft1), 1 }, | ||
}; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetCompareVersionAndDateData))] | ||
public void CompareVersionAndDate(Instance x, Instance y, int expected) | ||
{ | ||
var actual = InstanceComparer.VersionAndDate.Compare(x, y); | ||
Assert.Equal(expected, actual); | ||
} | ||
} | ||
} |
This file contains 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
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.
Maybe add new object[] { Mock(v2, ft1), Mock(v2, ft1), 0}?
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.
Already covered with line 45.
FILETIME
is a struct so, by default, all zeros; thus, we already checked for equality when the version was equal.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.
Oops, thanks. Didn't notice default(FILETIME) :P.