diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 38b8336..c8e63c9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,103 +4,39 @@ name: Test on: push: branches: - - main + - main pull_request: branches: - - main + - main jobs: - test-ruby-2-4-x: + test: runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: ["2.5", "2.6", "2.7", "3.0"] steps: - uses: actions/checkout@v1 - name: Setup Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: 2.4 + ruby-version: ${{ matrix.ruby-version }} 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 - bundle exec rake test - test-ruby-2-5-x: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v1 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - 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 - bundle exec rake test - test-ruby-2-6-x: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v1 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.6 - bundler-cache: true - - name: Build and run tests - env: - COVERAGE: true - TERM: xterm - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + - name: Uninstall existing Bundler run: | - gem install bundler - bundle install --jobs 4 --retry 3 - bundle exec rake test - test-ruby-2-7-x: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v1 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.7 - bundler-cache: true - - name: Build and run tests - env: - COVERAGE: true - TERM: xterm - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + gem uninstall bundler -a -x || true + - name: Install Bundler run: | - gem install bundler - bundle install --jobs 4 --retry 3 - bundle exec rake test - test-ruby-3-0-x: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v1 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 3.0 - bundler-cache: true + if [[ "${{ matrix.ruby-version }}" == "2.6" || "${{ matrix.ruby-version }}" == "2.7" ]]; then + gem install bundler -v "~> 2.4.0" + else + gem install bundler + fi - 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 bundle exec rake test \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 6100c04..627c3e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ * [BUGFIX: Add methods for calculating M/C and LOC/M](https://github.com/fastruby/rails_stats/pull/19) * [FEATURE: Support JSON output](https://github.com/fastruby/rails_stats/pull/20) * [FEATURE: Add dependency on bundler-stats and improve output](https://github.com/fastruby/rails_stats/pull/21) - +* [FEATURE: Add files count to the console and JSON output](https://github.com/fastruby/rails_stats/pull/25) # v1.0.2 / 2020-10-7 ([commits](https://github.com/fastruby/rails_stats/compare/v1.0.1...v1.0.2)) diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..e4b12ca --- /dev/null +++ b/codecov.yml @@ -0,0 +1,2 @@ +codecov: + token: d7028c0e-97c5-485f-85e5-f63daabeef63 \ No newline at end of file diff --git a/lib/rails_stats/code_statistics_calculator.rb b/lib/rails_stats/code_statistics_calculator.rb index 9a9730c..f881d88 100644 --- a/lib/rails_stats/code_statistics_calculator.rb +++ b/lib/rails_stats/code_statistics_calculator.rb @@ -2,7 +2,7 @@ module RailsStats class CodeStatisticsCalculator #:nodoc: - attr_reader :lines, :code_lines, :classes, :methods, :test + attr_reader :files_total, :lines, :code_lines, :classes, :methods, :test PATTERNS = { rb: { @@ -33,6 +33,7 @@ class CodeStatisticsCalculator #:nodoc: def initialize(test=false) @test = test + @files_total = 0 @lines = 0 @code_lines = 0 @classes = 0 @@ -40,6 +41,7 @@ def initialize(test=false) end def add(code_statistics_calculator) + @files_total += code_statistics_calculator.files_total @lines += code_statistics_calculator.lines @code_lines += code_statistics_calculator.code_lines @classes += code_statistics_calculator.classes @@ -47,6 +49,8 @@ def add(code_statistics_calculator) end def add_by_file_path(file_path) + @files_total += 1 + File.open(file_path) do |f| self.add_by_io(f, file_type(file_path)) end diff --git a/lib/rails_stats/console_formatter.rb b/lib/rails_stats/console_formatter.rb index 6adbc0b..5531649 100644 --- a/lib/rails_stats/console_formatter.rb +++ b/lib/rails_stats/console_formatter.rb @@ -24,19 +24,22 @@ def to_s def print_header print_splitter - puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |" + puts "| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M |" print_splitter end def print_splitter - puts "+----------------------+---------+---------+---------+---------+-----+-------+" + puts "+----------------------+---------+---------+---------+---------+---------+-----+-------+" end def print_line(name, statistics) m_over_c = (statistics.methods / statistics.classes) rescue m_over_c = 0 loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue loc_over_m = 0 + require 'byebug'; byebug if statistics.nil? + puts "| #{name.ljust(20)} " \ + "| #{statistics.files_total.to_s.rjust(7)} " \ "| #{statistics.lines.to_s.rjust(7)} " \ "| #{statistics.code_lines.to_s.rjust(7)} " \ "| #{statistics.classes.to_s.rjust(7)} " \ @@ -49,7 +52,7 @@ def print_code_test_stats code = calculator.code_loc tests = calculator.test_loc - puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}" + puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)} Files: #{calculator.files_total}" puts "" end end diff --git a/lib/rails_stats/json_formatter.rb b/lib/rails_stats/json_formatter.rb index c5f2a52..4590464 100644 --- a/lib/rails_stats/json_formatter.rb +++ b/lib/rails_stats/json_formatter.rb @@ -33,6 +33,7 @@ def stat_hash(name, statistics) { "name" => name, + "files" => statistics.files_total.to_s, "lines" => statistics.lines.to_s, "loc" => statistics.code_lines.to_s, "classes" => statistics.classes.to_s, diff --git a/lib/rails_stats/stats_calculator.rb b/lib/rails_stats/stats_calculator.rb index c5da90e..773ec14 100644 --- a/lib/rails_stats/stats_calculator.rb +++ b/lib/rails_stats/stats_calculator.rb @@ -14,10 +14,10 @@ def initialize(root_directory) @statistics = calculate_statistics @code_loc = calculate_code @test_loc = calculate_tests - @code_total, @tests_total, @grand_total = calculate_totals + @files_total, @code_total, @tests_total, @grand_total = calculate_totals end - attr_reader :code_loc, :code_total, :grand_total, :statistics, :test_loc, :tests_total + attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total private @@ -103,6 +103,10 @@ def calculate_statistics end def calculate_totals + files_total = @statistics.sum do |k,value| + value.files_total + end + code_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, code_total| code_total.add(pair.last) unless pair.last.test end @@ -115,7 +119,7 @@ def calculate_totals total.add(pair.last) end - [code_total, tests_total, grand_total] + [files_total, code_total, tests_total, grand_total] end def calculate_code diff --git a/lib/rails_stats/tasks.rb b/lib/rails_stats/tasks.rb index 92f968a..067aff5 100644 --- a/lib/rails_stats/tasks.rb +++ b/lib/rails_stats/tasks.rb @@ -11,4 +11,4 @@ root_directory = File.absolute_path(path) puts "\nDirectory: #{root_directory}\n\n" if args[:path] RailsStats::CodeStatistics.new(root_directory, format: fmt).to_s -end +end \ No newline at end of file diff --git a/rails_stats.gemspec b/rails_stats.gemspec index 100b543..4e71c0e 100644 --- a/rails_stats.gemspec +++ b/rails_stats.gemspec @@ -10,7 +10,7 @@ Gem::Specification.new do |spec| spec.email = ["brian@bleonard.com"] spec.summary = %q{Analyze a Rails project} spec.description = %q{Point it to a directory and see stuff about the app} - spec.homepage = "https://github.com/bleonard/rails_stats" + spec.homepage = "https://github.com/fastruby/rails_stats" spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0") diff --git a/test/dummy/Gemfile b/test/dummy/Gemfile index 4e7897d..f76018b 100644 --- a/test/dummy/Gemfile +++ b/test/dummy/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.6.3' +ruby '2.6.8' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.3', '>= 6.0.3.3' @@ -20,6 +20,7 @@ gem 'jbuilder', '~> 2.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' +gem 'rails_stats', path: '../../../rails_stats' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false diff --git a/test/fixtures/console-output.txt b/test/fixtures/console-output.txt new file mode 100644 index 0000000..844985b --- /dev/null +++ b/test/fixtures/console-output.txt @@ -0,0 +1,40 @@ ++-----------------------|------------|----------------+ +| Name | Total Deps | 1st Level Deps | ++-----------------------|------------|----------------+ +| simplecov-console | 7 | 3 | +| codecov | 5 | 2 | +| 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 | ++-----------------------|------------|----------------+ + + Declared Gems 9 + Total Gems 18 + Unpinned Versions 8 + Github Refs 0 + ++----------------------+---------+---------+---------+---------+---------+-----+-------+ +| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M | ++----------------------+---------+---------+---------+---------+---------+-----+-------+ +| Channels | 2 | 8 | 8 | 2 | 0 | 0 | 0 | +| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 | +| Controllers | 1 | 7 | 6 | 1 | 1 | 1 | 4 | +| Helpers | 1 | 3 | 3 | 0 | 0 | 0 | 0 | +| Javascripts | 3 | 27 | 7 | 0 | 0 | 0 | 0 | +| Jobs | 1 | 7 | 2 | 1 | 0 | 0 | 0 | +| Libraries | 1 | 1 | 1 | 0 | 0 | 0 | 0 | +| Mailers | 1 | 4 | 4 | 1 | 0 | 0 | 0 | +| Model Tests | 2 | 5 | 4 | 2 | 0 | 0 | 0 | +| Models | 1 | 3 | 3 | 1 | 0 | 0 | 0 | +| Spec Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 | +| Test Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 | ++----------------------+---------+---------+---------+---------+---------+-----+-------+ +| Code | 30 | 477 | 145 | 7 | 1 | 0 | 143 | +| Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 | +| Total | 34 | 484 | 151 | 9 | 1 | 0 | 149 | ++----------------------+---------+---------+---------+---------+---------+-----+-------+ + Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34 \ No newline at end of file diff --git a/test/lib/rails_stats/code_statistics_test.rb b/test/lib/rails_stats/code_statistics_test.rb index a44b49e..042bc09 100644 --- a/test/lib/rails_stats/code_statistics_test.rb +++ b/test/lib/rails_stats/code_statistics_test.rb @@ -2,98 +2,19 @@ require "test_helper" +Minitest::Test.make_my_diffs_pretty! + 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 | -| 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 - - 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__)) + table = File.read(File.expand_path('../../../fixtures/console-output.txt', __FILE__)) 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 + assert_equal table.delete(" \n"), out.delete(" \n") end end end diff --git a/test/lib/rails_stats/json_formatter_test.rb b/test/lib/rails_stats/json_formatter_test.rb index 74a153d..b4b65bf 100644 --- a/test/lib/rails_stats/json_formatter_test.rb +++ b/test/lib/rails_stats/json_formatter_test.rb @@ -7,6 +7,7 @@ JSON_STRING = <<~EOS [{ "name": "Libraries", + "files": "1", "lines": "1", "loc": "1", "classes": "0", @@ -15,6 +16,7 @@ "loc_over_m": "0" }, { "name": "Mailers", + "files": "1", "lines": "4", "loc": "4", "classes": "1", @@ -23,6 +25,7 @@ "loc_over_m": "0" }, { "name": "Model Tests", + "files": "2", "lines": "5", "loc": "4", "classes": "2", @@ -31,6 +34,7 @@ "loc_over_m": "0" }, { "name": "Models", + "files": "1", "lines": "3", "loc": "3", "classes": "1", @@ -39,6 +43,7 @@ "loc_over_m": "0" }, { "name": "Javascripts", + "files": "3", "lines": "27", "loc": "7", "classes": "0", @@ -47,6 +52,7 @@ "loc_over_m": "0" }, { "name": "Jobs", + "files": "1", "lines": "7", "loc": "2", "classes": "1", @@ -55,6 +61,7 @@ "loc_over_m": "0" }, { "name": "Controllers", + "files": "1", "lines": "7", "loc": "6", "classes": "1", @@ -63,6 +70,7 @@ "loc_over_m": "4" }, { "name": "Helpers", + "files": "1", "lines": "3", "loc": "3", "classes": "0", @@ -71,6 +79,7 @@ "loc_over_m": "0" }, { "name": "Channels", + "files": "2", "lines": "8", "loc": "8", "classes": "2", @@ -79,6 +88,7 @@ "loc_over_m": "0" }, { "name": "Configuration", + "files": "19", "lines": "417", "loc": "111", "classes": "1", @@ -87,6 +97,7 @@ "loc_over_m": "0" }, { "name": "Spec Support", + "files": "1", "lines": "1", "loc": "1", "classes": "0", @@ -95,6 +106,7 @@ "loc_over_m": "0" }, { "name": "Test Support", + "files": "1", "lines": "1", "loc": "1", "classes": "0", @@ -103,6 +115,7 @@ "loc_over_m": "0" }, { "name": "Total", + "files": "34", "lines": "484", "loc": "151", "classes": "9",