/tools/Rake/ncover.rb

http://github.com/agross/netopenspace · Ruby · 120 lines · 97 code · 23 blank · 0 comment · 12 complexity · dfd623dcac809e676be58c9b47d7118e MD5 · raw file

  1. require 'rexml/document'
  2. require 'win32/registry'
  3. include REXML
  4. include Win32
  5. class NCover
  6. def self.run_coverage(attributes)
  7. tool = attributes.fetch(:tool)
  8. program = attributes.fetch(:program).to_absolute
  9. assembly = attributes.fetch(:assembly)
  10. args = attributes.fetch(:args, [])
  11. reportDirectory = attributes.fetch(:report_dir, '.')
  12. workingDirectory = attributes.fetch(:working_dir, '.').to_absolute
  13. applicationAssemblies = attributes.fetch(:application_assemblies)
  14. registerProfiler = attributes.fetch(:register_profiler, true)
  15. xmlReport = assembly.name.ext('Coverage.xml').in(reportDirectory).to_absolute
  16. logFile = assembly.name.ext('Coverage.log').in(reportDirectory).to_absolute
  17. FileUtils.mkdir_p xmlReport.dirname
  18. FileUtils.mkdir_p logFile.dirname
  19. ncover = tool.to_absolute
  20. register registerProfiler, tool do
  21. sh "#{ncover.escape} #{program.escape} #{assembly} #{args.join(' ')} //a #{applicationAssemblies} //w #{workingDirectory.escape} //x #{xmlReport.escape} //l #{logFile.escape} //reg //v"
  22. end
  23. end
  24. def self.register(registerProfiler, tool)
  25. begin
  26. if registerProfiler
  27. Registry::HKEY_CURRENT_USER.create('Software\KiwiNova\NCoverExplorer', Registry::KEY_WRITE | Registry::KEY_READ) do |reg|
  28. begin
  29. refCount = reg.read_i 'NCoverRefCount'
  30. rescue
  31. refCount = 0
  32. end
  33. reg.write_i 'NCoverRefCount', refCount + 1
  34. end
  35. Registry::HKEY_CURRENT_USER.create('Software\Classes\CLSID\{6287B5F9-08A1-45e7-9498-B5B2E7B02995}', Registry::KEY_WRITE) do |reg|
  36. reg.write_s nil, 'NCover Profiler Object'
  37. reg.create('InprocServer32', Registry::KEY_WRITE) do |subkey|
  38. subkey.write_s nil, 'CoverLib.dll'.in(tool.dirname).to_absolute
  39. subkey.write_s 'ThreadingModel', 'Both'
  40. end
  41. end
  42. end
  43. yield if block_given?
  44. ensure
  45. if registerProfiler
  46. Registry::HKEY_CURRENT_USER.create('Software\KiwiNova\NCoverExplorer', Registry::KEY_WRITE | Registry::KEY_READ) do |reg|
  47. begin
  48. refCount = reg.read_i 'NCoverRefCount'
  49. rescue
  50. refCount = 1
  51. end
  52. refCount -= 1
  53. if refCount == 0
  54. begin
  55. Registry::HKEY_CURRENT_USER.delete_key 'Software\KiwiNova\NCoverExplorer', true
  56. Registry::HKEY_CURRENT_USER.delete_key 'Software\Classes\CLSID\{6287B5F9-08A1-45e7-9498-B5B2E7B02995}', true
  57. rescue
  58. end
  59. else
  60. reg.write_i 'NCoverRefCount', refCount if refCount >= 1
  61. end
  62. end
  63. end
  64. end
  65. end
  66. def self.explore(attributes)
  67. tool = attributes.fetch(:tool)
  68. project = attributes.fetch(:project)
  69. reportDirectory = attributes.fetch(:report_dir, '.')
  70. minCoverage = attributes.fetch(:min_coverage, 90)
  71. fail = attributes.fetch(:fail_if_under_min_coverage, false)
  72. htmlReport = attributes.fetch(:html_report, 'Coverage.html').in(reportDirectory).to_absolute
  73. xmlReport = attributes.fetch(:xml_report, 'Coverage.xml').in(reportDirectory).to_absolute
  74. statistics = attributes.fetch(:statistics, {})
  75. files = FileList.new("#{reportDirectory}/*.Coverage.xml").map! do |file|
  76. file.to_absolute.escape
  77. end
  78. return if files.empty?
  79. FileUtils.mkdir_p htmlReport.dirname
  80. FileUtils.mkdir_p xmlReport.dirname
  81. ncoverExplorer = tool.to_absolute
  82. sh "#{ncoverExplorer.escape} #{files} /report:ModuleClassFunctionSummary /sort:Name #{'/failCombinedMinimum' if fail} /minCoverage:#{minCoverage} /project:#{project.escape} /html:#{htmlReport.escape} /xml:#{xmlReport.escape}" \
  83. do |ok, status|
  84. if ok or status.exitstatus == 3
  85. doc = Document.new(File.read(xmlReport))
  86. statistics.each do |key, query|
  87. value = XPath.first(doc, query)
  88. yield key, value if block_given?
  89. statistics[key] = value
  90. end
  91. end
  92. TeamCity.append_build_status_text "Code coverage is below accepted level of #{minCoverage}%" if status.exitstatus == 3
  93. raise "NCoverExplorer failed" if not ok or (fail and status.exitstatus == 3)
  94. end
  95. return statistics
  96. end
  97. end