Skip to content

Commit

Permalink
Merge pull request #114 from sfoslund/FixingCompareIssue
Browse files Browse the repository at this point in the history
Fixing comparer exception issue
  • Loading branch information
sfoslund authored Jan 23, 2020
2 parents 6fc40ba + e0f466c commit 36be3f8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/dotnet-core-uninstall/Shared/BundleInfo/Bundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override string ToString()
}
}

internal class Bundle<TBundleVersion> : Bundle, IComparable, IComparable<Bundle<TBundleVersion>>, IEquatable<Bundle<TBundleVersion>>
internal class Bundle<TBundleVersion> : Bundle, IComparable, IComparable<Bundle>, IEquatable<Bundle<TBundleVersion>>
where TBundleVersion: BundleVersion, IComparable<TBundleVersion>
{
public new TBundleVersion Version => base.Version as TBundleVersion;
Expand All @@ -64,10 +64,10 @@ public Bundle(TBundleVersion version, BundleArch arch, string uninstallCommand,

public int CompareTo(object obj)
{
return CompareTo(obj as Bundle<TBundleVersion>);
return CompareTo(obj as Bundle);
}

public int CompareTo(Bundle<TBundleVersion> other)
public int CompareTo(Bundle other)
{
if (other == null)
{
Expand All @@ -76,7 +76,7 @@ public int CompareTo(Bundle<TBundleVersion> other)

return Version.Equals(other.Version) ?
Arch - other.Arch :
Version.CompareTo(other.Version);
Version.SemVer.CompareTo(other.Version.SemVer);
}

public static IEnumerable<Bundle<TBundleVersion>> FilterWithSameBundleType(IEnumerable<Bundle> bundles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,10 @@ public override int GetHashCode()
}

public abstract Bundle ToBundle(BundleArch arch, string uninstallCommand, string displayName);

public SemanticVersion GetVersionWithoutTags()
{
return new SemanticVersion(this.Major, this.Minor, this.SemVer.Patch);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ private static (IDictionary<IEnumerable<Bundle>, string>, IEnumerable<Bundle>) A
var dividedBundles = new Dictionary<IEnumerable<Bundle>, string>();
foreach (var (division, explaination) in WindowsVersionDivisionsToExplaination)
{
var bundlesInRange = bundleList.Where(bundle => bundle.Version is SdkVersion && division.Item1 <= bundle.Version.SemVer && bundle.Version.SemVer < division.Item2);
var bundlesInRange = bundleList.Where(bundle => bundle.Version is SdkVersion &&
division.Item1 <= bundle.Version.GetVersionWithoutTags() && bundle.Version.GetVersionWithoutTags() < division.Item2);
bundleList = bundleList.Except(bundlesInRange);
if (bundlesInRange.Count() > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,60 @@ private string[] ExpandExpectationShortHand(string[] input)

return output;
}

[WindowsOnlyFact]
internal void TestUninstallableStringsCorrectManySDKs()
{
var bundles = new List<Bundle>
{
new Bundle<SdkVersion>(new SdkVersion("3.0.100-preview-0"), BundleArch.X64, string.Empty, "3.0.100"),
new Bundle<RuntimeVersion>(new RuntimeVersion("2.0.0"), BundleArch.X64, string.Empty, "2.0.0"),
};

for (int i = 0; i < 5; i++)
{
bundles.Add(new Bundle<SdkVersion>(new SdkVersion("2.0." + i), BundleArch.X64, string.Empty, "2.0." + i));
bundles.Add(new Bundle<SdkVersion>(new SdkVersion("2.0." + i + "-preview-0"), BundleArch.X64, string.Empty, "2.0." + i + "-preview-0"));
bundles.Add(new Bundle<SdkVersion>(new SdkVersion("2.0." + i + "-preview-1"), BundleArch.X64, string.Empty, "2.0." + i + "-preview-1"));
}

var strings = VisualStudioSafeVersionsExtractor.GetReasonRequiredStrings(bundles);
strings.Count.Should().Be(bundles.Count);

var expectedProtected = new string[]{ "3.0.100", "2.0.4" };
AssertRequirementStringsCorrect(bundles, strings, expectedProtected);
}

[WindowsOnlyFact]
internal void TestUninstallableStringsCorrectAcrossRequirementDivisions()
{
var bundles = new List<Bundle>
{
new Bundle<SdkVersion>(new SdkVersion("2.0.0"), BundleArch.X64, string.Empty, "2.0.0"),
new Bundle<SdkVersion>(new SdkVersion("2.0.0-preview-0"), BundleArch.X64, string.Empty, "2.0.0-preview-0"),
new Bundle<SdkVersion>(new SdkVersion("2.0.0-preview-1"), BundleArch.X64, string.Empty, "2.0.0-preview-1")
};

var strings = VisualStudioSafeVersionsExtractor.GetReasonRequiredStrings(bundles);
var expectedProtected = new string[] { "2.0.0" };
AssertRequirementStringsCorrect(bundles, strings, expectedProtected);
}

private void AssertRequirementStringsCorrect(List<Bundle> bundles, Dictionary<Bundle, string> bundleStringPairs, string[] expectedProtected)
{
bundleStringPairs.Count.Should().Be(bundles.Count);

var expectedUninstallable = bundles.Select(bundle => bundle.DisplayName)
.Except(expectedProtected);

bundleStringPairs.Where(pair => pair.Key.Version is SdkVersion)
.Where(pair => string.IsNullOrEmpty(pair.Value))
.Select(pair => pair.Key.DisplayName)
.Should().BeEquivalentTo(expectedUninstallable);

bundleStringPairs.Where(pair => !string.IsNullOrEmpty(pair.Value))
.Select(pair => pair.Key.DisplayName)
.Should().BeEquivalentTo(expectedProtected);
}
}
}

0 comments on commit 36be3f8

Please sign in to comment.