yacht 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,179 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe YachtLoader do
4
- before(:each) do
5
- # mock existence and content of files to avoid file system in specs
6
- %w(base whitelist local).each do |prefix|
7
- conjure_config_file_from_prefix(prefix)
8
- end
9
-
10
- # stub bad config files to check error handling
11
- %w(empty_whitelist invalid_whitelist empty_local invalid_local).each do |baddie|
12
- conjure_bad_config_file_from_prefix(baddie)
13
- end
14
- end
15
-
16
- it "raises an error if base config file is missing" do
17
- banish_config_file_from_prefix('base')
18
-
19
- expect {
20
- YachtLoader.to_hash
21
- }.to raise_error( YachtLoader::LoadError, /Couldn't load base config/)
22
- end
23
-
24
- context "whitelist" do
25
- it "loads keys into an Array" do
26
- YachtLoader.whitelist.should == ["defaultkey", "hashkey"]
27
- end
28
-
29
- it "returns all keys by default, ignoring whitelist" do
30
- YachtLoader.environment = 'default'
31
- YachtLoader.to_hash.keys.should include("defaultkey", "hashkey")
32
- end
33
-
34
- it "only returns keys included in whitelist when :apply_whitelist? option is true" do
35
- YachtLoader.environment = 'default'
36
- YachtLoader.to_hash(:apply_whitelist? => true).keys.should == ["defaultkey", "hashkey"]
37
- end
38
-
39
- context "config file" do
40
- it "raises an error if missing and whitelist is applied" do
41
- banish_config_file_from_prefix('whitelist')
42
-
43
- expect {
44
- YachtLoader.to_hash(:apply_whitelist? => true)
45
- }.to raise_error( YachtLoader::LoadError, /Couldn't load whitelist/)
46
- end
47
-
48
- it "raises an error if empty and whitelist is applied" do
49
- conjure_config_file_from_prefix( :whitelist, File.read('empty_whitelist_config_file') )
50
-
51
- expect {
52
- YachtLoader.to_hash(:apply_whitelist? => true)
53
- }.to raise_error( YachtLoader::LoadError, /whitelist.+ cannot be empty/)
54
- end
55
-
56
- it "raises an error if invalid and whitelist is applied" do
57
- conjure_config_file_from_prefix( :whitelist, File.read('invalid_whitelist_config_file') )
58
-
59
- expect {
60
- YachtLoader.to_hash(:apply_whitelist? => true)
61
- }.to raise_error( YachtLoader::LoadError, /whitelist.+ must contain Array/)
62
- end
63
- end
64
- end
65
-
66
- context "checks environment and sets sensible defaults" do
67
- it "sets the environment to 'default'" do
68
- YachtLoader.environment.should == "default"
69
- end
70
-
71
- it "allows setting the environment by passing an option to `to_hash`" do
72
- YachtLoader.to_hash(:env => 'a_child_environment')
73
- YachtLoader.environment.should == 'a_child_environment'
74
- end
75
-
76
-
77
- it "raises an error if an environment is requested that doesn't exist" do
78
- expect {
79
- YachtLoader.environment = 'nonexistent'
80
- YachtLoader.to_hash
81
- }.to raise_error( YachtLoader::LoadError, /does not exist/)
82
- end
83
-
84
- it "merges configuration for named environment onto defaults" do
85
- YachtLoader.environment = 'an_environment'
86
- YachtLoader.to_hash['defaultkey'].should == 'defaultvalue'
87
- YachtLoader.to_hash['name'].should == 'an_environment'
88
- end
89
- end
90
-
91
- context "handles child environment" do
92
- before do
93
- YachtLoader.environment = 'a_child_environment'
94
- end
95
-
96
- it "merges child onto the parent it names" do
97
- YachtLoader.to_hash['dog'].should == 'terrier'
98
- end
99
-
100
- it "merges the hashes recursively" do
101
- child = YachtLoader.to_hash['hashkey']
102
-
103
- child['foo'].should == 'kung'
104
- child['baz'].should == 'yay'
105
- child['xyzzy'].should == 'thud'
106
- end
107
- end
108
-
109
- context "handling of local config file" do
110
- before do
111
- YachtLoader.environment = 'an_environment'
112
- end
113
-
114
- it "merges values onto named environment and defaults" do
115
- YachtLoader.to_hash['defaultkey'].should == 'defaultvalue'
116
- YachtLoader.to_hash['name'].should == 'an_environment'
117
- YachtLoader.to_hash['localkey'].should == 'localvalue'
118
- end
119
-
120
- it "uses base config if missing" do
121
- banish_config_file_from_prefix('local')
122
-
123
- File.should_not_receive(:read).with('local_config_file')
124
-
125
- YachtLoader.send(:local_config).should == {}
126
- YachtLoader.to_hash['defaultkey'].should == 'defaultvalue'
127
- YachtLoader.to_hash['name'].should == 'an_environment'
128
- end
129
-
130
- it "uses base config if empty" do
131
- conjure_config_file_from_prefix(:local, File.read('empty_local_config_file'))
132
-
133
- File.should_not_receive(:read).with('local_config_file')
134
-
135
- YachtLoader.send(:local_config).should == {}
136
- YachtLoader.to_hash['defaultkey'].should == 'defaultvalue'
137
- YachtLoader.to_hash['name'].should == 'an_environment'
138
- end
139
-
140
- it "raises an error if invalid" do
141
- conjure_config_file_from_prefix( :local, File.read('invalid_local_config_file') )
142
- expect {
143
- YachtLoader.to_hash
144
- }.to raise_error( YachtLoader::LoadError, %r{local.yml must contain Hash})
145
- end
146
- end
147
-
148
- describe :config_file_for do
149
- it "returns the full file path for the following config files: base, local & whitelist" do
150
- %w(base local whitelist).each do |config_file|
151
- YachtLoader.should_receive(:full_file_path_for_config).with(config_file)
152
-
153
- YachtLoader.config_file_for(config_file)
154
- end
155
- end
156
-
157
- it "raises an error if the config file is not found" do
158
- expect {
159
- YachtLoader.config_file_for(:foo)
160
- }.to raise_error( YachtLoader::LoadError, "foo is not a valid config type")
161
- end
162
- end
163
-
164
- describe :load_config_file do
165
- it "loads the specified file" do
166
- conjure_config_file_from_prefix("base", "some contents")
167
-
168
- YachtLoader.send(:load_config_file, "base", :expect_to_load => String).should == "some contents"
169
- end
170
-
171
- it "raises an error if opening the file leads to an exception" do
172
- YachtLoader.stub(:config_file_for).and_raise(StandardError.new("my_unique_error_message"))
173
-
174
- expect {
175
- YachtLoader.send(:load_config_file, "some file")
176
- }.to raise_error( YachtLoader::LoadError, %r{ERROR: loading.+my_unique_error_message} )
177
- end
178
- end
179
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "to_classy_struct" do
4
- before do
5
- conjure_config_file_from_prefix("base")
6
- conjure_config_file_from_prefix("local")
7
- conjure_config_file_from_prefix("whitelist")
8
-
9
- YachtLoader.environment = 'an_environment'
10
- end
11
-
12
- it "creates a ClassyStruct based on to_hash" do
13
- Yacht = YachtLoader.to_classy_struct
14
- Yacht.dog.should == "terrier"
15
- end
16
-
17
- # ClassyStruct improves performance by adding accessors to the instance object
18
- # If the instance is not reused, there is no advantage to ClassyStruct over OpenStruct
19
- it "reuses the instance of ClassyStruct on subsequent calls" do
20
- first_obj = YachtLoader.classy_struct_instance
21
- second_obj = YachtLoader.classy_struct_instance
22
-
23
- first_obj.object_id.should == second_obj.object_id.should
24
- end
25
-
26
- it "passes options to to_hash" do
27
- YachtLoader.should_receive(:to_hash).with({:my => :awesome_option})
28
-
29
- YachtLoader.to_classy_struct({:my => :awesome_option})
30
- end
31
-
32
- it "raises a custom error if ClassyStruct cannot be created" do
33
- YachtLoader.stub!(:to_hash).and_raise("some funky error")
34
-
35
- expect {
36
- YachtLoader.to_classy_struct
37
- }.to raise_error(YachtLoader::LoadError, /some funky error/)
38
- end
39
- end