diff --git a/railties/CHANGELOG b/railties/CHANGELOG index a0e402e5e17..38f4cb1928e 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -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] diff --git a/railties/fresh_rakefile b/railties/fresh_rakefile index e9f3afd7ae9..437c9995226 100755 --- a/railties/fresh_rakefile +++ b/railties/fresh_rakefile @@ -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 \ No newline at end of file +end diff --git a/railties/lib/code_statistics.rb b/railties/lib/code_statistics.rb index 53e7feb1c08..825772fcf19 100644 --- a/railties/lib/code_statistics.rb +++ b/railties/lib/code_statistics.rb @@ -7,13 +7,15 @@ class CodeStatistics 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 @@ class CodeStatistics 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 @@ class CodeStatistics 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,7 +79,13 @@ class CodeStatistics 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)} " + @@ -68,4 +93,12 @@ class CodeStatistics "| #{m_over_c.to_s.rjust(3)} " + "| #{loc_over_m.to_s.rjust(5)} |" end -end \ No newline at end of file + + 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