|
7 | 7 |
|
8 | 8 | context 'when requiring files' do |
9 | 9 | context 'with unsorted index' do |
10 | | - it 'registers an offsense' do |
| 10 | + it 'registers an offsense and autocorrects to add .sort' do |
11 | 11 | expect_offense(<<~RUBY) |
12 | 12 | Dir["./lib/**/*.rb"].each do |file| |
13 | 13 | ^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them. |
14 | 14 | require file |
15 | 15 | end |
16 | 16 | RUBY |
| 17 | + expect_correction(<<~RUBY) |
| 18 | + Dir["./lib/**/*.rb"].sort.each do |file| |
| 19 | + require file |
| 20 | + end |
| 21 | + RUBY |
17 | 22 | end |
18 | 23 |
|
19 | 24 | it 'registers an offsense with extra logic' do |
20 | 25 | expect_offense(<<~RUBY) |
21 | 26 | Dir["./lib/**/*.rb"].each do |file| |
22 | 27 | ^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them. |
23 | | - if file.starts_with('_') |
| 28 | + if file.start_with?('_') |
24 | 29 | puts "Not required." |
25 | 30 | else |
26 | 31 | require file |
27 | 32 | end |
28 | 33 | end |
29 | 34 | RUBY |
30 | | - end |
31 | | - |
32 | | - it 'registers an offense with block passed as parameter' do |
33 | | - pending |
34 | | - expect_offense(<<~RUBY) |
35 | | - Dir["./lib/**/*.rb"].each(&method(:require)) |
36 | | - ^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them. |
37 | | - RUBY |
38 | | - end |
39 | | - |
40 | | - it 'auto-corrects to add .sort' do |
41 | | - new_source = autocorrect_source(<<~RUBY) |
42 | | - Dir["./lib/**/*.rb"].each do |file| |
43 | | - require file |
44 | | - end |
45 | | - RUBY |
46 | | - expect(new_source).to eq(<<~RUBY) |
| 35 | + expect_correction(<<~RUBY) |
47 | 36 | Dir["./lib/**/*.rb"].sort.each do |file| |
48 | | - require file |
| 37 | + if file.start_with?('_') |
| 38 | + puts "Not required." |
| 39 | + else |
| 40 | + require file |
| 41 | + end |
49 | 42 | end |
50 | 43 | RUBY |
51 | 44 | end |
52 | 45 |
|
| 46 | + context 'with require block passed as parameter' do |
| 47 | + it 'registers an offense an autocorrects to add sort' do |
| 48 | + expect_offense(<<~RUBY) |
| 49 | + Dir["./lib/**/*.rb"].each(&method(:require)) |
| 50 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them. |
| 51 | + RUBY |
| 52 | + expect_correction(<<~RUBY) |
| 53 | + Dir["./lib/**/*.rb"].sort.each(&method(:require)) |
| 54 | + RUBY |
| 55 | + end |
| 56 | + end |
| 57 | + |
53 | 58 | context 'with top-level ::Dir' do |
54 | 59 | it 'registers an offense and corrects to add .sort' do |
55 | 60 | expect_offense(<<~RUBY) |
|
68 | 73 | end |
69 | 74 |
|
70 | 75 | context 'with unsorted glob' do |
71 | | - it 'registers an offsense' do |
| 76 | + it 'registers an offsense and autocorrects to add .sort' do |
72 | 77 | expect_offense(<<~RUBY) |
73 | 78 | Dir.glob(Rails.root.join(__dir__, 'test', '*.rb'), File::FNM_DOTMATCH).each do |file| |
74 | 79 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them. |
75 | 80 | require file |
76 | 81 | end |
77 | 82 | RUBY |
78 | | - end |
79 | | - |
80 | | - it 'auto-corrects to add .sort' do |
81 | | - new_source = autocorrect_source(<<~RUBY) |
82 | | - Dir.glob(Rails.root.join(__dir__, 'test', '*.rb'), File::FNM_DOTMATCH).each do |file| |
83 | | - require file |
84 | | - end |
85 | | - RUBY |
86 | | - expect(new_source).to eq(<<~RUBY) |
| 83 | + expect_correction(<<~RUBY) |
87 | 84 | Dir.glob(Rails.root.join(__dir__, 'test', '*.rb'), File::FNM_DOTMATCH).sort.each do |file| |
88 | 85 | require file |
89 | 86 | end |
90 | 87 | RUBY |
91 | 88 | end |
92 | 89 |
|
| 90 | + context 'with require block passed as parameter' do |
| 91 | + it 'registers an offense an autocorrects to add sort' do |
| 92 | + expect_offense(<<~RUBY) |
| 93 | + Dir.glob(Rails.root.join('test', '*.rb')).each(&method(:require)) |
| 94 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them. |
| 95 | + RUBY |
| 96 | + expect_correction(<<~RUBY) |
| 97 | + Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require)) |
| 98 | + RUBY |
| 99 | + end |
| 100 | + end |
| 101 | + |
93 | 102 | context 'with top-level ::Dir' do |
94 | 103 | it 'registers an offense and corrects to add .sort' do |
95 | 104 | expect_offense(<<~RUBY) |
|
108 | 117 | end |
109 | 118 |
|
110 | 119 | context 'with direct block glob' do |
111 | | - it 'registers an offsense' do |
| 120 | + it 'registers an offsense and autocorrects to add .sort.each' do |
112 | 121 | expect_offense(<<~RUBY) |
113 | 122 | Dir.glob("./lib/**/*.rb") do |file| |
114 | 123 | ^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them. |
115 | 124 | require file |
116 | 125 | end |
117 | 126 | RUBY |
118 | | - end |
119 | | - |
120 | | - it 'auto-corrects to add .sort.each' do |
121 | | - new_source = autocorrect_source(<<~RUBY) |
122 | | - Dir.glob("./lib/**/*.rb") do |file| |
123 | | - require file |
124 | | - end |
125 | | - RUBY |
126 | | - expect(new_source).to eq(<<~RUBY) |
| 127 | + expect_correction(<<~RUBY) |
127 | 128 | Dir.glob("./lib/**/*.rb").sort.each do |file| |
128 | 129 | require file |
129 | 130 | end |
130 | 131 | RUBY |
131 | 132 | end |
132 | 133 |
|
| 134 | + context 'with require block passed as parameter' do |
| 135 | + it 'registers an offense and autocorrects to add sort' do |
| 136 | + expect_offense(<<~RUBY) |
| 137 | + Dir.glob( |
| 138 | + ^^^^^^^^^ Sort files before requiring them. |
| 139 | + Rails.root.join('./lib/**/*.rb'), |
| 140 | + File::FNM_DOTMATCH, |
| 141 | + &method(:require) |
| 142 | + ) |
| 143 | + RUBY |
| 144 | + expect_correction(<<~RUBY) |
| 145 | + Dir.glob( |
| 146 | + Rails.root.join('./lib/**/*.rb'), |
| 147 | + File::FNM_DOTMATCH |
| 148 | + ).sort.each(&method(:require)) |
| 149 | + RUBY |
| 150 | + end |
| 151 | + end |
| 152 | + |
133 | 153 | context 'with top-level ::Dir' do |
134 | 154 | it 'registers an offense and corrects to add .sort.each' do |
135 | 155 | expect_offense(<<~RUBY) |
|
138 | 158 | require file |
139 | 159 | end |
140 | 160 | RUBY |
| 161 | + expect_correction(<<~RUBY) |
| 162 | + ::Dir.glob("./lib/**/*.rb").sort.each do |file| |
| 163 | + require file |
| 164 | + end |
| 165 | + RUBY |
141 | 166 | end |
142 | 167 | end |
143 | 168 | end |
|
0 commit comments