mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Add unit tests for incorrect settings json files (#7644)
* added some test config files * Added tests for each powertoy to ensure that they don't crash even when there is a corrupt json * Revert "Added tests for each powertoy to ensure that they don't crash even when there is a corrupt json" This reverts commitfe3ed40902. * Revert "added some test config files" This reverts commitd08928423a. * add a settings utils to test that a default item is returned when the json file is corrupt
This commit is contained in:
Binary file not shown.
@@ -7,7 +7,9 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.PowerToys.Settings.UnitTest;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
@@ -19,7 +21,6 @@ namespace CommonLibTest
|
||||
public class SettingsUtilsTests
|
||||
{
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void SaveSettingsSaveSettingsToFileWhenFilePathExists()
|
||||
{
|
||||
@@ -79,6 +80,21 @@ namespace CommonLibTest
|
||||
Assert.IsTrue(pathFound);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SettingsUtilsMustReturnDefaultItemWhenFileIsCorrupt()
|
||||
{
|
||||
// Arrange
|
||||
var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider("CorruptJson", string.Empty, "settings.json");
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
|
||||
// Act
|
||||
TestClass settings = mockSettingsUtils.GetSettings<TestClass>(string.Empty);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(settings.TestInt, 100);
|
||||
Assert.AreEqual(settings.TestString, "test");
|
||||
}
|
||||
|
||||
public static string RandomString()
|
||||
{
|
||||
Random random = new Random();
|
||||
@@ -88,5 +104,26 @@ namespace CommonLibTest
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
partial class TestClass : ISettingsConfig
|
||||
{
|
||||
public int TestInt { get; set; } = 100;
|
||||
public string TestString { get; set; } = "test";
|
||||
|
||||
public string GetModuleName()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public bool UpgradeSettingsConfiguration()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user