Skip to content

Commit ede9d8a

Browse files
authored
Merge pull request #14 from mocktools/develop
RSpec::Mock v0.4.0
2 parents 6925af9 + 2484a51 commit ede9d8a

File tree

14 files changed

+131
-136
lines changed

14 files changed

+131
-136
lines changed

.circleci/gemspecs/compatible

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
1717
spec.require_paths = %w[lib]
1818

1919
spec.add_runtime_dependency 'colorize', '>= 0.8.1'
20+
spec.add_runtime_dependency 'rake', '~> 13.2', '>= 13.2.1'
2021
spec.add_runtime_dependency 'rspec-core', '~> 3.10'
2122
spec.add_runtime_dependency 'rspec-mocks', '~> 3.10'
2223
spec.add_runtime_dependency 'terminal-table', '~> 3.0'

.circleci/gemspecs/latest

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
1717
spec.require_paths = %w[lib]
1818

1919
spec.add_runtime_dependency 'colorize', '>= 0.8.1'
20+
spec.add_runtime_dependency 'rake', '~> 13.2', '>= 13.2.1'
2021
spec.add_runtime_dependency 'rspec-core', '~> 3.10'
2122
spec.add_runtime_dependency 'rspec-mocks', '~> 3.10'
2223
spec.add_runtime_dependency 'terminal-table', '~> 3.0'

CHANGELOG.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22

33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
## [0.4.0] - 2024-11-09
6+
7+
### Added
8+
9+
- Added rake task to analyze Flexmock usage and track migration progress to RSpec mocks
10+
11+
### Removed
12+
13+
- Removed CLI
14+
515
## [0.3.1] - 2024-11-08
616

717
### Fixed
818

9-
- Fixed CLI broken import.
19+
- Fixed CLI broken import
1020

1121
## [0.3.0] - 2024-11-08
1222

1323
### Added
1424

15-
- Added CLI to analyze Flexmock usage and track migration progress to RSpec mocks.
25+
- Added CLI to analyze Flexmock usage and track migration progress to RSpec mocks
1626

1727
## [0.2.0] - 2024-11-04
1828

@@ -25,4 +35,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2535

2636
### Added
2737

28-
- First release of `RSpec::Mock`.
38+
- First release of `RSpec::Mock`

README.md

+7-22
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,18 @@ end
130130

131131
### Migration Analytics
132132

