-
Notifications
You must be signed in to change notification settings - Fork 23
task: Rewrite Settings loading and writing in to the DI Feature pattern #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
42f81be
first rewrite
DirkDoes a99abb0
split the Dolphin Installer. it still needs some changes. but it was …
DirkDoes 31a1826
add settings singal bus and move ofver to the IFileSystem service
DirkDoes 152e64f
small
DirkDoes efd1ee0
how to add new settings
DirkDoes 684eb73
more changes
DirkDoes c465f62
move settings files
DirkDoes 858f18d
guard clauses
DirkDoes 67e7280
add tests for the settings
DirkDoes 48dfde8
Update SettingsExtensions.cs
DirkDoes 6379e40
rename to types
DirkDoes 65e2dbc
Merge branch 'dev' into task/settings-rewrite-in-to-DI
DirkDoes 90d222b
fix spelling mistakes
DirkDoes fabc666
fix tests
DirkDoes 9a86aee
removed lock overkill
DirkDoes 77e4cc5
use filesystem + update comment
patchzyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
WheelWizard.Test/Features/LinuxDolphinInstallerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| using WheelWizard.DolphinInstaller; | ||
| using WheelWizard.Shared; | ||
|
|
||
| namespace WheelWizard.Test.Features; | ||
|
|
||
| public class LinuxDolphinInstallerTests | ||
| { | ||
| private readonly ILinuxCommandEnvironment _commandEnvironment; | ||
| private readonly ILinuxProcessService _processService; | ||
| private readonly LinuxDolphinInstaller _installer; | ||
|
|
||
| public LinuxDolphinInstallerTests() | ||
| { | ||
| _commandEnvironment = Substitute.For<ILinuxCommandEnvironment>(); | ||
| _processService = Substitute.For<ILinuxProcessService>(); | ||
| _installer = new LinuxDolphinInstaller(_commandEnvironment, _processService); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsDolphinInstalledInFlatpak_ReturnsTrue_WhenFlatpakInfoExitCodeIsZero() | ||
| { | ||
| _processService.Run("flatpak", "info org.DolphinEmu.dolphin-emu").Returns(Ok(0)); | ||
|
|
||
| var result = _installer.IsDolphinInstalledInFlatpak(); | ||
|
|
||
| Assert.True(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsDolphinInstalledInFlatpak_ReturnsFalse_WhenFlatpakInfoExitCodeIsNonZero() | ||
| { | ||
| _processService.Run("flatpak", "info org.DolphinEmu.dolphin-emu").Returns(Ok(1)); | ||
|
|
||
| var result = _installer.IsDolphinInstalledInFlatpak(); | ||
|
|
||
| Assert.False(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InstallFlatpak_ReturnsFailure_WhenPackageManagerCannotBeDetected() | ||
| { | ||
| _commandEnvironment.IsCommandAvailable("flatpak").Returns(false); | ||
| _commandEnvironment.DetectPackageManagerInstallCommand().Returns(string.Empty); | ||
|
|
||
| var result = await _installer.InstallFlatpak(); | ||
|
|
||
| Assert.True(result.IsFailure); | ||
| Assert.Contains("Unsupported Linux distribution", result.Error.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InstallFlatpak_ReturnsFailure_WhenPkexecIsUnauthorized() | ||
| { | ||
| _commandEnvironment.IsCommandAvailable("flatpak").Returns(false); | ||
| _commandEnvironment.DetectPackageManagerInstallCommand().Returns("apt-get install -y"); | ||
| _processService | ||
| .RunWithProgressAsync("pkexec", "apt-get install -y flatpak", Arg.Any<IProgress<int>?>()) | ||
| .Returns(Task.FromResult<OperationResult<int>>(Ok(126))); | ||
|
|
||
| var result = await _installer.InstallFlatpak(); | ||
|
|
||
| Assert.True(result.IsFailure); | ||
| Assert.Contains("administrator", result.Error.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InstallFlatpak_ReturnsSuccess_WhenInstallCompletesAndCommandBecomesAvailable() | ||
| { | ||
| _commandEnvironment.IsCommandAvailable("flatpak").Returns(false, true); | ||
| _commandEnvironment.DetectPackageManagerInstallCommand().Returns("apt-get install -y"); | ||
| _processService | ||
| .RunWithProgressAsync("pkexec", "apt-get install -y flatpak", Arg.Any<IProgress<int>?>()) | ||
| .Returns(Task.FromResult<OperationResult<int>>(Ok(0))); | ||
|
|
||
| var result = await _installer.InstallFlatpak(); | ||
|
|
||
| Assert.True(result.IsSuccess); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InstallFlatpakDolphin_ReturnsFailure_WhenDolphinInstallCommandFails() | ||
| { | ||
| _commandEnvironment.IsCommandAvailable("flatpak").Returns(true); | ||
| _processService | ||
| .RunWithProgressAsync("pkexec", "flatpak --system install -y org.DolphinEmu.dolphin-emu", Arg.Any<IProgress<int>?>()) | ||
| .Returns(Task.FromResult<OperationResult<int>>(Ok(1))); | ||
|
|
||
| var result = await _installer.InstallFlatpakDolphin(); | ||
|
|
||
| Assert.True(result.IsFailure); | ||
| Assert.Contains("exit code 1", result.Error.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InstallFlatpakDolphin_ReturnsFailure_WhenWarmupLaunchFails() | ||
| { | ||
| _commandEnvironment.IsCommandAvailable("flatpak").Returns(true); | ||
| _processService | ||
| .RunWithProgressAsync("pkexec", "flatpak --system install -y org.DolphinEmu.dolphin-emu", Arg.Any<IProgress<int>?>()) | ||
| .Returns(Task.FromResult<OperationResult<int>>(Ok(0))); | ||
| _processService | ||
| .LaunchAndStopAsync("flatpak", "run org.DolphinEmu.dolphin-emu", TimeSpan.FromSeconds(4)) | ||
| .Returns(Task.FromResult<OperationResult>(Fail("Launch failed"))); | ||
|
|
||
| var result = await _installer.InstallFlatpakDolphin(); | ||
|
|
||
| Assert.True(result.IsFailure); | ||
| Assert.Equal("Launch failed", result.Error.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InstallFlatpakDolphin_ReturnsSuccess_WhenInstallAndWarmupSucceed() | ||
| { | ||
| _commandEnvironment.IsCommandAvailable("flatpak").Returns(true); | ||
| _processService | ||
| .RunWithProgressAsync("pkexec", "flatpak --system install -y org.DolphinEmu.dolphin-emu", Arg.Any<IProgress<int>?>()) | ||
| .Returns(Task.FromResult<OperationResult<int>>(Ok(0))); | ||
| _processService | ||
| .LaunchAndStopAsync("flatpak", "run org.DolphinEmu.dolphin-emu", TimeSpan.FromSeconds(4)) | ||
| .Returns(Task.FromResult<OperationResult>(Ok())); | ||
|
|
||
| var result = await _installer.InstallFlatpakDolphin(); | ||
|
|
||
| Assert.True(result.IsSuccess); | ||
| } | ||
| } | ||
149 changes: 149 additions & 0 deletions
149
WheelWizard.Test/Features/Settings/DolphinSettingsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| using Testably.Abstractions.Testing; | ||
| using WheelWizard.Services; | ||
| using WheelWizard.Settings; | ||
| using WheelWizard.Settings.Types; | ||
|
|
||
| namespace WheelWizard.Test.Features.Settings; | ||
|
|
||
| [Collection("SettingsFeature")] | ||
| public class DolphinSettingTests | ||
| { | ||
| [Fact] | ||
| public void Constructor_Throws_WhenFileNameIsNotIni() | ||
| { | ||
| var action = () => new DolphinSetting(typeof(string), ("Dolphin.cfg", "General", "NANDRootPath"), "value"); | ||
|
|
||
| Assert.Throws<ArgumentException>(action); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetFromString_ParsesEnumAndFormatsAsIntegerString() | ||
| { | ||
| var setting = new DolphinSetting( | ||
| typeof(DolphinShaderCompilationMode), | ||
| ("GFX.ini", "Settings", "ShaderCompilationMode"), | ||
| DolphinShaderCompilationMode.Default | ||
| ); | ||
|
|
||
| var result = setting.SetFromString("2", skipSave: true); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Equal(DolphinShaderCompilationMode.HybridUberShaders, Assert.IsType<DolphinShaderCompilationMode>(setting.Get())); | ||
| Assert.Equal("2", setting.GetStringValue()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Set_ReturnsFalseAndKeepsOldValue_WhenValidationFails() | ||
| { | ||
| var setting = new DolphinSetting(typeof(int), ("GFX.ini", "Settings", "InternalResolution"), 1).SetValidation(value => | ||
| (int)value! >= 0 | ||
| ); | ||
| setting.Set(2); | ||
|
|
||
| var result = setting.Set(-1); | ||
|
|
||
| Assert.False(result); | ||
| Assert.Equal(2, Assert.IsType<int>(setting.Get())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetFromString_Throws_WhenTypeIsUnsupported() | ||
| { | ||
| var setting = new DolphinSetting(typeof(decimal), ("GFX.ini", "Settings", "Price"), 1m); | ||
|
|
||
| Assert.Throws<InvalidOperationException>(() => setting.SetFromString("3.14")); | ||
| } | ||
| } | ||
|
|
||
| [Collection("SettingsFeature")] | ||
| public class DolphinSettingManagerTests : IDisposable | ||
| { | ||
| [Fact] | ||
| public void LoadSettings_ReadsExistingValue_FromIniFile() | ||
| { | ||
| var fileSystem = new MockFileSystem(); | ||
| var userFolderPath = $"/wheelwizard-user-{Guid.NewGuid():N}"; | ||
| SettingsTestUtils.InitializeSettingsRuntime(userFolderPath); | ||
| var configFolderPath = PathManager.ConfigFolderPath; | ||
| var iniPath = Path.Combine(configFolderPath, "Dolphin.ini"); | ||
| fileSystem.Directory.CreateDirectory(configFolderPath); | ||
| fileSystem.File.WriteAllLines(iniPath, ["[General]", "NANDRootPath = /persisted"]); | ||
| var manager = new DolphinSettingManager(fileSystem); | ||
| var setting = new DolphinSetting(typeof(string), ("Dolphin.ini", "General", "NANDRootPath"), "/default"); | ||
|
|
||
| manager.RegisterSetting(setting); | ||
| manager.LoadSettings(); | ||
|
|
||
| Assert.Equal("/persisted", Assert.IsType<string>(setting.Get())); | ||
| } | ||
|
DirkDoes marked this conversation as resolved.
|
||
|
|
||
| [Fact] | ||
| public void LoadSettings_WritesDefaultValue_WhenIniEntryIsMissing() | ||
| { | ||
| var fileSystem = new MockFileSystem(); | ||
| var userFolderPath = $"/wheelwizard-user-{Guid.NewGuid():N}"; | ||
| SettingsTestUtils.InitializeSettingsRuntime(userFolderPath); | ||
| var configFolderPath = PathManager.ConfigFolderPath; | ||
| var iniPath = Path.Combine(configFolderPath, "Dolphin.ini"); | ||
| fileSystem.Directory.CreateDirectory(configFolderPath); | ||
| fileSystem.File.WriteAllLines(iniPath, ["[General]", "OtherSetting = 1"]); | ||
| var manager = new DolphinSettingManager(fileSystem); | ||
| var setting = new DolphinSetting(typeof(string), ("Dolphin.ini", "General", "NANDRootPath"), "/default"); | ||
|
|
||
| manager.RegisterSetting(setting); | ||
| manager.LoadSettings(); | ||
|
|
||
| var updatedFile = fileSystem.File.ReadAllText(iniPath); | ||
| Assert.Contains("NANDRootPath = /default", updatedFile); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SaveSettings_UpdatesExistingSettingLine_InIniFile() | ||
| { | ||
| var fileSystem = new MockFileSystem(); | ||
| var userFolderPath = $"/wheelwizard-user-{Guid.NewGuid():N}"; | ||
| SettingsTestUtils.InitializeSettingsRuntime(userFolderPath); | ||
| var configFolderPath = PathManager.ConfigFolderPath; | ||
| var iniPath = Path.Combine(configFolderPath, "Dolphin.ini"); | ||
| fileSystem.Directory.CreateDirectory(configFolderPath); | ||
| fileSystem.File.WriteAllLines(iniPath, ["[General]", "NANDRootPath = /old"]); | ||
| var manager = new DolphinSettingManager(fileSystem); | ||
| var setting = new DolphinSetting(typeof(string), ("Dolphin.ini", "General", "NANDRootPath"), "/default"); | ||
|
|
||
| manager.RegisterSetting(setting); | ||
| manager.LoadSettings(); | ||
| setting.Set("/new", skipSave: true); | ||
| manager.SaveSettings(setting); | ||
|
|
||
| var updatedFile = fileSystem.File.ReadAllText(iniPath); | ||
| Assert.Contains("NANDRootPath = /new", updatedFile); | ||
| Assert.DoesNotContain("NANDRootPath = /old", updatedFile); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReloadSettings_ReReadsFile_AfterItChangesOnDisk() | ||
| { | ||
| var fileSystem = new MockFileSystem(); | ||
| var userFolderPath = $"/wheelwizard-user-{Guid.NewGuid():N}"; | ||
| SettingsTestUtils.InitializeSettingsRuntime(userFolderPath); | ||
| var configFolderPath = PathManager.ConfigFolderPath; | ||
| var iniPath = Path.Combine(configFolderPath, "Dolphin.ini"); | ||
| fileSystem.Directory.CreateDirectory(configFolderPath); | ||
| fileSystem.File.WriteAllLines(iniPath, ["[General]", "NANDRootPath = /first"]); | ||
| var manager = new DolphinSettingManager(fileSystem); | ||
| var setting = new DolphinSetting(typeof(string), ("Dolphin.ini", "General", "NANDRootPath"), "/default"); | ||
|
|
||
| manager.RegisterSetting(setting); | ||
| manager.LoadSettings(); | ||
| fileSystem.File.WriteAllLines(iniPath, ["[General]", "NANDRootPath = /second"]); | ||
| manager.ReloadSettings(); | ||
|
|
||
| Assert.Equal("/second", Assert.IsType<string>(setting.Get())); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| SettingsTestUtils.ResetSettingsRuntime(); | ||
| SettingsTestUtils.ResetSignalRuntime(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.