|
3 | 3 |
|
4 | 4 | describe AnnotateRoutes do
|
5 | 5 | ROUTE_FILE = 'config/routes.rb'.freeze
|
6 |
| - ANNOTATION_ADDED = "#{ROUTE_FILE} annotated.".freeze |
7 |
| - ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}.".freeze |
8 |
| - FILE_UNCHANGED = "#{ROUTE_FILE} unchanged.".freeze |
| 6 | + |
| 7 | + MESSAGE_ANNOTATED = "#{ROUTE_FILE} was annotated.".freeze |
| 8 | + MESSAGE_UNCHANGED = "#{ROUTE_FILE} was not changed.".freeze |
| 9 | + MESSAGE_NOT_FOUND = "#{ROUTE_FILE} could not be found.".freeze |
| 10 | + MESSAGE_REMOVED = "Annotations were removed from #{ROUTE_FILE}.".freeze |
9 | 11 |
|
10 | 12 | MAGIC_COMMENTS = [
|
11 | 13 | '# encoding: UTF-8',
|
|
31 | 33 |
|
32 | 34 | it 'should check if routes.rb exists' do
|
33 | 35 | expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false)
|
34 |
| - expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb") |
| 36 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_NOT_FOUND) |
35 | 37 | AnnotateRoutes.do_annotations
|
36 | 38 | end
|
37 | 39 |
|
|
50 | 52 |
|
51 | 53 | expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once)
|
52 | 54 |
|
53 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED).at_least(:once) |
| 55 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) |
54 | 56 | end
|
55 | 57 |
|
56 | 58 | context 'without magic comments' do
|
|
202 | 204 | expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
|
203 | 205 | expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
|
204 | 206 | expect(mock_file).to receive(:puts).with("\n# == Route Map\n#\n")
|
205 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 207 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) |
206 | 208 |
|
207 | 209 | AnnotateRoutes.do_annotations
|
208 | 210 | end
|
|
211 | 213 | expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
|
212 | 214 | expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
|
213 | 215 | expect(mock_file).to receive(:puts).with("\n# == Route Map\n#\n")
|
214 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 216 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) |
215 | 217 |
|
216 | 218 | AnnotateRoutes.do_annotations(ignore_routes: 'my_route')
|
217 | 219 | end
|
|
220 | 222 | expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
|
221 | 223 | expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
|
222 | 224 | expect(mock_file).to receive(:puts).with("# == Route Map\n#\n")
|
223 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 225 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) |
224 | 226 |
|
225 | 227 | AnnotateRoutes.do_annotations(position_in_routes: 'top')
|
226 | 228 | end
|
227 | 229 |
|
228 | 230 | it 'should skip annotations if file does already contain annotation' do
|
229 | 231 | expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n# == Route Map\n#\n")
|
230 |
| - expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED) |
| 232 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) |
231 | 233 |
|
232 | 234 | AnnotateRoutes.do_annotations
|
233 | 235 | end
|
|
240 | 242 | MAGIC_COMMENTS.each do |magic_comment|
|
241 | 243 | expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething")
|
242 | 244 | expect(mock_file).to receive(:puts).with("#{magic_comment}\n\n# == Route Map\n#\n\nSomething\n")
|
243 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 245 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) |
244 | 246 | AnnotateRoutes.do_annotations(position_in_routes: 'top')
|
245 | 247 | end
|
246 | 248 | end
|
|
252 | 254 | MAGIC_COMMENTS.each do |magic_comment|
|
253 | 255 | expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething")
|
254 | 256 | expect(mock_file).to receive(:puts).with("#{magic_comment}\nSomething\n\n# == Route Map\n#\n")
|
255 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 257 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) |
256 | 258 | AnnotateRoutes.do_annotations(position_in_routes: 'bottom')
|
257 | 259 | end
|
258 | 260 | end
|
|
261 | 263 | MAGIC_COMMENTS.each do |magic_comment|
|
262 | 264 | expect(File).to receive(:read).with(ROUTE_FILE)
|
263 | 265 | .and_return("#{magic_comment}\n\n# == Route Map\n#\n")
|
264 |
| - expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED) |
| 266 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) |
265 | 267 |
|
266 | 268 | AnnotateRoutes.do_annotations
|
267 | 269 | end
|
|
274 | 276 | expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
|
275 | 277 | expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
|
276 | 278 | expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
|
277 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 279 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) |
278 | 280 | end
|
279 | 281 |
|
280 | 282 | it 'should annotate and add a newline!' do
|
|
295 | 297 | expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
|
296 | 298 | expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("another good line\ngood line")
|
297 | 299 | expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
|
298 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 300 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) |
299 | 301 | end
|
300 | 302 |
|
301 | 303 | it 'should annotate and add a newline!' do
|
|
321 | 323 | before(:each) do
|
322 | 324 | expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
|
323 | 325 | expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
|
324 |
| - expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED) |
| 326 | + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED) |
325 | 327 | end
|
326 | 328 |
|
327 | 329 | it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
|
|
0 commit comments