File: yaml_repository_spec.rb

package info (click to toggle)
ruby-feature 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 184 kB
  • sloc: ruby: 760; sh: 20; makefile: 9
file content (160 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'
require 'tempfile'

include Feature::Repository

describe Feature::Repository::YamlRepository do
  context 'proper config file' do
    before(:each) do
      @filename = Tempfile.new(['feature_config', '.yaml']).path
      fp = File.new(@filename, 'w')
      fp.write <<"EOF"
features:
    feature_a_active: true
    feature_b_active: true
    feature_c_inactive: false
    feature_d_inactive: false
EOF
      fp.close

      @repo = YamlRepository.new(@filename)
    end

    after(:each) do
      File.delete(@filename)
    end

    it 'should read active features from a config file' do
      expect(@repo.active_features).to eq([:feature_a_active, :feature_b_active])
    end

    context 're-read config file' do
      before(:each) do
        fp = File.new(@filename, 'w')
        fp.write <<"EOF"
features:
    feature_a_active: true
    feature_c_inactive: false
EOF
        fp.close
      end

      it 'should read active features new on each request' do
        expect(@repo.active_features).to eq([:feature_a_active])
      end
    end

    context 'with no features' do
      before(:each) do
        fp = File.new(@filename, 'w')
        fp.write <<"EOF"
features:
EOF
        fp.close
      end

      it 'should read active features new on each request' do
        expect(@repo.active_features).to eq([])
      end
    end

    context 'with optional environment name' do
      before(:each) do
        fp = File.new(@filename, 'w')
        fp.write <<"EOF"
development:
  features:
      feature_a: true
      feature_b: true
production:
  features:
      feature_a: true
      feature_b: false
EOF
        fp.close
      end

      it 'has two active features for development environment' do
        repo = YamlRepository.new(@filename, 'development')
        expect(repo.active_features).to eq([:feature_a, :feature_b])
      end

      it 'has one active feature for production environment' do
        repo = YamlRepository.new(@filename, 'production')
        expect(repo.active_features).to eq([:feature_a])
      end
    end

    # Sometimes needed when loading features from ENV variables or are time
    # based rules Ex: Date.today > Date.strptime('1/2/2012', '%d/%m/%Y')
    context 'a config file with embedded erb' do
      before(:each) do
        @filename = Tempfile.new(['feature_config', '.yaml']).path
        fp = File.new(@filename, 'w')
        fp.write <<"EOF"
features:
    feature_a_active: <%= 'true' == 'true' %>
    feature_b_active: true
    feature_c_inactive: <%= false %>
    feature_d_inactive: <%= 1 < 0 %>
EOF
        fp.close

        @repo = YamlRepository.new(@filename)
      end

      it 'should read active features from the config file' do
        expect(@repo.active_features).to eq([:feature_a_active, :feature_b_active])
      end
    end
  end

  it 'should raise exception on no file found' do
    repo = YamlRepository.new('/this/file/should/not/exist')
    expect do
      repo.active_features
    end.to raise_error(Errno::ENOENT, /No such file or directory/)
  end

  it 'should raise exception on invalid yaml' do
    @filename = Tempfile.new(['feature_config', '.yaml']).path
    fp = File.new(@filename, 'w')
    fp.write 'this is not valid feature config'
    fp.close

    repo = YamlRepository.new(@filename)
    expect do
      repo.active_features
    end.to raise_error(ArgumentError, 'yaml config does not contain proper config')
  end

  it 'should raise exception on yaml without features key' do
    @filename = Tempfile.new(['feature_config', '.yaml']).path
    fp = File.new(@filename, 'w')
    fp.write <<"EOF"
fail:
    feature: true
EOF
    fp.close

    repo = YamlRepository.new(@filename)
    expect do
      repo.active_features
    end.to raise_error(ArgumentError, 'yaml config does not contain proper config')
  end

  it 'should raise exception on not true/false value in config' do
    @filename = Tempfile.new(['feature_config', '.yaml']).path
    fp = File.new(@filename, 'w')
    fp.write <<"EOF"
features:
    invalid_feature: neither_true_or_false
EOF
    fp.close

    repo = YamlRepository.new(@filename)
    expect do
      repo.active_features
    end.to raise_error(ArgumentError, 'neither_true_or_false is not allowed value in config, use true/false')
  end
end