User/ryanbod/mock settings disk access (#6188)

* 1) Making Directory Methods private.
2) Removing the CreateDirectory / DeleteDirectory functionality from all Settings Unit Tests.

* Abstracting disk access via IIOProvider to be able to provide mocks for unit tests instead of writing to disk.   This also prevents developers who are running unit tests from interfering with the PowerToys settings on their local dev box.

* Dependency Injecting stub SettingsUtils for all tests

* Removing ISettingsUtils from constructors of objects that need to be deserialized (ColorPickerSettings/PowerLauncherSettings) as this breaks System.Text.Json

* Removing unused namespace reference

* Removing redifined mock

* As per PR feedback.  Stub Settings utils should work with any settings type if the intent is to compile / avoid null ref exceptions.

Strangely when implementing this fix it became apparent that a stub settings isn't enough, and disk access needed to be mocked.  I can't explain why the tests were passing previously.

* Leveraging GetMockIOProviderForSaveLoadExists
This commit is contained in:
ryanbodrug-microsoft
2020-09-21 10:14:44 -07:00
committed by GitHub
parent 6e89ef62e4
commit 0f6428eed0
40 changed files with 468 additions and 435 deletions

View File

@@ -6,11 +6,14 @@ using System;
using System.Runtime.CompilerServices;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
{
public class PowerLauncherViewModel : Observable
{
private readonly ISettingsUtils _settingsUtils;
private PowerLauncherSettings settings;
private GeneralSettings generalSettings;
@@ -20,21 +23,21 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
private Func<string, int> SendConfigMSG { get; }
public PowerLauncherViewModel(Func<string, int> ipcMSGCallBackFunc, int defaultKeyCode)
public PowerLauncherViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc, int defaultKeyCode)
{
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
// set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
callback = (PowerLauncherSettings settings) =>
{
// Propagate changes to Power Launcher through IPC
SendConfigMSG(
string.Format("{{ \"powertoys\": {{ \"{0}\": {1} }} }}", PowerLauncherSettings.ModuleName, JsonSerializer.Serialize(settings)));
};
if (SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
if (_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
{
settings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
settings = _settingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
}
else
{
@@ -45,9 +48,9 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
callback(settings);
}
if (SettingsUtils.SettingsExists())
if (_settingsUtils.SettingsExists())
{
generalSettings = SettingsUtils.GetSettings<GeneralSettings>();
generalSettings = _settingsUtils.GetSettings<GeneralSettings>();
}
else
{