-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
6e89ef6
commit 0f6428e
Showing
40 changed files
with
468 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/core/Microsoft.PowerToys.Settings.UI.Lib/ISettingsUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.PowerToys.Settings.UI.Lib | ||
{ | ||
public interface ISettingsUtils | ||
{ | ||
T GetSettings<T>(string powertoy = "", string fileName = "settings.json"); | ||
|
||
void SaveSettings(string jsonSettings, string powertoy = "", string fileName = "settings.json"); | ||
|
||
bool SettingsExists(string powertoy = "", string fileName = "settings.json"); | ||
|
||
void DeleteSettings(string powertoy = ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/IIOProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities | ||
{ | ||
public interface IIOProvider | ||
{ | ||
bool FileExists(string path); | ||
|
||
bool DirectoryExists(string path); | ||
|
||
bool CreateDirectory(string path); | ||
|
||
void DeleteDirectory(string path); | ||
|
||
void WriteAllText(string path, string content); | ||
|
||
string ReadAllText(string path); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/SystemIOProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.IO; | ||
|
||
namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities | ||
{ | ||
public class SystemIOProvider : IIOProvider | ||
{ | ||
public bool CreateDirectory(string path) | ||
{ | ||
var directioryInfo = Directory.CreateDirectory(path); | ||
return directioryInfo != null; | ||
} | ||
|
||
public void DeleteDirectory(string path) | ||
{ | ||
Directory.Delete(path); | ||
} | ||
|
||
public bool DirectoryExists(string path) | ||
{ | ||
return Directory.Exists(path); | ||
} | ||
|
||
public bool FileExists(string path) | ||
{ | ||
return File.Exists(path); | ||
} | ||
|
||
public string ReadAllText(string path) | ||
{ | ||
return File.ReadAllText(path); | ||
} | ||
|
||
public void WriteAllText(string path, string content) | ||
{ | ||
File.WriteAllText(path, content); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.