dry-system 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f148327cdac0f870ac0ac39a244d59cc2edfb15
4
- data.tar.gz: d712c57e08f1968fb7092a99e3c84fe706bb4097
3
+ metadata.gz: 94d4f0b0085ef8e7edf3938fe94d39f4c26d2ea9
4
+ data.tar.gz: 15a9499b38c9d1a8ef109728d86b577616dad388
5
5
  SHA512:
6
- metadata.gz: 9fa712dbe64d87958faa6eaac6fc7dfbbf0d5b30e20b1cc52c196c2159db05380fdb45da00c39b52f02488db0aebaebbc65602fab66cbc091406ac73fe44c354
7
- data.tar.gz: d5214fded99b89eed583d9fbb6397432d73407eba966bfab7026a8007d9670e7b214e3a50b07ffa964a84a0a6d1b349abae7b818e3d71b57e57851edc1ea5dde
6
+ metadata.gz: 4979edbd2d31b3039bc25438a2340462ce94e946e9ceeded7003c488e8322209a07b1d58ccfd5419a922ac1a96cf326bf8dddc5fa22069b82387e9578564a944
7
+ data.tar.gz: 887221bf029d0b6cf26f32e06ff8ac8d642e9cdc289cf8428351f8185a73161900ee26ee762d778f1912c0f7926c936e397cef28db3afb4b2d2c5ddda38518de
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 0.7.3 - 2017-08-02
2
+
3
+ ### Fixed
4
+
5
+ * `Container.enable_stubs!` calls super too, which actually adds `stub` API (solnic)
6
+ * Issues with lazy-loading and import in stub mode are gone (solnic)
7
+
1
8
  # 0.7.2 - 2017-08-02
2
9
 
3
10
  ### Added
@@ -80,6 +80,7 @@ module Dry
80
80
 
81
81
  class << self
82
82
  extend Dry::Core::Deprecations['Dry::System::Container']
83
+
83
84
  # Configures the container
84
85
  #
85
86
  # @example
@@ -213,6 +214,15 @@ module Dry
213
214
  self
214
215
  end
215
216
 
217
+ # Return if a container was finalized
218
+ #
219
+ # @return [TrueClass, FalseClass]
220
+ #
221
+ # @api public
222
+ def finalized?
223
+ @__finalized__.equal?(true)
224
+ end
225
+
216
226
  # Finalizes the container
217
227
  #
218
228
  # This triggers importing components from other containers, booting
@@ -243,7 +253,7 @@ module Dry
243
253
  #
244
254
  # @api public
245
255
  def finalize!(freeze: true, &block)
246
- return self if frozen?
256
+ return self if finalized?
247
257
 
248
258
  yield(self) if block
249
259
 
@@ -252,6 +262,8 @@ module Dry
252
262
  manual_registrar.finalize!
253
263
  auto_registrar.finalize!
254
264
 
265
+ @__finalized__ = true
266
+
255
267
  self.freeze if freeze
256
268
  end
257
269
 
@@ -433,7 +445,7 @@ module Dry
433
445
 
434
446
  # @api public
435
447
  def resolve(key)
436
- load_component(key) unless frozen?
448
+ load_component(key) unless finalized?
437
449
 
438
450
  super
439
451
  end
@@ -509,7 +521,7 @@ module Dry
509
521
  # @api private
510
522
  def load_local_component(component, default_namespace_fallback = false)
511
523
  if component.bootable?(booter.path) || component.file_exists?(load_paths)
512
- booter.boot_dependency(component) unless frozen?
524
+ booter.boot_dependency(component) unless finalized?
513
525
 
514
526
  require_component(component) do
515
527
  register(component.identifier) { component.instance }
@@ -1,10 +1,8 @@
1
+ require 'dry/container/stub'
2
+
1
3
  module Dry
2
4
  module System
3
5
  class Container
4
- # Incuded only in the Test environment
5
- # Sending the message enable_stubs! allow you to stub components after
6
- # finalize your container in your tests.
7
- #
8
6
  # @api private
9
7
  module Stubs
10
8
  def finalize!(&block)
@@ -12,8 +10,23 @@ module Dry
12
10
  end
13
11
  end
14
12
 
13
+ # Enables stubbing container's components
14
+ #
15
+ # @example
16
+ # require 'dry/system/stubs'
17
+ #
18
+ # MyContainer.enable_stubs!
19
+ # MyContainer.finalize!
20
+ #
21
+ # MyContainer.stub('some.component', some_stub_object)
22
+ #
23
+ # @return Container
24
+ #
25
+ # @api public
15
26
  def self.enable_stubs!
27
+ super
16
28
  extend ::Dry::System::Container::Stubs
29
+ self
17
30
  end
18
31
  end
19
32
  end
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module System
3
- VERSION = '0.7.2'.freeze
3
+ VERSION = '0.7.3'.freeze
4
4
  end
5
5
  end
@@ -1,4 +1,4 @@
1
- module Stubbing
1
+ module Test
2
2
  class Car
3
3
  def wheels_count
4
4
  4
@@ -0,0 +1,8 @@
1
+ Test::Container.finalize :db do |container|
2
+ module Test
3
+ class DB
4
+ end
5
+ end
6
+
7
+ container.register(:db, Test::DB.new)
8
+ end
@@ -1,3 +1,5 @@
1
+ require 'dry/system/stubs'
2
+
1
3
  RSpec.describe 'Lazy-booting external deps' do
