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 commit fe3ed40902.

* Revert "added some test config files"

This reverts commit d08928423a.

* add a settings utils to test that a default item is returned when the json file is corrupt
This commit is contained in:
Alekhya
2020-10-30 12:53:57 -07:00
committed by GitHub
parent c3e978793c
commit bd34127cd4
2 changed files with 38 additions and 1 deletions

View File

@@ -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();
}
}
}
}