File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11BeforeAll {
2+ # 为 CI 环境中不存在的外部命令创建占位函数,使 Pester Mock 能正常工作
3+ # Pester 的 Mock 要求目标命令在当前会话中可解析
4+ if (-not (Get-Command nvidia- smi - ErrorAction SilentlyContinue)) {
5+ function global :nvidia-smi { }
6+ }
7+ if (-not (Get-Command free - ErrorAction SilentlyContinue)) {
8+ function global :free { }
9+ }
210 Import-Module " $PSScriptRoot \..\modules\hardware.psm1" - Force
311 Import-Module " $PSScriptRoot \..\modules\os.psm1" - Force
412}
513
14+ AfterAll {
15+ # 清理占位函数
16+ Remove-Item Function:\nvidia- smi - ErrorAction SilentlyContinue
17+ Remove-Item Function:\free - ErrorAction SilentlyContinue
18+ }
19+
620Describe " Get-GpuInfo 函数测试" {
721 Context " Linux 环境下 nvidia-smi CSV 输出" {
822 It " 应该解析 nvidia-smi CSV 输出并返回正确的 GPU 信息" {
Original file line number Diff line number Diff line change @@ -32,6 +32,20 @@ Describe "New-Shortcut 函数测试" -Tag 'windowsOnly' {
3232 Set-ItResult - Skipped - Because " 仅 Windows 环境"
3333 return
3434 }
35+
36+ # Mock COM 对象以避免 TestDrive 路径上 WScript.Shell 的兼容性问题
37+ $mockShortcut = [PSCustomObject ]@ {
38+ TargetPath = ' '
39+ Arguments = ' '
40+ WorkingDirectory = ' '
41+ IconLocation = ' '
42+ }
43+ $mockShortcut | Add-Member - MemberType ScriptMethod - Name Save - Value { }
44+ $mockWshShell = [PSCustomObject ]@ {}
45+ $mockWshShell | Add-Member - MemberType ScriptMethod - Name CreateShortcut - Value { param ($path ) $mockShortcut }
46+
47+ Mock - ModuleName win New-Object { $mockWshShell } - ParameterFilter { $ComObject -eq ' WScript.Shell' }
48+
3549 $testTarget = " $TestDrive \target.txt"
3650 " " | Out-File - FilePath $testTarget - Encoding utf8
3751 $testShortcut = " $TestDrive \shortcut.lnk"
You can’t perform that action at this time.
0 commit comments