Skip to content

Commit 59947e2

Browse files
committed
Register stats directories with Rails::CodeStatistics.register_directory.
Rails `main` use Thor for the `bin/rails stats` command instead of Rake. The global constant STATS_DIRECTORIES defined by Rails, has been deprecated in favor of Rails::CodeStatistics.register_directory. https://github.com/rails/rails/blob/8c7754dfdf39ed94cc93bbc40ee721c311b6d32c/railties/CHANGELOG.md?plain=1#L1-L11
1 parent d0e322b commit 59947e2

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/rspec-rails.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ class Railtie < ::Rails::Railtie
99
# As of Rails 5.1.0 you can register directories to work with `rake notes`
1010
require 'rails/source_annotation_extractor'
1111
::Rails::SourceAnnotationExtractor::Annotation.register_directories("spec")
12+
13+
# As of Rails 8.0.0 you can register directories to work with `rails stats`
14+
if ::Rails::VERSION::STRING >= "8.0.0"
15+
require 'rails/code_statistics'
16+
types = begin
17+
dirs = Dir['./spec/**/*_spec.rb']
18+
.map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }
19+
.uniq
20+
.select { |f| File.directory?(f) }
21+
Hash[dirs.map { |d| [d.split('/').last, d] }]
22+
end
23+
types.each do |type, dir|
24+
name = type.singularize.capitalize
25+
26+
::Rails::CodeStatistics.register_directory "#{name} specs", dir
27+
::Rails::CodeStatistics::TEST_TYPES << "#{name} specs"
28+
end
29+
end
30+
1231
generators = config.app_generators
1332
generators.integration_tool :rspec
1433
generators.test_framework :rspec

lib/rspec/rails/tasks/rspec.rake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ end
55

66
task default: :spec
77

8-
task stats: "spec:statsetup"
8+
if ::Rails::VERSION::STRING < "8.0.0"
9+
task stats: "spec:statsetup"
10+
end
911

1012
desc "Run all specs in spec directory (excluding plugin specs)"
1113
RSpec::Core::RakeTask.new(spec: "spec:prepare")

0 commit comments

Comments
 (0)