133-
You can create a Rake task to analyze Flexmock usage and track migration progress to RSpec mocks. Or use the CLI directly.
133+
This time implemented migration analytics for [Flexmock](https://github.com/doudou/flexmock) only. You can run Rake task to analyze Flexmock usage and track migration progress to RSpec mocks.
134134

135-
Example of the Rake task:
135+
For non-Rails applications to use the task, you need to load it:
136136

137137
```ruby
138-
namespace :rspec_mock do
139-
namespace :migration_analytics do
140-
desc 'Analyze Flexmock usage and track migration progress to RSpec mocks'
141-
task :flexmock do
142-
require 'rspec/mock/migration_analytics/cli'
143-
144-
path = ::ARGV[1] || 'spec'
145-
puts("\n🔍 Analyzing Flexmock usage in: #{path}")
146-
RSpec::Mock::MigrationAnalytics::Cli.verify_path(path)
147-
end
148-
end
149-
end
138+
require 'rspec/mock/task'
139+
140+
RSpec::Mock::Task.load
150141
```
151142

143+
For Rails applications it will be automatically loaded, so just run:
144+
152145
```bash
153146
# Analyze entire spec directory (default)
154147
rake rspec_mock:migration_analytics:flexmock
@@ -160,14 +153,6 @@ rake rspec_mock:migration_analytics:flexmock spec/services
160153
rake rspec_mock:migration_analytics:flexmock spec/services/sandbox_service_spec.rb
161154
```
162155

163-
Example of the CLI usage:
164-
165-
```bash
166-
ruby cli.rb spec
167-
ruby cli.rb spec/services
168-
ruby cli.rb spec/services/sandbox_service_spec.rb
169-
```
170-
171156
## Contributing
172157

173158
Bug reports and pull requests are welcome on GitHub at <https://github.com/mocktools/ruby-rspec-mock>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. Please check the [open tickets](https://github.com/mocktools/ruby-rspec-mock/issues). Be sure to follow Contributor Code of Conduct below and our [Contributing Guidelines](CONTRIBUTING.md).

lib/rspec/mock/core.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ module Tracker
1313
end
1414

1515
require_relative 'migration_analytics/file_analyzer'
16-
require_relative 'migration_analytics/cli'
16+
require_relative 'migration_analytics/printer'
1717
end
1818

1919
require_relative 'configuration'
2020
require_relative 'context'
2121
require_relative 'methods'
2222
require_relative 'version'
23+
24+
require_relative(defined?(::Rails) ? 'railtie' : 'task')
2325
end
2426

2527
module Core

lib/rspec/mock/migration_analytics/cli.rb renamed to lib/rspec/mock/migration_analytics/printer.rb

+4-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env ruby
21
# frozen_string_literal: true
32

43
require 'colorize'
@@ -12,39 +11,16 @@
1211
module RSpec
1312
module Mock
1413
module MigrationAnalytics
15-
class Cli
14+
class Printer
1615
class << self
17-
def call
18-
if ::ARGV.empty?
19-
print_usage
20-
exit 1
21-
end
22-
23-
begin
24-
verify_path(::ARGV[0])
25-
rescue => error
26-
puts("\n❌ Error: #{error.message}".red)
27-
puts(error.backtrace) if ENV['DEBUG']
28-
end
29-
end
16+
def call(path)
17+
return verify_directory(path) if ::File.directory?(path)
3018

31-
def verify_path(path)
32-
case
33-
when ::File.directory?(path) then verify_directory(path)
34-
else verify_file(path)
35-
end
19+
verify_file(path)
3620
end
3721

3822
private
3923

40-
def print_usage
41-
puts('Usage: ruby cli.rb <path_to_spec_file_or_directory>'.yellow)
42-
puts("\nExamples:".blue)
43-
puts(' ruby cli.rb spec/models/user_spec.rb')
44-
puts(' ruby cli.rb spec/models/')
45-
puts(' ruby cli.rb spec/')
46-
end
47-
4824
def verify_directory(dir_path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
4925
results = []
5026
stats = {
@@ -188,5 +164,3 @@ def create_file_row(result)
188164
end
189165
end
190166
end
191-
192-
RSpec::Mock::MigrationAnalytics::Cli.call if __FILE__.eql?($PROGRAM_NAME)

lib/rspec/mock/railtie.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module RSpec
4+
module Mock
5+
class Railtie < ::Rails::Railtie
6+
rake_tasks do
7+
load(::File.join(::File.dirname(__FILE__), 'tasks', 'rspec_mock.rake'))
8+
end
9+
end
10+
end
11+
end

lib/rspec/mock/task.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require 'rake'
4+
5+
module RSpec
6+
module Mock
7+
class Task
8+
def self.load
9+
::Kernel.load(::File.join(::File.dirname(__FILE__), 'tasks', 'rspec_mock.rake'))
10+
end
11+
end
12+
end
13+
end

lib/rspec/mock/tasks/rspec_mock.rake

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
namespace :rspec_mock do
4+
namespace :migration_analytics do
5+
desc 'Analyze Flexmock usage and track migration progress to RSpec mocks'
6+
# :nocov:
7+
task :flexmock do
8+
require 'rspec/mock/migration_analytics/printer'
9+
10+
path = ::ARGV[1] || 'spec'
11+
puts("\n🔍 Analyzing Flexmock usage in: #{path}")
12+
RSpec::Mock::MigrationAnalytics::Printer.call(path)
13+
end
14+
# :nocov:
15+
end
16+
end

lib/rspec/mock/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module RSpec
44
module Mock
5-
VERSION = '0.3.1'
5+
VERSION = '0.4.0'
66
end
77
end

rspec-mock.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
2727
spec.require_paths = %w[lib]
2828

2929
spec.add_runtime_dependency 'colorize', '>= 0.8.1'
30+
spec.add_runtime_dependency 'rake', '~> 13.2', '>= 13.2.1'
3031
spec.add_runtime_dependency 'rspec-core', '~> 3.10'
3132
spec.add_runtime_dependency 'rspec-mocks', '~> 3.10'
3233
spec.add_runtime_dependency 'terminal-table', '~> 3.0'

spec/rspec/mock/migration_analytics/cli_spec.rb renamed to spec/rspec/mock/migration_analytics/printer_spec.rb

+14-79
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
RSpec.describe RSpec::Mock::MigrationAnalytics::Cli do
3+
RSpec.describe RSpec::Mock::MigrationAnalytics::Printer do
44
let(:sample_file_path) { 'spec/models/user_spec.rb' }
55
let(:sample_dir_path) { 'spec/models' }
66

@@ -12,69 +12,21 @@
1212
end
1313

1414
describe '.call' do
15-
context 'when no arguments are provided' do
16-
before { stub_const('ARGV', []) }
17-
18-
it 'prints usage and exits with status 1' do
19-
expect(described_class).to receive(:print_usage)
20-
expect { described_class.call }.to raise_error(SystemExit) do |error|
21-
expect(error.status).to eq(1)
22-
end
23-
end
24-
end
25-
26-
context 'when path is provided' do
27-
before { stub_const('ARGV', [sample_file_path]) }
28-
29-
it 'verifies the provided path' do
30-
expect(described_class).to receive(:verify_path).with(sample_file_path)
31-
described_class.call
32-
end
33-
34-
context 'when an error occurs' do
35-
let(:error_message) { 'Something went wrong' }
36-
37-
before do
38-
allow(described_class).to receive(:verify_path).and_raise(StandardError.new(error_message))
39-
allow(ENV).to receive(:[]).and_return(nil)
40-
allow(ENV).to receive(:[]).with('COLUMNS').and_return('80')
41-
end
42-
43-
it 'prints the error message in red' do
44-
expect { described_class.call }
45-
.to output(/❌ Error: #{error_message}/).to_stdout
46-
end
47-
48-
context 'when DEBUG env is set' do
49-
before do
50-
allow(ENV).to receive(:[]).with('DEBUG').and_return('true')
51-
end
52-
53-
it 'prints the backtrace' do
54-
expect { described_class.call }
55-
.to output(%r{#{error_message}.*gems/rspec}m).to_stdout
56-
end
57-
end
58-
end
59-
end
60-
end
61-
62-
describe '.verify_path' do
6315
context 'when path is a directory' do
64-
before { allow(File).to receive(:directory?).with(sample_dir_path).and_return(true) }
16+
before { allow(::File).to receive(:directory?).with(sample_dir_path).and_return(true) }
6517

6618
it 'calls verify_directory' do
6719
expect(described_class).to receive(:verify_directory).with(sample_dir_path)
68-
described_class.verify_path(sample_dir_path)
20+
described_class.call(sample_dir_path)
6921
end
7022
end
7123

7224
context 'when path is a file' do
73-
before { allow(File).to receive(:directory?).with(sample_file_path).and_return(false) }
25+
before { allow(::File).to receive(:directory?).with(sample_file_path).and_return(false) }
7426

7527
it 'calls verify_file' do
7628
expect(described_class).to receive(:verify_file).with(sample_file_path)
77-
described_class.verify_path(sample_file_path)
29+
described_class.call(sample_file_path)
7830
end
7931
end
8032
end
@@ -83,7 +35,7 @@
8335
include_context 'with stubbed ENV'
8436

8537
context 'when file does not exist' do
86-
before { allow(File).to receive(:exist?).with(sample_file_path).and_return(false) }
38+
before { allow(::File).to receive(:exist?).with(sample_file_path).and_return(false) }
8739

8840
it 'prints error message' do
8941
expect { described_class.send(:verify_file, sample_file_path) }
@@ -94,7 +46,7 @@
9446
context 'when file is not a spec file' do
9547
let(:non_spec_file) { 'app/models/user.rb' }
9648

97-
before { allow(File).to receive(:exist?).with(non_spec_file).and_return(true) }
49+
before { allow(::File).to receive(:exist?).with(non_spec_file).and_return(true) }
9850

9951
it 'prints warning message' do
10052
expect { described_class.send(:verify_file, non_spec_file) }
@@ -115,7 +67,7 @@
11567
end
11668

11769
before do
118-
allow(File).to receive(:exist?).with(sample_file_path).and_return(true)
70+
allow(::File).to receive(:exist?).with(sample_file_path).and_return(true)
11971
allow(RSpec::Mock::MigrationAnalytics::FileAnalyzer).to receive(:call)
12072
.with(sample_file_path).and_return(analysis_result)
12173
end
@@ -140,7 +92,7 @@
14092
end
14193

14294
before do
143-
allow(File).to receive(:exist?).with(sample_file_path).and_return(true)
95+
allow(::File).to receive(:exist?).with(sample_file_path).and_return(true)
14496
allow(RSpec::Mock::MigrationAnalytics::FileAnalyzer).to receive(:call)
14597
.with(sample_file_path).and_return(analysis_result)
14698
end
@@ -180,14 +132,16 @@
180132
end
181133

182134
before do
183-
allow(Dir).to receive(:glob).with("#{sample_dir_path}/**/*_spec.rb").and_return(spec_files)
184-
allow(RSpec::Mock::MigrationAnalytics::FileAnalyzer).to receive(:call)
135+
allow(::Dir).to receive(:glob).with("#{sample_dir_path}/**/*_spec.rb").and_return(spec_files)
136+
allow(RSpec::Mock::MigrationAnalytics::FileAnalyzer)
137+
.to receive(:call)
185138
.and_return(*analysis_results)
186139
end
187140

188141
it 'analyzes all spec files and prints summary' do
189142
expect { described_class.send(:verify_directory, sample_dir_path) }
190-
.to output(/=== Migration Status Report ===.*Files Requiring Migration/m).to_stdout
143+
.to output(/=== Migration Status Report ===.*Files Requiring Migration/m)
144+
.to_stdout
191145
end
192146
end
193147

@@ -215,23 +169,4 @@
215169
end
216170
end
217171
end
218-
219-
describe '.print_usage' do
220-
include_context 'with stubbed ENV'
221-
222-
it 'prints usage instructions with examples' do
223-
expected_lines = [
224-
'Usage: ruby cli.rb <path_to_spec_file_or_directory>',
225-
'',
226-
'Examples:',
227-
' ruby cli.rb spec/models/user_spec.rb',
228-
' ruby cli.rb spec/models/',
229-
' ruby cli.rb spec/'
230-
]
231-
232-
expect { described_class.send(:print_usage) }
233-
.to output(/#{expected_lines.join('.*')}/m)
234-
.to_stdout
235-
end
236-
end
237172
end

0 commit comments

Comments
 (0)