better test assertions and more test case coverage

This commit is contained in:
Chet Husk 2022-11-17 12:31:58 -06:00
Родитель 0aaf17fa24
Коммит 4c635e3d48
3 изменённых файлов: 35 добавлений и 21 удалений

Просмотреть файл

@ -54,6 +54,11 @@ namespace Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo
{
return $"{Version.ToString()} ({Arch.ToString().ToLower()})";
}
public virtual string ToDebugString()
{
return $"{Version.ToString()} ({Arch.ToString().ToLower()})";
}
}
internal class Bundle<TBundleVersion> : Bundle, IComparable, IComparable<Bundle>, IEquatable<Bundle<TBundleVersion>>
@ -104,5 +109,9 @@ namespace Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo
{
return HashCode.Combine(base.GetHashCode());
}
public override string ToDebugString() {
return $"{nameof(TBundleVersion)} {Version.ToString()} ({Arch.ToString().ToLower()}) - {UninstallCommand ?? "n/a"}";
}
}
}

Просмотреть файл

@ -19,7 +19,7 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.Commands
{
public class CommandBundleFilterTests
{
private static readonly string[] versions = { "1.0.0", "1.0.1", "1.1.0", "2.1.0", "2.1.500", "2.1.600", "2.2.100", "2.2.200", "5.0.0", "7.0.1", "10.10.10" };
private static readonly string[] versions = { "1.0.0", "1.0.1", "1.1.0", "2.1.0", "2.1.500", "2.1.600", "2.2.100", "2.2.200", "5.0.0", "7.0.1", "8.0.1", "10.10.10" };
private Dictionary<string, BundleArch> versionsWithArch = new Dictionary<string, BundleArch>
{
{ "3.0.0", BundleArch.X64 },
@ -42,7 +42,7 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.Commands
[InlineData("remove --sdk 1.0.1", new string[] { "1.0.1" })]
[InlineData("remove --sdk 1.0.0", new string[] { "1.0.0" })]
[InlineData("remove --sdk 1.0.1 2.1.0 1.0.1", new string[] { "2.1.0", "1.0.1", "1.0.1" })]
[InlineData("remove --sdk 1.0.0 1.0.1 1.1.0 2.1.0 2.1.500 2.1.600 2.2.100 2.2.200",
[InlineData("remove --sdk 1.0.0 1.0.1 1.1.0 2.1.0 2.1.500 2.1.600 2.2.100 2.2.200",
new string[] { "1.0.0", "1.0.1", "1.1.0", "2.1.0", "2.1.500", "2.1.600", "2.2.100", "2.2.200" })]
internal void TestRequiredUninstallableWhenExplicitlyAddedWindows(string command, string[] expectedUninstallable)
{
@ -122,7 +122,7 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.Commands
}
[Theory]
[InlineData("remove {0} 7.0.1")]
[InlineData("remove {0} 8.0.1")]
[InlineData("remove {0} 10.10.10")]
[InlineData("remove {0} --all --force")]
[InlineData("remove {0} 1.0.0 1.0.1 1.1.0 2.1.0 2.1.500 2.1.600 2.2.100 2.2.200 5.0.0 7.0.1 10.10.10")]
@ -158,13 +158,13 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.Commands
{
var parseResult = CommandLineConfigs.UninstallRootCommand.Parse(command);
Action filteringAction = () => CommandBundleFilter.GetFilteredBundles(bundles, parseResult);
filteringAction.Should().Throw<UninstallationNotAllowedException>();
filteringAction.Should().Throw<UninstallationNotAllowedException>("Expected command '{0}' to fail when the following bundles were installed: {1}", command, String.Join(", ", bundles.Select(b => b.DisplayName).ToList()));
}
[Fact]
public void TestHelpOutputContainsExplainationParagraph()
{
foreach (var command in new string[] { "dry-run -h", "whatif -h", "remove -h" })
foreach (var command in new string[] { "dry-run -h", "whatif -h", "remove -h" })
{
var console = new TestConsole();
_ = CommandLineConfigs.UninstallCommandParser.InvokeAsync(command, console).Result;

Просмотреть файл

@ -1,6 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo;
using Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo.Versioning;
using Microsoft.DotNet.Tools.Uninstall.Shared.VSVersioning;
@ -26,7 +28,7 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.VSVersioning
[InlineData(new string[] { "2.2.100", "2.2.200", "2.2.300" }, new bool[] { false, true, false })]
[InlineData(new string[] { "3.0.0", "3.0.1", "5.0.0" }, new bool[] { true, true, false })]
[InlineData(new string[] { "5.0.0", "5.0.1", "6.0.1" }, new bool[] { true, true, false })]
[InlineData(new string[] { "6.0.0", "6.0.1", "7.0.0" }, new bool[] { true, false, false })]
[InlineData(new string[] { "6.0.0", "6.0.1", "7.0.0" }, new bool[] { true, true, false })]
[InlineData(new string[] { "9.0.0", "9.0.1", "10.100.100" }, new bool[] { false, false, false })]
internal void TestGetUninstallableWindows(string[] versions, bool[] allowed)
{
@ -48,7 +50,7 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.VSVersioning
[InlineData(new string[] { "1.0.0" }, new bool[] { false }, new string[] { "1.0.0" }, new bool[] { false })]
[InlineData(new string[] { "1.0.0", "1.0.1" }, new bool[] { true, false }, new string[] { "1.0.0", "1.0.1" }, new bool[] { true, false })]
[InlineData(new string[] { "2.1.0", "1.0.1" }, new bool[] { false, true }, new string[] { "1.0.0", "1.1.0" }, new bool[] { false, false })]
[InlineData(new string[] { "3.0.0", "7.0.0" }, new bool[] { false, false }, new string[] { "1.0.0", "1.1.0", "1.0.1", "1.0.2", "1.1.3" }, new bool[] { true, true, true, false, false })]
[InlineData(new string[] { "3.0.0", "7.0.0" }, new bool[] { false, true }, new string[] { "1.0.0", "1.1.0", "1.0.1", "1.0.2", "1.1.3" }, new bool[] { true, true, true, false, false })]
[InlineData(new string[] { "3.0.0", "5.0.0" }, new bool[] { true, false }, new string[] { "1.0.0", "1.1.0", "1.0.1", "5.0.0" }, new bool[] { true, false, false, false })]
[InlineData(new string[] { "5.0.0", "5.0.1", "10.100.100" }, new bool[] { true, false, false }, new string[] { "5.0.0", "10.0.0" }, new bool[] { false, false })]
[InlineData(new string[] { "5.0.0", "6.0.0", "6.0.1" }, new bool[] { true, true, false }, new string[] { "5.0.0" }, new bool[] { false })]
@ -113,7 +115,7 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.VSVersioning
internal void TestGetUninstallableNonSdkVersions(IEnumerable<Bundle> bundles, bool[] sdkAllowed, bool[] runtimeAllowed)
{
bundles = bundles.Concat(new List<Bundle>
{
{
new Bundle<AspNetRuntimeVersion>(new AspNetRuntimeVersion("1.0.0"), new BundleArch(), string.Empty, "AspNetVersion"),
new Bundle<AspNetRuntimeVersion>(new AspNetRuntimeVersion("10.0.0"), new BundleArch(), string.Empty, "AspNetVersion"),
new Bundle<HostingBundleVersion>(new HostingBundleVersion("1.0.0"), new BundleArch(), string.Empty, "HostingBundleVersion"),
@ -129,32 +131,35 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.VSVersioning
CheckAllowed(bundles, uninstallable, sdkAllowed, runtimeAllowed);
}
private void CheckAllowed(IEnumerable<Bundle> bundles, IEnumerable<Bundle> uninstallable, bool[] sdkAllowed, bool[] runtimeAllowed)
private void CheckAllowed(IEnumerable<Bundle> allBundles, IEnumerable<Bundle> uninstallableBundles, bool[] sdkAllowed, bool[] runtimeAllowed)
{
var sdkBundles = bundles.Where(bundle => bundle.Version is SdkVersion).ToArray();
var runtimeBundles = bundles.Where(bundle => bundle.Version is RuntimeVersion).ToArray();
var otherBundles = bundles.Except(sdkBundles).Except(runtimeBundles);
using var scope = new AssertionScope();
scope.AddReportable("allBundles", () => String.Join(Environment.NewLine, allBundles.Select(b => b.ToDebugString())));
scope.AddReportable("uninstallableBundles", () => String.Join(Environment.NewLine, uninstallableBundles.Select(b => b.ToDebugString())));
var sdkBundles = allBundles.Where(bundle => bundle.Version is SdkVersion).ToArray();
var runtimeBundles = allBundles.Where(bundle => bundle.Version is RuntimeVersion).ToArray();
var otherBundles = allBundles.Except(sdkBundles).Except(runtimeBundles);
for (int i = 0; i < sdkBundles.Count(); i++)
{
if (sdkAllowed[i])
{
uninstallable.Should().Contain(sdkBundles[i]);
uninstallableBundles.Should().Contain(sdkBundles[i]);
}
else
{
uninstallable.Should().NotContain(sdkBundles[i]);
uninstallableBundles.Should().NotContain(sdkBundles[i]);
}
}
for (int i = 0; i < runtimeBundles.Count(); i++)
{
if (runtimeAllowed[i])
{
uninstallable.Should().Contain(runtimeBundles[i]);
uninstallableBundles.Should().Contain(runtimeBundles[i]);
}
else
{
uninstallable.Should().NotContain(runtimeBundles[i]);
uninstallableBundles.Should().NotContain(runtimeBundles[i]);
}
}
// Check others are uninstallable unless their version is above the upper limit
@ -162,11 +167,11 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.VSVersioning
{
if (bundle.Version.SemVer > VisualStudioSafeVersionsExtractor.UpperLimit)
{
uninstallable.Should().NotContain(bundle);
uninstallableBundles.Should().NotContain(bundle);
}
else
{
uninstallable.Should().Contain(bundle);
uninstallableBundles.Should().Contain(bundle);
}
}
}
@ -262,7 +267,7 @@ namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.VSVersioning
{ "Runtime", LocalizableStrings.MacRuntimeRequirementExplainationString}
};
var output = new string[input.Length];
for (int i = 0; i < input.Length; i++)
{
output[i] = shortHandToFullExpectedString[input[i]];