Skip to content

Commit

Permalink
PR feedback, Adding tests for user input on uninstall command prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
sfoslund committed Jan 14, 2020
1 parent 4ea3d85 commit 36aa40d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/dotnet-core-uninstall/MacOs/FileSystemExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public virtual IEnumerable<Bundle> GetAllInstalledBundles()
return sdks.Concat(runtimes).ToList();
}

private IEnumerable<Bundle> GetInstalledBundles<TBundleVersion>(params string[] paths)
private static IEnumerable<Bundle> GetInstalledBundles<TBundleVersion>(params string[] paths)
where TBundleVersion : BundleVersion, IComparable<TBundleVersion>, new()
{
string bundleTypeString;
Expand All @@ -50,7 +50,7 @@ private IEnumerable<Bundle> GetInstalledBundles<TBundleVersion>(params string[]
string.Format(LocalizableStrings.MacOsBundleDisplayNameFormat, bundleTypeString, group.First().Version.ToString())));
}

private IEnumerable<(TBundleVersion Version, string Path)> GetInstalledVersionsAndUninstallCommands<TBundleVersion>(string path)
private static IEnumerable<(TBundleVersion Version, string Path)> GetInstalledVersionsAndUninstallCommands<TBundleVersion>(string path)
where TBundleVersion : BundleVersion, IComparable<TBundleVersion>, new()
{
return Directory.Exists(path) ?
Expand All @@ -66,7 +66,7 @@ private IEnumerable<Bundle> GetInstalledBundles<TBundleVersion>(params string[]
new List<(TBundleVersion Version, string Path)>();
}

private string GetUninstallCommand(IEnumerable<string> paths)
private static string GetUninstallCommand(IEnumerable<string> paths)
{
return string.Join(" ", paths);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ private static IEnumerable<string> ParseCommandToArgs(string command)
return args;
}

private static bool AskItAndReturnUserAnswer(IDictionary<Bundle, string> bundles)
public static bool AskItAndReturnUserAnswer(IDictionary<Bundle, string> bundles, string userResponse = null)
{
var displayNames = string.Join("\n", bundles.Select(bundle => $" {bundle.Key.DisplayName}"));
Console.Write(string.Format(RuntimeInfo.RunningOnWindows ? LocalizableStrings.WindowsConfirmationPromptOutputFormat :
LocalizableStrings.MacConfirmationPromptOutputFormat, displayNames));

var response = Console.ReadLine().Trim().ToUpper();
var response = userResponse == null ? Console.ReadLine().Trim().ToUpper() : userResponse.ToUpper();

if (response.Equals("Y") || response.Equals("YES"))
{
Expand All @@ -230,7 +230,7 @@ private static bool AskItAndReturnUserAnswer(IDictionary<Bundle, string> bundles
}
}

private static bool AskWithWarningsForRequiredBundles(IDictionary<Bundle, string> bundles)
public static bool AskWithWarningsForRequiredBundles(IDictionary<Bundle, string> bundles, string userResponse = null)
{
var requiredBundles = bundles.Where(b => !b.Value.Equals(string.Empty));
foreach (var pair in requiredBundles)
Expand All @@ -239,7 +239,7 @@ private static bool AskWithWarningsForRequiredBundles(IDictionary<Bundle, string
Console.Write(string.Format(RuntimeInfo.RunningOnWindows ? LocalizableStrings.WindowsRequiredBundleConfirmationPromptOutputFormat :
LocalizableStrings.MacRequiredBundleConfirmationPromptOutputFormat, pair.Key.DisplayName, pair.Value));
Console.ResetColor();
var response = Console.ReadLine().Trim().ToUpper();
var response = userResponse == null ? Console.ReadLine().Trim().ToUpper() : userResponse.ToUpper();
if (response.Equals("N"))
{
return false ;
Expand Down
15 changes: 8 additions & 7 deletions src/dotnet-core-uninstall/Windows/RegistryQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,39 @@ public virtual IEnumerable<Bundle> GetAllInstalledBundles()
return wrappedBundles;
}

private bool IsDotNetCoreBundle(RegistryKey registryKey)
private static bool IsDotNetCoreBundle(RegistryKey registryKey)
{
return IsDotNetCoreBundleDisplayName(registryKey.GetValue("DisplayName") as string)
&& IsDotNetCoreBundlePublisher(registryKey.GetValue("Publisher") as string)
&& IsDotNetCoreBundleUninstaller(registryKey.GetValue("WindowsInstaller") as int?)
&& IsNotVisualStudioDummyVersion(registryKey.GetValue("DisplayName") as string);
}

private bool IsNotVisualStudioDummyVersion(string displayName)
private static bool IsNotVisualStudioDummyVersion(string displayName)
{
return !displayName.Contains(" from Visual Studio");
}

private bool IsDotNetCoreBundleDisplayName(string displayName)
private static bool IsDotNetCoreBundleDisplayName(string displayName)
{
return displayName == null ?
false :
Regexes.BundleDisplayNameRegex.IsMatch(displayName);
}

private bool IsDotNetCoreBundlePublisher(string publisher)
private static bool IsDotNetCoreBundlePublisher(string publisher)
{
return publisher == null ?
false :
Regexes.BundlePublisherRegex.IsMatch(publisher);
}

private bool IsDotNetCoreBundleUninstaller(int? windowsInstaller)
private static bool IsDotNetCoreBundleUninstaller(int? windowsInstaller)
{
return windowsInstaller == null;
}

private Bundle WrapRegistryKey(RegistryKey registryKey)
private static Bundle WrapRegistryKey(RegistryKey registryKey)
{
var displayName = registryKey.GetValue("DisplayName") as string;
var uninstallCommand = registryKey.GetValue("QuietUninstallString") as string;
Expand All @@ -97,12 +97,13 @@ private Bundle WrapRegistryKey(RegistryKey registryKey)
return Bundle.From(version, arch, uninstallCommand, displayName);
}

private void ParseVersionAndArch(RegistryKey registryKey, string displayName, string bundleCachePath, out BundleVersion version, out BundleArch arch)
private static void ParseVersionAndArch(RegistryKey registryKey, string displayName, string bundleCachePath, out BundleVersion version, out BundleArch arch)
{
var match = Regexes.BundleDisplayNameRegex.Match(displayName);
var cachePathMatch = Regexes.BundleCachePathRegex.Match(bundleCachePath);
var archString = cachePathMatch.Groups[Regexes.ArchGroupName].Value ?? string.Empty;
var versionFromCachePath = cachePathMatch.Groups[Regexes.VersionGroupName].Value;
// Note: ASP.NET Core runtimes do not include version in the cache path, need to get version from registry:
var versionFromRegistry = string.Join('.', (registryKey.GetValue("DisplayVersion") as string).Split('.').Take(3));
var versionString = string.IsNullOrEmpty(versionFromCachePath) ? versionFromRegistry : versionFromCachePath;
var hasAuxVersion = cachePathMatch.Groups[Regexes.AuxVersionGroupName].Success;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo;
using Microsoft.DotNet.Tools.Uninstall.Shared.Commands;
using Xunit;
using FluentAssertions;
using Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo.Versioning;

namespace Microsoft.DotNet.Tools.Uninstall.Tests.Shared.Commands
{
public class UninstallCommandExecTests
{
internal enum Results
{
Success,
Reject,
Error
}

[Theory]
[InlineData("Y", Results.Success)]
[InlineData("YES", Results.Success)]
[InlineData("yes", Results.Success)]
[InlineData("n", Results.Reject)]
[InlineData("", Results.Error)]
[InlineData("foo", Results.Error)]
internal void UserInputIsInterpretedCorrectly(string userResponse, Results expectedResult)
{
var bundles = new Dictionary<Bundle, string>() { { new Bundle<SdkVersion>(new SdkVersion(), new BundleArch(), string.Empty, string.Empty), "Required" } };
try
{
var res = UninstallCommandExec.AskWithWarningsForRequiredBundles(bundles, userResponse);
res.Should().Be(expectedResult.Equals(Results.Success));
}
catch
{
expectedResult.Should().Be(Results.Error);
}

try
{
var res = UninstallCommandExec.AskItAndReturnUserAnswer(bundles, userResponse);
res.Should().Be(expectedResult.Equals(Results.Success));
}
catch
{
expectedResult.Should().Be(Results.Error);
}
}
}
}

0 comments on commit 36aa40d

Please sign in to comment.