Skip to content

Commit

Permalink
Add unit tests for incorrect settings json files (#7644)
Browse files Browse the repository at this point in the history
* 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 fe3ed40.

* Revert "added some test config files"

This reverts commit d089284.

* add a settings utils to test that a default item is returned when the json file is corrupt
  • Loading branch information
alekhyareddy28 committed Oct 30, 2020
1 parent c3e9787 commit bd34127
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
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;
Expand All @@ -19,7 +21,6 @@ namespace CommonLibTest
public class SettingsUtilsTests
{


[TestMethod]
public void SaveSettingsSaveSettingsToFileWhenFilePathExists()
{
Expand Down Expand Up @@ -79,6 +80,21 @@ public void SettingsFolderExistsShouldReturnFalseWhenFilePathIsNotFound()
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();
Expand All @@ -88,5 +104,26 @@ public static string RandomString()
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();
}
}
}
}

0 comments on commit bd34127

Please sign in to comment.