Rake stats and Cucumber

前端 未结 5 636
陌清茗
陌清茗 2020-12-29 10:03

I\'m using Cucumber with RSpec in a Rails project. When I use the \"rake stats\" task, I get the following :

+----------------------+-------+-------+--------         


        
相关标签:
5条回答
  • 2020-12-29 10:30

    RSpec creates a lib/tasks/rspec.rake file. And redefines the stats directories inside of it.

    At the line 108 of that file, you'll see :

    # Setup specs for stats
    task :statsetup do
        require 'code_statistics'
        ::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models')
        ::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views')
        ....
    end
    

    You just need to add your cucumber features directory there, right before the end of the task.

    #
    # Adding Cucumber features to the stats
    #
    ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
    ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
    
    0 讨论(0)
  • 2020-12-29 10:44
    I copy paste my custom set up for RSpec 1:
    
    +----------------------+-------+-------+---------+---------+-----+-------+
    | Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
    +----------------------+-------+-------+---------+---------+-----+-------+
    | Controllers          |  1110 |   791 |      39 |      92 |   2 |     6 |
    | Helpers              |   449 |   373 |       0 |      39 |   0 |     7 |
    | Models               |  1986 |  1338 |      25 |     111 |   4 |    10 |
    | Libraries            |   652 |   479 |      11 |      41 |   3 |     9 |
    | Views                |  2911 |  2730 |       0 |       0 |   0 |     0 |
    | Static CSS           |  2776 |  2174 |       0 |       0 |   0 |     0 |
    | App javascript       |    16 |    14 |       0 |       0 |   0 |     0 |
    | Model specs          |   517 |   289 |       0 |       2 |   0 |   142 |
    | Helper specs         |    27 |    22 |       0 |       0 |   0 |     0 |
    | Library specs        |    31 |    24 |       0 |       0 |   0 |     0 |
    | Cucumber features    |   907 |   657 |       3 |      13 |   4 |    48 |
    +----------------------+-------+-------+---------+---------+-----+-------+
    | Total                | 11382 |  8891 |      78 |     298 |   3 |    27 |
    +----------------------+-------+-------+---------+---------+-----+-------+
      Code LOC: 7899     Test LOC: 992     Code to Test Ratio: 1:0.1
    

    Code:

    # here is my set up
        task :statsetup do
          require 'code_statistics'
    
          class CodeStatistics
            alias calculate_statistics_orig calculate_statistics
    
            def calculate_statistics
              @pairs.inject({}) do |stats, pair|
                if 3 == pair.size
                  stats[pair.first] = calculate_directory_statistics(pair[1], pair[2]); stats
                else
                  stats[pair.first] = calculate_directory_statistics(pair.last); stats
                end
              end
            end
          end
          # http://www.pervasivecode.com/blog/2007/06/28/hacking-rakestats-to-get-gross-loc/
          ::STATS_DIRECTORIES << ['Views', 'app/views', /\.(rhtml|erb|rb)$/]
          ::STATS_DIRECTORIES << ['Static CSS',  'public/stylesheets', /\.css$/]
          ::STATS_DIRECTORIES << ['App javascript',  'public/javascripts', /application.js$/]
    
          # RSpec default
          ::STATS_DIRECTORIES << %w( Model\ specs spec/models ) if File.exist?('spec/models')
          ::STATS_DIRECTORIES << %w( View\ specs spec/views ) if File.exist?('spec/views')
          ::STATS_DIRECTORIES << %w( Controller\ specs spec/controllers ) if File.exist?('spec/controllers')
          ::STATS_DIRECTORIES << %w( Helper\ specs spec/helpers ) if File.exist?('spec/helpers')
          ::STATS_DIRECTORIES << %w( Library\ specs spec/lib ) if File.exist?('spec/lib')
          ::STATS_DIRECTORIES << %w( Routing\ specs spec/routing ) if File.exist?('spec/routing')
          ::STATS_DIRECTORIES << %w( Integration\ specs spec/integration ) if File.exist?('spec/integration')
          ::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models')
          ::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views')
          ::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers')
          ::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers')
          ::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib')
          ::CodeStatistics::TEST_TYPES << "Routing specs" if File.exist?('spec/routing')
          ::CodeStatistics::TEST_TYPES << "Integration specs" if File.exist?('spec/integration')
    
          # Cuke
          ::STATS_DIRECTORIES << %w( Cucumber\ features features ) if File.exist?('features')
          ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
    
        end
    
    0 讨论(0)
  • 2020-12-29 10:46

    Here's a version that works with Turnip and RSpec 2

    https://gist.github.com/2360892

    0 讨论(0)
  • 2020-12-29 10:51

    Although this is an extremely old question, it is still first when googling "rails rake stats cucumber" and the answers are misleading or outdated.

    For future googlers, all you need to do is run

    $ rails generate cucumber:install
    

    This will create the necessary files, and specifically the lib/tasks/cucumber.rake file, which adds "Cucumber Features" to the rake stats output.

    0 讨论(0)
  • 2020-12-29 10:54

    Why monkey patch the gem like that? You'll have to add your fix for it every time you update rspec-rails. Just extend the task locally with something like this:

    echo "# Adding Cucumber features to be reported by the command:
    # rake stats
    # But e.g. in production environment we probably don't have rspec-rails, nor it's
    # statsetup task, we could extend. So lets check if stasetup is available and only
    # then extend it. If it isn't then just do nothing.
    if Rake::Task.task_defined? 'spec:statsetup'
      Rake::Task['spec:statsetup'].enhance do
        require 'rails/code_statistics'
        ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
        ::CodeStatistics::TEST_TYPES << 'Cucumber features' if File.exist?('features')
      end
    end" > lib/tasks/cucumber_stats.rake
    

    And FYI Lichtamberg and Damien MATHIEU this works just fine with rspec 2. However these rails specific rake tasks are ofc not part of the rspec gem itself, but a part of the rspec-rails gem.

    P.S. This all was tested with ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux], rails 3.0.8, rspec 2.6.0 and rspec-rails 2.6.1 and it might or might not work older versions of them.

    0 讨论(0)
提交回复
热议问题