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