Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

feat: upgrade xUnit test to latest and add some test cases

fatDuckf1a8fa757b14
diff --git a/optimizerDuck.Test/Services/Managers/RevertManagerTests.cs b/optimizerDuck.Test/Services/Managers/RevertManagerTests.cs
index 2cc24d5..5d07170 100644
--- a/optimizerDuck.Test/Services/Managers/RevertManagerTests.cs
+++ b/optimizerDuck.Test/Services/Managers/RevertManagerTests.cs
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
-using optimizerDuck.Shared.Helpers;
+using optimizerDuck.Common.Helpers;
using optimizerDuck.Domain.Abstractions;
using optimizerDuck.Domain.Optimizations.Models;
using optimizerDuck.Domain.Revert;
@@ -29,6 +29,7 @@ public class RevertManagerTests
{
var id = Guid.NewGuid();
var path = Path.Combine(Shared.RevertDirectory, id + ".json");
+ var cancellationToken = TestContext.Current.CancellationToken;
Directory.CreateDirectory(Shared.RevertDirectory);
var payload = new RevertData
@@ -42,7 +43,7 @@ public class RevertManagerTests
try
{
var json = JsonConvert.SerializeObject(payload, Formatting.Indented);
- await File.WriteAllTextAsync(path, json);
+ await File.WriteAllTextAsync(path, json, cancellationToken);
var data = await RevertManager.GetRevertDataAsync(id);
@@ -61,11 +62,12 @@ public class RevertManagerTests
{
var id = Guid.NewGuid();
var path = Path.Combine(Shared.RevertDirectory, id + ".json");
+ var cancellationToken = TestContext.Current.CancellationToken;
Directory.CreateDirectory(Shared.RevertDirectory);
try
{
- await File.WriteAllTextAsync(path, "{ invalid json }");
+ await File.WriteAllTextAsync(path, "{ invalid json }", cancellationToken);
var manager = new RevertManager(NullLogger<RevertManager>.Instance, NullLoggerFactory.Instance);
var op = new MockOptimization(id);
@@ -85,6 +87,7 @@ public class RevertManagerTests
{
var id = Guid.NewGuid();
var path = Path.Combine(Shared.RevertDirectory, id + ".json");
+ var cancellationToken = TestContext.Current.CancellationToken;
Directory.CreateDirectory(Shared.RevertDirectory);
var payload = new RevertData
@@ -118,7 +121,7 @@ public class RevertManagerTests
try
{
var json = JsonConvert.SerializeObject(payload, Formatting.Indented);
- await File.WriteAllTextAsync(path, json);
+ await File.WriteAllTextAsync(path, json, cancellationToken);
var manager = new RevertManager(NullLogger<RevertManager>.Instance, NullLoggerFactory.Instance);
var result = await manager.RevertAsync(new MockOptimization(id));
@@ -147,4 +150,4 @@ public class MockOptimization(Guid id) : IOptimization
{
return Task.FromResult(ApplyResult.True());
}
-}
\ No newline at end of file
+}
diff --git a/optimizerDuck.Test/Services/OptimizationServiceTests.cs b/optimizerDuck.Test/Services/OptimizationServiceTests.cs
index cb6442e..85b744d 100644
--- a/optimizerDuck.Test/Services/OptimizationServiceTests.cs
+++ b/optimizerDuck.Test/Services/OptimizationServiceTests.cs
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
-using optimizerDuck.Shared.Helpers;
+using optimizerDuck.Common.Helpers;
using optimizerDuck.Domain.Abstractions;
using optimizerDuck.Domain.Execution;
using optimizerDuck.Domain.Optimizations.Models;
diff --git a/optimizerDuck.Test/Services/OptimizationServices/RegistryServiceTests.cs b/optimizerDuck.Test/Services/OptimizationServices/RegistryServiceTests.cs
index 4555753..ff8352d 100644
--- a/optimizerDuck.Test/Services/OptimizationServices/RegistryServiceTests.cs
+++ b/optimizerDuck.Test/Services/OptimizationServices/RegistryServiceTests.cs
@@ -97,6 +97,40 @@ public class RegistryServiceTests : IDisposable
Assert.Equal("Hello", val);
}
+ [Fact]
+ public async Task DeleteDefaultValue_RestorePreviousValue_OnRevert()
+ {
+ var key = $@"{BaseTestKey}\DeleteDefaultValueTest";
+
+ RegistryService.Write(new RegistryItem(key, null, "DefaultHello"));
+ RegistryService.DeleteValue(new RegistryItem(key, null));
+
+ var step = _scope.ExecutedSteps.Last();
+
+ await step.RevertStep!.ExecuteAsync();
+
+ var value = RegistryService.Read<string>(new RegistryItem(key, null));
+ Assert.Equal("DefaultHello", value);
+ }
+
+ [Fact]
+ public async Task WriteDefaultValue_OverwriteValue_RevertRestoresPreviousValue()
+ {
+ var key = $@"{BaseTestKey}\WriteDefaultValueTest";
+
+ Assert.True(RegistryService.Write(new RegistryItem(key, null, "OriginalDefault")));
+ Assert.True(RegistryService.Write(new RegistryItem(key, null, "UpdatedDefault")));
+
+ var value = RegistryService.Read<string>(new RegistryItem(key, null));
+ Assert.Equal("UpdatedDefault", value);
+
+ var step = _scope.ExecutedSteps.Last();
+ Assert.True(await step.RevertStep!.ExecuteAsync());
+
+ var restoredValue = RegistryService.Read<string>(new RegistryItem(key, null));
+ Assert.Equal("OriginalDefault", restoredValue);
+ }
+
[Fact]
public async Task DeleteSubKeyTree_CreatesRecursiveBackupAndRestoresCorrectly()
{
@@ -168,4 +202,4 @@ public class RegistryServiceTests : IDisposable
Assert.False(RegistryService.KeyExists(new RegistryItem(keyPathB)));
Assert.False(RegistryService.KeyExists(new RegistryItem(keyPathA)));
}
-}
\ No newline at end of file
+}
diff --git a/optimizerDuck.Test/Services/SystemInfoServiceTests.cs b/optimizerDuck.Test/Services/SystemInfoServiceTests.cs
index 48663d0..c2a0716 100644
--- a/optimizerDuck.Test/Services/SystemInfoServiceTests.cs
+++ b/optimizerDuck.Test/Services/SystemInfoServiceTests.cs
@@ -9,8 +9,9 @@ public class SystemInfoServiceTests
public async Task RefreshAsync_SetsSnapshot()
{
var service = new SystemInfoService(NullLogger<SystemInfoService>.Instance);
+ var cancellationToken = TestContext.Current.CancellationToken;
- var snapshot = await service.RefreshAsync();
+ var snapshot = await service.RefreshAsync(cancellationToken);
Assert.Equal(snapshot, service.Snapshot);
}
@@ -22,4 +23,4 @@ public class SystemInfoServiceTests
service.LogSummary();
}
-}
\ No newline at end of file
+}
diff --git a/optimizerDuck.Test/optimizerDuck.Test.csproj b/optimizerDuck.Test/optimizerDuck.Test.csproj
index a78e306..38de7b5 100644
--- a/optimizerDuck.Test/optimizerDuck.Test.csproj
+++ b/optimizerDuck.Test/optimizerDuck.Test.csproj
@@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
+ <OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
@@ -12,10 +13,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
- <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.4"/>
+ <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.5"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4"/>
- <PackageReference Include="xunit" Version="2.9.3"/>
+ <PackageReference Include="xunit.v3" Version="3.2.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
diff --git a/optimizerDuck/Domain/Features/Categories/Preferences.cs b/optimizerDuck/Domain/Features/Categories/Preferences.cs
index 38c350c..3089211 100644
--- a/optimizerDuck/Domain/Features/Categories/Preferences.cs
+++ b/optimizerDuck/Domain/Features/Categories/Preferences.cs
@@ -388,7 +388,7 @@ public class Preferences : IFeatureCategory
public override async Task EnableAsync()
{
- RegistryService.Write(new RegistryItem(InprocPath, null!, ""));
+ RegistryService.Write(new RegistryItem(InprocPath, null, ""));
if (NeedsPostAction)
await ExecutePostActionAsync();
diff --git a/optimizerDuck/Domain/Optimizations/Models/Services/RegistryItem.cs b/optimizerDuck/Domain/Optimizations/Models/Services/RegistryItem.cs
index ba2be5c..ae999c3 100644
--- a/optimizerDuck/Domain/Optimizations/Models/Services/RegistryItem.cs
+++ b/optimizerDuck/Domain/Optimizations/Models/Services/RegistryItem.cs
@@ -34,7 +34,7 @@ public record struct RegistryItem
/// <param name="name">The value name.</param>
/// <param name="value">The value data.</param>
/// <param name="kind">The value kind.</param>
- public RegistryItem(string path, string name, object value, RegistryValueKind kind)
+ public RegistryItem(string path, string? name, object? value, RegistryValueKind kind)
{
Path = path;
Name = name;
@@ -48,7 +48,7 @@ public record struct RegistryItem
/// <param name="path">The registry key path.</param>
/// <param name="name">The value name.</param>
/// <param name="value">The value data (kind is auto-detected from the type).</param>
- public RegistryItem(string path, string name, object value)
+ public RegistryItem(string path, string? name, object? value)
{
Path = path;
Name = name;
@@ -61,7 +61,7 @@ public record struct RegistryItem
/// </summary>
/// <param name="path">The registry key path.</param>
/// <param name="name">The value name to target.</param>
- public RegistryItem(string path, string name)
+ public RegistryItem(string path, string? name)
{
Path = path;
Name = name;
@@ -98,4 +98,4 @@ public record struct RegistryItem
_ => RegistryValueKind.Unknown
};
}
-}
\ No newline at end of file
+}
diff --git a/optimizerDuck/Services/Optimization/Providers/RegistryService.cs b/optimizerDuck/Services/Optimization/Providers/RegistryService.cs
index a3e8d5a..6996b8b 100644
--- a/optimizerDuck/Services/Optimization/Providers/RegistryService.cs
+++ b/optimizerDuck/Services/Optimization/Providers/RegistryService.cs
@@ -140,6 +140,11 @@ public static class RegistryService
}
}
+ private static string NormalizeValueName(string? name)
+ {
+ return name ?? string.Empty;
+ }
+
public static bool KeyExists(RegistryItem item)
{
return WithKey(item, key => true, false);
@@ -151,7 +156,8 @@ public static class RegistryService
{
try
{
- var value = key.GetValue(item.Name, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
+ var valueName = NormalizeValueName(item.Name);
+ var value = key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (value is null) return default;
var result = ConvertRegistryValue<T>(value);
@@ -183,12 +189,13 @@ public static class RegistryService
var description = string.Format(Translations.Service_Registry_Description_Write, item.Path, item.Name);
try
{
- var backupValue = key.GetValue(item.Name, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
+ var valueName = NormalizeValueName(item.Name);
+ var backupValue = key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
var valueExists = backupValue != null ||
- key.GetValueNames().Contains(item.Name, StringComparer.OrdinalIgnoreCase);
- var backupKind = valueExists ? key.GetValueKind(item.Name) : RegistryValueKind.Unknown;
+ key.GetValueNames().Contains(valueName, StringComparer.OrdinalIgnoreCase);
+ var backupKind = valueExists ? key.GetValueKind(valueName) : RegistryValueKind.Unknown;
- key.SetValue(item.Name, item.Value, item.Kind);
+ key.SetValue(valueName, item.Value, item.Kind);
var revertStep = new RegistryRevertStep
{
@@ -232,8 +239,9 @@ public static class RegistryService
var description = string.Format(Translations.Service_Registry_Description_Delete, item.Path, item.Name ?? "(Default)");
try
{
- var backupValue = key.GetValue(item.Name, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
- if (backupValue == null && !key.GetValueNames().Contains(item.Name, StringComparer.OrdinalIgnoreCase))
+ var valueName = NormalizeValueName(item.Name);
+ var backupValue = key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
+ if (backupValue == null && !key.GetValueNames().Contains(valueName, StringComparer.OrdinalIgnoreCase))
{
ExecutionScope.LogInfo("Skip delete registry {Path}:{Name} (not found)", item.Path, item.Name!);
ExecutionScope.Track(nameof(DeleteValue), true);
@@ -241,8 +249,8 @@ public static class RegistryService
return true;
}
- var backupKind = key.GetValueKind(item.Name);
- key.DeleteValue(item.Name!, false);
+ var backupKind = key.GetValueKind(valueName);
+ key.DeleteValue(valueName, false);
var revertStep = new RegistryRevertStep
{
@@ -620,4 +628,4 @@ public static class RegistryService
}
#endregion Helpers
-}
\ No newline at end of file
+}
diff --git a/publish.bat b/publish.bat
index 40adcb4..4165000 100644
--- a/publish.bat
+++ b/publish.bat
@@ -1,37 +1,164 @@
@echo off
-echo hi itsfatduck bro :D
+setlocal EnableExtensions EnableDelayedExpansion
-echo Available publish profiles:
-echo 1. Portable
-echo 2. Single
-set /p buildOption="Choose publish profile (1/2): "
-if "%buildOption%"=="1" (
- echo Publishing portable publish...
- dotnet publish optimizerDuck/optimizerDuck.csproj /p:PublishProfile=Portable
- pause
-) else if "%buildOption%"=="2" (
- echo Publishing single publish...
- dotnet publish optimizerDuck/optimizerDuck.csproj /p:PublishProfile=Single
- pause
-) else (
- echo Invalid choice
- pause
- exit /b 1
+set "SCRIPT_DIR=%~dp0"
+set "PROJECT_PATH=%SCRIPT_DIR%optimizerDuck\optimizerDuck.csproj"
+set "TEST_PROJECT=%SCRIPT_DIR%optimizerDuck.Test\optimizerDuck.Test.csproj"
+set "CONFIGURATION=Release"
+set "PROFILE="
+set "RUN_TESTS=1"
+set "PAUSE_ON_EXIT=1"
+
+call :parse_args %*
+if errorlevel 1 goto :fail
+
+pushd "%SCRIPT_DIR%" >nul || (
+ echo Failed to open repository root.
+ goto :fail
)
+call :ensure_dotnet || goto :cleanup_fail
+
+if not exist "%PROJECT_PATH%" (
+ echo Project file not found: "%PROJECT_PATH%"
+ goto :cleanup_fail
+)
+
+if not defined PROFILE call :choose_profile
+if not defined PROFILE goto :cleanup_fail
+
echo.
-echo Do you want to run tests? (y/n)
-set /p runTests="Run tests? (y/n): "
-if "%runTests%"=="y" (
- echo Running tests...
- dotnet test
- pause
+echo ========================================
+echo optimizerDuck publish
+echo ========================================
+echo Profile: %PROFILE%
+echo Configuration: %CONFIGURATION%
+if "%RUN_TESTS%"=="1" (
+ echo Run tests: Yes
+) else (
+ echo Run tests: No
+)
+
+if "%RUN_TESTS%"=="1" (
+ echo.
+ echo [1/2] Running tests...
+ dotnet test "%TEST_PROJECT%" -c %CONFIGURATION% --nologo
+ if errorlevel 1 (
+ echo.
+ echo Tests failed. Publish stopped.
+ goto :cleanup_fail
+ )
) else (
- echo Skipping tests...
- pause
+ echo.
+ echo [1/2] Skipping tests...
+)
+
+echo.
+echo [2/2] Publishing %PROFILE% profile...
+dotnet publish "%PROJECT_PATH%" -c %CONFIGURATION% --nologo /p:PublishProfile=%PROFILE%
+if errorlevel 1 (
+ echo.
+ echo Publish failed.
+ goto :cleanup_fail
+)
+
+echo.
+echo Publish completed successfully.
+goto :cleanup_success
+
+:parse_args
+if "%~1"=="" exit /b 0
+
+if /I "%~1"=="portable" (
+ set "PROFILE=Portable"
+ shift
+ goto :parse_args
+)
+
+if /I "%~1"=="single" (
+ set "PROFILE=Single"
+ shift
+ goto :parse_args
+)
+
+if /I "%~1"=="--portable" (
+ set "PROFILE=Portable"
+ shift
+ goto :parse_args
+)
+
+if /I "%~1"=="--single" (
+ set "PROFILE=Single"
+ shift
+ goto :parse_args
+)
+
+if /I "%~1"=="--skip-tests" (
+ set "RUN_TESTS=0"
+ shift
+ goto :parse_args
+)
+
+if /I "%~1"=="--no-pause" (
+ set "PAUSE_ON_EXIT=0"
+ shift
+ goto :parse_args
+)
+
+if /I "%~1"=="--help" goto :help
+if /I "%~1"=="-h" goto :help
+
+echo Unknown argument: %~1
+echo Use --help to see available options.
+exit /b 1
+
+:choose_profile
+echo.
+echo Available publish profiles:
+echo 1. Portable
+echo 2. Single
+set /p "choice=Choose publish profile (1/2): "
+
+if "%choice%"=="1" (
+ set "PROFILE=Portable"
exit /b 0
)
+if "%choice%"=="2" (
+ set "PROFILE=Single"
+ exit /b 0
+)
+
+echo Invalid choice.
+exit /b 1
+
+:ensure_dotnet
+dotnet --version >nul 2>&1
+if errorlevel 1 (
+ echo .NET SDK was not found in PATH.
+ exit /b 1
+)
+exit /b 0
+
+:help
+echo.
+echo Usage:
+echo publish.bat [portable^|single] [--skip-tests] [--no-pause]
+echo.
+echo Examples:
+echo publish.bat portable
+echo publish.bat single --skip-tests
echo.
-echo Build and test completed!
-pause
+exit /b 1
+
+:cleanup_success
+popd >nul
+if "%PAUSE_ON_EXIT%"=="1" pause
+exit /b 0
+
+:cleanup_fail
+popd >nul
+
+:fail
+if "%PAUSE_ON_EXIT%"=="1" pause
+exit /b 1