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