Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions AppDataStorage.Test/AppDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ public void TestLoadOrCreateHandlesCorruptFile()
Assert.AreEqual(string.Empty, appData.Data, "Data should be default if loaded from corrupt file.");
}

[TestMethod]
public void TestLoadOrCreateHandlesNullJsonFile()
{
AbsoluteFilePath filePath = TestAppData.Get().FilePath;
AppData.EnsureDirectoryExists(filePath);
AppData.FileSystem.File.WriteAllText(filePath, "null");

TestAppData appData = TestAppData.LoadOrCreate();

AssertAppDataNotNull(appData, "LoadOrCreate should recover if file deserializes to null.");
Assert.AreEqual(string.Empty, appData.Data, "Data should be default if loaded from a null json file.");
Assert.IsTrue(AppData.FileSystem.File.Exists(filePath), "Recovery should recreate a valid settings file.");
}

[TestMethod]
public async Task TestMultipleSavesOnlyWriteOnceWithinDebouncePeriod()
{
Expand Down
3 changes: 2 additions & 1 deletion AppDataStorage/AppData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,10 @@
/// </summary>
#if NET9_0_OR_GREATER
[JsonIgnore]
public static Lock Lock { get; } = new();

Check warning on line 411 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 411 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 411 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 411 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.
#else
[JsonIgnore]
public static object Lock { get; } = new();

Check warning on line 414 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 414 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 414 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 414 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 414 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 414 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.
#endif

internal bool IsSaveQueued()
Expand Down Expand Up @@ -515,7 +515,8 @@

try
{
newAppData = JsonSerializer.Deserialize<T>(jsonString, AppData.JsonSerializerOptions)!;
newAppData = JsonSerializer.Deserialize<T>(jsonString, AppData.JsonSerializerOptions)
?? throw new JsonException("Deserialized settings file to null.");
newAppData.Subdirectory = subdirectory;
newAppData.FileNameOverride = fileName;
return newAppData;
Expand Down