Skip to content

Commit 24782be

Browse files
CopilotJusterZhu
andauthored
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>
1 parent 85144a1 commit 24782be

File tree

7 files changed

+1909
-0
lines changed

7 files changed

+1909
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
using GeneralUpdate.Drivelution.Core;
2+
using GeneralUpdate.Drivelution.Abstractions;
3+
using GeneralUpdate.Drivelution.Abstractions.Configuration;
4+
using Serilog;
5+
using Serilog.Core;
6+
7+
namespace DrivelutionTest.Core;
8+
9+
/// <summary>
10+
/// Tests for DriverUpdaterFactory class.
11+
/// Validates platform detection, factory creation, and platform-specific implementations.
12+
/// </summary>
13+
public class DriverUpdaterFactoryTests
14+
{
15+
/// <summary>
16+
/// Tests that Create method returns a non-null instance.
17+
/// </summary>
18+
[Fact]
19+
public void Create_WithoutParameters_ReturnsNonNullInstance()
20+
{
21+
// Arrange & Act
22+
var updater = DriverUpdaterFactory.Create();
23+
24+
// Assert
25+
Assert.NotNull(updater);
26+
Assert.IsAssignableFrom<IGeneralDrivelution>(updater);
27+
}
28+
29+
/// <summary>
30+
/// Tests that Create method accepts custom logger.
31+
/// </summary>
32+
[Fact]
33+
public void Create_WithCustomLogger_ReturnsInstance()
34+
{
35+
// Arrange
36+
var logger = new LoggerConfiguration()
37+
.MinimumLevel.Debug()
38+
.WriteTo.Console()
39+
.CreateLogger();
40+
41+
// Act
42+
var updater = DriverUpdaterFactory.Create(logger);
43+
44+
// Assert
45+
Assert.NotNull(updater);
46+
Assert.IsAssignableFrom<IGeneralDrivelution>(updater);
47+
}
48+
49+
/// <summary>
50+
/// Tests that Create method accepts custom options.
51+
/// </summary>
52+
[Fact]
53+
public void Create_WithCustomOptions_ReturnsInstance()
54+
{
55+
// Arrange
56+
var options = new DriverUpdateOptions
57+
{
58+
LogLevel = "Debug",
59+
LogFilePath = "./logs/test.log"
60+
};
61+
62+
// Act
63+
var updater = DriverUpdaterFactory.Create(null, options);
64+
65+
// Assert
66+
Assert.NotNull(updater);
67+
}
68+
69+
/// <summary>
70+
/// Tests that GetCurrentPlatform returns a valid platform name.
71+
/// </summary>
72+
[Fact]
73+
public void GetCurrentPlatform_ReturnsValidPlatformName()
74+
{
75+
// Act
76+
var platform = DriverUpdaterFactory.GetCurrentPlatform();
77+
78+
// Assert
79+
Assert.NotNull(platform);
80+
Assert.Contains(platform, new[] { "Windows", "Linux", "MacOS", "Unknown" });
81+
}
82+
83+
/// <summary>
84+
/// Tests that IsPlatformSupported returns a boolean value.
85+
/// </summary>
86+
[Fact]
87+
public void IsPlatformSupported_ReturnsBooleanValue()
88+
{
89+
// Act
90+
var isSupported = DriverUpdaterFactory.IsPlatformSupported();
91+
92+
// Assert
93+
// Windows and Linux should be supported
94+
Assert.True(isSupported || DriverUpdaterFactory.GetCurrentPlatform() == "MacOS" || DriverUpdaterFactory.GetCurrentPlatform() == "Unknown");
95+
}
96+
97+
/// <summary>
98+
/// Tests that CreateValidator returns a non-null instance.
99+
/// </summary>
100+
[Fact]
101+
public void CreateValidator_WithoutLogger_ReturnsNonNullInstance()
102+
{
103+
// Skip on MacOS and Unknown platforms
104+
var platform = DriverUpdaterFactory.GetCurrentPlatform();
105+
if (platform == "MacOS" || platform == "Unknown")
106+
{
107+
return;
108+
}
109+
110+
// Act
111+
var validator = DriverUpdaterFactory.CreateValidator();
112+
113+
// Assert
114+
Assert.NotNull(validator);
115+
Assert.IsAssignableFrom<IDriverValidator>(validator);
116+
}
117+
118+
/// <summary>
119+
/// Tests that CreateBackup returns a non-null instance.
120+
/// </summary>
121+
[Fact]
122+
public void CreateBackup_WithoutLogger_ReturnsNonNullInstance()
123+
{
124+
// Skip on MacOS and Unknown platforms
125+
var platform = DriverUpdaterFactory.GetCurrentPlatform();
126+
if (platform == "MacOS" || platform == "Unknown")
127+
{
128+
return;
129+
}
130+
131+
// Act
132+
var backup = DriverUpdaterFactory.CreateBackup();
133+
134+
// Assert
135+
Assert.NotNull(backup);
136+
Assert.IsAssignableFrom<IDriverBackup>(backup);
137+
}
138+
139+
/// <summary>
140+
/// Tests that CreateValidator with custom logger works correctly.
141+
/// </summary>
142+
[Fact]
143+
public void CreateValidator_WithCustomLogger_ReturnsInstance()
144+
{
145+
// Skip on MacOS and Unknown platforms
146+
var platform = DriverUpdaterFactory.GetCurrentPlatform();
147+
if (platform == "MacOS" || platform == "Unknown")
148+
{
149+
return;
150+
}
151+
152+
// Arrange
153+
var logger = new LoggerConfiguration()
154+
.MinimumLevel.Debug()
155+
.WriteTo.Console()
156+
.CreateLogger();
157+
158+
// Act
159+
var validator = DriverUpdaterFactory.CreateValidator(logger);
160+
161+
// Assert
162+
Assert.NotNull(validator);
163+
}
164+
165+
/// <summary>
166+
/// Tests that CreateBackup with custom logger works correctly.
167+
/// </summary>
168+
[Fact]
169+
public void CreateBackup_WithCustomLogger_ReturnsInstance()
170+
{
171+
// Skip on MacOS and Unknown platforms
172+
var platform = DriverUpdaterFactory.GetCurrentPlatform();
173+
if (platform == "MacOS" || platform == "Unknown")
174+
{
175+
return;
176+
}
177+
178+
// Arrange
179+
var logger = new LoggerConfiguration()
180+
.MinimumLevel.Debug()
181+
.WriteTo.Console()
182+
.CreateLogger();
183+
184+
// Act
185+
var backup = DriverUpdaterFactory.CreateBackup(logger);
186+
187+
// Assert
188+
Assert.NotNull(backup);
189+
}
190+
191+
/// <summary>
192+
/// Tests that Create throws PlatformNotSupportedException on unsupported platforms.
193+
/// This test documents expected behavior but cannot be easily tested on supported platforms.
194+
/// </summary>
195+
[Fact]
196+
public void Create_OnSupportedPlatform_DoesNotThrow()
197+
{
198+
// Skip on MacOS as it's not yet implemented
199+
var platform = DriverUpdaterFactory.GetCurrentPlatform();
200+
201+
if (platform == "MacOS")
202+
{
203+
// MacOS should throw PlatformNotSupportedException
204+
Assert.Throws<PlatformNotSupportedException>(() => DriverUpdaterFactory.Create());
205+
return;
206+
}
207+
208+
// Act & Assert - should not throw on Windows/Linux
209+
var exception = Record.Exception(() => DriverUpdaterFactory.Create());
210+
Assert.Null(exception);
211+
}
212+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
15+
<PackageReference Include="xunit" Version="2.9.3" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
17+
<PackageReference Include="Moq" Version="4.20.72" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<Using Include="Xunit" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\GeneralUpdate.Drivelution\GeneralUpdate.Drivelution.csproj" />
26+
</ItemGroup>
27+
28+
</Project>

0 commit comments

Comments
 (0)