2
4
  before do
3
5
  module Test
@@ -42,6 +44,14 @@ RSpec.describe 'Lazy-booting external deps' do
42
44
  end
43
45
 
44
46
  it_behaves_like 'lazy booted dependency'
47
+
48
+ context 'when stubs are enabled' do
49
+ before do
50
+ system.enable_stubs!
51
+ end
52
+
53
+ it_behaves_like 'lazy booted dependency'
54
+ end
45
55
  end
46
56
 
47
57
  context 'when top-level container provides the dependency through import' do
@@ -59,5 +69,13 @@ RSpec.describe 'Lazy-booting external deps' do
59
69
  end
60
70
 
61
71
  it_behaves_like 'lazy booted dependency'
72
+
73
+ context 'when stubs are enabled' do
74
+ before do
75
+ system.enable_stubs!
76
+ end
77
+
78
+ it_behaves_like 'lazy booted dependency'
79
+ end
62
80
  end
63
81
  end
@@ -1,4 +1,5 @@
1
1
  require 'dry/system/container'
2
+ require 'dry/system/stubs'
2
3
 
3
4
  RSpec.describe Dry::System::Container do
4
5
  subject(:container) { Test::Container }
@@ -196,46 +197,47 @@ RSpec.describe Dry::System::Container do
196
197
  end
197
198
  end
198
199
 
199
- context 'Allow to stub container' do
200
+ describe '.stub' do
201
+ let(:stubbed_car) do
202
+ instance_double(Test::Car, wheels_count: 5)
203
+ end
204
+
200
205
  before do
201
206
  class Test::Container < Dry::System::Container
202
207
  configure do |config|
203
208
  config.root = SPEC_ROOT.join('fixtures/stubbing').realpath
204
209
  end
210
+
205
211
  load_paths!('lib')
206
212
  auto_register!('lib')
207
213
  end
208
214
  end
209
215
 
210
- describe 'without enable_stubs!' do
211
- before do
212
- container.finalize!
213
- end
214
-
215
- it 'raises error when trying to stub freeze container' do
216
- expect {
217
- allow(container).to receive(:[]).with('mock').and_return(true)
218
- }.to raise_error(RuntimeError, /frozen/)
216
+ describe 'with stubs disabled' do
217
+ it 'raises error when trying to stub frozen container' do
218
+ expect { container.stub('test.car', stubbed_car) }.to raise_error(NoMethodError, /stub/)
219
219
  end
220
220
  end
221
221
 
222
- describe 'with enable_stubs!' do
222
+ describe 'with stubs enabled' do
223
223
  before do
224
224
  container.enable_stubs!
225
- container.finalize!
226
225
  end
227
226
 
228
- it 'allows to stub the container it self' do
229
- expect(container['mock']).to eq false
230
- allow(container).to receive(:[]).with('mock').and_return(true)
231
- expect(container['mock']).to eq true
227
+ it 'lazy-loads a component' do
228
+ expect(container[:db]).to be_instance_of(Test::DB)
229
+ container.finalize!
230
+ expect(container[:db]).to be_instance_of(Test::DB)
232
231
  end
233
232
 
234
233
  it 'allows to stub components' do
235
- car = container['stubbing.car']
236
- expect(car.wheels_count).to eq 4
237
- allow(car).to receive(:wheels_count).and_return(5)
238
- expect(car.wheels_count).to eq 5
234
+ container.finalize!
235
+
236
+ expect(container['test.car'].wheels_count).to be(4)
237
+
238
+ container.stub('test.car', stubbed_car)
239
+
240
+ expect(container['test.car'].wheels_count).to be(5)
239
241
  end
240
242
  end
241
243
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-system
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
@@ -232,8 +232,8 @@ files:
232
232
  - spec/fixtures/other/lib/test/models.rb
233
233
  - spec/fixtures/other/lib/test/models/book.rb
234
234
  - spec/fixtures/other/lib/test/models/user.rb
235
- - spec/fixtures/stubbing/lib/stubbing/car.rb
236
- - spec/fixtures/stubbing/system/boot/mock.rb
235
+ - spec/fixtures/stubbing/lib/test/car.rb
236
+ - spec/fixtures/stubbing/system/boot/db.rb
237
237
  - spec/fixtures/test/config/application.yml
238
238
  - spec/fixtures/test/config/subapp.yml
239
239
  - spec/fixtures/test/lib/test/dep.rb
@@ -317,8 +317,8 @@ test_files:
317
317
  - spec/fixtures/other/lib/test/models.rb
318
318
  - spec/fixtures/other/lib/test/models/book.rb
319
319
  - spec/fixtures/other/lib/test/models/user.rb
320
- - spec/fixtures/stubbing/lib/stubbing/car.rb
321
- - spec/fixtures/stubbing/system/boot/mock.rb
320
+ - spec/fixtures/stubbing/lib/test/car.rb
321
+ - spec/fixtures/stubbing/system/boot/db.rb
322
322
  - spec/fixtures/test/config/application.yml
323
323
  - spec/fixtures/test/config/subapp.yml
324
324
  - spec/fixtures/test/lib/test/dep.rb
@@ -1,3 +0,0 @@
1
- Test::Container.finalize(:mock) do |container|
2
- container.register(:mock, false)
3
- end