-
Notifications
You must be signed in to change notification settings - Fork 13
/
configuration_spec.rb
195 lines (159 loc) · 6.38 KB
/
configuration_spec.rb
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
require 'fileutils'
require 'spec_helper'
describe Gemnasium::Configuration do
describe 'default config' do
it { expect(Gemnasium::Configuration::DEFAULT_CONFIG['site']).to eql 'gemnasium.com' }
it { expect(Gemnasium::Configuration::DEFAULT_CONFIG['use_ssl']).to eql true }
it { expect(Gemnasium::Configuration::DEFAULT_CONFIG['api_version']).to eql 'v3' }
it { expect(Gemnasium::Configuration::DEFAULT_CONFIG['ignored_paths']).to eql [] }
end
let(:config_file_path) { 'tmp/config.yml' }
let(:config) { described_class.new File.expand_path(config_file_path) }
let(:config_options) do
{
api_key: 'api_key',
project_name: 'gemnasium-gem',
project_branch: 'master',
ignored_paths: ['spec/','tmp/*.lock', '*.gemspec']
}
end
def write_config_file
config_hash = config_options.reduce({}) do |memo, (key,value)|
memo ||= {}
memo[key.to_s] = value
memo
end
File.open(config_file_path, 'w+') { |f| f.write(config_hash.to_yaml) }
end
after do
File.delete(config_file_path) if File.exist?(config_file_path)
end
describe 'initialize' do
context 'for an inexistant config file' do
it { expect { Gemnasium::Configuration.new File.expand_path(config_file_path) }.to raise_error Errno::ENOENT }
end
context 'for a config file that does exist' do
before { FileUtils.touch(config_file_path) }
context 'with missing mandatory values' do
let(:config_options) {{ project_name: 'gemnasium-gem' }}
before { write_config_file }
it { expect { Gemnasium::Configuration.new File.expand_path(config_file_path) }.to raise_error('Your configuration file does not contain all mandatory parameters or contain invalid values. Please check the documentation.') }
end
context 'with all mandatory values' do
let(:config_options) {{ api_key: 'api_key', project_name: 'gemnasium-gem', project_branch: 'master', ignored_paths: ['spec/','tmp/*.lock', '*.gemspec'] }}
before { write_config_file }
it { expect(config.api_key).to eql config_options[:api_key] }
# Keep profile name for backward compatibility with version =< 2.0
it { expect(config.profile_name).to eql config_options[:profile_name] }
it { expect(config.project_name).to eql config_options[:project_name] }
it { expect(config.project_slug).to eql config_options[:project_slug] }
it { expect(config.project_branch).to eql config_options[:project_branch] }
it { expect(config.site).to eql Gemnasium::Configuration::DEFAULT_CONFIG['site'] }
it { expect(config.use_ssl).to eql Gemnasium::Configuration::DEFAULT_CONFIG['use_ssl'] }
it { expect(config.api_version).to eql Gemnasium::Configuration::DEFAULT_CONFIG['api_version'] }
it { expect(config.ignored_paths).to include Regexp.new("^spec/") }
it { expect(config.ignored_paths).to include Regexp.new("^tmp/[^/]+\\.lock") }
it { expect(config.ignored_paths).to include Regexp.new("^[^\/]+\\.gemspec") }
end
end
context 'for a config file with parsable ERB code' do
before { write_config_file }
before { ENV['GEMNASIUM_API_KEY'] = 'api_key_from_env' }
let(:config_options) do
{
api_key: "<%= ENV['GEMNASIUM_API_KEY'] %>",
project_name: 'gemnasium-gem',
project_branch: 'master',
ignored_paths: ['spec/','tmp/*.lock', '*.gemspec']
}
end
it 'parses ERB code' do
expect(config.api_key).to eql 'api_key_from_env'
end
it 'keeps values unchanged if not ERB code' do
expect(config.project_name).to eql config_options[:project_name]
expect(config.project_branch).to eql config_options[:project_branch]
expect(config.ignored_paths).to include Regexp.new("^spec/")
expect(config.ignored_paths).to include Regexp.new("^tmp/[^/]+\\.lock")
expect(config.ignored_paths).to include Regexp.new("^[^\/]+\\.gemspec")
end
end
end
pending "writable?"
describe "#store_value!" do
context "with a new value for an existing key" do
let(:key) { :project_name }
let(:value) { 'new-name' }
# HACK: fake config reload
let(:new_config) { Gemnasium::Configuration.new File.expand_path(config_file_path) }
before do
write_config_file
config.store_value! key, value, "my project name"
end
it "updates the given key-value pair" do
expect(new_config.project_name).to eql 'new-name'
end
it "keeps other key-value pairs unchanged" do
expect(new_config.api_key).to eql config_options[:api_key]
expect(new_config.project_branch).to eql config_options[:project_branch]
end
it "stores the given comment" do
content = File.read File.expand_path(config_file_path)
expect(content).to match /project_name:.*# my project name/
end
end
end
describe "#migrate!" do
before { write_config_file }
subject { config.needs_to_migrate? }
context "with NO profile_name key" do
let(:config_options) do
{
api_key: 'api_key',
project_name: 'gemnasium-gem',
project_branch: 'master'
# no profile_name
}
end
it { should be false }
end
context "with profile_name key" do
let(:config_options) do
{
project_name: 'gemnasium-gem',
profile_name: 'tech-angels',
api_key: '1337'
}
end
it { should be true }
end
end
describe "#migrate!" do
before { write_config_file }
let(:config_options) do
{
project_name: 'gemnasium-gem',
profile_name: 'tech-angels',
api_key: '1337'
}
end
# HACK: fake config reload
let(:new_config) { Gemnasium::Configuration.new File.expand_path(config_file_path) }
it "removes the profile_name" do
expect(config.profile_name).to eql 'tech-angels'
config.migrate!
expect(new_config.profile_name).to eql nil
end
it "adds an empty project_slug" do
config.migrate!
expect(new_config.project_slug).to eql nil
content = File.read File.expand_path(config_file_path)
expect(content).to match /^project_slug:$/
end
it "preserves the other keys" do
config.migrate!
expect(new_config.project_name).to eql 'gemnasium-gem'
expect(new_config.api_key).to eql '1337'
end
end
end