From d2157a35a4f42fcc81eb6643eb6258b1ca69f34c Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Sun, 28 Feb 2021 18:39:51 -0800 Subject: [PATCH 1/4] Remove SettingsService --- src/NUnitConsole/ConsoleVersion.cs | 4 +- src/NUnitEngine/EngineApiVersion.cs | 2 +- src/NUnitEngine/EngineVersion.cs | 4 +- src/NUnitEngine/nunit.engine.api/ISettings.cs | 79 ----------- .../Api/ServiceLocatorTests.cs | 2 +- .../Internal/SettingsGroupTests.cs | 99 -------------- .../Internal/SettingsStoreTests.cs | 87 ------------ .../Services/Fakes/FakeSettingsService.cs | 36 ----- .../Services/ServiceManagerTests.cs | 62 ++++----- .../Services/SettingsServiceTests.cs | 29 ---- .../nunit.engine/Internal/SettingsGroup.cs | 123 ----------------- .../nunit.engine/Internal/SettingsStore.cs | 126 ------------------ .../nunit.engine/Services/SettingsService.cs | 50 ------- src/NUnitEngine/nunit.engine/TestEngine.cs | 1 - 14 files changed, 35 insertions(+), 669 deletions(-) delete mode 100644 src/NUnitEngine/nunit.engine.api/ISettings.cs delete mode 100644 src/NUnitEngine/nunit.engine.tests/Internal/SettingsGroupTests.cs delete mode 100644 src/NUnitEngine/nunit.engine.tests/Internal/SettingsStoreTests.cs delete mode 100644 src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeSettingsService.cs delete mode 100644 src/NUnitEngine/nunit.engine.tests/Services/SettingsServiceTests.cs delete mode 100644 src/NUnitEngine/nunit.engine/Internal/SettingsGroup.cs delete mode 100644 src/NUnitEngine/nunit.engine/Internal/SettingsStore.cs delete mode 100644 src/NUnitEngine/nunit.engine/Services/SettingsService.cs diff --git a/src/NUnitConsole/ConsoleVersion.cs b/src/NUnitConsole/ConsoleVersion.cs index 5ede437d2..fcbc8f899 100644 --- a/src/NUnitConsole/ConsoleVersion.cs +++ b/src/NUnitConsole/ConsoleVersion.cs @@ -3,5 +3,5 @@ using System.Reflection; [assembly: AssemblyProduct("NUnit Console Runner")] -[assembly: AssemblyVersion("3.13.0")] -[assembly: AssemblyInformationalVersion("3.13.0")] +[assembly: AssemblyVersion("4.0.0")] +[assembly: AssemblyInformationalVersion("4.0.0")] diff --git a/src/NUnitEngine/EngineApiVersion.cs b/src/NUnitEngine/EngineApiVersion.cs index d3d3968c2..864518bf9 100644 --- a/src/NUnitEngine/EngineApiVersion.cs +++ b/src/NUnitEngine/EngineApiVersion.cs @@ -4,5 +4,5 @@ [assembly: AssemblyProduct("NUnit Engine API")] [assembly: AssemblyVersion("3.0.0.0")] -[assembly: AssemblyInformationalVersion("3.13.0")] +[assembly: AssemblyInformationalVersion("4.0.0")] [assembly: AssemblyFileVersion("3.13.0")] diff --git a/src/NUnitEngine/EngineVersion.cs b/src/NUnitEngine/EngineVersion.cs index 6ad1134d7..a0a132804 100644 --- a/src/NUnitEngine/EngineVersion.cs +++ b/src/NUnitEngine/EngineVersion.cs @@ -3,5 +3,5 @@ using System.Reflection; [assembly: AssemblyProduct("NUnit Engine")] -[assembly: AssemblyVersion("3.13.0")] -[assembly: AssemblyInformationalVersion("3.13.0")] +[assembly: AssemblyVersion("4.0.0")] +[assembly: AssemblyInformationalVersion("4.0.0")] diff --git a/src/NUnitEngine/nunit.engine.api/ISettings.cs b/src/NUnitEngine/nunit.engine.api/ISettings.cs deleted file mode 100644 index 3d3e336e5..000000000 --- a/src/NUnitEngine/nunit.engine.api/ISettings.cs +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; - -namespace NUnit.Engine -{ - /// - /// Event handler for settings changes - /// - /// The sender. - /// The instance containing the event data. - public delegate void SettingsEventHandler(object sender, SettingsEventArgs args); - - /// - /// Event argument for settings changes - /// - public class SettingsEventArgs : EventArgs - { - /// - /// Initializes a new instance of the class. - /// - /// Name of the setting that has changed. - public SettingsEventArgs(string settingName) - { - SettingName = settingName; - } - - /// - /// Gets the name of the setting that has changed - /// - public string SettingName { get; private set; } - } - - /// - /// The ISettings interface is used to access all user - /// settings and options. - /// - public interface ISettings - { - /// - /// Occurs when the settings are changed. - /// - event SettingsEventHandler Changed; - - /// - /// Load a setting from the storage. - /// - /// Name of the setting to load - /// Value of the setting or null - object GetSetting(string settingName); - - /// - /// Load a setting from the storage or return a default value - /// - /// Name of the setting to load - /// Value to return if the setting is missing - /// Value of the setting or the default value - T GetSetting(string settingName, T defaultValue); - - /// - /// Remove a setting from the storage - /// - /// Name of the setting to remove - void RemoveSetting(string settingName); - - /// - /// Remove an entire group of settings from the storage - /// - /// Name of the group to remove - void RemoveGroup(string groupName); - - /// - /// Save a setting in the storage - /// - /// Name of the setting to save - /// Value to be saved - void SaveSetting(string settingName, object settingValue); - } -} diff --git a/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs b/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs index af2a4bbf3..5148214a9 100644 --- a/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs +++ b/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs @@ -18,7 +18,7 @@ public void CreateEngine() _testEngine.InternalTraceLevel = InternalTraceLevel.Off; } - [TestCase(typeof(ISettings))] + //[TestCase(typeof(ISettings))] [TestCase(typeof(IDriverService))] public void CanAccessService(Type serviceType) { diff --git a/src/NUnitEngine/nunit.engine.tests/Internal/SettingsGroupTests.cs b/src/NUnitEngine/nunit.engine.tests/Internal/SettingsGroupTests.cs deleted file mode 100644 index 435ccdb31..000000000 --- a/src/NUnitEngine/nunit.engine.tests/Internal/SettingsGroupTests.cs +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; -using System.ComponentModel; -using NUnit.Framework; -#if NETFRAMEWORK -using System.Drawing; -#endif - -namespace NUnit.Engine.Internal.Tests -{ - [TestFixture] - public class SettingsGroupTests - { - private SettingsGroup settings; - - [SetUp] - public void BeforeEachTest() - { - settings = new SettingsGroup(); - } - - [Test] - public void WhenSettingIsNotInitialized_NullIsReturned() - { - Assert.IsNull(settings.GetSetting("X")); - Assert.IsNull(settings.GetSetting("NAME")); - } - - [TestCase("X", 5)] - [TestCase("Y", 2.5)] - [TestCase("NAME", "Charlie")] - [TestCase("Flag", true)] - [TestCase("Priority", PriorityValue.A)] - public void WhenSettingIsInitialized_ValueIsReturned(string name, object expected) - { - settings.SaveSetting(name, expected); - object actual = settings.GetSetting(name); - Assert.That(actual, Is.EqualTo(expected)); - Assert.IsInstanceOf(expected.GetType(),actual); - } - - private enum PriorityValue - { - A, - B, - C - }; - - [Test] - public void WhenSettingIsRemoved_NullIsReturnedAndOtherSettingsAreNotAffected() - { - settings.SaveSetting("X", 5); - settings.SaveSetting("NAME", "Charlie"); - - settings.RemoveSetting("X"); - Assert.IsNull(settings.GetSetting("X"), "X not removed"); - Assert.That(settings.GetSetting("NAME"), Is.EqualTo("Charlie")); - - settings.RemoveSetting("NAME"); - Assert.IsNull(settings.GetSetting("NAME"), "NAME not removed"); - } - - [Test] - public void WhenSettingIsNotInitialized_DefaultValueIsReturned() - { - - Assert.That(settings.GetSetting( "X", 5 ), Is.EqualTo(5)); - Assert.That(settings.GetSetting( "X", 6 ), Is.EqualTo(6)); - Assert.That(settings.GetSetting( "X", "7" ), Is.EqualTo("7")); - - Assert.That(settings.GetSetting( "NAME", "Charlie" ), Is.EqualTo("Charlie")); - Assert.That(settings.GetSetting( "NAME", "Fred" ), Is.EqualTo("Fred")); - } - - [Test] - public void WhenSettingIsNotValid_DefaultSettingIsReturned() - { - settings.SaveSetting( "X", "1y25" ); - Assert.That(settings.GetSetting( "X", 42 ), Is.EqualTo(42)); - } - -#if NETFRAMEWORK - [Test] - [SetCulture("da-DK")] - public void SaveAndGetSettingShouldReturnTheOriginalValue() - { - var settingName = "MySetting"; - var settingValue = new Point(10, 20); - var typeConverter = TypeDescriptor.GetConverter(settingValue); - var settingsValue = typeConverter.ConvertToInvariantString(settingValue); - - settings.SaveSetting(settingName, settingsValue); - var point = settings.GetSetting(settingName, new Point(30, 40)); - Assert.That(point, Is.EqualTo(settingValue)); - } -#endif - } -} diff --git a/src/NUnitEngine/nunit.engine.tests/Internal/SettingsStoreTests.cs b/src/NUnitEngine/nunit.engine.tests/Internal/SettingsStoreTests.cs deleted file mode 100644 index 5e8dcee2c..000000000 --- a/src/NUnitEngine/nunit.engine.tests/Internal/SettingsStoreTests.cs +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; -using System.IO; -using System.Xml.Schema; -using NUnit.Framework; - -namespace NUnit.Engine.Internal.Tests -{ - [TestFixture] - public class SettingsStoreTests - { - private string _settingsFile; - private SettingsStore _settings; - - [SetUp] - public void SetUp() - { - _settingsFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); - _settings = new SettingsStore(_settingsFile, true); - } - - [TearDown] - public void TearDown() - { - if (File.Exists(_settingsFile)) - File.Delete(_settingsFile); - } - - [Test] - public void SaveSettingsThrowsExceptionOnErrorInSetting() - { - var wrongValue = -1; - XmlTypeCode settingValue = (XmlTypeCode)wrongValue; - _settings.SaveSetting("setting", settingValue); - Assert.Throws(() => _settings.SaveSettings()); - Assert.That(_settingsFile, Does.Not.Exist); - } - - [Test] - public void SaveSettingsCanBeSavedAndLoaded() - { - const string xmlTypeCodeSettingName = "setting"; - const string dateSettingName = "date"; - var xmlTypeCodeSettingValue = XmlTypeCode.ProcessingInstruction; - var dateSettingValue = new DateTime(2017, 05, 28); - - _settings.SaveSetting(xmlTypeCodeSettingName, xmlTypeCodeSettingValue); - _settings.SaveSetting(dateSettingName, dateSettingValue); - _settings.SaveSettings(); - _settings.LoadSettings(); - - var actualXmlTypeCodeValue = _settings.GetSetting(xmlTypeCodeSettingName, XmlTypeCode.Comment); - var actualDateValue = _settings.GetSetting(dateSettingName, DateTime.MinValue); - Assert.That(actualXmlTypeCodeValue, Is.EqualTo(xmlTypeCodeSettingValue)); - Assert.That(actualDateValue, Is.EqualTo(dateSettingValue)); - } - - [Test] - public void SaveSettingsDoesNotOverwriteExistingFileWhenFailing() - { - const string xmlTypeCodeSettingName = "setting"; - const string dateSettingName = "date"; - var xmlTypeCodeSettingValue = XmlTypeCode.ProcessingInstruction; - var dateSettingValue = new DateTime(2017, 03, 19); - - // Save initial version of the settings file - _settings.SaveSetting(xmlTypeCodeSettingName, xmlTypeCodeSettingValue); - _settings.SaveSetting(dateSettingName, dateSettingValue); - _settings.SaveSettings(); - - // Try to save setting that fails - _settings.LoadSettings(); - var wrongValue = -1; - var settingValue = (XmlTypeCode)wrongValue; - _settings.SaveSetting("setting", settingValue); - Assert.Throws(() => _settings.SaveSettings()); - - // Assert that the initial version is not overwritten - _settings.LoadSettings(); - var actualXmlTypeCodeValue = _settings.GetSetting(xmlTypeCodeSettingName, XmlTypeCode.Comment); - var actualDateValue = _settings.GetSetting(dateSettingName, DateTime.MinValue); - Assert.That(actualXmlTypeCodeValue, Is.EqualTo(xmlTypeCodeSettingValue)); - Assert.That(actualDateValue, Is.EqualTo(dateSettingValue)); - } - } -} diff --git a/src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeSettingsService.cs b/src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeSettingsService.cs deleted file mode 100644 index e9f8fbd2b..000000000 --- a/src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeSettingsService.cs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; -using NUnit.Engine.Internal; - -namespace NUnit.Engine.Services.Tests.Fakes -{ - public class FakeSettingsService : SettingsStore, IService - { - IServiceLocator IService.ServiceContext { get; set; } - - private ServiceStatus _status; - ServiceStatus IService.Status - { - get { return _status; } - } - - void IService.StartService() - { - _status = FailToStart - ? ServiceStatus.Error - : ServiceStatus.Started; - } - - void IService.StopService() - { - _status = ServiceStatus.Stopped; - if (FailedToStop) - throw new ArgumentException(nameof(FailedToStop)); - } - - public bool FailToStart { get; set; } - - public bool FailedToStop { get; set; } - } -} diff --git a/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs b/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs index 0fbea9f9e..4873cb751 100644 --- a/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs +++ b/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs @@ -9,7 +9,6 @@ namespace NUnit.Engine.Services.Tests public class ServiceManagerTests { - private IService _settingsService; private ServiceManager _serviceManager; private IService _projectService; @@ -19,9 +18,6 @@ public void SetUp() { _serviceManager = new ServiceManager(); - _settingsService = new FakeSettingsService(); - _serviceManager.AddService(_settingsService); - _projectService = new Fakes.FakeProjectService(); _serviceManager.AddService(_projectService); } @@ -31,41 +27,41 @@ public void InitializeServices() { _serviceManager.StartServices(); - IService service = _serviceManager.GetService(typeof(ISettings)); - Assert.That(service.Status, Is.EqualTo(ServiceStatus.Started)); - service = _serviceManager.GetService(typeof(IProjectService)); + //IService service = _serviceManager.GetService(typeof(ISettings)); + //Assert.That(service.Status, Is.EqualTo(ServiceStatus.Started)); + IService service = _serviceManager.GetService(typeof(IProjectService)); Assert.That(service.Status, Is.EqualTo(ServiceStatus.Started)); } - [Test] - public void InitializationFailure() - { - ((FakeSettingsService)_settingsService).FailToStart = true; - Assert.That(() => _serviceManager.StartServices(), - Throws.InstanceOf().And.Message.Contains("FakeSettingsService")); - } + //[Test] + //public void InitializationFailure() + //{ + // ((FakeSettingsService)_settingsService).FailToStart = true; + // Assert.That(() => _serviceManager.StartServices(), + // Throws.InstanceOf().And.Message.Contains("FakeSettingsService")); + //} - [Test] - public void TerminationFailure() - { - ((FakeSettingsService)_settingsService).FailedToStop = true; - _settingsService.StartService(); + //[Test] + //public void TerminationFailure() + //{ + // ((FakeSettingsService)_settingsService).FailedToStop = true; + // _settingsService.StartService(); - Assert.DoesNotThrow(() => _serviceManager.StopServices()); - } + // Assert.DoesNotThrow(() => _serviceManager.StopServices()); + //} - [Test] - public void AccessServiceByClass() - { - IService service = _serviceManager.GetService(typeof(FakeSettingsService)); - Assert.That(service, Is.SameAs(_settingsService)); - } + //[Test] + //public void AccessServiceByClass() + //{ + // IService service = _serviceManager.GetService(typeof(FakeSettingsService)); + // Assert.That(service, Is.SameAs(_settingsService)); + //} - [Test] - public void AccessServiceByInterface() - { - IService service = _serviceManager.GetService(typeof(ISettings)); - Assert.That(service, Is.SameAs(_settingsService)); - } + //[Test] + //public void AccessServiceByInterface() + //{ + // IService service = _serviceManager.GetService(typeof(ISettings)); + // Assert.That(service, Is.SameAs(_settingsService)); + //} } } diff --git a/src/NUnitEngine/nunit.engine.tests/Services/SettingsServiceTests.cs b/src/NUnitEngine/nunit.engine.tests/Services/SettingsServiceTests.cs deleted file mode 100644 index 5b16b5826..000000000 --- a/src/NUnitEngine/nunit.engine.tests/Services/SettingsServiceTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; -using System.Collections.Generic; -using System.Text; -using NUnit.Framework; - -namespace NUnit.Engine.Services.Tests -{ - public class SettingsServiceTests - { - private SettingsService _settingsService; - - [SetUp] - public void CreateServiceContext() - { - var services = new ServiceContext(); - _settingsService = new SettingsService(false); - services.Add(_settingsService); - services.ServiceManager.StartServices(); - } - - [Test] - public void ServiceIsStarted() - { - Assert.That(_settingsService.Status, Is.EqualTo(ServiceStatus.Started)); - } - } -} diff --git a/src/NUnitEngine/nunit.engine/Internal/SettingsGroup.cs b/src/NUnitEngine/nunit.engine/Internal/SettingsGroup.cs deleted file mode 100644 index 1c28bda24..000000000 --- a/src/NUnitEngine/nunit.engine/Internal/SettingsGroup.cs +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; - -namespace NUnit.Engine.Internal -{ - /// - /// SettingsGroup is the base class representing a group - /// of user or system settings. - /// - public class SettingsGroup : ISettings - { - static Logger log = InternalTrace.GetLogger("SettingsGroup"); - - protected Dictionary _settings = new Dictionary(); - - public event SettingsEventHandler Changed; - - /// - /// Load the value of one of the group's settings - /// - /// The name of setting to load - /// The value of the setting - public object GetSetting(string settingName) - { - return _settings.ContainsKey(settingName) - ? _settings[settingName] - : null; - } - - /// - /// Load the value of one of the group's settings or return a default value - /// - /// The name of setting to load - /// The value to return if the setting is not present - /// The value of the setting or the default - public T GetSetting(string settingName, T defaultValue) - { - object result = GetSetting(settingName); - - if (result == null) - return defaultValue; - - if (result is T) - return (T)result; - - try - { - TypeConverter converter = TypeDescriptor.GetConverter(typeof(T)); - if (converter == null) - return defaultValue; - - return (T)converter.ConvertFrom(null, CultureInfo.InvariantCulture, result); - } - catch(Exception ex) - { - log.Error("Unable to convert setting {0} to {1}", settingName, typeof(T).Name); - log.Error(ex.Message); - return defaultValue; - } - } - - /// - /// Remove a setting from the group - /// - /// The name of the setting to remove - public void RemoveSetting(string settingName) - { - _settings.Remove(settingName); - - if (Changed != null) - Changed(this, new SettingsEventArgs(settingName)); - } - - /// - /// Remove a group of settings - /// - /// The name of the group to remove - public void RemoveGroup(string groupName) - { - List keysToRemove = new List(); - - string prefix = groupName; - if (!prefix.EndsWith(".")) - prefix = prefix + "."; - - foreach (string key in _settings.Keys) - if (key.StartsWith(prefix)) - keysToRemove.Add(key); - - foreach (string key in keysToRemove) - _settings.Remove(key); - } - - /// - /// Save the value of one of the group's settings - /// - /// The name of the setting to save - /// The value to be saved - public void SaveSetting(string settingName, object settingValue) - { - object oldValue = GetSetting(settingName); - - // Avoid signaling "changes" when there is not really a change - if (oldValue != null) - { - if (oldValue is string && settingValue is string && (string)oldValue == (string)settingValue || - oldValue is int && settingValue is int && (int)oldValue == (int)settingValue || - oldValue is bool && settingValue is bool && (bool)oldValue == (bool)settingValue || - oldValue is Enum && settingValue is Enum && oldValue.Equals(settingValue)) - return; - } - - _settings[settingName] = settingValue; - - if (Changed != null) - Changed(this, new SettingsEventArgs(settingName)); - } - } -} \ No newline at end of file diff --git a/src/NUnitEngine/nunit.engine/Internal/SettingsStore.cs b/src/NUnitEngine/nunit.engine/Internal/SettingsStore.cs deleted file mode 100644 index 6d0213b17..000000000 --- a/src/NUnitEngine/nunit.engine/Internal/SettingsStore.cs +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.IO; -using System.Text; -using System.Xml; - -namespace NUnit.Engine.Internal -{ - /// - /// SettingsStore extends SettingsGroup to provide for - /// loading and saving the settings from an XML file. - /// - public class SettingsStore : SettingsGroup - { - private string _settingsFile; - private bool _writeable; - - /// - /// Construct a SettingsStore without a backing file - used for testing. - /// - public SettingsStore() { } - - /// - /// Construct a SettingsStore with a file name and indicate whether it is writeable - /// - /// - /// - public SettingsStore(string settingsFile, bool writeable) - { - _settingsFile = Path.GetFullPath(settingsFile); - _writeable = writeable; - } - - public void LoadSettings() - { - FileInfo info = new FileInfo(_settingsFile); - if (!info.Exists || info.Length == 0) - return; - - try - { - XmlDocument doc = new XmlDocument(); - using (var stream = new FileStream(_settingsFile, FileMode.Open, FileAccess.Read)) - { - doc.Load(stream); - } - - foreach (XmlElement element in doc.DocumentElement["Settings"].ChildNodes) - { - if (element.Name != "Setting") - throw new Exception("Unknown element in settings file: " + element.Name); - - if (!element.HasAttribute("name")) - throw new Exception("Setting must have 'name' attribute"); - - if (!element.HasAttribute("value")) - throw new Exception("Setting must have 'value' attribute"); - - SaveSetting(element.GetAttribute("name"), element.GetAttribute("value")); - } - } - catch (Exception ex) - { - string msg = string.Format("Error loading settings {0}. {1}", _settingsFile, ex.Message); - throw new NUnitEngineException(msg, ex); - } - } - - public void SaveSettings() - { - if (!_writeable || _settings.Keys.Count <= 0) - return; - - try - { - string dirPath = Path.GetDirectoryName(_settingsFile); - if (!Directory.Exists(dirPath)) - Directory.CreateDirectory(dirPath); - - var stream = new MemoryStream(); - using (var writer = new XmlTextWriter(stream, Encoding.UTF8)) - { - writer.Formatting = Formatting.Indented; - - writer.WriteProcessingInstruction("xml", "version=\"1.0\""); - writer.WriteStartElement("NUnitSettings"); - writer.WriteStartElement("Settings"); - - List keys = new List(_settings.Keys); - keys.Sort(); - - foreach (string name in keys) - { - object val = GetSetting(name); - if (val != null) - { - writer.WriteStartElement("Setting"); - writer.WriteAttributeString("name", name); - writer.WriteAttributeString("value", - TypeDescriptor.GetConverter(val).ConvertToInvariantString(val)); - writer.WriteEndElement(); - } - } - - writer.WriteEndElement(); - writer.WriteEndElement(); - writer.Flush(); - - var reader = new StreamReader(stream, Encoding.UTF8, true); - stream.Seek(0, SeekOrigin.Begin); - var contents = reader.ReadToEnd(); - File.WriteAllText(_settingsFile, contents, Encoding.UTF8); - } - } - catch (Exception) - { - // So we won't try this again - _writeable = false; - throw; - } - } - } -} diff --git a/src/NUnitEngine/nunit.engine/Services/SettingsService.cs b/src/NUnitEngine/nunit.engine/Services/SettingsService.cs deleted file mode 100644 index 4c20e751e..000000000 --- a/src/NUnitEngine/nunit.engine/Services/SettingsService.cs +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - -using System; -using System.IO; -using NUnit.Engine.Internal; - -namespace NUnit.Engine.Services -{ - /// - /// Summary description for UserSettingsService. - /// - public class SettingsService : SettingsStore, IService - { - private const string SETTINGS_FILE = "Nunit30Settings.xml"; - - public SettingsService(bool writeable) - : base(Path.Combine(NUnitConfiguration.ApplicationDirectory, SETTINGS_FILE), writeable) { } - - public IServiceLocator ServiceContext { get; set; } - - public ServiceStatus Status { get; private set; } - - public void StartService() - { - try - { - LoadSettings(); - - Status = ServiceStatus.Started; - } - catch - { - Status = ServiceStatus.Error; - throw; - } - } - - public void StopService() - { - try - { - SaveSettings(); - } - finally - { - Status = ServiceStatus.Stopped; - } - } - } -} diff --git a/src/NUnitEngine/nunit.engine/TestEngine.cs b/src/NUnitEngine/nunit.engine/TestEngine.cs index 5a3cf8252..421335c74 100644 --- a/src/NUnitEngine/nunit.engine/TestEngine.cs +++ b/src/NUnitEngine/nunit.engine/TestEngine.cs @@ -57,7 +57,6 @@ public void Initialize() // Services that depend on other services must be added after their dependencies // For example, ResultService uses ExtensionService, so ExtensionService is added // later. - Services.Add(new SettingsService(true)); Services.Add(new TestFilterService()); Services.Add(new ExtensionService()); Services.Add(new ProjectService()); From c3ccd755359eab6d36ec0452c4c0a9d771d4a8c8 Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Mon, 1 Mar 2021 01:43:39 -0800 Subject: [PATCH 2/4] Update service-related tests --- .../Api/ServiceLocatorTests.cs | 13 +++- .../Services/Fakes/FakeService.cs | 8 ++- .../Services/ServiceManagerTests.cs | 68 ++++++++++--------- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs b/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs index 5148214a9..2f7519e3b 100644 --- a/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs +++ b/src/NUnitEngine/nunit.engine.tests/Api/ServiceLocatorTests.cs @@ -1,8 +1,6 @@ // Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt using System; -using System.Collections.Generic; -using System.Text; using NUnit.Framework; namespace NUnit.Engine.Api.Tests @@ -18,8 +16,17 @@ public void CreateEngine() _testEngine.InternalTraceLevel = InternalTraceLevel.Off; } - //[TestCase(typeof(ISettings))] + [TestCase(typeof(ITestFilterService))] + [TestCase(typeof(IExtensionService))] + [TestCase(typeof(Services.ProjectService))] +#if NETFRAMEWORK + [TestCase(typeof(Services.DomainManager))] + [TestCase(typeof(IRuntimeFrameworkService))] + [TestCase(typeof(ITestAgency))] +#endif [TestCase(typeof(IDriverService))] + [TestCase(typeof(IResultService))] + [TestCase(typeof(ITestRunnerFactory))] public void CanAccessService(Type serviceType) { IService service = _testEngine.Services.GetService(serviceType) as IService; diff --git a/src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeService.cs b/src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeService.cs index e942c7ede..96bde5d54 100644 --- a/src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeService.cs +++ b/src/NUnitEngine/nunit.engine.tests/Services/Fakes/FakeService.cs @@ -21,11 +21,17 @@ void IService.StartService() void IService.StopService() { - _status = ServiceStatus.Stopped; + _status = FailToStop + ? ServiceStatus.Error + : ServiceStatus.Stopped; } // Set to true to cause the service to give // an error result when started public bool FailToStart { get; set; } + + // Set to true to cause the service to give + // an error result when stopped + public bool FailToStop { get; set; } } } diff --git a/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs b/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs index 4873cb751..eae61e0e0 100644 --- a/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs +++ b/src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs @@ -11,15 +11,18 @@ public class ServiceManagerTests { private ServiceManager _serviceManager; - private IService _projectService; + private IService1 _service1; + private IService2 _service2; [SetUp] public void SetUp() { _serviceManager = new ServiceManager(); - _projectService = new Fakes.FakeProjectService(); - _serviceManager.AddService(_projectService); + _service1 = new FakeService1(); + _serviceManager.AddService(_service1); + _service2 = new FakeService2(); + _serviceManager.AddService(_service2); } [Test] @@ -27,41 +30,42 @@ public void InitializeServices() { _serviceManager.StartServices(); - //IService service = _serviceManager.GetService(typeof(ISettings)); - //Assert.That(service.Status, Is.EqualTo(ServiceStatus.Started)); - IService service = _serviceManager.GetService(typeof(IProjectService)); + IService service = _serviceManager.GetService(typeof(IService1)); Assert.That(service.Status, Is.EqualTo(ServiceStatus.Started)); + Assert.That(service, Is.SameAs(_service1)); + service = _serviceManager.GetService(typeof(IService2)); + Assert.That(service.Status, Is.EqualTo(ServiceStatus.Started)); + Assert.That(service, Is.SameAs(_service2)); } - //[Test] - //public void InitializationFailure() - //{ - // ((FakeSettingsService)_settingsService).FailToStart = true; - // Assert.That(() => _serviceManager.StartServices(), - // Throws.InstanceOf().And.Message.Contains("FakeSettingsService")); - //} + [Test] + public void InitializationFailure() + { + ((FakeService)_service1).FailToStart = true; + Assert.That(() => _serviceManager.StartServices(), + Throws.InstanceOf().And.Message.Contains("FakeService1")); + } - //[Test] - //public void TerminationFailure() - //{ - // ((FakeSettingsService)_settingsService).FailedToStop = true; - // _settingsService.StartService(); + [Test] + public void TerminationFailure() + { + ((FakeService)_service1).FailToStop = true; + _service1.StartService(); - // Assert.DoesNotThrow(() => _serviceManager.StopServices()); - //} + Assert.DoesNotThrow(() => _serviceManager.StopServices()); + } + + [Test] + public void AccessServiceByClass() + { + IService service = _serviceManager.GetService(typeof(FakeService2)); + Assert.That(service, Is.SameAs(_service2)); + } - //[Test] - //public void AccessServiceByClass() - //{ - // IService service = _serviceManager.GetService(typeof(FakeSettingsService)); - // Assert.That(service, Is.SameAs(_settingsService)); - //} + public interface IService1 : IService { } + public interface IService2 : IService { } - //[Test] - //public void AccessServiceByInterface() - //{ - // IService service = _serviceManager.GetService(typeof(ISettings)); - // Assert.That(service, Is.SameAs(_settingsService)); - //} + public class FakeService1 : FakeService, IService1 { } + public class FakeService2 : FakeService, IService2 { } } } From 104dbcae5d97f9ffcb96adcbdc0ef2bad6e6e831 Mon Sep 17 00:00:00 2001 From: CharliePoole Date: Mon, 1 Mar 2021 12:38:08 -0800 Subject: [PATCH 3/4] Update EngineApiVersion.cs --- src/NUnitEngine/EngineApiVersion.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NUnitEngine/EngineApiVersion.cs b/src/NUnitEngine/EngineApiVersion.cs index 864518bf9..703b71d67 100644 --- a/src/NUnitEngine/EngineApiVersion.cs +++ b/src/NUnitEngine/EngineApiVersion.cs @@ -5,4 +5,4 @@ [assembly: AssemblyProduct("NUnit Engine API")] [assembly: AssemblyVersion("3.0.0.0")] [assembly: AssemblyInformationalVersion("4.0.0")] -[assembly: AssemblyFileVersion("3.13.0")] +[assembly: AssemblyFileVersion("4.0.0")] From 8f618c5c757323649c077eb388789ef810ef0dac Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Mon, 1 Mar 2021 13:31:44 -0800 Subject: [PATCH 4/4] Remove property no longer in use --- .../Internal/NUnitConfiguration.cs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/NUnitEngine/nunit.engine.core/Internal/NUnitConfiguration.cs b/src/NUnitEngine/nunit.engine.core/Internal/NUnitConfiguration.cs index 68e7b8f1a..3c5473fd4 100644 --- a/src/NUnitEngine/nunit.engine.core/Internal/NUnitConfiguration.cs +++ b/src/NUnitEngine/nunit.engine.core/Internal/NUnitConfiguration.cs @@ -25,21 +25,5 @@ public static string EngineDirectory return _engineDirectory; } } - - private static string _applicationDirectory; - public static string ApplicationDirectory - { - get - { - if (_applicationDirectory == null) - { - _applicationDirectory = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - "NUnit"); - } - - return _applicationDirectory; - } - } } }