code_statistics 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{code_statistics}
8
- s.version = "0.1.9"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dan Mayer"]
12
- s.date = %q{2009-11-09}
12
+ s.date = %q{2009-11-15}
13
13
  s.description = %q{"This is a port of the rails 'rake stats' method so it can be made more robust and work for non rails projects. New features may eventually be added as well."}
14
14
  s.email = %q{dan@devver.net}
15
15
  s.extra_rdoc_files = [
@@ -1,38 +1,45 @@
1
1
  module CodeStatistics
2
2
  class CodeStatistics #:nodoc:
3
-
4
- TEST_TYPES = %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests)
5
-
6
-
3
+
4
+ attr_reader :print_buffer
5
+
7
6
  def initialize(*pairs)
8
- @pairs = pairs
9
- @test_types = []
10
- directory = Dir.pwd
11
- @specs_found = false
7
+ @pairs = pairs
8
+ @test_types = []
9
+ @print_buffer = ""
10
+ directory = Dir.pwd
12
11
 
13
- #if tests weren't broken up into test/unit functional etc, add the root test directory
14
- if local_file_exists?(directory, 'test') &&
15
- (!local_file_exists?(directory, 'test/unit') &&
16
- !local_file_exists?(directory, 'test/functional') &&
17
- !local_file_exists?(directory, 'test/integration'))
18
- @pairs << %w(Tests test)
19
- end
12
+ directories_to_search = ['app','test','spec','merb','features', 'bin']
13
+ recursively_add_directories(directories_to_search)
20
14
 
21
15
  @pairs.each do |key, dir_path|
