dcov 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,15 @@
1
+ == 0.0.1 2007-05-28
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+
6
+ == 0.0.2 2007-06-20
7
+
8
+ * 3 major enhancements:
9
+ * Refactored generators out to separate classes
10
+ * Added rudimentary Ruport support
11
+ * Refactored analyzers out to separate classes to allow users to easily define custom analyzers
12
+
13
+ == 0.1.0
14
+
15
+ No serious enhancements other than a pretty major bug fix and a new version number. :)
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Jeremy McAnally
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,21 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ bin/dcov
7
+ lib/dcov.rb
8
+ lib/dcov/analyzer.rb
9
+ lib/dcov/version.rb
10
+ lib/dcov/stats.rb
11
+ lib/dcov/analyzed_token.rb
12
+ lib/dcov/generators/html/generator.rb
13
+ lib/dcov/analyzers/coverage_analyzer.rb
14
+ setup.rb
15
+ spec/dcov_spec.rb
16
+ spec/spec_helper.rb
17
+ spec/blank_code.rb
18
+ spec/just_a_class.rb
19
+ spec/just_a_method.rb
20
+ spec/just_a_module.rb
21
+ spec/mock_code.rb
data/README.txt ADDED
@@ -0,0 +1,12 @@
1
+ dcov -- Your friendly neighborhood Ruby documentation analyzer
2
+ ==============================================================
3
+
4
+ dcov is a tool to analyze your Ruby documentation. At this time, it only checks for coverage and not quality (even though it will check for that one day...).
5
+
6
+ To use dcov, you simply invoke the command from your terminal/command prompt, followed by a list of files to analyze:
7
+
8
+ dcov my_file.rb your_file.rb
9
+
10
+ That will spit out a file named coverage.html in the current directory (more formats such as plaintext and PDF to come), which contains information about your documentation coverage.
11
+
12
+
data/Rakefile ADDED
@@ -0,0 +1,120 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+ begin
12
+ require 'spec/rake/spectask'
13
+ rescue LoadError
14
+ puts 'To use rspec for testing you must install rspec gem:'
15
+ puts '$ sudo gem install rspec'
16
+ exit
17
+ end
18
+
19
+ include FileUtils
20
+ require File.join(File.dirname(__FILE__), 'lib', 'dcov', 'version')
21
+
22
+ AUTHOR = 'Jeremy McAnally' # can also be an array of Authors
23
+ EMAIL = "jeremymcanally@gmail.com"
24
+ DESCRIPTION = "A documentation analysis tool for Ruby."
25
+ GEM_NAME = 'dcov' # what ppl will type to install your gem
26
+
27
+ @config_file = "~/.rubyforge/user-config.yml"
28
+ @config = nil
29
+ def rubyforge_username
30
+ unless @config
31
+ begin
32
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
33
+ rescue
34
+ puts <<-EOS
35
+ ERROR: No rubyforge config file found: #{@config_file}"
36
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
37
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
38
+ EOS
39
+ exit
40
+ end
41
+ end
42
+ @rubyforge_username ||= @config["username"]
43
+ end
44
+
45
+ RUBYFORGE_PROJECT = 'dcov' # The unix name for your project
46
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
47
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
48
+
49
+ NAME = "dcov"
50
+ REV = nil
51
+ # UNCOMMENT IF REQUIRED:
52
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
53
+ VERS = Dcov::VERSION::STRING + (REV ? ".#{REV}" : "")
54
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
55
+ RDOC_OPTS = ['--quiet', '--title', 'dcov documentation',
56
+ "--opname", "index.html",
57
+ "--line-numbers",
58
+ "--main", "README",
59
+ "--inline-source"]
60
+
61
+ class Hoe
62
+ def extra_deps
63
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
64
+ end
65
+ end
66
+
67
+ # Generate all the Rake tasks
68
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
69
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
70
+ p.author = AUTHOR
71
+ p.description = DESCRIPTION
72
+ p.email = EMAIL
73
+ p.summary = DESCRIPTION
74
+ p.url = HOMEPATH
75
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
76
+ p.test_globs = ["test/**/test_*.rb"]
77
+ p.clean_globs = CLEAN #An array of file patterns to delete on clean.
78
+
79
+ # == Optional
80
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
81
+ p.extra_deps = [['ruport', '>= 0.0.1']] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
82
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
83
+ end
84
+
85
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
86
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
87
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
88
+
89
+ desc 'Release the website and new gem version'
90
+ task :deploy => [:check_version, :release] do
91
+ puts "Remember to create SVN tag:"
92
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
93
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
94
+ puts "Suggested comment:"
95
+ puts "Tagging release #{CHANGES}"
96
+ end
97
+
98
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
99
+ task :local_deploy => [:install_gem]
100
+
101
+ task :check_version do
102
+ unless ENV['VERSION']
103
+ puts 'Must pass a VERSION=x.y.z release version'
104
+ exit
105
+ end
106
+ unless ENV['VERSION'] == VERS
107
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
108
+ exit
109
+ end
110
+ end
111
+
112
+ desc "Run the specs under spec/models"
113
+ Spec::Rake::SpecTask.new do |t|
114
+ t.spec_opts = ['--color']
115
+ t.spec_files = FileList['spec/*_spec.rb']
116
+ end
117
+
118
+ desc "Default task is to run specs"
119
+ task :default => :spec
120
+
data/bin/dcov ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Jeremy McAnally on 2007-5-28.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ begin
7
+ require 'rubygems'
8
+ rescue LoadError
9
+ # no rubygems to load, so we fail silently
10
+ end
11
+
12
+ require 'optparse'
13
+ require File.dirname(__FILE__) + '/../lib/dcov'
14
+
15
+ # NOTE: the option -p/--path= is given as an example, and should probably be replaced in your application.
16
+
17
+ OPTIONS = {
18
+ :path => Dir.getwd,
19
+ :output_format => 'html',
20
+ :files => ''
21
+ }
22
+ MANDATORY_OPTIONS = %w( )
23
+
24
+ parser = OptionParser.new do |opts|
25
+ opts.banner = <<BANNER
26
+ dcov - A documentation coverage and quality analyzer for Ruby
27
+
28
+ Usage: #{File.basename($0)} [options]
29
+
30
+ Options are:
31
+ BANNER
32
+ opts.separator ""
33
+ opts.on("-p", "--path=PATH", String,
34
+ "The root path for selecting files",
35
+ "Default: current working directory") { |OPTIONS[:path]| }
36
+ opts.on("-f", "--output-format=FORMAT", String,
37
+ "The output format for the coverage report",
38
+ "Default: HTML") { |OPTIONS[:output_format]| }
39
+ opts.on("-h", "--help",
40
+ "Show this help message.") { puts opts; exit }
41
+ opts.parse!(ARGV)
42
+
43
+ if MANDATORY_OPTIONS && MANDATORY_OPTIONS.find { |option| OPTIONS[option.to_sym].nil? }
44
+ puts opts; exit
45
+ end
46
+ end
47
+
48
+ if (ARGV != [])
49
+ OPTIONS[:files] = ARGV
50
+ @coverage = Dcov::Analyzer.new(OPTIONS)
51
+ else
52
+ raise "No files to analyze!"
53
+ end
@@ -0,0 +1,12 @@
1
+ class AnalyzedToken
2
+ attr_accessor :name, :parent
3
+
4
+ def initialize(name, parent)
5
+ @name = name
6
+ @parent = parent
7
+ end
8
+
9
+ def ==(other)
10
+ (self.name == other.name) && (self.parent == other.parent)
11
+ end
12
+ end
@@ -0,0 +1,163 @@
1
+ require 'rdoc/rdoc'
2
+ require 'dcov/analyzed_token.rb'
3
+ require 'dcov/stats'
4
+
5
+ # Makes +stats+ accessible to us so we can inject our own.
6
+ # We also add a +classifier+ attribtue to the code object classes.
7
+ module RDoc
8
+ class RDoc
9
+ attr_accessor :stats
10
+ end
11
+
12
+ class AnyMethod
13
+ def classifier
14
+ :method
15
+ end
16
+ end
17
+
18
+ class NormalClass
19
+ def classifier
20
+ :class
21
+ end
22
+ end
23
+
24
+ class NormalModule
25
+ def classifier
26
+ :module
27
+ end
28
+ end
29
+ end
30
+
31
+ module Dcov
32
+
33
+ # Mocked options object to feed to RDoc
34
+ class RDocOptionsMock
35
+ attr_accessor :files
36
+
37
+ def initialize(file_list)
38
+ @files = file_list
39
+ end
40
+
41
+ def method_missing(*args)
42
+ false
43
+ end
44
+ end
45
+
46
+ # Main class
47
+ class Analyzer
48
+ attr_accessor :classes, :methods, :modules, :hierarchy, :stats
49
+
50
+ # Grab the arguments from the DCov init script or
51
+ # another calling script and feed them to RDoc for
52
+ # parsing.
53
+ def initialize(options)
54
+ @options = options
55
+
56
+ # Setup the analyzed tokens array so we can keep track of which methods we've already
57
+ # taken a look at...
58
+ @analyzed_tokens = []
59
+
60
+ r = RDoc::RDoc.new
61
+
62
+ # Instantiate our little hacked Stats class...
63
+ @stats = Dcov::Stats.new
64
+ r.stats = @stats
65
+
66
+ # Get our analyzers together...
67
+ @analyzers = []
68
+ find_analyzers
69
+
70
+ # Setup any options we need here...
71
+ Options.instance.parse(["--tab-width", 2], {})
72
+
73
+ # We have to use #send because #parse_files is private
74
+ parsed_structure = r.send(:parse_files, RDocOptionsMock.new(options[:files]))
75
+
76
+ # Analyze it, Spiderman!
77
+ analyze parsed_structure
78
+
79
+ # Generate the report!
80
+ generate
81
+ end
82
+
83
+ # Method to initialize analysis of the code; passes
84
+ # structure off to the process method which actually
85
+ # processes the tokens.
86
+ def analyze(hierarchy)
87
+ @hierarchy = hierarchy
88
+ process
89
+
90
+ @stats.print
91
+ end
92
+
93
+ # Method to start walking the hierarchy of tokens,
94
+ # separating them into covered/not covered (and
95
+ # eventually lexing their comments for quality).
96
+ def process
97
+ @hierarchy.each do |hier|
98
+ hier.classes.each do |cls|
99
+ process_token cls
100
+ end
101
+
102
+ hier.modules.each do |mod|
103
+ process_token mod
104
+ end
105
+ end
106
+ end
107
+
108
+ # Method to process all the tokens for a token...recursion FTW! :)
109
+ def process_token(token)
110
+ analyzed_token = AnalyzedToken.new(token.name, token.parent)
111
+ unless @analyzed_tokens.include?(analyzed_token)
112
+ expose_to_analyzers(token)
113
+ @analyzed_tokens << analyzed_token
114
+
115
+ [:method_list, :classes, :modules].each do |meth, type|
116
+ token.send(meth).each do |item|
117
+ process_token item
118
+ end if token.respond_to?(meth)
119
+ end
120
+ end
121
+ end
122
+
123
+ # Generate the output based on the format specified
124
+ # TODO: Have an argument sanity check at startup to make sure we actually have a generator for the format
125
+ def generate
126
+ print "Generating report..."
127
+ require File.dirname(__FILE__) + "/generators/#{@options[:output_format]}/generator.rb"
128
+ report = @stats.as(@options[:output_format].to_sym)
129
+ print "done.\n"
130
+
131
+ print "Writing report..."
132
+ if (!File.exists?("#{@options[:path]}/coverage.html")) || (File.writable?("#{@options[:path]}/coverage.html"))
133
+ output_file = File.open("#{@options[:path]}/coverage.html", "w")
134
+ output_file.write report
135
+ output_file.close
136
+ print "done.\n"
137
+ else
138
+ raise "Can't write to [#{@options[:path]}/coverage.html]."
139
+ end
140
+ end
141
+
142
+ # Grok the analyzers directory and find all analyzers
143
+ def find_analyzers
144
+ Dir::entries(File.dirname(__FILE__) + "/analyzers").each do |analyzer|
145
+ next unless /(\w+)_analyzer.rb$/ =~ analyzer
146
+ type = $1
147
+
148
+ require File.dirname(__FILE__) + "/analyzers/#{analyzer}"
149
+ @analyzers << "#{type.capitalize}Analyzer".intern
150
+ end
151
+ end
152
+
153
+ # Fire off the token to each analyzer, letting it have its way with
154
+ # the token and the Stats instance.
155
+ def expose_to_analyzers(token)
156
+ @analyzers.each do |analyzer|
157
+ classifier = token.classifier
158
+
159
+ Dcov::Analyzers.const_get(analyzer).analyze(token, classifier, @stats)
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,9 @@
1
+ module Dcov
2
+ module Analyzers
3
+ class CoverageAnalyzer
4
+ def self.analyze(token, classifier, stats)
5
+ (token.comment.nil? || token.comment == '') ? stats.coverage[classifier][:not_covered] << token : stats.coverage[classifier][:covered] << token
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ module Dcov
2
+ # Generates HTML output
3
+ class Generator < Ruport::Formatter::HTML
4
+ renders :html, :for => StatsRenderer
5
+
6
+ def build_stats_header
7
+ output << "<html><head><title>dcov results</title>"+
8
+ "</head><body>\n<h1>dcov results</h1>\n\n"
9
+ end
10
+
11
+ def build_stats_body
12
+ output << "<p>\n"
13
+ output << "Class coverage: <b>#{class_coverage}%</b><br>\n"
14
+ output << "Module coverage: <b>#{module_coverage}%</b><br>\n"
15
+ output << "Method coverage: <b>#{method_coverage}%</b><br>\n"
16
+ output << "</p>\n\n"
17
+ output << "<ul>\n"
18
+
19
+ data[:structured].each do |key,value|
20
+ output << ((value[0].comment.nil? || value[0].comment == '') ?
21
+ "\t<li><font color='#f00;'>#{key}</font>\n\t\t<ul>\n" :
22
+ "\t<li> #{key}\n\t\t<ul>")
23
+
24
+ value[1].each do |itm|
25
+ output << ((itm.comment.nil? || itm.comment == '') ?
26
+ "\t\t\t<li><font color='#f00;'>#{itm.name}</font></li>\n" :
27
+ "\t\t\t<li> #{itm.name}</li>\n")
28
+ end
29
+
30
+ output << "\t\t</ul>\n\t</li>\n\n"
31
+ end
32
+ end
33
+
34
+ def build_stats_footer
35
+ output << "\n\n</body></html>"
36
+ end
37
+
38
+ end
39
+ end
data/lib/dcov/stats.rb ADDED
@@ -0,0 +1,106 @@
1
+ require 'rubygems'
2
+ require 'ruport'
3
+
4
+ module Dcov
5
+ class StatsRenderer < Ruport::Renderer
6
+
7
+ stage :stats_header, :stats_body, :stats_footer
8
+
9
+ module Helpers
10
+
11
+ def class_coverage
12
+ data[:coverage_rating][:class]
13
+ end
14
+
15
+ def module_coverage
16
+ data[:coverage_rating][:module]
17
+ end
18
+
19
+ def method_coverage
20
+ data[:coverage_rating][:method]
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ class TopLevel
27
+ attr_accessor :comment
28
+ end
29
+
30
+ # RDoc's Stats class with our own stats thrown in and our own custom
31
+ # print method.
32
+ class Stats
33
+
34
+ attr_accessor :num_files, :num_classes, :num_modules, :num_methods, :coverage
35
+
36
+ include Ruport::Renderer::Hooks
37
+ renders_with Dcov::StatsRenderer
38
+
39
+ def initialize
40
+ @num_files = @num_classes = @num_modules = @num_methods = 0
41
+ @start = Time.now
42
+
43
+ @coverage = { }
44
+
45
+ [:class, :method, :module].each do |type|
46
+ @coverage[type] = { :covered => [], :not_covered => [] }
47
+ end
48
+ end
49
+
50
+ # Print out the coverage rating
51
+ def print
52
+ puts "\n\nFiles: #{@num_files}"
53
+ puts "Total Classes: #{@num_classes}"
54
+ puts "Total Modules: #{@num_modules}"
55
+ puts "Total Methods: #{@num_methods}"
56
+ puts "\n\n\n"
57
+ puts "Class coverage: #{coverage_rating(:class)}%\n"
58
+ puts "\tNot covered: #{@coverage[:class][:not_covered].map{|itm| itm.name}.join(', ')}\n\n"
59
+
60
+ puts "Method coverage: #{coverage_rating(:method)}%\n"
61
+ puts "\tNot covered: #{@coverage[:method][:not_covered].map{|itm| itm.parent.name + "#" + itm.name}.join(', ')}\n\n"
62
+
63
+ puts "Module coverage: #{coverage_rating(:module)}%"
64
+ puts "\tNot covered: #{@coverage[:module][:not_covered].map{|itm| itm.name}.join(', ')}\n\n"
65
+ end
66
+
67
+ # Get the coverage rating (e.g., 34%) for the tokens of the type specified
68
+ def coverage_rating(tokens)
69
+ rating = ((@coverage[tokens][:covered].size.to_f / (@coverage[tokens][:covered].size.to_f + @coverage[tokens][:not_covered].size.to_f)) * 100)
70
+
71
+ # If it's NaN (for whatever reason, be it an irrational or irrationally small number), return 0
72
+ (rating.nan?) ? 0 : rating.to_i
73
+ end
74
+
75
+ def renderable_data
76
+ data = {}
77
+ data[:coverage_rating] = {}
78
+
79
+ [:class, :method, :module].each do |token|
80
+ data[:coverage_rating][token] = coverage_rating(token)
81
+ end
82
+
83
+ classes = @coverage[:class][:not_covered] + @coverage[:class][:covered]
84
+ modules = @coverage[:module][:not_covered] + @coverage[:module][:covered]
85
+ methods = @coverage[:method][:not_covered] + @coverage[:method][:covered]
86
+
87
+ # Create a properly nested structure
88
+ data[:structured] = {}
89
+ classes.each {|cls| data[:structured][cls.name] = [cls, []]}
90
+ modules.each {|mod| data[:structured][mod.name] = [mod, []]}
91
+ data[:structured]['[Toplevel]'] = [TopLevel.new, []]
92
+
93
+ methods.each do |method|
94
+ if (data[:structured].has_key?(method.parent.name))
95
+ data[:structured][method.parent.name][1] << method
96
+ else
97
+ data[:structured]['[Toplevel]'][1] << method
98
+ end
99
+ end
100
+
101
+ data[:analyzed] = @coverage
102
+
103
+ data
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,9 @@
1
+ module Dcov #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/dcov.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Dcov
2
+ end
3
+
4
+ require 'dcov/version.rb'
5
+ require 'dcov/analyzer.rb'