-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Select latest based on version then date
Fixes #40 and updates the test data to make sure we're properly sorting and selecting. Also made a corresponding test-only change to Microsoft/vswhere to make sure this case isn't regressed there (was already handled correctly).
- Loading branch information
Showing
7 changed files
with
139 additions
and
11 deletions.
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