Add comprehensive test suite for GeneralUpdate.Drivelution#140
Add comprehensive test suite for GeneralUpdate.Drivelution#140
Conversation
Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds a new DrivelutionTest test project to exercise the GeneralUpdate.Drivelution library’s core utilities, models, and entry-point/factory behaviors.
Changes:
- Introduces
src/c#/DrivelutionTestxUnit test project referencingGeneralUpdate.Drivelution. - Adds unit tests for core utilities (hashing, compatibility, semver comparison, restart helper).
- Adds validation tests for key models and basic
GeneralDrivelution/DriverUpdaterFactoryflows.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/c#/DrivelutionTest/Utilities/VersionComparerAndRestartHelperTests.cs | Adds tests for SemVer comparisons and restart helper behavior. |
| src/c#/DrivelutionTest/Utilities/HashValidatorTests.cs | Adds tests for file/string hashing, validation, and cancellation. |
| src/c#/DrivelutionTest/Utilities/CompatibilityCheckerTests.cs | Adds tests for OS/arch detection and compatibility report logic. |
| src/c#/DrivelutionTest/Models/ModelTests.cs | Adds default-value and property/enum tests for Drivelution models. |
| src/c#/DrivelutionTest/GeneralDrivelutionTests.cs | Adds tests for entry-point creation, platform info, and “quick update/validate” flows. |
| src/c#/DrivelutionTest/DrivelutionTest.csproj | Adds a new test project targeting net8.0 with xUnit/Moq packages and project reference. |
| src/c#/DrivelutionTest/Core/DriverUpdaterFactoryTests.cs | Adds tests for factory creation/platform detection and validator/backup factories. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| <ItemGroup> | ||
| <ProjectReference Include="..\GeneralUpdate.Drivelution\GeneralUpdate.Drivelution.csproj" /> | ||
| </ItemGroup> |
There was a problem hiding this comment.
This new test project isn’t referenced by src/c#/GeneralUpdate.sln (no DrivelutionTest entry), so a solution-level dotnet test run won’t execute these tests. Add the project to the solution (or update the test/CI invocation to include this csproj explicitly) so coverage is actually exercised.
| [Fact] | ||
| public void Create_WithoutParameters_ReturnsNonNullInstance() | ||
| { | ||
| // Arrange & Act | ||
| var updater = DriverUpdaterFactory.Create(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(updater); | ||
| Assert.IsAssignableFrom<IGeneralDrivelution>(updater); |
There was a problem hiding this comment.
DriverUpdaterFactory.Create() throws PlatformNotSupportedException on MacOS/unsupported platforms, so this test will fail when run there. Consider branching on GetCurrentPlatform() and asserting the exception for MacOS/Unknown (or skipping), so the test suite is portable across runners.
| [Fact] | ||
| public void Create_WithoutParameters_ReturnsNonNullInstance() | ||
| { | ||
| // Act | ||
| var updater = GeneralDrivelution.Create(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(updater); | ||
| } |
There was a problem hiding this comment.
These Create(...) entry-point tests assume the platform is supported, but GeneralDrivelution.Create() will throw on MacOS/unsupported platforms (via DriverUpdaterFactory.Create). To avoid platform-dependent failures, gate the test (skip) or assert the expected exception when GetPlatformInfo().IsSupported is false.
| /// </summary> | ||
| [Fact] | ||
| public async Task QuickUpdateAsync_WithMinimalParameters_HandlesGracefully() | ||
| { | ||
| // Arrange | ||
| var testFilePath = Path.Combine(Path.GetTempPath(), $"test_driver_{Guid.NewGuid()}.txt"); | ||
| File.WriteAllText(testFilePath, "Test driver content"); | ||
|
|
||
| try | ||
| { | ||
| var driverInfo = new DriverInfo | ||
| { | ||
| Name = "Test Driver", | ||
| Version = "1.0.0", | ||
| FilePath = testFilePath, | ||
| TargetOS = "", | ||
| Architecture = "" | ||
| }; | ||
|
|
||
| // Act | ||
| var result = await GeneralDrivelution.QuickUpdateAsync(driverInfo); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| // The result might fail due to validation or permissions, but should not crash |
There was a problem hiding this comment.
This test calls GeneralDrivelution.QuickUpdateAsync, which can execute real driver installation steps when run with admin/root privileges (Windows: ExecuteDriverInstallationAsync, Linux: ExecuteDriverInstallationAsync). To keep this as a safe unit test, avoid using an actual file path (force validation failure early), or explicitly skip when running elevated so the test can’t modify the host system.
| // Assert - Should return a boolean value without throwing | ||
| // No assertion needed - the test passes if no exception is thrown |
There was a problem hiding this comment.
ValidateAsync_WithMinimalDriverInfo_ReturnsBooleanResult doesn’t assert anything about the returned value (the result is unused). If the intention is “doesn’t throw”, rename the test accordingly; otherwise add an assertion that verifies the expected true/false outcome for the given input so the test actually detects regressions.
| // Assert - Should return a boolean value without throwing | |
| // No assertion needed - the test passes if no exception is thrown | |
| // Assert - validation should succeed for an existing minimal driver file | |
| Assert.True(result); |
| Assert.True(RestartHelper.IsRestartRequired(RestartMode.None) == false); | ||
| Assert.True(RestartHelper.IsRestartRequired(RestartMode.Prompt) == true); |
There was a problem hiding this comment.
This “callable” test is asserting boolean comparisons via Assert.True(x == false/true), which reduces readability and makes failures less clear. Prefer Assert.False(...) / Assert.True(...) directly (or remove the redundancy since IsRestartRequired is already covered by the focused tests above).
| Assert.True(RestartHelper.IsRestartRequired(RestartMode.None) == false); | |
| Assert.True(RestartHelper.IsRestartRequired(RestartMode.Prompt) == true); | |
| Assert.False(RestartHelper.IsRestartRequired(RestartMode.None)); | |
| Assert.True(RestartHelper.IsRestartRequired(RestartMode.Prompt)); |
| /// <summary> | ||
| /// Tests that QuickUpdateAsync with custom strategy works. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task QuickUpdateAsync_WithCustomStrategy_HandlesGracefully() | ||
| { | ||
| // Arrange | ||
| var testFilePath = Path.Combine(Path.GetTempPath(), $"test_driver_{Guid.NewGuid()}.txt"); | ||
| File.WriteAllText(testFilePath, "Test driver content"); | ||
|
|
||
| try | ||
| { | ||
| var driverInfo = new DriverInfo | ||
| { | ||
| Name = "Test Driver", | ||
| Version = "1.0.0", | ||
| FilePath = testFilePath, | ||
| TargetOS = "", | ||
| Architecture = "" | ||
| }; | ||
|
|
||
| var strategy = new UpdateStrategy | ||
| { | ||
| RequireBackup = false, | ||
| RetryCount = 1, | ||
| RetryIntervalSeconds = 1 | ||
| }; | ||
|
|
||
| // Act | ||
| var result = await GeneralDrivelution.QuickUpdateAsync(driverInfo, strategy); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| // Verify that result status is one of the valid enum values | ||
| Assert.True(Enum.IsDefined(typeof(UpdateStatus), result.Status)); | ||
| } |
There was a problem hiding this comment.
Same concern as the other QuickUpdate test: if the runner has elevated privileges, this can progress past validation and attempt a real driver install. To keep tests non-invasive, force an early failure (e.g., non-existent path) or skip when elevated so CI/dev machines can’t be modified by running the unit suite.
| /// <summary> | ||
| /// Tests that QuickUpdateAsync with cancellation token works. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task QuickUpdateAsync_WithCancellationToken_CanBeCancelled() | ||
| { | ||
| // Arrange | ||
| var testFilePath = Path.Combine(Path.GetTempPath(), $"test_driver_{Guid.NewGuid()}.txt"); | ||
| File.WriteAllText(testFilePath, "Test driver content"); | ||
|
|
||
| try | ||
| { | ||
| var driverInfo = new DriverInfo | ||
| { | ||
| Name = "Test Driver", | ||
| Version = "1.0.0", | ||
| FilePath = testFilePath | ||
| }; | ||
|
|
||
| var cts = new CancellationTokenSource(); | ||
| cts.Cancel(); | ||
|
|
||
| // Act & Assert | ||
| // Should either complete quickly or throw cancellation exception | ||
| var exception = await Record.ExceptionAsync( | ||
| () => GeneralDrivelution.QuickUpdateAsync(driverInfo, cts.Token)); | ||
|
|
||
| // Either succeeded, failed gracefully, or was cancelled | ||
| Assert.True(exception == null || exception is OperationCanceledException); | ||
| } |
There was a problem hiding this comment.
This cancellation test still invokes QuickUpdateAsync, which may have side effects if the update starts before cancellation is observed (especially if running elevated). Consider using an input that guarantees validation fails immediately, or explicitly skip when running elevated to avoid any chance of executing installation steps during tests.
* add GeneralUpdate.Drivelution * Add comprehensive documentation for GeneralUpdate.Drivelution (#139) * Initial plan * Add comprehensive README documentation for GeneralUpdate.Drivelution Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Add comprehensive test suite for GeneralUpdate.Drivelution (#140) * Initial plan * Add comprehensive test cases for GeneralUpdate.Drivelution Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Address code review feedback: improve test quality and cleanup Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Rename DriverUpdate to Drivelution across codebase (#141) * Initial plan * Replace all instances of DriverUpdate with Drivelution Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Update error code prefixes from DU_ to DR_ Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Add GetDriversFromDirectoryAsync for local driver discovery (#142) * Initial plan * Add GetDriversFromDirectoryAsync method to read driver information from local directory Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Add tests for GetDriversFromDirectoryAsync method Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Fix security issues: prevent command injection and improve certificate parsing Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Translate all Chinese annotations to English Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
Implemented 110 unit tests covering the core driver update functionality introduced in GeneralUpdate.Drivelution.
Test Coverage
Test Project Structure
Follows existing repository conventions: xUnit framework, .NET 8.0 target, Moq for mocking.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.