File: nenv_spec.rb

package info (click to toggle)
ruby-nenv 0.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: ruby: 517; sh: 4; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,193 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
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.method(:reset).call
        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