|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -RSpec.describe RSpec::Mock::MigrationAnalytics::Cli do |
| 3 | +RSpec.describe RSpec::Mock::MigrationAnalytics::Printer do |
4 | 4 | let(:sample_file_path) { 'spec/models/user_spec.rb' }
|
5 | 5 | let(:sample_dir_path) { 'spec/models' }
|
6 | 6 |
|
|
12 | 12 | end
|
13 | 13 |
|
14 | 14 | 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 |
63 | 15 | 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) } |
65 | 17 |
|
66 | 18 | it 'calls verify_directory' do
|
67 | 19 | 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) |
69 | 21 | end
|
70 | 22 | end
|
71 | 23 |
|
72 | 24 | 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) } |
74 | 26 |
|
75 | 27 | it 'calls verify_file' do
|
76 | 28 | 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) |
78 | 30 | end
|
79 | 31 | end
|
80 | 32 | end
|
|
83 | 35 | include_context 'with stubbed ENV'
|
84 | 36 |
|
85 | 37 | 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) } |
87 | 39 |
|
88 | 40 | it 'prints error message' do
|
89 | 41 | expect { described_class.send(:verify_file, sample_file_path) }
|
|
94 | 46 | context 'when file is not a spec file' do
|
95 | 47 | let(:non_spec_file) { 'app/models/user.rb' }
|
96 | 48 |
|
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) } |
98 | 50 |
|
99 | 51 | it 'prints warning message' do
|
100 | 52 | expect { described_class.send(:verify_file, non_spec_file) }
|
|
115 | 67 | end
|
116 | 68 |
|
117 | 69 | 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) |
119 | 71 | allow(RSpec::Mock::MigrationAnalytics::FileAnalyzer).to receive(:call)
|
120 | 72 | .with(sample_file_path).and_return(analysis_result)
|
121 | 73 | end
|
|
140 | 92 | end
|
141 | 93 |
|
142 | 94 | 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) |
144 | 96 | allow(RSpec::Mock::MigrationAnalytics::FileAnalyzer).to receive(:call)
|
145 | 97 | .with(sample_file_path).and_return(analysis_result)
|
146 | 98 | end
|
|
180 | 132 | end
|
181 | 133 |
|
182 | 134 | 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) |
185 | 138 | .and_return(*analysis_results)
|
186 | 139 | end
|
187 | 140 |
|
188 | 141 | it 'analyzes all spec files and prints summary' do
|
189 | 142 | 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 |
191 | 145 | end
|
192 | 146 | end
|
193 | 147 |
|
|
215 | 169 | end
|
216 | 170 | end
|
217 | 171 | 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 |
237 | 172 | end
|
0 commit comments