Skip to content

Commit 23018bc

Browse files
authored
Merge pull request #21 from fastruby/feature/bundler-stats
Add dependency on bundler-stats and improve output
2 parents cbeff8c + 601594d commit 23018bc

File tree

13 files changed

+161
-31
lines changed

13 files changed

+161
-31
lines changed

.github/workflows/test.yml

+20-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ on:
88
pull_request:
99
branches:
1010
- main
11-
env:
12-
COVERAGE: true
13-
CODECOV_TOKEN: d7028c0e-97c5-485f-85e5-f63daabeef63
1411
jobs:
1512
test-ruby-2-4-x:
1613
runs-on: ubuntu-latest
@@ -23,6 +20,10 @@ jobs:
2320
ruby-version: 2.4
2421
bundler-cache: true
2522
- name: Build and run tests
23+
env:
24+
COVERAGE: true
25+
TERM: xterm
26+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2627
run: |
2728
gem install bundler
2829
bundle install --jobs 4 --retry 3
@@ -38,6 +39,10 @@ jobs:
3839
ruby-version: 2.5
3940
bundler-cache: true
4041
- name: Build and run tests
42+
env:
43+
COVERAGE: true
44+
TERM: xterm
45+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
4146
run: |
4247
gem install bundler
4348
bundle install --jobs 4 --retry 3
@@ -53,6 +58,10 @@ jobs:
5358
ruby-version: 2.6
5459
bundler-cache: true
5560
- name: Build and run tests
61+
env:
62+
COVERAGE: true
63+
TERM: xterm
64+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5665
run: |
5766
gem install bundler
5867
bundle install --jobs 4 --retry 3
@@ -68,6 +77,10 @@ jobs:
6877
ruby-version: 2.7
6978
bundler-cache: true
7079
- name: Build and run tests
80+
env:
81+
COVERAGE: true
82+
TERM: xterm
83+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
7184
run: |
7285
gem install bundler
7386
bundle install --jobs 4 --retry 3
@@ -83,6 +96,10 @@ jobs:
8396
ruby-version: 3.0
8497
bundler-cache: true
8598
- name: Build and run tests
99+
env:
100+
COVERAGE: true
101+
TERM: xterm
102+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
86103
run: |
87104
gem install bundler
88105
bundle install --jobs 4 --retry 3

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
.byebug_history
12
*.gem
23
*.rbc
34
.bundle
45
.config
6+
.ruby-version
57
.yardoc
68
Gemfile.lock
79
InstalledFiles

Gemfile

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
source 'https://rubygems.org'
1+
source "https://rubygems.org"
22

3-
# Specify your gem's dependencies in rails_stats.gemspec
43
gemspec
4+
5+
group :development, :test do
6+
gem "bundler", ">= 1.6", "< 3.0"
7+
gem "byebug"
8+
gem "codecov"
9+
gem "minitest"
10+
gem "minitest-around"
11+
gem "minitest-spec-context"
12+
gem "simplecov"
13+
gem "simplecov-console"
14+
end

lib/rails_stats/app_statistics.rb

+9-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ class AppStatistics
33
attr_reader :statistics, :total, :test
44

55
def initialize(directory)
6+
@directories = []
67
@test = false
7-
@directory = directory
8-
@statistics = calculate_statistics
9-
@total = calculate_total
8+
@directory = directory
9+
@statistics = calculate_statistics
10+
@total = calculate_total
1011
end
1112

1213
def key_concepts
@@ -28,15 +29,15 @@ def calculate_statistics
2829
end
2930

3031
def directories
31-
return @directories if @directories
32-
out = []
32+
return @directories if @directories.any?
33+
3334
Dir.foreach(@directory) do |file_name|
3435
path = File.join(@directory, file_name)
3536
next unless File.directory?(path)
3637
next if (/^\./ =~ file_name)
3738
next if file_name == "assets" # doing separately
3839
next if file_name == "views" # TODO
39-
out << path
40+
@directories << path
4041
end
4142

4243
assets = File.join(@directory, "assets")
@@ -48,13 +49,13 @@ def directories
4849

4950
case file_name
5051
when "javascripts"
51-
out << path
52+
@directories << path
5253
# TODO when "css"
5354
end
5455
end
5556
end
5657

57-
out
58+
@directories
5859
end
5960
end
6061

