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

@@ -2,6 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
@@ -13,7 +15,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public ColorPickerPage()
{
ViewModel = new ColorPickerViewModel(ShellPage.SendDefaultIPCMessage);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new ColorPickerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
InitializeComponent();
}

View File

@@ -2,6 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
@@ -14,7 +16,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public FancyZonesPage()
{
InitializeComponent();
ViewModel = new FancyZonesViewModel(ShellPage.SendDefaultIPCMessage);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new FancyZonesViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
}
}

View File

@@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.ApplicationModel.Resources;
using Windows.Data.Json;
@@ -31,8 +33,10 @@ namespace Microsoft.PowerToys.Settings.UI.Views
// Load string resources
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new GeneralViewModel(
settingsUtils,
loader.GetString("GeneralSettings_RunningAsAdminText"),
loader.GetString("GeneralSettings_RunningAsUserText"),
ShellPage.IsElevated,

View File

@@ -2,6 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
@@ -16,7 +18,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
InitializeComponent();
ViewModel = new ImageResizerViewModel(ShellPage.SendDefaultIPCMessage);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new ImageResizerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
}

View File

@@ -31,7 +31,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
dispatcher = Window.Current.Dispatcher;
ViewModel = new KeyboardManagerViewModel(ShellPage.SendDefaultIPCMessage, FilterRemapKeysList);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new KeyboardManagerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage, FilterRemapKeysList);
watcher = Helper.GetFileWatcher(
PowerToyName,

View File

@@ -4,6 +4,8 @@
using System;
using System.Collections.ObjectModel;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
@@ -19,7 +21,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public PowerLauncherPage()
{
InitializeComponent();
ViewModel = new PowerLauncherViewModel(ShellPage.SendDefaultIPCMessage, (int)Windows.System.VirtualKey.Space);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new PowerLauncherViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage, (int)Windows.System.VirtualKey.Space);
DataContext = ViewModel;
var loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();

View File

@@ -2,6 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
@@ -17,7 +19,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public PowerPreviewPage()
{
InitializeComponent();
ViewModel = new PowerPreviewViewModel(ShellPage.SendDefaultIPCMessage);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new PowerPreviewViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
}
}

View File

@@ -2,6 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
@@ -14,7 +16,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public PowerRenamePage()
{
InitializeComponent();
ViewModel = new PowerRenameViewModel(ShellPage.SendDefaultIPCMessage);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new PowerRenameViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
}

View File

@@ -2,6 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
@@ -14,8 +16,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public ShortcutGuidePage()
{
InitializeComponent();
ViewModel = new ShortcutGuideViewModel(ShellPage.SendDefaultIPCMessage);
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new ShortcutGuideViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
}
}