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

@@ -15,6 +15,7 @@ namespace ColorPicker.Settings
[Export(typeof(IUserSettings))]
public class UserSettings : IUserSettings
{
private readonly ISettingsUtils _settingsUtils;
private const string ColorPickerModuleName = "ColorPicker";
private const string DefaultActivationShortcut = "Ctrl + Break";
private const int MaxNumberOfRetry = 5;
@@ -27,6 +28,7 @@ namespace ColorPicker.Settings
[ImportingConstructor]
public UserSettings()
{
_settingsUtils = new SettingsUtils(new SystemIOProvider());
ChangeCursor = new SettingItem<bool>(true);
ActivationShortcut = new SettingItem<string>(DefaultActivationShortcut);
CopiedColorRepresentation = new SettingItem<ColorRepresentationType>(ColorRepresentationType.HEX);
@@ -56,14 +58,14 @@ namespace ColorPicker.Settings
{
retryCount++;
if (!SettingsUtils.SettingsExists(ColorPickerModuleName))
if (!_settingsUtils.SettingsExists(ColorPickerModuleName))
{
Logger.LogInfo("ColorPicker settings.json was missing, creating a new one");
var defaultColorPickerSettings = new ColorPickerSettings();
defaultColorPickerSettings.Save();
defaultColorPickerSettings.Save(_settingsUtils);
}
var settings = SettingsUtils.GetSettings<ColorPickerSettings>(ColorPickerModuleName);
var settings = _settingsUtils.GetSettings<ColorPickerSettings>(ColorPickerModuleName);
if (settings != null)
{
ChangeCursor.Value = settings.Properties.ChangeCursor;

View File

@@ -7,6 +7,7 @@ using System.IO;
using System.Threading;
using System.Windows.Input;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using PowerLauncher.Helper;
using Wox.Core.Plugin;
using Wox.Infrastructure.Hotkey;
@@ -20,6 +21,8 @@ namespace PowerLauncher
// Watch for /Local/Microsoft/PowerToys/Launcher/Settings.json changes
public class SettingsWatcher : BaseModel
{
private readonly ISettingsUtils _settingsUtils;
private const int MaxRetries = 10;
private static readonly object _watcherSyncObject = new object();
private readonly FileSystemWatcher _watcher;
@@ -27,6 +30,7 @@ namespace PowerLauncher
public SettingsWatcher(Settings settings)
{
_settingsUtils = new SettingsUtils(new SystemIOProvider());
_settings = settings;
// Set up watcher
@@ -36,13 +40,14 @@ namespace PowerLauncher
OverloadSettings();
}
public static void CreateSettingsIfNotExists()
public void CreateSettingsIfNotExists()
{
if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
if (!_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
{
Log.Info("|SettingsWatcher.OverloadSettings|PT Run settings.json was missing, creating a new one");
var defaultSettings = new PowerLauncherSettings();
defaultSettings.Save();
defaultSettings.Save(_settingsUtils);
}
}
@@ -58,7 +63,7 @@ namespace PowerLauncher
retryCount++;
CreateSettingsIfNotExists();
var overloadSettings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
var overloadSettings = _settingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher);
if (_settings.Hotkey != openPowerlauncher)
@@ -121,7 +126,7 @@ namespace PowerLauncher
// Settings.json could possibly be corrupted. To mitigate this we delete the
// current file and replace it with a correct json value.
SettingsUtils.DeleteSettings(PowerLauncherSettings.ModuleName);
_settingsUtils.DeleteSettings(PowerLauncherSettings.ModuleName);
CreateSettingsIfNotExists();
ErrorReporting.ShowMessageBox(Properties.Resources.deseralization_error_title, Properties.Resources.deseralization_error_message);
}