| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- using System.Reflection;
- using System.Text.Json;
- namespace UnitTests_Parallelizable.ConfigurationTests;
- public class SourcesManagerTests
- {
- #region Update (Stream)
- [Fact]
- public void Load_WithNullSettingsScope_ReturnsFalse ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var stream = new MemoryStream ();
- var source = "test.json";
- var location = ConfigLocations.AppCurrent;
- // Act
- bool result = sourcesManager.Load (null, stream, source, location);
- // Assert
- Assert.False (result);
- }
- [Fact]
- public void Load_WithValidStream_UpdatesSettingsScope ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- settingsScope.LoadHardCodedDefaults ();
- settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
- var json = """
- {
- "Application.QuitKey": "Ctrl+Z"
- }
- """;
- var location = ConfigLocations.HardCoded;
- var source = "stream";
- var stream = new MemoryStream ();
- var writer = new StreamWriter (stream);
- writer.Write (json);
- writer.Flush ();
- stream.Position = 0;
- // Act
- bool result = sourcesManager.Load (settingsScope, stream, source, location);
- // Assert
- // Assert
- Assert.True (result);
- Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
- Assert.Contains (source, sourcesManager.Sources.Values);
- }
- [Fact]
- public void Load_WithInvalidJson_AddsJsonError ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- var invalidJson = "{ invalid json }";
- var stream = new MemoryStream ();
- var writer = new StreamWriter (stream);
- writer.Write (invalidJson);
- writer.Flush ();
- stream.Position = 0;
- var source = "test.json";
- var location = ConfigLocations.AppCurrent;
- // Act
- bool result = sourcesManager.Load (settingsScope, stream, source, location);
- // Assert
- Assert.False (result);
- // Assuming AddJsonError logs errors, verify the error was logged (mock or inspect logs if possible).
- }
- #endregion
- #region Update (FilePath)
- [Fact]
- public void Load_WithNonExistentFile_AddsToSourcesAndReturnsTrue ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- var filePath = "nonexistent.json";
- var location = ConfigLocations.AppCurrent;
- // Act
- bool result = sourcesManager.Load (settingsScope, filePath, location);
- // Assert
- Assert.True (result);
- Assert.Contains (filePath, sourcesManager.Sources.Values);
- }
- [Fact]
- public void Load_WithValidFile_UpdatesSettingsScope ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- settingsScope.LoadHardCodedDefaults ();
- settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
- var json = """
- {
- "Application.QuitKey": "Ctrl+Z"
- }
- """;
- var source = Path.GetTempFileName ();
- var location = ConfigLocations.HardCoded;
- File.WriteAllText (source, json);
- try
- {
- // Act
- bool result = sourcesManager.Load (settingsScope, source, location);
- // Assert
- Assert.True (result);
- Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
- Assert.Contains (source, sourcesManager.Sources.Values);
- }
- finally
- {
- // Cleanup
- File.Delete (source);
- }
- }
- [Fact]
- public void Load_WithIOException_RetriesAndFailsGracefully ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- settingsScope.LoadHardCodedDefaults ();
- var filePath = "locked.json";
- var json = "{\"Application.UseSystemConsole\": true}";
- File.WriteAllText (filePath, json);
- var location = ConfigLocations.AppCurrent;
- try
- {
- using FileStream fileStream = File.Open (filePath, FileMode.Open, FileAccess.Read, FileShare.None);
- // Act
- bool result = sourcesManager.Load (settingsScope, filePath, location);
- // Assert
- Assert.False (result);
- }
- finally
- {
- // Cleanup
- File.Delete (filePath);
- }
- }
- #endregion
- #region Update (Json String)
- [Fact]
- public void Load_WithNullOrEmptyJson_ReturnsFalse ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- var source = "test.json";
- var location = ConfigLocations.AppCurrent;
- // Act
- bool resultWithNull = sourcesManager.Load (settingsScope, json: null, source, location);
- bool resultWithEmpty = sourcesManager.Load (settingsScope, string.Empty, source, location);
- // Assert
- Assert.False (resultWithNull);
- Assert.False (resultWithEmpty);
- }
- [Fact]
- public void Load_WithValidJson_UpdatesSettingsScope ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- settingsScope.LoadHardCodedDefaults ();
- settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
- var json = """
- {
- "Application.QuitKey": "Ctrl+Z"
- }
- """;
- var source = "test.json";
- var location = ConfigLocations.HardCoded;
- // Act
- bool result = sourcesManager.Load (settingsScope, json, source, location);
- // Assert
- Assert.True (result);
- Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
- Assert.Contains (source, sourcesManager.Sources.Values);
- }
- //[Fact]
- //public void Update_WithValidJson_UpdatesThemeScope ()
- //{
- // // Arrange
- // var sourcesManager = new SourcesManager ();
- // var themeScope = new ThemeScope ();
- // themeScope.LoadHardCodedDefaults ();
- // themeScope ["Button.DefaultShadowStyle"].PropertyValue = ShadowStyle.Opaque;
- // var json = """
- // {
- // "Button.DefaultShadowStyle": "None"
- // }
- // """;
- // var source = "test.json";
- // var location = ConfigLocations.HardCoded;
- // // Act
- // bool result = sourcesManager.Load (themeScope, json, source, location);
- // // Assert
- // Assert.True (result);
- // Assert.Equal (Key.Z.WithCtrl, themeScope ["Application.QuitKey"].PropertyValue as Key);
- // Assert.Contains (source, sourcesManager.Sources.Values);
- //}
- #endregion
- #region Load
- [Fact]
- public void Load_WithNullResourceName_ReturnsFalse ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- settingsScope.LoadHardCodedDefaults ();
- settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
- var assembly = Assembly.GetExecutingAssembly ();
- var location = ConfigLocations.AppResources;
- // Act
- bool result = sourcesManager.Load (settingsScope, assembly, null, location);
- // Assert
- Assert.False (result);
- }
- [Fact]
- public void Load_WithValidResource_UpdatesSettingsScope ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
- var resourceName = "Terminal.Gui.Resources.config.json";
- var location = ConfigLocations.LibraryResources;
- // Act
- bool result = sourcesManager.Load (settingsScope, assembly!, resourceName, location);
- // Assert
- Assert.True (result);
- // Verify settingsScope is updated as expected
- }
- [Fact]
- public void Load_Runtime_Overrides ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
- var resourceName = "Terminal.Gui.Resources.config.json";
- var location = ConfigLocations.LibraryResources;
- sourcesManager.Load (settingsScope, assembly!, resourceName, location);
- Assert.Equal (Key.Esc, settingsScope ["Application.QuitKey"].PropertyValue);
- var runtimeJson = """
- {
- "Application.QuitKey": "Ctrl+Z"
- }
- """;
- var runtimeSource = "runtime.json";
- var runtimeLocation = ConfigLocations.Runtime;
- var runtimeStream = new MemoryStream ();
- var writer = new StreamWriter (runtimeStream);
- writer.Write (runtimeJson);
- writer.Flush ();
- runtimeStream.Position = 0;
- // Act
- bool result = sourcesManager.Load (settingsScope, runtimeStream, runtimeSource, runtimeLocation);
- // Assert
- Assert.True (result);
- // Verify settingsScope is updated as expected
- Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue);
- }
- #endregion
- #region ToJson and ToStream
- [Fact]
- public void ToJson_WithValidScope_ReturnsJsonString ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- settingsScope.LoadHardCodedDefaults ();
- settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
- // Act
- string json = sourcesManager.ToJson (settingsScope);
- // Assert
- Assert.Contains ("""Application.QuitKey": "Ctrl+Q""", json);
- }
- [Fact]
- public void ToStream_WithValidScope_ReturnsStream ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- settingsScope.LoadHardCodedDefaults ();
- settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
- // Act
- var stream = sourcesManager.ToStream (settingsScope);
- // Assert
- Assert.NotNull (stream);
- stream.Position = 0;
- var reader = new StreamReader (stream);
- string json = reader.ReadToEnd ();
- Assert.Contains ("""Application.QuitKey": "Ctrl+Q""", json);
- }
- #endregion
- #region Sources Dictionary Tests
- [Fact]
- public void Sources_Dictionary_IsInitializedEmpty ()
- {
- // Arrange & Act
- var sourcesManager = new SourcesManager ();
- // Assert
- Assert.NotNull (sourcesManager.Sources);
- Assert.Empty (sourcesManager.Sources);
- }
- [Fact]
- public void Load_WhenCalledMultipleTimes_MaintainsLastSourceForLocation ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- // Act - Update with first source for location
- var firstSource = "first.json";
- sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+A"}""", firstSource, ConfigLocations.Runtime);
- // Update with second source for same location
- var secondSource = "second.json";
- sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+B"}""", secondSource, ConfigLocations.Runtime);
- // Assert - Only the last source should be stored for the location
- Assert.Single (sourcesManager.Sources);
- Assert.Equal (secondSource, sourcesManager.Sources [ConfigLocations.Runtime]);
- }
- [Fact]
- public void Load_WithDifferentLocations_AddsAllSourcesToCollection ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- ConfigLocations [] locations =
- [
- ConfigLocations.LibraryResources,
- ConfigLocations.Runtime,
- ConfigLocations.AppCurrent,
- ConfigLocations.GlobalHome
- ];
- // Act - Update with different sources for different locations
- foreach (var location in locations)
- {
- var source = $"config-{location}.json";
- sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", source, location);
- }
- // Assert
- Assert.Equal (locations.Length, sourcesManager.Sources.Count);
- foreach (var location in locations)
- {
- Assert.Contains (location, sourcesManager.Sources.Keys);
- Assert.Equal ($"config-{location}.json", sourcesManager.Sources [location]);
- }
- }
- [Fact]
- public void Load_AddsResourceSourceToCollection ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
- var resourceName = "Terminal.Gui.Resources.config.json";
- var location = ConfigLocations.LibraryResources;
- // Act
- bool result = sourcesManager.Load (settingsScope, assembly!, resourceName, location);
- // Assert
- Assert.True (result);
- Assert.Contains (location, sourcesManager.Sources.Keys);
- Assert.Equal ($"resource://[{assembly!.GetName ().Name}]/{resourceName}", sourcesManager.Sources [location]);
- }
- [Fact]
- public void Load_WithNonExistentFileAndDifferentLocations_TracksAllSources ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- // Define multiple files and locations
- var fileLocations = new Dictionary<string, ConfigLocations> (StringComparer.InvariantCultureIgnoreCase)
- {
- { "file1.json", ConfigLocations.AppCurrent },
- { "file2.json", ConfigLocations.GlobalHome },
- { "file3.json", ConfigLocations.AppHome }
- };
- // Act
- foreach (var pair in fileLocations)
- {
- sourcesManager.Load (settingsScope, pair.Key, pair.Value);
- }
- // Assert
- Assert.Equal (fileLocations.Count, sourcesManager.Sources.Count);
- foreach (var pair in fileLocations)
- {
- Assert.Contains (pair.Value, sourcesManager.Sources.Keys);
- Assert.Equal (pair.Key, sourcesManager.Sources [pair.Value]);
- }
- }
- [Fact]
- public void Sources_IsPreservedAcrossOperations ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- // First operation - file update
- var filePath = "testfile.json";
- var location1 = ConfigLocations.AppCurrent;
- sourcesManager.Load (settingsScope, filePath, location1);
- // Second operation - json string update
- var jsonSource = "jsonstring";
- var location2 = ConfigLocations.Runtime;
- sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", jsonSource, location2);
- // Perform a stream operation
- var streamSource = "streamdata";
- var location3 = ConfigLocations.GlobalCurrent;
- var stream = new MemoryStream ();
- var writer = new StreamWriter (stream);
- writer.Write ("""{"Application.QuitKey": "Ctrl+Z"}""");
- writer.Flush ();
- stream.Position = 0;
- sourcesManager.Load (settingsScope, stream, streamSource, location3);
- // Assert - all sources should be preserved
- Assert.Equal (3, sourcesManager.Sources.Count);
- Assert.Equal (filePath, sourcesManager.Sources [location1]);
- Assert.Equal (jsonSource, sourcesManager.Sources [location2]);
- Assert.Equal (streamSource, sourcesManager.Sources [location3]);
- }
- [Fact]
- public void Sources_StaysConsistentWhenUpdateFails ()
- {
- // Arrange
- var sourcesManager = new SourcesManager ();
- var settingsScope = new SettingsScope ();
- // Add one successful source
- var validSource = "valid.json";
- var validLocation = ConfigLocations.Runtime;
- sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", validSource, validLocation);
- try
- {
- // Configure to throw on errors
- ConfigurationManager.ThrowOnJsonErrors = true;
- // Act & Assert - attempt to update with invalid JSON
- var invalidSource = "invalid.json";
- var invalidLocation = ConfigLocations.AppCurrent;
- var invalidJson = "{ invalid json }";
- Assert.Throws<JsonException> (
- () =>
- sourcesManager.Load (settingsScope, invalidJson, invalidSource, invalidLocation));
- // The valid source should still be there
- Assert.Single (sourcesManager.Sources);
- Assert.Equal (validSource, sourcesManager.Sources [validLocation]);
- // The invalid source should not have been added
- Assert.DoesNotContain (invalidLocation, sourcesManager.Sources.Keys);
- }
- finally
- {
- // Reset for other tests
- ConfigurationManager.ThrowOnJsonErrors = false;
- }
- }
- #endregion
- }
|