Permalink
Browse files

Fixed "rake stats" to work with sub-directories in models and control…

…lers and to report the code to test ration [Scott Baron]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@245 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent dcc4868 commit ebf2b12b07641cbdf07218d2661a721ebab6092f @dhh dhh committed Dec 22, 2004
Showing with 42 additions and 7 deletions.
  1. +2 −0 railties/CHANGELOG
  2. +4 −4 railties/fresh_rakefile
  3. +36 −3 railties/lib/code_statistics.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* Fixed "rake stats" to work with sub-directories in models and controllers and to report the code to test ration [Scott Baron]
+
* Added that Active Record associations are now reloaded instead of cleared to work with the new const_missing hook in Active Record.
* Added graceful handling of an inaccessible log file by redirecting output to STDERR with a warning #330 [rainmkr]
@@ -63,11 +63,11 @@ Rake::RDocTask.new("apidoc") { |rdoc|
desc "Report code statistics (KLOCs, etc) from the application"
task :stats do
CodeStatistics.new(
- ["Controllers", "app/controllers"],
["Helpers", "app/helpers"],
+ ["Controllers", "app/controllers"],
+ ["Functionals", "test/functional"],
["Models", "app/models"],
- ["Units", "test/unit"],
- ["Functionals", "test/functional"]
+ ["Units", "test/unit"]
).to_s
end
@@ -103,4 +103,4 @@ task :purge_test_database do
`dropdb -U #{ActiveRecord::Base.configurations["test"]["username"]} #{ActiveRecord::Base.configurations["test"]["database"]}`
`createdb -U #{ActiveRecord::Base.configurations["test"]["username"]} #{ActiveRecord::Base.configurations["test"]["database"]}`
end
-end
+end
@@ -7,13 +7,15 @@ def initialize(*pairs)
def to_s
print_header
- @statistics.each{ |k, v| print_line(k, v) }
+ @pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
print_splitter
if @total
print_line("Total", @total)
print_splitter
end
+
+ print_code_test_stats
end
private
@@ -25,6 +27,11 @@ def calculate_directory_statistics(directory, pattern = /.*rb/)
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
Dir.foreach(directory) do |file_name|
+ if File.stat(directory + "/" + file_name).directory? and (/^\./ !~ file_name)
+ newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
+ stats.each { |k, v| stats[k] += newstats[k] }
+ end
+
next unless file_name =~ pattern
f = File.open(directory + "/" + file_name)
@@ -46,6 +53,18 @@ def calculate_total
total
end
+ def calculate_code
+ code_loc = 0
+ @statistics.each { |k, v| code_loc += v['codelines'] unless ['Units', 'Functionals'].include? k }
+ code_loc
+ end
+
+ def calculate_tests
+ test_loc = 0
+ @statistics.each { |k, v| test_loc += v['codelines'] if ['Units', 'Functionals'].include? k }
+ test_loc
+ end
+
def print_header
print_splitter
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
@@ -60,12 +79,26 @@ def print_line(name, statistics)
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
- puts "| #{name.ljust(20)} " +
+ start = if ['Units', 'Functionals'].include? name
+ "| #{name.ljust(18)} "
+ else
+ "| #{name.ljust(20)} "
+ end
+
+ puts start +
"| #{statistics["lines"].to_s.rjust(5)} " +
"| #{statistics["codelines"].to_s.rjust(5)} " +
"| #{statistics["classes"].to_s.rjust(7)} " +
"| #{statistics["methods"].to_s.rjust(7)} " +
"| #{m_over_c.to_s.rjust(3)} " +
"| #{loc_over_m.to_s.rjust(5)} |"
end
-end
+
+ def print_code_test_stats
+ code = calculate_code
+ tests = calculate_tests
+
+ puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: #{sprintf("%.1f", code/tests.to_f)}:1"
+ puts ""
+ end
+ end

0 comments on commit ebf2b12

Please sign in to comment.