File: reloader_spec.rb

package info (click to toggle)
ruby-sinatra 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: ruby: 17,700; sh: 25; makefile: 8
file content (497 lines) | stat: -rw-r--r-- 15,720 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
require 'spec_helper'
require 'fileutils'

RSpec.describe Sinatra::Reloader do
  # Returns the temporary directory.
  def tmp_dir
    File.expand_path('../tmp', __dir__)
  end

  # Returns the path of the Sinatra application file created by
  # +setup_example_app+.
  def app_file_path
    File.join(tmp_dir, "example_app_#{$example_app_counter}.rb")
  end

  # Returns the name of the Sinatra application created by
  # +setup_example_app+: 'ExampleApp1' for the first application,
  # 'ExampleApp2' fo the second one, and so on...
  def app_name
    "ExampleApp#{$example_app_counter}"
  end

  # Returns the (constant of the) Sinatra application created by
  # +setup_example_app+.
  def app_const
    Module.const_get(app_name)
  end

  # Writes a file with a Sinatra application using the template
  # located at <tt>specs/reloader/app.rb.erb</tt>.  It expects an
  # +options+ hash, with an array of strings containing the
  # application's routes (+:routes+ key), a hash with the inline
  # template's names as keys and the bodys as values
  # (+:inline_templates+ key) and an optional application name
  # (+:name+) otherwise +app_name+ is used.
  #
  # It ensures to change the written file's mtime when it already
  # exists.
  def write_app_file(options={})
    options[:routes] ||= ['get("/foo") { erb :foo }']
    options[:inline_templates] ||= nil
    options[:extensions] ||= []
    options[:middlewares] ||= []
    options[:filters] ||= []
    options[:errors] ||= {}
    options[:name] ||= app_name
    options[:enable_reloader] = true unless options[:enable_reloader] === false
    options[:parent] ||= 'Sinatra::Base'

    update_file(app_file_path) do |f|
      template_path = File.expand_path('reloader/app.rb.erb', __dir__)
      template = Tilt.new(template_path, nil, :trim => '<>')
      f.write template.render(Object.new, options)
    end
  end

  alias update_app_file write_app_file

  # It calls <tt>File.open(path, 'w', &block)</tt> all the times
  # needed to change the file's mtime.
  def update_file(path, &block)
    original_mtime = File.exist?(path) ? File.mtime(path) : Time.at(0)
    new_time = original_mtime + 1
    File.open(path, 'w', &block)
    File.utime(new_time, new_time, path)
  end

  # Writes a Sinatra application to a file, requires the file, sets
  # the new application as the one being tested and enables the
  # reloader.
  def setup_example_app(options={})
    $example_app_counter ||= 0
    $example_app_counter += 1

    FileUtils.mkdir_p(tmp_dir)
    write_app_file(options)
    $LOADED_FEATURES.delete app_file_path
    require app_file_path
    self.app = app_const
    app_const.enable :reloader
  end

  after(:all) { FileUtils.rm_rf(tmp_dir) }

  describe "default route reloading mechanism" do
    before(:each) do
      setup_example_app(:routes => ['get("/foo") { "foo" }'])
    end

    it "doesn't mess up the application" do
      expect(get('/foo').body).to eq('foo')
    end

    it "knows when a route has been modified" do
      update_app_file(:routes => ['get("/foo") { "bar" }'])
      expect(get('/foo').body).to eq('bar')
    end

    it "knows when a route has been added" do
      update_app_file(
        :routes => ['get("/foo") { "foo" }', 'get("/bar") { "bar" }']
      )
      expect(get('/foo').body).to eq('foo')
      expect(get('/bar').body).to eq('bar')
    end

    it "knows when a route has been removed" do
      update_app_file(:routes => ['get("/bar") { "bar" }'])
      expect(get('/foo').status).to eq(404)
    end

    it "doesn't try to reload a removed file" do
      update_app_file(:routes => ['get("/foo") { "i shall not be reloaded" }'])
      FileUtils.rm app_file_path
      expect(get('/foo').body.strip).to eq('foo')
    end
  end

  describe "default inline templates reloading mechanism" do
    before(:each) do
      setup_example_app(
        :routes => ['get("/foo") { erb :foo }'],
        :inline_templates => { :foo => 'foo' }
      )
    end

    it "doesn't mess up the application" do
      expect(get('/foo').body.strip).to eq('foo')
    end

    it "reloads inline templates in the app file" do
      update_app_file(
        :routes => ['get("/foo") { erb :foo }'],
        :inline_templates => { :foo => 'bar' }
      )
      expect(get('/foo').body.strip).to eq('bar')
    end

    it "reloads inline templates in other file" do
      setup_example_app(:routes => ['get("/foo") { erb :foo }'])
      template_file_path = File.join(tmp_dir, 'templates.rb')
      File.open(template_file_path, 'w') do |f|
        f.write "__END__\n\n@@foo\nfoo"
      end
      require template_file_path
      app_const.inline_templates= template_file_path
      expect(get('/foo').body.strip).to eq('foo')
      update_file(template_file_path) do |f|
        f.write "__END__\n\n@@foo\nbar"
      end
      expect(get('/foo').body.strip).to eq('bar')
    end
  end

  describe "default middleware reloading mechanism" do
    it "knows when a middleware has been added" do
      setup_example_app(:routes => ['get("/foo") { "foo" }'])
      update_app_file(
        :routes => ['get("/foo") { "foo" }'],
        :middlewares => [Rack::Head]
      )
      get('/foo') # ...to perform the reload
      expect(app_const.middleware).not_to be_empty
    end

    it "knows when a middleware has been removed" do
      setup_example_app(
        :routes => ['get("/foo") { "foo" }'],
        :middlewares => [Rack::Head]
      )
      update_app_file(:routes => ['get("/foo") { "foo" }'])
      get('/foo') # ...to perform the reload
      expect(app_const.middleware).to be_empty
    end
  end

  describe "default filter reloading mechanism" do
    it "knows when a before filter has been added" do
      setup_example_app(:routes => ['get("/foo") { "foo" }'])
      expect {
        update_app_file(
          :routes => ['get("/foo") { "foo" }'],
          :filters => ['before { @hi = "hi" }']
        )
        get('/foo') # ...to perform the reload
      }.to change { app_const.filters[:before].size }.by(1)
    end

    it "knows when an after filter has been added" do
      setup_example_app(:routes => ['get("/foo") { "foo" }'])
      expect {
        update_app_file(
          :routes => ['get("/foo") { "foo" }'],
          :filters => ['after { @bye = "bye" }']
        )
        get('/foo') # ...to perform the reload
      }.to change { app_const.filters[:after].size }.by(1)
    end

    it "knows when a before filter has been removed" do
      setup_example_app(
        :routes => ['get("/foo") { "foo" }'],
        :filters => ['before { @hi = "hi" }']
      )
      expect {
        update_app_file(:routes => ['get("/foo") { "foo" }'])
        get('/foo') # ...to perform the reload
      }.to change { app_const.filters[:before].size }.by(-1)
    end

    it "knows when an after filter has been removed" do
      setup_example_app(
        :routes => ['get("/foo") { "foo" }'],
        :filters => ['after { @bye = "bye" }']
      )
      expect {
        update_app_file(:routes => ['get("/foo") { "foo" }'])
        get('/foo') # ...to perform the reload
      }.to change { app_const.filters[:after].size }.by(-1)
    end
  end

  describe "error reloading" do
    before do
      setup_example_app(
        :routes => ['get("/secret") { 403 }'],
        :errors => { 403 => "'Access forbiden'" }
      )
    end

    it "doesn't mess up the application" do
      expect(get('/secret')).to be_client_error
      expect(get('/secret').body.strip).to eq('Access forbiden')
    end

    it "knows when a error has been added" do
      update_app_file(:errors => { 404 => "'Nowhere'" })
      expect(get('/nowhere')).to be_not_found
      expect(get('/nowhere').body).to eq('Nowhere')
    end

    it "knows when a error has been removed" do
      update_app_file(:routes => ['get("/secret") { 403 }'])
      expect(get('/secret')).to be_client_error
      expect(get('/secret').body).not_to eq('Access forbiden')
    end

    it "knows when a error has been modified" do
      update_app_file(
        :routes => ['get("/secret") { 403 }'],
        :errors => { 403 => "'What are you doing here?'" }
      )
      expect(get('/secret')).to be_client_error
      expect(get('/secret').body).to eq('What are you doing here?')
    end
  end

  describe "extension reloading" do
    it "doesn't duplicate routes with every reload" do
      module ::RouteExtension
        def self.registered(klass)
          klass.get('/bar') { 'bar' }
        end
      end

      setup_example_app(
        :routes => ['get("/foo") { "foo" }'],
        :extensions => ['RouteExtension']
      )

      expect {
        update_app_file(
          :routes => ['get("/foo") { "foo" }'],
          :extensions => ['RouteExtension']
        )
        get('/foo') # ...to perform the reload
      }.to_not change { app_const.routes['GET'].size }
    end

    it "doesn't duplicate middleware with every reload" do
      module ::MiddlewareExtension
        def self.registered(klass)
          klass.use Rack::Head
        end
      end

      setup_example_app(
        :routes => ['get("/foo") { "foo" }'],
        :extensions => ['MiddlewareExtension']
      )

      expect {
        update_app_file(
          :routes => ['get("/foo") { "foo" }'],
          :extensions => ['MiddlewareExtension']
        )
        get('/foo') # ...to perform the reload
      }.to_not change { app_const.middleware.size }
    end

    it "doesn't duplicate before filters with every reload" do
      module ::BeforeFilterExtension
        def self.registered(klass)
          klass.before { @hi = 'hi' }
        end
      end

      setup_example_app(
        :routes => ['get("/foo") { "foo" }'],
        :extensions => ['BeforeFilterExtension']
      )

      expect {
        update_app_file(
          :routes => ['get("/foo") { "foo" }'],
          :extensions => ['BeforeFilterExtension']
        )
        get('/foo') # ...to perform the reload
      }.to_not change { app_const.filters[:before].size }
    end

    it "doesn't duplicate after filters with every reload" do
      module ::AfterFilterExtension
        def self.registered(klass)
          klass.after { @bye = 'bye' }
        end
      end

      setup_example_app(
        :routes => ['get("/foo") { "foo" }'],
        :extensions => ['AfterFilterExtension']
      )

      expect {
        update_app_file(
          :routes => ['get("/foo") { "foo" }'],
          :extensions => ['AfterFilterExtension']
        )
        get('/foo') # ...to perform the reload
      }.to_not change { app_const.filters[:after].size }
    end
  end

  describe ".dont_reload" do
    before(:each) do
      setup_example_app(
        :routes => ['get("/foo") { erb :foo }'],
        :inline_templates => { :foo => 'foo' }
      )
    end

    it "allows to specify a file to stop from being reloaded" do
      app_const.dont_reload app_file_path
      update_app_file(:routes => ['get("/foo") { "bar" }'])
      expect(get('/foo').body.strip).to eq('foo')
    end

    it "allows to specify a glob to stop matching files from being reloaded" do
      app_const.dont_reload '**/*.rb'
      update_app_file(:routes => ['get("/foo") { "bar" }'])
      expect(get('/foo').body.strip).to eq('foo')
    end

    it "doesn't interfere with other application's reloading policy" do
      app_const.dont_reload '**/*.rb'
      setup_example_app(:routes => ['get("/foo") { "foo" }'])
      update_app_file(:routes => ['get("/foo") { "bar" }'])
      expect(get('/foo').body.strip).to eq('bar')
    end
  end

  describe ".also_reload" do
    before(:each) do
      setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
      @foo_path = File.join(tmp_dir, 'foo.rb')
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "foo" end end'
      end
      $LOADED_FEATURES.delete @foo_path
      require @foo_path
      app_const.also_reload @foo_path
    end

    it "allows to specify a file to be reloaded" do
      expect(get('/foo').body.strip).to eq('foo')
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "bar" end end'
      end
      expect(get('/foo').body.strip).to eq('bar')
    end

    it "allows to specify glob to reaload matching files" do
      expect(get('/foo').body.strip).to eq('foo')
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "bar" end end'
      end
      expect(get('/foo').body.strip).to eq('bar')
    end

    it "doesn't try to reload a removed file" do
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "bar" end end'
      end
      FileUtils.rm @foo_path
      expect(get('/foo').body.strip).to eq('foo')
    end

    it "doesn't interfere with other application's reloading policy" do
      app_const.also_reload '**/*.rb'
      setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
      expect(get('/foo').body.strip).to eq('foo')
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "bar" end end'
      end
      expect(get('/foo').body.strip).to eq('foo')
    end
  end

  describe ".after_reload" do
    before(:each) do
      $reloaded = nil
      setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
      @foo_path = File.join(tmp_dir, 'foo.rb')
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "foo" end end'
      end
      $LOADED_FEATURES.delete @foo_path
      require @foo_path
      app_const.also_reload @foo_path
    end

    it "allows block execution after reloading files" do
      app_const.after_reload do |files|
        $reloaded = files
      end
      expect($reloaded).to eq(nil)
      expect(get('/foo').body.strip).to eq('foo')
      expect($reloaded).to eq(nil) # after_reload was not called
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "bar" end end'
      end
      expect(get("/foo").body.strip).to eq("bar") # Makes the reload happen
      expect($reloaded.size).to eq(1)
      expect(File.basename($reloaded[0])).to eq(File.basename(@foo_path))
    end

    it "does not break block without input param" do
      app_const.after_reload do
        $reloaded = "worked without param"
      end
      expect($reloaded).to eq(nil)
      expect(get('/foo').body.strip).to eq('foo')
      expect($reloaded).to eq(nil) # after_reload was not called
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "bar" end end'
      end
      expect { get("/foo") }.to_not raise_error # Makes the reload happen
      expect($reloaded).to eq("worked without param")
    end

    it "handles lambdas with arity 0" do
      user_proc = -> { $reloaded = "lambda?=true arity=0" }
      expect { user_proc.call(1) }.to raise_error(ArgumentError) # What we avoid
      app_const.after_reload(&user_proc)
      expect($reloaded).to eq(nil)
      expect(get('/foo').body.strip).to eq('foo')
      expect($reloaded).to eq(nil) # after_reload was not called
      update_file(@foo_path) do |f|
        f.write 'class Foo; def self.foo() "bar" end end'
      end
      expect { get("/foo") }.to_not raise_error # Makes the reload happen
      expect($reloaded).to eq("lambda?=true arity=0")
    end
  end

  it "automatically registers the reloader in the subclasses" do
    class ::Parent < Sinatra::Base
      register Sinatra::Reloader
      enable :reloader
    end

    setup_example_app(
      :routes => ['get("/foo") { "foo" }'],
      :enable_reloader => false,
      :parent => 'Parent'
    )

    update_app_file(
      :routes => ['get("/foo") { "bar" }'],
      :enable_reloader => false,
      :parent => 'Parent'
    )

    expect(get('/foo').body).to eq('bar')
  end

end