Fixed "rake stats" to work with sub-directories in models and controllers 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
This commit is contained in:
David Heinemeier Hansson 2004-12-22 13:08:26 +00:00
parent dcc486805e
commit ebf2b12b07
3 changed files with 42 additions and 7 deletions

View File

@ -1,5 +1,7 @@
*SVN* *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 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] * Added graceful handling of an inaccessible log file by redirecting output to STDERR with a warning #330 [rainmkr]

View File

@ -63,11 +63,11 @@ Rake::RDocTask.new("apidoc") { |rdoc|
desc "Report code statistics (KLOCs, etc) from the application" desc "Report code statistics (KLOCs, etc) from the application"
task :stats do task :stats do
CodeStatistics.new( CodeStatistics.new(
["Controllers", "app/controllers"],
["Helpers", "app/helpers"], ["Helpers", "app/helpers"],
["Controllers", "app/controllers"],
["Functionals", "test/functional"],
["Models", "app/models"], ["Models", "app/models"],
["Units", "test/unit"], ["Units", "test/unit"]
["Functionals", "test/functional"]
).to_s ).to_s
end end
@ -103,4 +103,4 @@ task :purge_test_database do
`dropdb -U #{ActiveRecord::Base.configurations["test"]["username"]} #{ActiveRecord::Base.configurations["test"]["database"]}` `dropdb -U #{ActiveRecord::Base.configurations["test"]["username"]} #{ActiveRecord::Base.configurations["test"]["database"]}`
`createdb -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 end

View File

@ -7,13 +7,15 @@ class CodeStatistics
def to_s def to_s
print_header print_header
@statistics.each{ |k, v| print_line(k, v) } @pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
print_splitter print_splitter
if @total if @total
print_line("Total", @total) print_line("Total", @total)
print_splitter print_splitter
end end
print_code_test_stats
end end
private private
@ -25,6 +27,11 @@ class CodeStatistics
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 } stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
Dir.foreach(directory) do |file_name| 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 next unless file_name =~ pattern
f = File.open(directory + "/" + file_name) f = File.open(directory + "/" + file_name)
@ -46,6 +53,18 @@ class CodeStatistics
total total
end 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 def print_header
print_splitter print_splitter
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |" puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
@ -60,7 +79,13 @@ class CodeStatistics
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0 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 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["lines"].to_s.rjust(5)} " +
"| #{statistics["codelines"].to_s.rjust(5)} " + "| #{statistics["codelines"].to_s.rjust(5)} " +
"| #{statistics["classes"].to_s.rjust(7)} " + "| #{statistics["classes"].to_s.rjust(7)} " +
@ -68,4 +93,12 @@ class CodeStatistics
"| #{m_over_c.to_s.rjust(3)} " + "| #{m_over_c.to_s.rjust(3)} " +
"| #{loc_over_m.to_s.rjust(5)} |" "| #{loc_over_m.to_s.rjust(5)} |"
end 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