Skip to content
Newer
Older
100644 115 lines (93 sloc) 3.36 KB
ae294af @jamis Documentation updates/fixes for railties
jamis authored
1 class CodeStatistics #:nodoc:
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
2
1a91abe @jamis Add integration test support to app generation and testing
jamis authored
3 TEST_TYPES = %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests)
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
4
db045db @dhh Initial
dhh authored
5 def initialize(*pairs)
6 @pairs = pairs
7 @statistics = calculate_statistics
8 @total = calculate_total if pairs.length > 1
9 end
10
11 def to_s
12 print_header
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
13 @pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
db045db @dhh Initial
dhh authored
14 print_splitter
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
15
db045db @dhh Initial
dhh authored
16 if @total
17 print_line("Total", @total)
18 print_splitter
19 end
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
20
21 print_code_test_stats
db045db @dhh Initial
dhh authored
22 end
23
24 private
25 def calculate_statistics
63039b9 @miloops Fix typo and add sanity test for code statistics rake task.
miloops authored
26 Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
db045db @dhh Initial
dhh authored
27 end
28
e1024e5 #2261 was supposed to have been closed as wontfix, not sure why it wa…
Scott Barron authored
29 def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
db045db @dhh Initial
dhh authored
30 stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
31
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
32 Dir.foreach(directory) do |file_name|
624b118 @thedarkone Use shorter class-level File methods instead of going through File.stat.
thedarkone authored
33 if File.directory?(directory + "/" + file_name) and (/^\./ !~ file_name)
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
34 newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
35 stats.each { |k, v| stats[k] += newstats[k] }
36 end
37
db045db @dhh Initial
dhh authored
38 next unless file_name =~ pattern
39
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
40 comment_started = false
6831ab1 @rahul100885 Used block to make sure file get auto closed after use
rahul100885 authored
41
42 File.open(directory + "/" + file_name) do |f|
43 while line = f.gets
44 stats["lines"] += 1
45 if(comment_started)
46 if line =~ /^=end/
47 comment_started = false
48 end
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
49 next
6831ab1 @rahul100885 Used block to make sure file get auto closed after use
rahul100885 authored
50 else
51 if line =~ /^=begin/
52 comment_started = true
53 next
54 end
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
55 end
6831ab1 @rahul100885 Used block to make sure file get auto closed after use
rahul100885 authored
56 stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/
57 stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/
58 stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
59 end
db045db @dhh Initial
dhh authored
60 end
61 end
62
63 stats
64 end
65
66 def calculate_total
67 total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
68 @statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
69 total
70 end
71
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
72 def calculate_code
73 code_loc = 0
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
74 @statistics.each { |k, v| code_loc += v['codelines'] unless TEST_TYPES.include? k }
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
75 code_loc
76 end
77
78 def calculate_tests
79 test_loc = 0
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
80 @statistics.each { |k, v| test_loc += v['codelines'] if TEST_TYPES.include? k }
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
81 test_loc
82 end
83
db045db @dhh Initial
dhh authored
84 def print_header
85 print_splitter
86 puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
87 print_splitter
88 end
89
90 def print_splitter
91 puts "+----------------------+-------+-------+---------+---------+-----+-------+"
92 end
93
94 def print_line(name, statistics)
95 m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
96 loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
97
68926dd @phiggins Remove useless conditional.
phiggins authored
98 puts "| #{name.ljust(20)} " +
db045db @dhh Initial
dhh authored
99 "| #{statistics["lines"].to_s.rjust(5)} " +
100 "| #{statistics["codelines"].to_s.rjust(5)} " +
101 "| #{statistics["classes"].to_s.rjust(7)} " +
102 "| #{statistics["methods"].to_s.rjust(7)} " +
103 "| #{m_over_c.to_s.rjust(3)} " +
104 "| #{loc_over_m.to_s.rjust(5)} |"
105 end
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
106
107 def print_code_test_stats
108 code = calculate_code
109 tests = calculate_tests
110
2aac6d9 @dhh Flipped code-to-test ratio around to be more readable #468 [Scott Baron]
dhh authored
111 puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
112 puts ""
113 end
548e5ba @vijaydev fix indentation
vijaydev authored
114 end
Something went wrong with that request. Please try again.