Skip to content

Commit 406494e

Browse files
CopilotJusterZhu
andcommitted
Add tests for GetDriversFromDirectoryAsync method
Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
1 parent 54acd63 commit 406494e

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

src/c#/DrivelutionTest/GeneralDrivelutionTests.cs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,185 @@ public void GetPlatformInfo_ReportsCorrectSupportStatus()
327327
Assert.False(platformInfo.IsSupported); // MacOS not yet implemented
328328
}
329329
}
330+
331+
/// <summary>
332+
/// Tests that GetDriversFromDirectoryAsync returns empty list for non-existent directory.
333+
/// </summary>
334+
[Fact]
335+
public async Task GetDriversFromDirectoryAsync_WithNonExistentDirectory_ReturnsEmptyList()
336+
{
337+
// Arrange
338+
var nonExistentPath = Path.Combine(Path.GetTempPath(), $"nonexistent_{Guid.NewGuid()}");
339+
340+
// Act
341+
var result = await GeneralDrivelution.GetDriversFromDirectoryAsync(nonExistentPath);
342+
343+
// Assert
344+
Assert.NotNull(result);
345+
Assert.Empty(result);
346+
}
347+
348+
/// <summary>
349+
/// Tests that GetDriversFromDirectoryAsync returns empty list for empty directory.
350+
/// </summary>
351+
[Fact]
352+
public async Task GetDriversFromDirectoryAsync_WithEmptyDirectory_ReturnsEmptyList()
353+
{
354+
// Arrange
355+
var emptyDir = Path.Combine(Path.GetTempPath(), $"empty_{Guid.NewGuid()}");
356+
Directory.CreateDirectory(emptyDir);
357+
358+
try
359+
{
360+
// Act
361+
var result = await GeneralDrivelution.GetDriversFromDirectoryAsync(emptyDir);
362+
363+
// Assert
364+
Assert.NotNull(result);
365+
Assert.Empty(result);
366+
}
367+
finally
368+
{
369+
if (Directory.Exists(emptyDir))
370+
{
371+
Directory.Delete(emptyDir, true);
372+
}
373+
}
374+
}
375+
376+
/// <summary>
377+
/// Tests that GetDriversFromDirectoryAsync discovers driver files in directory.
378+
/// </summary>
379+
[Fact]
380+
public async Task GetDriversFromDirectoryAsync_WithDriverFiles_ReturnsDriverInfoList()
381+
{
382+
// Arrange
383+
var testDir = Path.Combine(Path.GetTempPath(), $"drivers_{Guid.NewGuid()}");
384+
Directory.CreateDirectory(testDir);
385+
386+
try
387+
{
388+
// Create test driver files based on platform
389+
var platformInfo = GeneralDrivelution.GetPlatformInfo();
390+
var testFiles = new List<string>();
391+
392+
if (platformInfo.Platform == "Windows")
393+
{
394+
// Create mock .inf file
395+
var infFile = Path.Combine(testDir, "test_driver.inf");
396+
File.WriteAllText(infFile, @"
397+
[Version]
398+
Signature=""$Windows NT$""
399+
DriverVer=01/15/2024,1.0.0.0
400+
401+
[DriverInfo]
402+
DriverDesc=""Test Driver""
403+
");
404+
testFiles.Add(infFile);
405+
}
406+
else if (platformInfo.Platform == "Linux")
407+
{
408+
// Create mock .ko file
409+
var koFile = Path.Combine(testDir, "test_driver.ko");
410+
File.WriteAllText(koFile, "Mock kernel module content");
411+
testFiles.Add(koFile);
412+
}
413+
414+
// Act
415+
var result = await GeneralDrivelution.GetDriversFromDirectoryAsync(testDir);
416+
417+
// Assert
418+
Assert.NotNull(result);
419+
420+
// Should find at least one driver if platform is supported
421+
if (platformInfo.IsSupported && testFiles.Any())
422+
{
423+
Assert.NotEmpty(result);
424+
425+
// Check that driver info has expected properties
426+
var driver = result.First();
427+
Assert.NotNull(driver.Name);
428+
Assert.NotEmpty(driver.Name);
429+
Assert.NotNull(driver.FilePath);
430+
Assert.NotEmpty(driver.FilePath);
431+
Assert.NotNull(driver.Version);
432+
Assert.NotEmpty(driver.Version);
433+
Assert.NotNull(driver.Hash);
434+
Assert.NotEmpty(driver.Hash);
435+
}
436+
}
437+
finally
438+
{
439+
if (Directory.Exists(testDir))
440+
{
441+
Directory.Delete(testDir, true);
442+
}
443+
}
444+
}
445+
446+
/// <summary>
447+
/// Tests that GetDriversFromDirectoryAsync with search pattern filters correctly.
448+
/// </summary>
449+
[Fact]
450+
public async Task GetDriversFromDirectoryAsync_WithSearchPattern_FiltersCorrectly()
451+
{
452+
// Arrange
453+
var testDir = Path.Combine(Path.GetTempPath(), $"drivers_{Guid.NewGuid()}");
454+
Directory.CreateDirectory(testDir);
455+
456+
try
457+
{
458+
// Create different types of files
459+
var infFile = Path.Combine(testDir, "driver1.inf");
460+
var txtFile = Path.Combine(testDir, "readme.txt");
461+
File.WriteAllText(infFile, "INF content");
462+
File.WriteAllText(txtFile, "Text content");
463+
464+
// Act
465+
var result = await GeneralDrivelution.GetDriversFromDirectoryAsync(testDir, "*.inf");
466+
467+
// Assert
468+
Assert.NotNull(result);
469+
470+
// Should only find .inf files
471+
// The count depends on whether the platform supports parsing .inf files
472+
}
473+
finally
474+
{
475+
if (Directory.Exists(testDir))
476+
{
477+
Directory.Delete(testDir, true);
478+
}
479+
}
480+
}
481+
482+
/// <summary>
483+
/// Tests that GetDriversFromDirectoryAsync handles cancellation.
484+
/// </summary>
485+
[Fact]
486+
public async Task GetDriversFromDirectoryAsync_WithCancellation_HandlesCancellation()
487+
{
488+
// Arrange
489+
var testDir = Path.Combine(Path.GetTempPath(), $"drivers_{Guid.NewGuid()}");
490+
Directory.CreateDirectory(testDir);
491+
492+
try
493+
{
494+
var cts = new CancellationTokenSource();
495+
cts.Cancel();
496+
497+
// Act
498+
var result = await GeneralDrivelution.GetDriversFromDirectoryAsync(testDir, null, cts.Token);
499+
500+
// Assert - Should complete without throwing or return empty list
501+
Assert.NotNull(result);
502+
}
503+
finally
504+
{
505+
if (Directory.Exists(testDir))
506+
{
507+
Directory.Delete(testDir, true);
508+
}
509+
}
510+
}
330511
}

0 commit comments

Comments
 (0)