Windows环境下Go测试排除指定包的等效命令咨询
Windows下排除examples包执行Go测试的等效命令
Windows常用终端分为CMD和PowerShell,对应两种不同的等效命令:
1. CMD 环境
CMD不支持类Unix的命令替换和grep,需要用for循环结合findstr实现需求:
for /f "delims=" %i in ('go list ./...') do @echo %i | findstr /v /c:"/examples" && go test %i
如果是在批处理(.bat/.cmd)文件中执行,需把%i改成%%i:
for /f "delims=" %%i in ('go list ./...') do @echo %%i | findstr /v /c:"/examples" && go test %%i
/f "delims=":确保完整读取go list输出的每一行,避免被空格截断findstr /v /c:"/examples":反向匹配,排除包含/examples的包路径&& go test %i:仅当包路径未被排除时,执行测试命令
2. PowerShell 环境
PowerShell支持更接近Unix的管道和对象处理,命令更简洁:
go test (go list ./... | Where-Object { $_ -notmatch '/examples' })
也可以用Where-Object的别名?简化写法:
go test (go list ./... | ? { $_ -notmatch '/examples' })
Where-Object { $_ -notmatch '/examples' }:过滤掉名称中包含/examples的包路径- 括号
()实现命令替换,把过滤后的包列表直接传给go test
内容的提问来源于stack exchange,提问作者Mojtaba Arezoomand
相关产品推荐
相关产品推荐

