SourcesManagerTests.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Reflection;
  2. using System.Text.Json;
  3. namespace UnitTests.ConfigurationTests;
  4. public class SourcesManagerTests
  5. {
  6. [Fact]
  7. public void Sources_StaysConsistentWhenUpdateFails ()
  8. {
  9. // Arrange
  10. var sourcesManager = new SourcesManager ();
  11. var settingsScope = new SettingsScope ();
  12. // Add one successful source
  13. var validSource = "valid.json";
  14. var validLocation = ConfigLocations.Runtime;
  15. sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", validSource, validLocation);
  16. try
  17. {
  18. // Configure to throw on errors
  19. ConfigurationManager.ThrowOnJsonErrors = true;
  20. // Act & Assert - attempt to update with invalid JSON
  21. var invalidSource = "invalid.json";
  22. var invalidLocation = ConfigLocations.AppCurrent;
  23. var invalidJson = "{ invalid json }";
  24. Assert.Throws<JsonException> (
  25. () =>
  26. sourcesManager.Load (settingsScope, invalidJson, invalidSource, invalidLocation));
  27. // The valid source should still be there
  28. Assert.Single (sourcesManager.Sources);
  29. Assert.Equal (validSource, sourcesManager.Sources [validLocation]);
  30. // The invalid source should not have been added
  31. Assert.DoesNotContain (invalidLocation, sourcesManager.Sources.Keys);
  32. }
  33. finally
  34. {
  35. // Reset for other tests
  36. ConfigurationManager.ThrowOnJsonErrors = false;
  37. }
  38. }
  39. }