objectified_environments 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helpers', 'rails_helper')
2
+ require File.join(File.dirname(__FILE__), '..', 'helpers', 'command_helpers')
3
+
4
+ describe "ObjectifiedEnvironments Rails integration" do
5
+ include ObjectifiedEnvironments::Specs::Helpers::CommandHelpers
6
+
7
+ before :each do
8
+ @gem_root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..'))
9
+ end
10
+
11
+ def lib_objenv_dir
12
+ File.join('config', 'lib', 'objenv')
13
+ end
14
+
15
+ def splat_file(path, contents)
16
+ FileUtils.mkdir_p(File.dirname(path))
17
+ File.open(path, 'w') { |f| f << contents }
18
+ end
19
+
20
+ def append_file(path, contents)
21
+ File.open(path, 'a') { |f| f << contents }
22
+ end
23
+
24
+ def add_gem_to_gemfile
25
+ append_file('Gemfile', "gem 'objectified_environments', :path => '#{@gem_root}'\n")
26
+ end
27
+
28
+ def new_rails_helper(options = { })
29
+ tmp_dir = File.join(@gem_root, 'tmp')
30
+ ObjectifiedEnvironments::Specs::Helpers::RailsHelper.new(tmp_dir, options)
31
+ end
32
+
33
+ it "should instantiate and use the development environment properly when RAILS_ENV=development" do
34
+ new_rails_helper.run! do |rh|
35
+ add_gem_to_gemfile
36
+ splat_file(File.join(lib_objenv_dir, 'development.rb'), %{class Objenv::Development; def spec_output; "this_is_dev_env_1"; end; end})
37
+
38
+ old_rails_env = ENV['RAILS_ENV']
39
+ begin
40
+ ENV['RAILS_ENV'] = 'development'
41
+ result = rh.run_as_script!("puts Rails.objenv.spec_output", :script_name => "check_env_spec_script")
42
+ result.strip.should match(/^this_is_dev_env_1$/mi)
43
+ ensure
44
+ ENV['RAILS_ENV'] = old_rails_env
45
+ end
46
+ end
47
+ end
48
+
49
+ it "should be available during initializer and configuration time" do
50
+ new_rails_helper.run! do |rh|
51
+ add_gem_to_gemfile
52
+ splat_file(File.join(lib_objenv_dir, 'test.rb'), %{class Objenv::Test; def spec_output; "this_is_test_env_2"; end; end})
53
+ splat_file(File.join(rh.root, 'config', 'initializers', 'spec_initializer.rb'), "puts 'initializer: ' + Rails.objenv.spec_output")
54
+
55
+ test_env_file = File.join(rh.root, "config", "environments", "test.rb")
56
+ test_env = "puts 'before env: ' + Rails.objenv.spec_output\n" + File.read(test_env_file) + "\nputs 'after env: ' + Rails.objenv.spec_output\n"
57
+ File.open(test_env_file, 'w') { |f| f << test_env }
58
+
59
+ result = rh.run_as_script!("puts 'done'", :script_name => "check_initializer_spec_script")
60
+ result.should match(/^initializer: this_is_test_env_2$/mi)
61
+ result.should match(/^before env: this_is_test_env_2$/mi)
62
+ result.should match(/^after env: this_is_test_env_2$/mi)
63
+ result.should match(/^done$/mi)
64
+ end
65
+ end
66
+
67
+ context "generator" do
68
+ it "should create all necessary classes, including one for whatever RAILS_ENV is set" do
69
+ new_rails_helper(:rails_env => 'bar').run! do |rh|
70
+ add_gem_to_gemfile
71
+ rh.run_generator("objectified_environments")
72
+
73
+ script = <<-EOS
74
+ %w{Bar Development Test Production LocalEnvironment ProductionEnvironment Environment}.each do |class_name|
75
+ klass = eval("Objenv::" + class_name) rescue nil
76
+ superclasses = [ ]
77
+
78
+ current_class = klass
79
+ while true
80
+ break unless current_class
81
+ current_class = current_class.superclass
82
+ break if current_class == Object
83
+ superclasses.unshift(current_class)
84
+ end
85
+
86
+ $stdout << class_name
87
+ $stdout << ": "
88
+
89
+ unless klass
90
+ $stdout << "NOT PRESENT"
91
+ end
92
+
93
+ superclasses = superclasses.map { |sc| sc.name }.sort
94
+ $stdout.puts superclasses.join(" ")
95
+ $stdout.flush
96
+ end
97
+ EOS
98
+
99
+ result = rh.run_as_script!(script, :script_name => "check_generator_spec_script")
100
+
101
+ result.should match(/^Environment: ObjectifiedEnvironments::Base$/mi)
102
+ result.should match(/^LocalEnvironment: ObjectifiedEnvironments::Base Objenv::Environment$/mi)
103
+ result.should match(/^Development: ObjectifiedEnvironments::Base Objenv::Environment Objenv::LocalEnvironment$/mi)
104
+ result.should match(/^Test: ObjectifiedEnvironments::Base Objenv::Environment Objenv::LocalEnvironment$/mi)
105
+ result.should match(/^ProductionEnvironment: ObjectifiedEnvironments::Base Objenv::Environment$/mi)
106
+ result.should match(/^Production: ObjectifiedEnvironments::Base Objenv::Environment Objenv::ProductionEnvironment$/mi)
107
+ result.should match(/^Bar: ObjectifiedEnvironments::Base Objenv::Environment$/mi)
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,34 @@
1
+ require 'objectified_environments/base'
2
+
3
+ describe ObjectifiedEnvironments::Base do
4
+ it "should hold environment data" do
5
+ b = ObjectifiedEnvironments::Base.new(:rails_env => 'foo', :user_name => 'bar', :host_name => 'baz')
6
+ b.send(:rails_env).should == 'foo'
7
+ b.send(:user_name).should == 'bar'
8
+ b.send(:host_name).should == 'baz'
9
+ end
10
+
11
+ it "should not require any data but rails_env" do
12
+ b = ObjectifiedEnvironments::Base.new(:rails_env => 'foo')
13
+ b.send(:rails_env).should == 'foo'
14
+ b.send(:user_name).should == nil
15
+ b.send(:host_name).should == nil
16
+ end
17
+
18
+ it "should not expose this data by default" do
19
+ lambda { b.rails_env }.should raise_error
20
+ lambda { b.user_name }.should raise_error
21
+ lambda { b.host_name }.should raise_error
22
+ end
23
+
24
+ it "should implement #must_implement as something that raises" do
25
+ b = ObjectifiedEnvironments::Base.new(:rails_env => 'foo')
26
+ class << b
27
+ def foo
28
+ must_implement
29
+ end
30
+ end
31
+
32
+ lambda { b.foo }.should raise_error
33
+ end
34
+ end
@@ -0,0 +1,155 @@
1
+ require 'objectified_environments/data_provider'
2
+
3
+ describe ObjectifiedEnvironments::DataProvider do
4
+ it "should be instantiable with no arguments" do
5
+ lambda { subject.class.new }.should_not raise_error
6
+ end
7
+
8
+ describe "Rails.env" do
9
+ it "should expose correctly" do
10
+ expect(Rails).to receive(:env).once { 'bongo' }
11
+ subject.rails_env.should == 'bongo'
12
+ end
13
+
14
+ it "should raise an error if there's none" do
15
+ expect(Rails).to receive(:env).once { nil }
16
+ lambda { subject.rails_env }.should raise_error
17
+ end
18
+ end
19
+
20
+ describe "user_name" do
21
+ before(:each) { @saved_env = { }; ENV.each { |k,v| @saved_env[k] = v } }
22
+ after(:each) { @saved_env.each { |k,v| ENV[k] = v } }
23
+
24
+ context "Etc.getlogin" do
25
+ require 'etc'
26
+
27
+ it "should return this first" do
28
+ expect(Etc).to receive(:getlogin).once { 'marph' }
29
+ subject.user_name.should == 'marph'
30
+ end
31
+
32
+ it "should skip this if blank" do
33
+ expect(Etc).to receive(:getlogin).once { ' ' }
34
+ ENV['USER'] = 'foo'
35
+
36
+ subject.user_name.should == 'foo'
37
+ end
38
+
39
+ it "should skip this if nil" do
40
+ expect(Etc).to receive(:getlogin).once { nil }
41
+ ENV['USER'] = 'bar'
42
+
43
+ subject.user_name.should == 'bar'
44
+ end
45
+ end
46
+
47
+ context "environment variables" do
48
+ def check_env(user, logname, username, expected_result)
49
+ ENV['USER'] = user
50
+ ENV['LOGNAME'] = logname
51
+ ENV['USERNAME'] = username
52
+
53
+ allow(Etc).to receive(:getlogin) { nil }
54
+
55
+ dp = subject.class.new
56
+ dp.user_name.should == expected_result
57
+ end
58
+
59
+ it "should check them in the right order" do
60
+ check_env('foo', 'bar', 'baz', 'foo')
61
+ check_env(' ', 'bar', 'baz', 'bar')
62
+ check_env(nil, 'bar', 'baz', 'bar')
63
+
64
+ check_env(nil, ' ', 'baz', 'baz')
65
+ check_env(nil, nil, 'baz', 'baz')
66
+ end
67
+
68
+ it "should return nil if nothing is available" do
69
+ check_env(nil, nil, nil, nil)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "host_name" do
75
+ require 'socket'
76
+
77
+ def set_hostname_from_hostname_command(dp, x)
78
+ class << dp
79
+ def specified_host_name=(x)
80
+ @_specified_host_name = x
81
+ end
82
+
83
+ def host_name_from_hostname_command
84
+ @_specified_host_name
85
+ end
86
+ end
87
+
88
+ dp.specified_host_name = x
89
+ end
90
+
91
+ it "should return it from the hostname command by default" do
92
+ set_hostname_from_hostname_command(subject, 'baz')
93
+ subject.host_name.should == 'baz'
94
+ end
95
+
96
+ it "should strip whitespace from the hostname command and normalize it" do
97
+ set_hostname_from_hostname_command(subject, "\n Ba-Z\n \n")
98
+ subject.host_name.should == 'ba_z'
99
+ end
100
+
101
+ it "should skip the hostname command if blank" do
102
+ set_hostname_from_hostname_command(subject, ' ')
103
+ expect(Socket).to receive(:gethostname).once { 'foo' }
104
+ subject.host_name.should == 'foo'
105
+ end
106
+
107
+ it "should skip the hostname command if nil" do
108
+ set_hostname_from_hostname_command(subject, nil)
109
+ expect(Socket).to receive(:gethostname).once { 'foo' }
110
+ subject.host_name.should == 'foo'
111
+ end
112
+
113
+ it "should skip the hostname command if garbage" do
114
+ set_hostname_from_hostname_command(subject, '$#@*($')
115
+ expect(Socket).to receive(:gethostname).once { 'foo' }
116
+ subject.host_name.should == 'foo'
117
+ end
118
+
119
+ it "should skip the hostname command if not a Ruby identifier" do
120
+ set_hostname_from_hostname_command(subject, '0bar')
121
+ expect(Socket).to receive(:gethostname).once { 'foo' }
122
+ subject.host_name.should == 'foo'
123
+ end
124
+
125
+ it "should skip Socket.gethostname if blank" do
126
+ set_hostname_from_hostname_command(subject, nil)
127
+ expect(Socket).to receive(:gethostname).once { ' ' }
128
+ subject.host_name.should be_nil
129
+ end
130
+
131
+ it "should skip Socket.gethostname if nil" do
132
+ set_hostname_from_hostname_command(subject, nil)
133
+ expect(Socket).to receive(:gethostname).once { nil }
134
+ subject.host_name.should be_nil
135
+ end
136
+
137
+ it "should skip Socket.gethostname if garbage" do
138
+ set_hostname_from_hostname_command(subject, nil)
139
+ expect(Socket).to receive(:gethostname).once { "$*(@$" }
140
+ subject.host_name.should be_nil
141
+ end
142
+
143
+ it "should skip Socket.gethostname if not a Ruby identifir" do
144
+ set_hostname_from_hostname_command(subject, nil)
145
+ expect(Socket).to receive(:gethostname).once { "0foo" }
146
+ subject.host_name.should be_nil
147
+ end
148
+
149
+ it "should skip Socket.gethostname if it raises" do
150
+ set_hostname_from_hostname_command(subject, nil)
151
+ expect(Socket).to receive(:gethostname).once { raise "kaboom!" }
152
+ subject.host_name.should be_nil
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,232 @@
1
+ require 'objectified_environments/environment_builder'
2
+
3
+ describe ObjectifiedEnvironments::EnvironmentBuilder do
4
+ class FakeDataProvider
5
+ def initialize(rails_env, user_name, host_name)
6
+ @rails_env = rails_env
7
+ @user_name = user_name
8
+ @host_name = host_name
9
+ end
10
+
11
+ attr_reader :rails_env, :user_name, :host_name
12
+ end
13
+
14
+ def new_with(*args)
15
+ fake_data_provider = FakeDataProvider.new(*args)
16
+ ObjectifiedEnvironments::EnvironmentBuilder.new(fake_data_provider)
17
+ end
18
+
19
+ describe "construction" do
20
+ it "should require a non-blank, non-nil Rails.env" do
21
+ lambda { new_with(' ', nil, nil) }.should raise_error
22
+ lambda { new_with(nil, nil, nil) }.should raise_error
23
+ end
24
+
25
+ it "should work with just a Rails.env" do
26
+ lambda { new_with('foo', nil, nil) }.should_not raise_error
27
+ end
28
+ end
29
+
30
+ describe "#environment" do
31
+ def define_classes_for(name_or_names, options = { }, &block)
32
+ names = Array(name_or_names)
33
+
34
+ names.each do |name|
35
+ n = name
36
+ root = Object
37
+
38
+ while n =~ /^([^:]+)::(.*)$/
39
+ module_name = $1; n = $2
40
+ is_defined = eval("defined?(#{module_name})")
41
+ unless is_defined
42
+ root.module_eval "module #{module_name}; end"
43
+ end
44
+ root = eval("#{root.name}::#{module_name}")
45
+ end
46
+
47
+ root.module_eval "class #{name}; end"
48
+ end
49
+
50
+ block.call
51
+
52
+ names.each do |name|
53
+ root = Object
54
+ if name =~ /^(.*)::([^:]+)$/
55
+ root = $1.constantize
56
+ name = $2
57
+ end
58
+
59
+ root.send(:remove_const, name.to_sym)
60
+ end
61
+ end
62
+
63
+ context "simple Rails.env classes" do
64
+ it "should create an environment just named after Rails.env if defined" do
65
+ define_classes_for('Objenv::Foo') do
66
+ env = new_with('foo', 'bar', 'baz').environment
67
+ env.class.should == Objenv::Foo
68
+ end
69
+ end
70
+
71
+ it "should create an environment just named after Rails.env even if user and host are missing" do
72
+ define_classes_for('Objenv::Foo') do
73
+ env = new_with('foo', nil, nil).environment
74
+ env.class.should == Objenv::Foo
75
+ end
76
+ end
77
+
78
+ it "should fail with a nice error message if the class is not defined, showing what we looked for" do
79
+ define_classes_for('Objenv::Marph') do
80
+ eb = new_with('foo', 'bar', 'baz')
81
+
82
+ exception = nil
83
+ begin
84
+ eb.environment
85
+ rescue ObjectifiedEnvironments::EnvironmentMissingError => eme
86
+ exception = eme
87
+ end
88
+
89
+ exception.should_not be_nil
90
+
91
+ exception.message.should =~ /foo/
92
+ exception.message.should =~ /bar/
93
+ exception.message.should =~ /baz/
94
+
95
+ exception.message.should =~ /Objenv::Foo/
96
+ exception.message.should =~ /Objenv::User::BarFoo/
97
+ exception.message.should =~ /Objenv::Host::BazFoo/
98
+ exception.message.should =~ /Objenv::UserHost::BarBazFoo/
99
+ end
100
+ end
101
+
102
+ it "should fail with a nice error message if the class blows up upon instantiation" do
103
+ define_classes_for('Objenv::Foo') do
104
+ class Objenv::Foo
105
+ def initialize(*args)
106
+ raise "kaboomba"
107
+ end
108
+ end
109
+
110
+ eb = new_with('foo', 'bar', 'baz')
111
+
112
+ exception = nil
113
+ begin
114
+ eb.environment
115
+ rescue ObjectifiedEnvironments::UnableToInstantiateEnvironmentError => utiee
116
+ exception = utiee
117
+ end
118
+
119
+ exception.should_not be_nil
120
+
121
+ exception.message.should =~ /Objenv::Foo/
122
+ exception.message.should =~ /kaboomba/
123
+ end
124
+ end
125
+
126
+ it "should fail with a nice error message if the class's constructor takes too many parameters" do
127
+ define_classes_for('Objenv::Foo') do
128
+ class Objenv::Foo
129
+ def initialize(x, y)
130
+ # ok
131
+ end
132
+ end
133
+
134
+ eb = new_with('foo', 'bar', 'baz')
135
+
136
+ exception = nil
137
+ begin
138
+ eb.environment
139
+ rescue ObjectifiedEnvironments::UnableToInstantiateEnvironmentError => utiee
140
+ exception = utiee
141
+ end
142
+
143
+ exception.should_not be_nil
144
+
145
+ exception.message.should =~ /Objenv::Foo/
146
+ end
147
+ end
148
+
149
+ it "should pass in environment information if the class accepts it" do
150
+ define_classes_for('Objenv::Foo') do
151
+ class Objenv::Foo
152
+ def initialize(h)
153
+ @h = h
154
+ end
155
+ attr_reader :h
156
+ end
157
+
158
+ env = new_with('foo', 'bar', 'baz').environment
159
+ env.class.should == Objenv::Foo
160
+ env.h[:rails_env].should == 'foo'
161
+ env.h[:user_name].should == 'bar'
162
+ env.h[:host_name].should == 'baz'
163
+ end
164
+ end
165
+
166
+ it "should pass in environment information if the class accepts it with optional arguments" do
167
+ define_classes_for('Objenv::Foo') do
168
+ class Objenv::Foo
169
+ def initialize(h, x = nil, y = nil)
170
+ @h = h
171
+ end
172
+ attr_reader :h
173
+ end
174
+
175
+ env = new_with('foo', 'bar', 'baz').environment
176
+ env.class.should == Objenv::Foo
177
+ env.h[:rails_env].should == 'foo'
178
+ env.h[:user_name].should == 'bar'
179
+ env.h[:host_name].should == 'baz'
180
+ end
181
+ end
182
+ end
183
+
184
+ context "with a username" do
185
+ it "should prefer a username-specified class" do
186
+ define_classes_for([ 'Objenv::User::BarFoo', 'Objenv::Foo' ]) do
187
+ env = new_with('foo', 'bar', 'baz').environment
188
+ env.class.should == Objenv::User::BarFoo
189
+ end
190
+ end
191
+
192
+ it "should not require a Rails-env-only class" do
193
+ define_classes_for([ 'Objenv::User::BarFoo' ]) do
194
+ env = new_with('foo', 'bar', 'baz').environment
195
+ env.class.should == Objenv::User::BarFoo
196
+ end
197
+ end
198
+ end
199
+
200
+ context "with a hostname" do
201
+ it "should prefer a hostname-specified class" do
202
+ define_classes_for([ 'Objenv::Host::BazFoo', 'Objenv::Foo' ]) do
203
+ env = new_with('foo', 'bar', 'baz').environment
204
+ env.class.should == Objenv::Host::BazFoo
205
+ end
206
+ end
207
+
208
+ it "should not require a Rails-env-only class" do
209
+ define_classes_for([ 'Objenv::Host::BazFoo' ]) do
210
+ env = new_with('foo', 'bar', 'baz').environment
211
+ env.class.should == Objenv::Host::BazFoo
212
+ end
213
+ end
214
+ end
215
+
216
+ context "with a username and hostname both" do
217
+ it "should prefer a username-and-hostname-specified class" do
218
+ define_classes_for([ 'Objenv::UserHost::BarBazFoo', 'Objenv::Host::BazFoo', 'Objenv::User::BarFoo', 'Objenv::Foo' ]) do
219
+ env = new_with('foo', 'bar', 'baz').environment
220
+ env.class.should == Objenv::UserHost::BarBazFoo
221
+ end
222
+ end
223
+
224
+ it "should not require any other classes" do
225
+ define_classes_for([ 'Objenv::UserHost::BarBazFoo' ]) do
226
+ env = new_with('foo', 'bar', 'baz').environment
227
+ env.class.should == Objenv::UserHost::BarBazFoo
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,17 @@
1
+ require 'objectified_environments/errors'
2
+
3
+ describe ObjectifiedEnvironments do
4
+ it "should have the right hierarchy" do
5
+ ObjectifiedEnvironments::ObjectifiedEnvironmentError.superclass.should == StandardError
6
+ ObjectifiedEnvironments::EnvironmentMissingError.superclass.should == ObjectifiedEnvironments::ObjectifiedEnvironmentError
7
+ ObjectifiedEnvironments::UnableToInstantiateEnvironmentError.superclass.should == ObjectifiedEnvironments::ObjectifiedEnvironmentError
8
+ end
9
+
10
+ it "should be able to be created with just a message" do
11
+ m = "foo"
12
+
13
+ lambda { ObjectifiedEnvironments::ObjectifiedEnvironmentError.new(m) }.should_not raise_error
14
+ lambda { ObjectifiedEnvironments::EnvironmentMissingError.new(m) }.should_not raise_error
15
+ lambda { ObjectifiedEnvironments::UnableToInstantiateEnvironmentError.new(m) }.should_not raise_error
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require 'objectified_environments/version'
2
+
3
+ describe "ObjectifiedEnvironments::VERSION" do
4
+ it "should return the right version" do
5
+ ObjectifiedEnvironments::VERSION.should == "1.0.0"
6
+ end
7
+ end