lib/rails_stats/console_formatter.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
require "bundler/stats/cli"
2+
13
module RailsStats
24
class ConsoleFormatter < StatsFormatter
35
def to_s
6+
Bundler::Stats::CLI.start
7+
48
print_header
59
sorted_keys = @statistics.keys.sort
610
sorted_keys.each { |key| print_line(key, @statistics[key]) }

rails_stats.gemspec

+1-8
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,5 @@ Gem::Specification.new do |spec|
1919
spec.require_paths = ["lib"]
2020

2121
spec.add_dependency "rake"
22-
spec.add_development_dependency "bundler", ">= 1.6", "< 3.0"
23-
spec.add_development_dependency "byebug"
24-
spec.add_development_dependency "codecov"
25-
spec.add_development_dependency "minitest"
26-
spec.add_development_dependency "minitest-around"
27-
spec.add_development_dependency "minitest-spec-context"
28-
spec.add_development_dependency "simplecov"
29-
spec.add_development_dependency "simplecov-console"
22+
spec.add_dependency "bundler-stats", ">= 2.1"
3023
end

test/dummy/lib/monkeypatches.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "Monkeypatches go here"

test/dummy/spec/models/user_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class FooBar
2+
3+
end
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "This is a spec support file"

test/dummy/test/models/user_test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class UserTest
2+
end
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "This is a test support file"

test/lib/rails_stats/code_statistics_test.rb

+69-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,20 @@
55
describe RailsStats::CodeStatistics do
66
describe "#to_s" do
77
TABLE = <<~EOS
8-
+----------------------+---------+---------+---------+---------+-----+-------+
8+
+-----------------------|------------|----------------+
9+
| Name | Total Deps | 1st Level Deps |
10+
+-----------------------|------------|----------------+
11+
| simplecov-console | 7 | 3 |
12+
| codecov | 4 | 1 |
13+
| rails_stats | 4 | 2 |
14+
| simplecov | 3 | 3 |
15+
| minitest-around | 1 | 1 |
16+
| bundler | 0 | 0 |
17+
| byebug | 0 | 0 |
18+
| minitest | 0 | 0 |
19+
| minitest-spec-context | 0 | 0 |
20+
+-----------------------|------------|----------------+
21+
\n Declared Gems 9 \n Total Gems 17 \n Unpinned Versions 8 \n Github Refs 0 \n \n+----------------------+---------+---------+---------+---------+-----+-------+
922
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
1023
+----------------------+---------+---------+---------+---------+-----+-------+
1124
| Channels | 8 | 8 | 2 | 0 | 0 | 0 |
@@ -14,23 +27,73 @@
1427
| Helpers | 3 | 3 | 0 | 0 | 0 | 0 |
1528
| Javascripts | 27 | 7 | 0 | 0 | 0 | 0 |
1629
| Jobs | 7 | 2 | 1 | 0 | 0 | 0 |
30+
| Libraries | 1 | 1 | 0 | 0 | 0 | 0 |
1731
| Mailers | 4 | 4 | 1 | 0 | 0 | 0 |
32+
| Model Tests | 5 | 4 | 2 | 0 | 0 | 0 |
1833
| Models | 3 | 3 | 1 | 0 | 0 | 0 |
34+
| Spec Support | 1 | 1 | 0 | 0 | 0 | 0 |
35+
| Test Support | 1 | 1 | 0 | 0 | 0 | 0 |
1936
+----------------------+---------+---------+---------+---------+-----+-------+
20-
| Code | 476 | 144 | 7 | 1 | 0 | 142 |
21-
| Tests | 0 | 0 | 0 | 0 | 0 | 0 |
22-
| Total | 476 | 144 | 7 | 1 | 0 | 142 |
37+
| Code | 477 | 145 | 7 | 1 | 0 | 143 |
38+
| Tests | 7 | 6 | 2 | 0 | 0 | 0 |
39+
| Total | 484 | 151 | 9 | 1 | 0 | 149 |
2340
+----------------------+---------+---------+---------+---------+-----+-------+
24-
Code LOC: 144 Test LOC: 0 Code to Test Ratio: 1:0.0
41+
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0
2542
2643
EOS
2744

