From 93206a7edf69e4750be0cd1ff965db61a9864db8 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Thu, 7 Nov 2024 17:17:04 +0700 Subject: [PATCH] Refactored the SettingsProvider tests to work with .NET8 With .NET8, the LocalApplicationData property has a different value than with Framework, causing a test failure in this class. That test ended reading the temp file from a previous test. I refactored the file pathing and directory creation to be adapted from the SettingsProvider itself, which should make it more flexible. --- .../SettingsProviderTests.cs | 103 ++++++++++++------ 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/SIL.Windows.Forms.Tests/SettingsProviderTests.cs b/SIL.Windows.Forms.Tests/SettingsProviderTests.cs index 047b40096..74ac50003 100644 --- a/SIL.Windows.Forms.Tests/SettingsProviderTests.cs +++ b/SIL.Windows.Forms.Tests/SettingsProviderTests.cs @@ -90,16 +90,15 @@ public void SettingsFolderWithNewerConfig_SortsBeforeOneWithOlderConfig() [Test] public void CanOverrideDefaultLocation() { - string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests"); - Directory.CreateDirectory(settingsPath); - using (TemporaryFolder.TrackExisting(settingsPath)) + RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); + var settingsProvider = new TestCrossPlatformSettingsProvider(); + settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings + + string dirPath = settingsProvider.UserConfigLocation; + Directory.CreateDirectory(dirPath); + using (TemporaryFolder.TrackExisting(dirPath)) { - RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); - var settingsProvider = new TestCrossPlatformSettingsProvider(); - settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings - string dirPath = settingsProvider.UserConfigLocation; Assert.That(dirPath, Does.Contain("SettingsProviderTests")); - Directory.CreateDirectory(dirPath); string filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); using (new TempFile(filePath, true)) { @@ -160,15 +159,15 @@ public void CanSaveBothRegularAndRegistrationSettings() [Category("KnownMonoIssue")] public void Upgrade_SectionsRenamed_SettingsMigrated() { - string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests"); - Directory.CreateDirectory(settingsPath); - using (TemporaryFolder.TrackExisting(settingsPath)) + RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); + var settingsProvider = new TestCrossPlatformSettingsProvider(); + settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings + + string appSettingsRoot = Path.Combine(settingsProvider.UserConfigLocation, ".."); + string dirPath = Path.Combine(appSettingsRoot, "0.0.0.0"); + Directory.CreateDirectory(dirPath); + using (TemporaryFolder.TrackExisting(dirPath)) { - RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); - var settingsProvider = new TestCrossPlatformSettingsProvider(); - settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings - string dirPath = Path.Combine(settingsPath, "0.0.0.0"); - Directory.CreateDirectory(dirPath); string filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); using (new TempFile(filePath, true)) { @@ -180,6 +179,48 @@ public void Upgrade_SectionsRenamed_SettingsMigrated() someone@somewhere.org + + +"); + + Registration.Registration regSettings = Registration.Registration.Default; + regSettings.Upgrade(); + Assert.That(regSettings.Email, Is.EqualTo("someone@somewhere.org")); + } + } + } + + /// + /// This test is ignored on Mono, because of known issues in the ApplicationSettingsBase class (Xamarin-15818 + /// and Xamarin-2315). This issue is fixed in Mono-SIL. + /// + /// This test adds and changes values from the earlier tests. This is necessary to confirm + /// we're reading the temp file for this test, as opposed to a temp file a previous. + /// + [Test] + [Category("KnownMonoIssue")] + public void Upgrade_ExtraFields_SettingsMigrated() + { + RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); + var settingsProvider = new TestCrossPlatformSettingsProvider(); + settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings + + string appSettingsRoot = Path.Combine(settingsProvider.UserConfigLocation, ".."); + string dirPath = Path.Combine(appSettingsRoot, "0.0.0.0"); + Directory.CreateDirectory(dirPath); + using (TemporaryFolder.TrackExisting(dirPath)) + { + string filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); + using (new TempFile(filePath, true)) + { + File.WriteAllText(filePath, + @" + + + + + someone2@somewhere.org + 10 @@ -189,7 +230,7 @@ public void Upgrade_SectionsRenamed_SettingsMigrated() Registration.Registration regSettings = Registration.Registration.Default; regSettings.Upgrade(); - Assert.That(regSettings.Email, Is.EqualTo("someone@somewhere.org")); + Assert.That(regSettings.Email, Is.EqualTo("someone2@somewhere.org")); Assert.That(regSettings.LaunchCount, Is.EqualTo(10)); } } @@ -198,14 +239,14 @@ public void Upgrade_SectionsRenamed_SettingsMigrated() [Test, Ignore("Probably due to statics on CrossPlatformSettingsProvider, this test is corrupted by other tests. Works fine in isolation")] public void LoadSettings_FileCorrupt_ShowsErrorAndSelfHeals() { - var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests"); - Directory.CreateDirectory(settingsPath); - using (TemporaryFolder.TrackExisting(settingsPath)) + RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); + var settingsProvider = new TestCrossPlatformSettingsProvider(); + settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings + + string dirPath = settingsProvider.UserConfigLocation; + Directory.CreateDirectory(dirPath); + using (TemporaryFolder.TrackExisting(dirPath)) { - var settingsProvider = new TestCrossPlatformSettingsProvider(); - settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings - var dirPath = settingsProvider.UserConfigLocation; - Directory.CreateDirectory(dirPath); var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); File.Delete(filePath); using (new TempFile(filePath, true)) @@ -232,14 +273,14 @@ public void LoadSettings_FileCorrupt_ShowsErrorAndSelfHeals() [Test] public void CheckForErrorsInFile_FileCorrupt_ReturnsMessage() { - var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests"); - Directory.CreateDirectory(settingsPath); - using (TemporaryFolder.TrackExisting(settingsPath)) + RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); + var settingsProvider = new TestCrossPlatformSettingsProvider(); + settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings + + string dirPath = settingsProvider.UserConfigLocation; + Directory.CreateDirectory(dirPath); + using (TemporaryFolder.TrackExisting(dirPath)) { - var settingsProvider = new TestCrossPlatformSettingsProvider(); - settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings - var dirPath = settingsProvider.UserConfigLocation; - Directory.CreateDirectory(dirPath); var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); using (new TempFile(filePath, true)) {