Skip to content

Commit

Permalink
Merge pull request #21 from fastruby/feature/bundler-stats
Browse files Browse the repository at this point in the history
Add dependency on bundler-stats and improve output
  • Loading branch information
lubc authored Dec 2, 2021
2 parents cbeff8c + 601594d commit 23018bc
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 31 deletions.
23 changes: 20 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ on:
pull_request:
branches:
- main
env:
COVERAGE: true
CODECOV_TOKEN: d7028c0e-97c5-485f-85e5-f63daabeef63
jobs:
test-ruby-2-4-x:
runs-on: ubuntu-latest
Expand All @@ -23,6 +20,10 @@ jobs:
ruby-version: 2.4
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
Expand All @@ -38,6 +39,10 @@ jobs:
ruby-version: 2.5
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
Expand All @@ -53,6 +58,10 @@ jobs:
ruby-version: 2.6
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
Expand All @@ -68,6 +77,10 @@ jobs:
ruby-version: 2.7
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
Expand All @@ -83,6 +96,10 @@ jobs:
ruby-version: 3.0
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.byebug_history
*.gem
*.rbc
.bundle
.config
.ruby-version
.yardoc
Gemfile.lock
InstalledFiles
Expand Down
14 changes: 12 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in rails_stats.gemspec
gemspec

group :development, :test do
gem "bundler", ">= 1.6", "< 3.0"
gem "byebug"
gem "codecov"
gem "minitest"
gem "minitest-around"
gem "minitest-spec-context"
gem "simplecov"
gem "simplecov-console"
end
17 changes: 9 additions & 8 deletions lib/rails_stats/app_statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ class AppStatistics
attr_reader :statistics, :total, :test

def initialize(directory)
@directories = []
@test = false
@directory = directory
@statistics = calculate_statistics
@total = calculate_total
@directory = directory
@statistics = calculate_statistics
@total = calculate_total
end

def key_concepts
Expand All @@ -28,15 +29,15 @@ def calculate_statistics
end

def directories
return @directories if @directories
out = []
return @directories if @directories.any?

Dir.foreach(@directory) do |file_name|
path = File.join(@directory, file_name)
next unless File.directory?(path)
next if (/^\./ =~ file_name)
next if file_name == "assets" # doing separately
next if file_name == "views" # TODO
out << path
@directories << path
end

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

case file_name
when "javascripts"
out << path
@directories << path
# TODO when "css"
end
end
end

out
@directories
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/rails_stats/console_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "bundler/stats/cli"

module RailsStats
class ConsoleFormatter < StatsFormatter
def to_s
Bundler::Stats::CLI.start

print_header
sorted_keys = @statistics.keys.sort
sorted_keys.each { |key| print_line(key, @statistics[key]) }
Expand Down
9 changes: 1 addition & 8 deletions rails_stats.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,5 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "rake"
spec.add_development_dependency "bundler", ">= 1.6", "< 3.0"
spec.add_development_dependency "byebug"
spec.add_development_dependency "codecov"
spec.add_development_dependency "minitest"
spec.add_development_dependency "minitest-around"
spec.add_development_dependency "minitest-spec-context"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "simplecov-console"
spec.add_dependency "bundler-stats", ">= 2.1"
end
1 change: 1 addition & 0 deletions test/dummy/lib/monkeypatches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Monkeypatches go here"
3 changes: 3 additions & 0 deletions test/dummy/spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class FooBar

end
1 change: 1 addition & 0 deletions test/dummy/spec/support/support_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "This is a spec support file"
2 changes: 2 additions & 0 deletions test/dummy/test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class UserTest
end
1 change: 1 addition & 0 deletions test/dummy/test/support/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "This is a test support file"
75 changes: 69 additions & 6 deletions test/lib/rails_stats/code_statistics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@
describe RailsStats::CodeStatistics do
describe "#to_s" do
TABLE = <<~EOS
+----------------------+---------+---------+---------+---------+-----+-------+
+-----------------------|------------|----------------+
| Name | Total Deps | 1st Level Deps |
+-----------------------|------------|----------------+
| simplecov-console | 7 | 3 |
| codecov | 4 | 1 |
| rails_stats | 4 | 2 |
| simplecov | 3 | 3 |
| minitest-around | 1 | 1 |
| bundler | 0 | 0 |
| byebug | 0 | 0 |
| minitest | 0 | 0 |
| minitest-spec-context | 0 | 0 |
+-----------------------|------------|----------------+
\n Declared Gems 9 \n Total Gems 17 \n Unpinned Versions 8 \n Github Refs 0 \n \n+----------------------+---------+---------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+---------+---------+---------+---------+-----+-------+
| Channels | 8 | 8 | 2 | 0 | 0 | 0 |
Expand All @@ -14,23 +27,73 @@
| Helpers | 3 | 3 | 0 | 0 | 0 | 0 |
| Javascripts | 27 | 7 | 0 | 0 | 0 | 0 |
| Jobs | 7 | 2 | 1 | 0 | 0 | 0 |
| Libraries | 1 | 1 | 0 | 0 | 0 | 0 |
| Mailers | 4 | 4 | 1 | 0 | 0 | 0 |
| Model Tests | 5 | 4 | 2 | 0 | 0 | 0 |
| Models | 3 | 3 | 1 | 0 | 0 | 0 |
| Spec Support | 1 | 1 | 0 | 0 | 0 | 0 |
| Test Support | 1 | 1 | 0 | 0 | 0 | 0 |
+----------------------+---------+---------+---------+---------+-----+-------+
| Code | 476 | 144 | 7 | 1 | 0 | 142 |
| Tests | 0 | 0 | 0 | 0 | 0 | 0 |
| Total | 476 | 144 | 7 | 1 | 0 | 142 |
| Code | 477 | 145 | 7 | 1 | 0 | 143 |
| Tests | 7 | 6 | 2 | 0 | 0 | 0 |
| Total | 484 | 151 | 9 | 1 | 0 | 149 |
+----------------------+---------+---------+---------+---------+-----+-------+
Code LOC: 144 Test LOC: 0 Code to Test Ratio: 1:0.0
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0
EOS

