File: add_specs.patch

package info (click to toggle)
ruby-nenv 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 180 kB
  • sloc: ruby: 517; sh: 4; makefile: 2
file content (449 lines) | stat: -rw-r--r-- 12,111 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
Description: add specs
Author: HIGUCHI Daisuke (VDR dai) <dai@debian.org>
Origin: upstream
Forwarded: not-needed
Last-Update: 2017-11-27

Index: ruby-nenv/spec/lib/nenv/builder_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/builder_spec.rb
@@ -0,0 +1,27 @@
+require 'nenv/builder'
+
+RSpec.describe Nenv::Builder do
+  describe '#build' do
+    before do
+      allow(ENV).to receive(:[]).with('FOO')
+    end
+
+    it 'returns a class with the given methods' do
+      FooEnv = Nenv::Builder.build do
+        create_method(:foo?)
+      end
+      FooEnv.new.foo?
+    end
+
+    context 'with duplicate methods' do
+      it 'fails' do
+        expect do
+          FooEnv = Nenv::Builder.build do
+            create_method(:foo?)
+            create_method(:foo?)
+          end
+        end.to raise_error(Nenv::Environment::AlreadyExistsError)
+      end
+    end
+  end
+end
Index: ruby-nenv/spec/lib/nenv/environment/dumper_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/environment/dumper_spec.rb
@@ -0,0 +1,34 @@
+require 'yaml'
+
+require 'nenv/environment/dumper'
+
+RSpec.describe Nenv::Environment::Dumper do
+  subject { described_class.setup.(value) }
+
+  context "with \"abc\"" do
+    let(:value) { 'abc' }
+    it { is_expected.to eq('abc') }
+  end
+
+  context 'with 123' do
+    let(:value) { 123 }
+    it { is_expected.to eq('123') }
+  end
+
+  context 'with nil' do
+    let(:value) { nil }
+    it { is_expected.to eq(nil) }
+  end
+
+  context 'with a block' do
+    subject do
+      described_class.setup { |data| YAML.dump(data) }.(value)
+    end
+
+    context 'with a yaml string' do
+      let(:value) { { foo: 3 } }
+      let(:yaml) { "---\n:foo: 3\n" }
+      it { is_expected.to eq(yaml) }
+    end
+  end
+end
Index: ruby-nenv/spec/lib/nenv/environment/loader_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/environment/loader_spec.rb
@@ -0,0 +1,59 @@
+require 'yaml'
+require 'nenv/environment/loader'
+
+RSpec.describe Nenv::Environment::Loader do
+  context 'with no block' do
+    subject { described_class.setup(meth).(value) }
+
+    context 'with a normal method' do
+      let(:meth) { :foo }
+
+      context "with \"abc\"" do
+        let(:value) { 'abc' }
+        it { is_expected.to eq('abc') }
+      end
+    end
+
+    context 'with a bool method' do
+      let(:meth) { :foo? }
+
+      %w(1 true y yes TRUE YES foobar).each do |data|
+        context "with #{data.inspect}" do
+          let(:value) { data }
+          it { is_expected.to eq(true) }
+        end
+      end
+
+      %w(0 false n no FALSE NO).each do |data|
+        context "with #{data.inspect}" do
+          let(:value) { data }
+          it { is_expected.to eq(false) }
+        end
+      end
+
+      context 'with nil' do
+        let(:value) { nil }
+        it { is_expected.to eq(nil) }
+      end
+
+      context 'when empty string' do
+        let(:value) { '' }
+        it do
+          expect { subject }.to raise_error(
+            ArgumentError, /Can't convert empty string into Bool/
+          )
+        end
+      end
+    end
+  end
+
+  context 'with a block' do
+    subject do
+      described_class.setup(:foo) { |data| YAML.load(data) }.(value)
+    end
+    context 'with a yaml string' do
+      let(:value) { "--- foo\n...\n" }
+      it { is_expected.to eq('foo') }
+    end
+  end
+end
Index: ruby-nenv/spec/lib/nenv/environment_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/environment_spec.rb
@@ -0,0 +1,160 @@
+require 'yaml'
+
+require 'nenv/environment'
+
+RSpec.describe Nenv::Environment do
+  class MockEnv < Hash # a hash is close enough
+    def []=(k, v)
+      fail TypeError, "no implicit conversion of #{k.class} into String" unless k.respond_to? :to_str
+      fail TypeError, "no implicit conversion of #{v.class} into String" unless v.respond_to? :to_str
+      super(k.to_str, v.to_str)
+    end
+  end
+
+  before { stub_const('ENV', MockEnv.new) }
+  subject { instance }
+
+  shared_examples 'accessor methods' do
+    describe 'predicate method' do
+      before { subject.create_method(:foo?) }
+
+      it 'responds to it' do
+        expect(subject).to respond_to(:foo?)
+      end
+
+      context 'when the method already exists' do
+        let(:error) { described_class::AlreadyExistsError }
+        let(:message) { 'Method :foo? already exists' }
+        specify do
+          expect do
+            subject.create_method(:foo?)
+          end.to raise_error(error, message)
+        end
+      end
+
+      context 'with value stored in ENV' do
+        before { ENV[sample_key] = value }
+
+        describe 'when value is truthy' do
+          let(:value) { 'true' }
+          it 'should return true' do
+            expect(subject.foo?).to eq true
+          end
+        end
+
+        describe 'when value is falsey' do
+          let(:value) { '0' }
+          it 'should return false' do
+            expect(subject.foo?).to eq false
+          end
+        end
+      end
+    end
+
+    describe 'reader method' do
+      context 'when added' do
+        before { subject.create_method(:foo) }
+
+        it 'responds to it' do
+          expect(subject).to respond_to(:foo)
+        end
+
+        context 'when the method already exists' do
+          let(:error) { described_class::AlreadyExistsError }
+          let(:message) { 'Method :foo already exists' }
+          specify do
+            expect do
+              subject.create_method(:foo)
+            end.to raise_error(error, message)
+          end
+        end
+      end
+
+      context 'with value stored in ENV' do
+        before { ENV[sample_key] = value }
+
+        context 'with no block' do
+          before { instance.create_method(:foo) }
+          let(:value) { '123' }
+
+          it 'returns marshalled stored value' do
+            expect(subject.foo).to eq '123'
+          end
+        end
+
+        context 'with block' do
+          before { instance.create_method(:foo) { |data| YAML.load(data) } }
+          let(:value) { "---\n:foo: 5\n" }
+
+          it 'returns unmarshalled stored value' do
+            expect(subject.foo).to eq(foo: 5)
+          end
+        end
+      end
+    end
+
+    describe 'writer method' do
+      context 'when added' do
+        before { subject.create_method(:foo=) }
+
+        it 'responds to it' do
+          expect(subject).to respond_to(:foo=)
+        end
+
+        context 'when the method already exists' do
+          let(:error) { described_class::AlreadyExistsError }
+          let(:message) { 'Method :foo= already exists' }
+          specify do
+            expect do
+              subject.create_method(:foo=)
+            end.to raise_error(error, message)
+          end
+        end
+      end
+
+      describe 'env variable' do
+        after { expect(ENV[sample_key]).to eq result }
+
+        context 'with no block' do
+          before { subject.create_method(:foo=) }
+          let(:result) { '123' }
+
+          it 'stores a converted to string value' do
+            subject.foo = 123
+          end
+        end
+
+        context 'with block' do
+          before { subject.create_method(:foo=) { |data| YAML.dump(data) } }
+          let(:result) { "---\n:foo: 5\n" }
+
+          it 'stores a marshaled value' do
+            subject.foo = { foo: 5 }
+          end
+        end
+      end
+    end
+  end
+
+  context 'with no namespace' do
+    let(:instance) { described_class.new }
+    let(:sample_key) { 'FOO' }
+    include_examples 'accessor methods'
+  end
+
+  context 'with any namespace' do
+    let(:namespace) { 'bar' }
+    let(:sample_key) { 'BAR_FOO' }
+    let(:instance) { described_class.new(namespace) }
+    include_examples 'accessor methods'
+
+    context 'with a method containing underscores' do
+      before { instance.create_method(:foo_baz) }
+
+      it 'reads the correct variable' do
+        ENV['BAR_FOO_BAZ'] = '123'
+        expect(subject.foo_baz).to eq '123'
+      end
+    end
+  end
+end
Index: ruby-nenv/spec/lib/nenv_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv_spec.rb
@@ -0,0 +1,84 @@
+require 'nenv'
+
+RSpec.describe Nenv do
+  let(:env) { instance_double(Hash) } # Hash is close enough
+  before { stub_const('ENV', env) }
+
+  describe 'Nenv() helper method' do
+    it 'reads from env' do
+      expect(ENV).to receive(:[]).with('GIT_BROWSER').and_return('chrome')
+      Nenv('git').browser
+    end
+
+    it 'return the value from env' do
+      allow(ENV).to receive(:[]).with('GIT_BROWSER').and_return('firefox')
+      expect(Nenv('git').browser).to eq('firefox')
+    end
+  end
+
+  describe 'Nenv() helper method with block' do
+    it 'reads from env' do
+      expect(ENV).to receive(:[]).with('GIT_BROWSER').and_return('chrome')
+      Nenv('git') do |git|
+        git.browser
+      end
+    end
+
+    it 'return the value from env' do
+      allow(ENV).to receive(:[]).with('GIT_BROWSER').and_return('firefox')
+      result = nil
+      Nenv('git') do |git|
+        result = git.browser
+      end
+      expect(result).to eq('firefox')
+    end
+  end
+
+  describe 'Nenv module' do
+    it 'reads from env' do
+      expect(ENV).to receive(:[]).with('CI').and_return('true')
+      Nenv.ci?
+    end
+
+    it 'return the value from env' do
+      allow(ENV).to receive(:[]).with('CI').and_return('false')
+      expect(Nenv.ci?).to be(false)
+    end
+
+    context 'with no method' do
+      it 'automatically creates the method' do
+        expect(ENV).to receive(:[]).with('FOO').and_return('true')
+        Nenv.foo?
+      end
+    end
+
+    context 'with existing method' do
+      before do
+        Nenv.instance.create_method(:foo?)
+      end
+
+      it 'reads from env' do
+        expect(ENV).to receive(:[]).with('FOO').and_return('true')
+        Nenv.foo?
+      end
+
+      it 'return the value from env' do
+        expect(ENV).to receive(:[]).with('FOO').and_return('true')
+        expect(Nenv.foo?).to be(true)
+      end
+    end
+  end
+
+  # Test added here to properly test if builder is required
+  describe 'Nenv builder' do
+    before do
+      allow(ENV).to receive(:[]).with('FOO').and_return('false')
+    end
+    it 'is required and works' do
+      FooEnv = Nenv::Builder.build do
+        create_method(:foo?)
+      end
+      FooEnv.new.foo?
+    end
+  end
+end
Index: ruby-nenv/spec/spec_helper.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/spec_helper.rb
@@ -0,0 +1,49 @@
+require 'coveralls'
+Coveralls.wear!
+
+RSpec.configure do |config|
+  config.expect_with :rspec do |expectations|
+    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
+  end
+
+  config.mock_with :rspec do |mocks|
+    mocks.verify_partial_doubles = true
+  end
+
+  config.filter_run focus: ENV['CI'] != 'true'
+  config.run_all_when_everything_filtered = true
+
+  config.disable_monkey_patching!
+
+  # config.warnings = true
+
+  config.default_formatter = 'doc' if config.files_to_run.one?
+
+  # config.profile_examples = 10
+
+  config.order = :random
+
+  Kernel.srand config.seed
+
+  config.before do
+    allow(ENV).to receive(:[]) do |key|
+      fail "stub me: ENV[#{key.inspect}]"
+    end
+
+    allow(ENV).to receive(:[]=) do |key, value|
+      fail "stub me: ENV[#{key.inspect}] = #{value.inspect}"
+    end
+
+    allow(ENV).to receive(:[]).with('PRYRC').and_call_original
+    allow(ENV).to receive(:[]).with('DISABLE_PRY').and_call_original
+    allow(ENV).to receive(:[]).with('ANSICON').and_call_original
+    allow(ENV).to receive(:[]).with('TERM').and_call_original
+  end
+
+  config.after do
+    begin
+      Nenv.method(:reset).call
+    rescue NameError
+    end
+  end
+end