45+
TABLE_RUBY_2_4 = <<~EOS
46+
+-----------------------|------------|----------------+
47+
| Name | Total Deps | 1st Level Deps |
48+
+-----------------------|------------|----------------+
49+
| simplecov-console | 6 | 3 |
50+
| rails_stats | 4 | 2 |
51+
| codecov | 3 | 1 |
52+
| simplecov | 2 | 2 |
53+
| minitest-around | 1 | 1 |
54+
| bundler | 0 | 0 |
55+
| byebug | 0 | 0 |
56+
| minitest | 0 | 0 |
57+
| minitest-spec-context | 0 | 0 |
58+
+-----------------------|------------|----------------+
59+
\n Declared Gems 9 \n Total Gems 16 \n Unpinned Versions 8 \n Github Refs 0 \n \n+----------------------+---------+---------+---------+---------+-----+-------+
60+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
61+
+----------------------+---------+---------+---------+---------+-----+-------+
62+
| Channels | 8 | 8 | 2 | 0 | 0 | 0 |
63+
| Configuration | 417 | 111 | 1 | 0 | 0 | 0 |
64+
| Controllers | 7 | 6 | 1 | 1 | 1 | 4 |
65+
| Helpers | 3 | 3 | 0 | 0 | 0 | 0 |
66+
| Javascripts | 27 | 7 | 0 | 0 | 0 | 0 |
67+
| Jobs | 7 | 2 | 1 | 0 | 0 | 0 |
68+
| Libraries | 1 | 1 | 0 | 0 | 0 | 0 |
69+
| Mailers | 4 | 4 | 1 | 0 | 0 | 0 |
70+
| Model Tests | 5 | 4 | 2 | 0 | 0 | 0 |
71+
| Models | 3 | 3 | 1 | 0 | 0 | 0 |
72+
| Spec Support | 1 | 1 | 0 | 0 | 0 | 0 |
73+
| Test Support | 1 | 1 | 0 | 0 | 0 | 0 |
74+
+----------------------+---------+---------+---------+---------+-----+-------+
75+
| Code | 477 | 145 | 7 | 1 | 0 | 143 |
76+
| Tests | 7 | 6 | 2 | 0 | 0 | 0 |
77+
| Total | 484 | 151 | 9 | 1 | 0 | 149 |
78+
+----------------------+---------+---------+---------+---------+-----+-------+
79+
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0
80+
81+
EOS
82+
2883
it "outputs useful stats for a Rails project" do
2984
root_directory = File.expand_path('../../../test/dummy', File.dirname(__FILE__))
3085

31-
assert_output(TABLE) do
86+
out, err = capture_io do
3287
RailsStats::CodeStatistics.new(root_directory).to_s
3388
end
89+
90+
expectation = if RUBY_VERSION < "2.5.0"
91+
TABLE_RUBY_2_4
92+
else
93+
TABLE
94+
end
95+
96+
assert_equal expectation, out
3497
end
3598
end
3699
end

test/lib/rails_stats/json_formatter_test.rb

+36-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,29 @@
66
describe "#result" do
77
JSON_STRING = <<~EOS
88
[{
9+
"name": "Libraries",
10+
"lines": "1",
11+
"loc": "1",
12+
"classes": "0",
13+
"methods": "0",
14+
"m_over_c": "0",
15+
"loc_over_m": "0"
16+
}, {
917
"name": "Mailers",
1018
"lines": "4",
1119
"loc": "4",
1220
"classes": "1",
1321
"methods": "0",
1422
"m_over_c": "0",
1523
"loc_over_m": "0"
24+
}, {
25+
"name": "Model Tests",
26+
"lines": "5",
27+
"loc": "4",
28+
"classes": "2",
29+
"methods": "0",
30+
"m_over_c": "0",
31+
"loc_over_m": "0"
1632
}, {
1733
"name": "Models",
1834
"lines": "3",
@@ -69,14 +85,30 @@
6985
"methods": "0",
7086
"m_over_c": "0",
7187
"loc_over_m": "0"
88+
}, {
89+
"name": "Spec Support",
90+
"lines": "1",
91+
"loc": "1",
92+
"classes": "0",
93+
"methods": "0",
94+
"m_over_c": "0",
95+
"loc_over_m": "0"
96+
}, {
97+
"name": "Test Support",
98+
"lines": "1",
99+
"loc": "1",
100+
"classes": "0",
101+
"methods": "0",
102+
"m_over_c": "0",
103+
"loc_over_m": "0"
72104
}, {
73105
"name": "Total",
74-
"lines": "476",
75-
"loc": "144",
76-
"classes": "7",
106+
"lines": "484",
107+
"loc": "151",
108+
"classes": "9",
77109
"methods": "1",
78110
"m_over_c": "0",
79-
"loc_over_m": "142",
111+
"loc_over_m": "149",
80112
"code_to_test_ratio": "0.0",
81113
"total": true
82114
}]

0 commit comments

Comments
 (0)