TABLE_RUBY_2_4 = <<~EOS
+-----------------------|------------|----------------+
| Name | Total Deps | 1st Level Deps |
+-----------------------|------------|----------------+
| simplecov-console | 6 | 3 |
| rails_stats | 4 | 2 |
| codecov | 3 | 1 |
| simplecov | 2 | 2 |
| minitest-around | 1 | 1 |
| bundler | 0 | 0 |
| byebug | 0 | 0 |
| minitest | 0 | 0 |
| minitest-spec-context | 0 | 0 |
+-----------------------|------------|----------------+
\n Declared Gems 9 \n Total Gems 16 \n Unpinned Versions 8 \n Github Refs 0 \n \n+----------------------+---------+---------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+---------+---------+---------+---------+-----+-------+
| Channels | 8 | 8 | 2 | 0 | 0 | 0 |
| Configuration | 417 | 111 | 1 | 0 | 0 | 0 |
| Controllers | 7 | 6 | 1 | 1 | 1 | 4 |
| Helpers | 3 | 3 | 0 | 0 | 0 | 0 |
| Javascripts | 27 | 7 | 0 | 0 | 0 | 0 |
| Jobs | 7 | 2 | 1 | 0 | 0 | 0 |
| Libraries | 1 | 1 | 0 | 0 | 0 | 0 |
| Mailers | 4 | 4 | 1 | 0 | 0 | 0 |
| Model Tests | 5 | 4 | 2 | 0 | 0 | 0 |
| Models | 3 | 3 | 1 | 0 | 0 | 0 |
| Spec Support | 1 | 1 | 0 | 0 | 0 | 0 |
| Test Support | 1 | 1 | 0 | 0 | 0 | 0 |
+----------------------+---------+---------+---------+---------+-----+-------+
| Code | 477 | 145 | 7 | 1 | 0 | 143 |
| Tests | 7 | 6 | 2 | 0 | 0 | 0 |
| Total | 484 | 151 | 9 | 1 | 0 | 149 |
+----------------------+---------+---------+---------+---------+-----+-------+
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0
EOS

it "outputs useful stats for a Rails project" do
root_directory = File.expand_path('../../../test/dummy', File.dirname(__FILE__))

assert_output(TABLE) do
out, err = capture_io do
RailsStats::CodeStatistics.new(root_directory).to_s
end

expectation = if RUBY_VERSION < "2.5.0"
TABLE_RUBY_2_4
else
TABLE
end

assert_equal expectation, out
end
end
end
40 changes: 36 additions & 4 deletions test/lib/rails_stats/json_formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@
describe "#result" do
JSON_STRING = <<~EOS
[{
"name": "Libraries",
"lines": "1",
"loc": "1",
"classes": "0",
"methods": "0",
"m_over_c": "0",
"loc_over_m": "0"
}, {
"name": "Mailers",
"lines": "4",
"loc": "4",
"classes": "1",
"methods": "0",
"m_over_c": "0",
"loc_over_m": "0"
}, {
"name": "Model Tests",
"lines": "5",
"loc": "4",
"classes": "2",
"methods": "0",
"m_over_c": "0",
"loc_over_m": "0"
}, {
"name": "Models",
"lines": "3",
Expand Down Expand Up @@ -69,14 +85,30 @@
"methods": "0",
"m_over_c": "0",
"loc_over_m": "0"
}, {
"name": "Spec Support",
"lines": "1",
"loc": "1",
"classes": "0",
"methods": "0",
"m_over_c": "0",
"loc_over_m": "0"
}, {
"name": "Test Support",
"lines": "1",
"loc": "1",
"classes": "0",
"methods": "0",
"m_over_c": "0",
"loc_over_m": "0"
}, {
"name": "Total",
"lines": "476",
"loc": "144",
"classes": "7",
"lines": "484",
"loc": "151",
"classes": "9",
"methods": "1",
"m_over_c": "0",
"loc_over_m": "142",
"loc_over_m": "149",
"code_to_test_ratio": "0.0",
"total": true
}]
Expand Down

0 comments on commit 23018bc

Please sign in to comment.