Skip to content

Commit ccf3cfc

Browse files
committed
test: 修复测试脚本的依赖和标签配置问题
- 修复 Switch-Mirrors 测试中直接调用脚本导致的依赖问题,改为导入函数并模拟网络请求 - 修正 test:slow 脚本中排除标签的逻辑,避免过滤掉 Slow 标签的测试 - 将网络模块测试的 BeforeAll 移至每个 Describe 块内,确保模块正确导入,并为部分测试添加 windowsOnly 标签
1 parent 1698fb4 commit ccf3cfc

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"test:debug": "pwsh -NoProfile -Command \"\\$env:PWSH_TEST_MODE='debug'; Invoke-Pester -Configuration ( ./PesterConfiguration.ps1 )\"",
2828
"test:serial:debug": "pwsh -NoProfile -Command \"\\$env:PWSH_TEST_MODE='serial'; \\$env:PWSH_TEST_VERBOSE='1'; Invoke-Pester -Configuration ( ./PesterConfiguration.ps1 )\"",
2929
"test:profile": "pwsh -NoProfile -Command \"\\$env:PWSH_TEST_MODE='serial'; \\$env:PWSH_TEST_PATH='./tests/ProfileMode.Tests.ps1'; \\$env:PWSH_TEST_VERBOSE='1'; Invoke-Pester -Configuration ( ./PesterConfiguration.ps1 )\"",
30-
"test:slow": "pwsh -NoProfile -Command \"\\$c = ./PesterConfiguration.ps1; \\$c.Filter.Tag = 'Slow'; \\$c.Filter.ExcludeTag = @(); Invoke-Pester -Configuration \\$c\"",
30+
"test:slow": "pwsh -NoProfile -Command \"\\$c = ./PesterConfiguration.ps1; \\$c.Filter.Tag = 'Slow'; \\$c.Filter.ExcludeTag = @(\\$c.Filter.ExcludeTag.Value | Where-Object { \\$_ -ne 'Slow' }); Invoke-Pester -Configuration \\$c\"",
3131
"test:detailed": "pwsh -Command \"Invoke-Pester -Output Detailed\"",
3232
"scripts:install": "pwsh -File ./install.ps1",
3333
"scoop:update": "scoop update -a",

psutils/tests/network.Tests.ps1

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@
66
使用Pester框架测试网络工具功能的各种场景
77
#>
88

9-
BeforeAll {
10-
# 导入被测试的模块
11-
Import-Module "$PSScriptRoot\..\modules\network.psm1" -Force
12-
13-
# 预先获取一个被占用的端口,避免在每个测试中重复查询
14-
$script:occupiedPort = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | Select-Object -First 1 -ExpandProperty LocalPort
15-
}
9+
Describe "Test-PortOccupation 函数测试" -Tag 'Network', 'Slow', 'windowsOnly' {
10+
BeforeAll {
11+
Import-Module "$PSScriptRoot\..\modules\network.psm1" -Force
12+
$script:occupiedPort = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | Select-Object -First 1 -ExpandProperty LocalPort
13+
}
1614

17-
Describe "Test-PortOccupation 函数测试" -Tag 'Network', 'Slow' {
1815
Context "端口占用检测" {
1916
It "应该能够检测到已占用的端口" {
2017
if ($script:occupiedPort) {
2118
$result = Test-PortOccupation -Port $script:occupiedPort
2219
$result | Should -Be $true
2320
}
2421
else {
25-
# 如果没有找到被占用的端口,跳过此测试
2622
Set-ItResult -Skipped -Because "没有找到被占用的端口进行测试"
2723
}
2824
}
@@ -42,7 +38,12 @@ Describe "Test-PortOccupation 函数测试" -Tag 'Network', 'Slow' {
4238
}
4339
}
4440

45-
Describe "Get-PortProcess 函数测试" -Tag 'Network', 'Slow' {
41+
Describe "Get-PortProcess 函数测试" -Tag 'Network', 'Slow', 'windowsOnly' {
42+
BeforeAll {
43+
Import-Module "$PSScriptRoot\..\modules\network.psm1" -Force
44+
$script:occupiedPort = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | Select-Object -First 1 -ExpandProperty LocalPort
45+
}
46+
4647
Context "进程信息获取" {
4748
It "应该能够获取占用端口的进程信息" {
4849
if ($script:occupiedPort) {
@@ -83,6 +84,10 @@ Describe "Get-PortProcess 函数测试" -Tag 'Network', 'Slow' {
8384
}
8485

8586
Describe "Wait-ForURL 函数测试" -Tag 'Network', 'Slow' {
87+
BeforeAll {
88+
Import-Module "$PSScriptRoot\..\modules\network.psm1" -Force
89+
}
90+
8691
Context "URL 可达性测试" {
8792
It "应该返回布尔值类型" {
8893
# 测试函数返回值类型,使用快速超时和短间隔避免长时间等待

tests/Switch-Mirrors.Tests.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ Describe 'Switch-Mirrors.ps1' {
2020

2121
It 'DryRun 写入不应实际修改文件(仅输出)' -Tag 'Slow' {
2222
$scriptPath = Join-Path $PSScriptRoot '..' 'scripts' 'pwsh' 'misc' 'Switch-Mirrors.ps1'
23-
$daemon = if ($IsLinux) { '/etc/docker/daemon.json' } elseif ($IsWindows) { Join-Path $env:ProgramData 'Docker\config\daemon.json' } else { Join-Path $HOME '.docker/daemon.json' }
23+
. (Resolve-Path $scriptPath)
24+
Mock -CommandName Invoke-WebRequest -MockWith { [pscustomobject]@{ StatusCode = 200 } }
25+
$daemon = Get-DockerDaemonPath
2426
$before = if (Test-Path -LiteralPath $daemon) { Get-Content -LiteralPath $daemon -Raw } else { '' }
25-
pwsh -NoLogo -NoProfile -File (Resolve-Path $scriptPath) -Target docker -MirrorUrls 'https://registry-1.docker.io' -DryRun | Out-Null
27+
Set-DockerRegistryMirror -MirrorUrls 'https://registry-1.docker.io' -DryRun
2628
$after = if (Test-Path -LiteralPath $daemon) { Get-Content -LiteralPath $daemon -Raw } else { '' }
2729
$after | Should -BeExactly $before
2830
}

0 commit comments

Comments
 (0)