build_csv.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # vim: set sw=2 sts=2 et tw=80 :
  2. require 'rspec'
  3. # XXX Super ugly hack to keep from starting beaker nodes
  4. module Kernel
  5. # make an alias of the original require
  6. alias original_require require
  7. # rewrite require
  8. def require(name)
  9. original_require name if name != 'spec_helper_acceptance'
  10. end
  11. end
  12. UNSUPPORTED_PLATFORMS = [].freeze
  13. def fact(*_args)
  14. []
  15. end
  16. # XXX End hax
  17. # Get a list of functions for test coverage
  18. function_list = Dir[File.join(File.dirname(__FILE__), '..', '..', 'lib', 'puppet', 'parser', 'functions', '*.rb')].map do |function_rb|
  19. File.basename(function_rb, '.rb')
  20. end
  21. ## Configure rspec to parse tests
  22. options = RSpec::Core::ConfigurationOptions.new(['spec/acceptance'])
  23. configuration = RSpec.configuration
  24. world = RSpec.world
  25. options.parse_options
  26. options.configure(configuration)
  27. configuration.load_spec_files
  28. ## Collect up tests and example groups into a hash
  29. def get_tests(children)
  30. children.each_with_object({}) do |c, memo|
  31. memo[c.description] = {}
  32. memo[c.description]['groups'] = get_tests(c.children) unless c.children.empty?
  33. unless c.examples.empty?
  34. memo[c.description]['tests'] = c.examples.map { |e|
  35. e.description unless e.pending?
  36. }.compact
  37. end
  38. next if c.examples.empty?
  39. memo[c.description]['pending_tests'] = c.examples.map { |e|
  40. e.description if e.pending?
  41. }.compact
  42. end
  43. end
  44. def count_test_types_in(type, group)
  45. return 0 if group.nil?
  46. group.reduce(0) do |m, (k, v)|
  47. m += v.length if k == type
  48. m += count_tests_in(v) if v.is_a?(Hash)
  49. m
  50. end
  51. end
  52. def count_tests_in(group)
  53. count_test_types_in('tests', group)
  54. end
  55. def count_pending_tests_in(group)
  56. count_test_types_in('pending_tests', group)
  57. end
  58. # Convert tests hash to csv format
  59. def to_csv(function_list, tests)
  60. function_list.map { |function_name|
  61. v = tests["#{function_name} function"]
  62. if v
  63. positive_tests = count_tests_in(v['groups']['success'])
  64. negative_tests = count_tests_in(v['groups']['failure'])
  65. pending_tests =
  66. count_pending_tests_in(v['groups']['failure']) +
  67. count_pending_tests_in(v['groups']['failure'])
  68. else
  69. positive_tests = 0
  70. negative_tests = 0
  71. pending_tests = 0
  72. end
  73. '%-25s, %-9d, %-9d, %-9d' % [function_name, positive_tests, negative_tests, pending_tests]
  74. }.compact
  75. end
  76. tests = get_tests(world.example_groups)
  77. csv = to_csv(function_list, tests)
  78. percentage_tested = "#{tests.count * 100 / function_list.count}%"
  79. printf("%-25s, %-9s, %-9s, %-9s\n", "#{percentage_tested} have tests.", 'Positive', 'Negative', 'Pending')
  80. puts csv