22
- add_test_type(key) if dir_path.match(/^test\//) || dir_path.match(/^spec\//)
23
- end
24
-
25
- #if spec tests weren't broken up into smaller test directories add the root spec directory
26
- if @specs_found==false && local_file_exists?(directory, 'spec')
27
- @pairs << %w(Specs spec)
28
- add_test_type("Specs")
16
+ add_test_type(key) if dir_path.match(/^test/) || dir_path.match(/^spec/) || dir_path.match(/^features/)
29
17
  end
30
18
 
31
19
  @statistics = calculate_statistics
32
20
  @total = calculate_total if pairs.length > 1
33
21
  end
22
+
23
+ def recursively_add_directories(dirs)
24
+ dirs.each do |dir|
25
+ if File.directory?(dir)
26
+ entries = Dir.entries(dir)
27
+ entries = entries.reject{ |entry| entry=='.' || entry=='..' }
28
+ has_directories = false
29
+ entries.each do |entry|
30
+ entry_path = File.join(dir,entry)
31
+ if File.directory?(entry_path)
32
+ @pairs << [entry_path.capitalize, entry_path]
33
+ has_directories = true
34
+ end
35
+ end
36
+ @pairs << [dir.capitalize, dir] unless has_directories
37
+ end
38
+ end
39
+ end
34
40
 
35
41
  def to_s
42
+ @print_buffer = ''
36
43
  print_header
37
44
  @pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
38
45
  print_splitter
@@ -43,6 +50,7 @@ module CodeStatistics
43
50
  end
44
51
 
45
52
  print_code_test_stats
53
+ @print_buffer
46
54
  end
47
55
 
48
56
  private
@@ -55,11 +63,10 @@ module CodeStatistics
55
63
  end
56
64
 
57
65
  def test_types
58
- (TEST_TYPES + @test_types).uniq
66
+ @test_types.uniq
59
67
  end
60
68
 
61
69
  def add_test_type(test_type)
62
- @specs_found = true if test_type.match(/spec/i)
63
70
  @test_types << test_type
64
71
  end
65
72
 
@@ -107,12 +114,12 @@ module CodeStatistics
107
114
 
108
115
  def print_header
109
116
  print_splitter
110
- puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
117
+ @print_buffer << "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |\n"
111
118
  print_splitter
112
119
  end
113
120
 
114
121
  def print_splitter
115
- puts "+----------------------+-------+-------+---------+---------+-----+-------+"
122
+ @print_buffer << "+----------------------+-------+-------+---------+---------+-----+-------+\n"
116
123
  end
117
124
 
118
125
  def print_line(name, statistics)
@@ -125,13 +132,13 @@ module CodeStatistics
125
132
  "| #{name.ljust(20)} "
126
133
  end
127
134
 
128
- puts start +
135
+ @print_buffer << start +
129
136
  "| #{statistics["lines"].to_s.rjust(5)} " +
130
137
  "| #{statistics["codelines"].to_s.rjust(5)} " +
131
138
  "| #{statistics["classes"].to_s.rjust(7)} " +
132
139
  "| #{statistics["methods"].to_s.rjust(7)} " +
133
140
  "| #{m_over_c.to_s.rjust(3)} " +
134
- "| #{loc_over_m.to_s.rjust(5)} |"
141
+ "| #{loc_over_m.to_s.rjust(5)} |\n"
135
142
  end
136
143
 
137
144
  def print_code_test_stats
@@ -143,8 +150,8 @@ module CodeStatistics
143
150
  else
144
151
  "0.0"
145
152
  end
146
- puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{ratio}"
147
- puts ""
153
+ @print_buffer << " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{ratio}\n"
154
+ @print_buffer << "\n"
148
155
  end
149
156
  end
150
157
  end
@@ -1,23 +1,7 @@
1
- #todo for both spec and test look through top level add any directory seperately
2
- #get rid of the hard coded test/units / etc in this file and the lib file.
3
1
  stats_directories = [
4
- %w(Controllers app/controllers),
5
- %w(Helpers app/helpers),
6
- %w(Models app/models),
7
2
  %w(Libraries lib/),
8
- %w(APIs app/apis),
9
- %w(Integration\ tests test/integration),
10
- %w(Functional\ tests test/functional),
11
- %w(Unit\ tests test/unit),
12
- %w(Model\ specs spec/models),
13
- %w(View\ specs spec/views),
14
- %w(Controller\ specs spec/controllers),
15
- %w(Helper\ specs spec/helpers),
16
- %w(Library\ specs spec/lib),
17
- %w(Routing\ specs spec/routing),
18
- %w(Integration\ specs spec/integration),
19
- %w(Public\ specs spec/public),
20
- %w(Semipublic\ specs spec/semipublic)
3
+ %w(Source source/),
4
+ %w(Src src/),
21
5
  ].collect { |name, dir| [ name, "#{Dir.pwd}/#{dir}" ] }.select { |name, dir| File.directory?(dir) }
22
6
 
23
7
  if ENV['DIRECTORIES_TO_CALCULATE']
@@ -32,5 +16,12 @@ end
32
16
  desc "Report code statistics (KLOCs, etc) from the application"
33
17
  task :stats do
34
18
  require File.join(File.dirname(__FILE__), '..', 'code_statistics', 'code_statistics')
35
- CodeStatistics::CodeStatistics.new(*stats_directories).to_s
19
+ puts CodeStatistics::CodeStatistics.new(*stats_directories).to_s
20
+ end
21
+
22
+ #this is for apps that already had a stats task, but want to use the newer features of this gem
23
+ desc "Report code statistics (KLOCs, etc) from the application"
24
+ task :code_stats do
25
+ require File.join(File.dirname(__FILE__), '..', 'code_statistics', 'code_statistics')
26
+ puts CodeStatistics::CodeStatistics.new(*stats_directories).to_s
36
27
  end
@@ -1,8 +1,110 @@
1
- #require 'test_helper'
2
1
  require File.expand_path("test_helper", File.dirname(__FILE__))
3
2
 
4
3
  class CodeStatisticsTest < Test::Unit::TestCase
5
- should "probably rename this file and start testing for real" do
6
- flunk "hey buddy, you should probably rename this file and start testing for real"
4
+
5
+ include Construct::Helpers
6
+
7
+ should "find passed in directory" do
8
+ within_construct do |construct|
9
+ dir = construct.directory("lib")
10
+ file = dir.file("real.rb", "this\nis\n\lame\n")
11
+ controllers_dir = dir.directory("controllers")
12
+ file = controllers_dir.file("fake.rb", "this\nis\n\lame\n")
13
+ code_stats = CodeStatistics::CodeStatistics.new(["Libraries", 'lib'])
14
+ assert code_stats.to_s.match(/Libraries/)
15
+ assert code_stats.to_s.match(/Code LOC: 6/)
16
+ end
17
+ end
18
+
19
+ should "find app controllers directory" do
20
+ within_construct do |construct|
21
+ dir = construct.directory("app")
22
+ controllers_dir = dir.directory("controllers")
23
+ file = controllers_dir.file("fake.rb", "this\nis\n\lame\n")
24
+ code_stats = CodeStatistics::CodeStatistics.new()
25
+ assert code_stats.to_s.match(/App\/controllers/)
26
+ assert code_stats.to_s.match(/Code LOC: 3/)
27
+ end
28
+ end
29
+
30
+ should "find app non rails directory" do
31
+ within_construct do |construct|
32
+ dir = construct.directory("app")
33
+ sub_dir = dir.directory("servers")
34
+ file = sub_dir.file("fake.rb", "this\nis\n\lame\n")
35
+ code_stats = CodeStatistics::CodeStatistics.new()
36
+ assert code_stats.to_s.match(/App\/servers/)
37
+ assert code_stats.to_s.match(/Code LOC: 3/)
38
+ end
39
+ end
40
+
41
+ should "add spec sub directories and count as test code" do
42
+ within_construct do |construct|
43
+ dir = construct.directory("spec")
44
+ sub_dir = dir.directory("models")
45
+ file = sub_dir.file("fake.rb", "this\nis\n\lame\n")
46
+ sub_dir2 = dir.directory("controllers")
47
+ file = sub_dir2.file("fake.rb", "this\nis\n\lame\n")
48
+ code_stats = CodeStatistics::CodeStatistics.new()
49
+ assert code_stats.to_s.match(/Spec\/models/)
50
+ assert code_stats.to_s.match(/Spec\/controllers/)
51
+ assert code_stats.to_s.match(/Test LOC: 6/)
52
+ end
53
+ end
54
+
55
+ should "add spec root directory and count as test code" do
56
+ within_construct do |construct|
57
+ dir = construct.directory("spec")
58
+ file = dir.file("fake.rb", "this\nis\n\lame\n")
59
+ code_stats = CodeStatistics::CodeStatistics.new()
60
+ assert code_stats.to_s.match(/Spec/)
61
+ assert code_stats.to_s.match(/Test LOC: 3/)
62
+ end
7
63
  end
64
+
65
+ should "add test sub directories and count as test code" do
66
+ within_construct do |construct|
67
+ dir = construct.directory("test")
68
+ sub_dir = dir.directory("models")
69
+ file = sub_dir.file("fake.rb", "this\nis\n\lame\n")
70
+ sub_dir2 = dir.directory("controllers")
71
+ file = sub_dir2.file("fake.rb", "this\nis\n\lame\n")
72
+ code_stats = CodeStatistics::CodeStatistics.new()
73
+ assert code_stats.to_s.match(/Test\/models/)
74
+ assert code_stats.to_s.match(/Test\/controllers/)
75
+ assert code_stats.to_s.match(/Test LOC: 6/)
76
+ end
77
+ end
78
+
79
+ should "add test root directory and count as test code" do
80
+ within_construct do |construct|
81
+ dir = construct.directory("test")
82
+ file = dir.file("fake.rb", "this\nis\n\lame\n")
83
+ code_stats = CodeStatistics::CodeStatistics.new()
84
+ assert code_stats.to_s.match(/Test/)
85
+ assert code_stats.to_s.match(/Test LOC: 3/)
86
+ end
87
+ end
88
+
89
+ should "calculate correct test to code ratio" do
90
+ within_construct do |construct|
91
+ dir = construct.directory("app")
92
+ sub_dir = dir.directory("models")
93
+ file = sub_dir.file("fake.rb", "this\n"*9)
94
+ sub_dir2 = dir.directory("controllers")
95
+ file = sub_dir2.file("fake.rb", "this\nis\n\lame\n")
96
+
97
+ dir = construct.directory("test")
98
+ sub_dir = dir.directory("models")
99
+ file = sub_dir.file("fake.rb", "this\nis\n\lame\n")
100
+ sub_dir2 = dir.directory("controllers")
101
+ file = sub_dir2.file("fake.rb", "this\nis\n\lame\n")
102
+ code_stats = CodeStatistics::CodeStatistics.new()
103
+ assert code_stats.to_s.match(/Code LOC: 12/)
104
+ assert code_stats.to_s.match(/Test LOC: 6/)
105
+ assert code_stats.to_s.match(/Code to Test Ratio: 1:0.5/)
106
+ end
107
+ end
108
+
109
+
8
110
  end
data/test/test_helper.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
+ require 'construct'
4
5
 
5
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'code_statistics'
8
+ require 'code_statistics/code_statistics'
8
9
 
9
10
  class Test::Unit::TestCase
10
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_statistics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Mayer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-09 00:00:00 -05:00
12
+ date: 2009-11-15 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency