gpt4 book ai didi

rake - 为什么这个 dotCover rake 任务使用这些相对文件路径失败?

转载 作者:行者123 更新时间:2023-12-01 01:11:09 27 4
gpt4 key购买 nike

我在让 dotCover 在 Albacore 中工作时遇到一些问题 exec使用相对路径的任务。

@xUnitRunnerPath = Pathname.new('../../Tools/xUnit/xunitcontrib-dotcover.2.0/xunit.runner.utility.dll').realpath
@myTestDll = 'C:\PathToProj\My.Project.Tests\bin\Release\My.project.Tests.dll'
@outputDir = 'C:\PathToTestResults\'

exec :testCoverage do |cmd|
cmd.command = "C:/BuildAgent/tools/dotCover/dotCover.exe"
cmd.parameters = [
"cover",
"/targetexecutable=$$#{@xUnitRunnerPath}$$",
"/targetarguments=$$#{@myTestDll}$$",
"/output=#{@outputDir}/My.Project.Tests.dll.dcvr"
]
end
dotCover错误只是告诉我路径错误没有帮助
Failed to convert relative paths to absolute in parameters for the 'cover' 
command. The given path's format is not supported.

这并没有提供太多帮助,我也尝试过 dotcover help cover 来提供帮助,但没有提供很多关于出了什么问题的线索。

我已关注 this post about rake and dotcover还有 this question .也许我在这里遗漏了相关文档,但能够让它工作真的很有帮助。

编辑:我刚刚发现这与 relative and absolute paths 有关,也许是因为我使用的是绝对路径,所以我需要以下内容。我们明天就会知道
/AnalyseTargetArguments=false

最佳答案

我将从您自己的答案中重新混合 rakefile/tasks。为了吸引更广泛的受众,您应该遵循一些 Ruby/Rake 约定。我对如何编写很棒的 rakefile 有一些看法。特别是...

1. 不要直接调用/执行 Rake 任务

Rake::Task[:unitTestWithCoverage].execute( testAssembly )

您不想与直接 Rake 混在一起的原因有很多 invokeexecute .其中一个不调用依赖任务,一个只运行一次依赖任务......它变得愚蠢。应该总是有一种方法来构建正确定义和依赖的任务。

2. 不要参数化“内部”任务
exec :unitTestWithCoverage, [:testAssembly] do |cmd, testAssembly|

您可能有测试程序集的静态列表或通配符匹配列表。您应该能够在不使用参数的情况下构建具体任务。当用户可以从命令行使用自定义输入调用参数化任务时,我只使用参数化任务。

3. 无需在每个任务中创建路径
testAssemblyRealPath = Pathname.new(testAssembly).realpath
testAssemblyName = File.basename(testAssemblyRealPath)

我们要探索 rake FileList 弄清楚如何创建自定义的,懒惰的, mapped文件名/路径/任意字符串列表!

混音(更新)

我在第一个答案中犯了一个严重错误(我保留在底部,以供引用)。我将解释该部分中您/我的教育出了什么问题!

接下来是我的新建议。这对我来说应该是显而易见的,因为我在自己的构建中使用 mspec 测试运行器任务犯了同样的错误。
dotcover_path = 'path/to/dotcover.exe'
xunit_runner_path = 'path/to/xunitrunner.exe'

test_assemblies = FileList['path/to/output/**/*.test.dll']
coverage_results = "#{test_results_path}/coverage_results.dcvr"

task :cover_all => [ :tests_with_coverage, :publish_coverage_results ]

exec :tests_with_coverage do |cmd|
cmd.comand = dotcover_path
cmd.parameters = [
"cover",
"/AnalyseTargetArguments=False",
"/TargetExecutable=\"#{xunit_runner_path}\"",
"/TargetArguments=\"#{test_assemblies.join ','}\"",
"/Output=\"#{coverage_results}\""
]
end

task :publish_coverage_results => [ :tests_with_coverage ] do
import_data 'dotNetCoverage', 'dotCover', coverage_results
end

def import_data(type, tool, file)
puts "##teamcity[importData type='#{type}' tool='#{tool}' path='#{file}']"
end

说明

我默认使用绝对路径(通常使用 File.expand_path __FILE__ 常量)。有些工具/任务需要相对路径,但您始终可以使用 File.basename 之类的方法。 .
dotcover_path = 'path/to/dotcover.exe'
xunit_runner_path = 'path/to/xunitrunner.exe'

我们仍然可以使用 FileList 构建的程序集来定义目标程序集。在测试任务的主体执行之前,我们不会对其进行评估。
test_assemblies = FileList['path/to/output/**/*.test.dll']

覆盖运行器支持具有单个结果文件的多个程序集。这样我们就没有另一个复杂的 pathmap .
coverage_results = "#{test_results_path}/coverage_results.dcvr"

从您的 CI 服务器调用它来运行测试和发布的覆盖率结果。
task :cover_all => [ :tests_with_coverage, :publish_coverage_results ]

这个任务现在简单明了。一些注意事项:
1.使用 join 将目标列表转换为正确格式的字符串。
2. 我倾向于引用具有文件路径的 exec 任务参数(需要转义, \" )。
exec :tests_with_coverage do |cmd|
cmd.command = dotcover_path
cmd.parameters = [
"cover",
"/AnalyseTargetArguments=False",
"/TargetExecutable=\"#{xunit_runner_path}\"",
"/TargetArguments=\"#{test_assemblies.join ','}\"",
"/Output=\"#{coverage_results}\""
]
end

相同的旧发布任务/方法。
task publish_coverage_results => [ :tests_with_coverage ] do 
import_data 'dotNetCoverage', 'dotCover', coverage_results
end

def import_data(type, tool, file)
puts "##teamcity[importData type='#{type}' tool='#{tool}' path='#{file}']"
end

旧混音

剪切以显示问题区域,假设其余部分无趣或也存在于新解决方案中。

测试程序集在构建任务之后才会存在。这通常不是问题,因为 FileList很懒。在您枚举它之前它不会计算(例如,使用 eachmapzip )。

然而,我们马上 each在它上面生成测试任务......所以这行不通。它不会在列表中包含任何内容并且不会生成任何任务。或者,更糟糕的是,它会获取先前构建的输出并可能做坏事(如果您没有完全清理输出目录)。
test_assemblies = FileList['path/to/output/**/*.test.dll']
coverage_results = test_assemblies.pathmap "#{test_results_path}/%n.dcvr"
cover_task_names = test_assemblies.pathmap "cover_%n"

test_assemblies.zip(coverage_results, cover_task_names) do |assembly, results, task_name|
exec task_name do |cmd|
cmd.command = dotcover_path
cmd.parameters = [
"cover",
"/AnalyseTargetArguments=False",
"/TargetExecutable=#{xunit_path}",
"/TargetArguments=#{assembly}",
"/Output=#{results}"
]
end
end

关于rake - 为什么这个 dotCover rake 任务使用这些相对文件路径失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15480845/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com