lucidimagination-warbler 1.3.2.dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/Gemfile +16 -0
  2. data/History.txt +217 -0
  3. data/LICENSE.txt +26 -0
  4. data/Manifest.txt +92 -0
  5. data/README.txt +255 -0
  6. data/Rakefile +103 -0
  7. data/bin/warble +11 -0
  8. data/ext/JarMain.java +148 -0
  9. data/ext/WarMain.java +112 -0
  10. data/ext/WarblerJar.java +182 -0
  11. data/ext/WarblerJarService.java +18 -0
  12. data/lib/warbler.rb +33 -0
  13. data/lib/warbler/application.rb +86 -0
  14. data/lib/warbler/config.rb +223 -0
  15. data/lib/warbler/gems.rb +37 -0
  16. data/lib/warbler/jar.rb +253 -0
  17. data/lib/warbler/task.rb +183 -0
  18. data/lib/warbler/templates/bundler.erb +3 -0
  19. data/lib/warbler/templates/config.erb +1 -0
  20. data/lib/warbler/templates/jar.erb +5 -0
  21. data/lib/warbler/templates/rack.erb +1 -0
  22. data/lib/warbler/templates/rails.erb +1 -0
  23. data/lib/warbler/templates/war.erb +1 -0
  24. data/lib/warbler/traits.rb +107 -0
  25. data/lib/warbler/traits/bundler.rb +104 -0
  26. data/lib/warbler/traits/gemspec.rb +59 -0
  27. data/lib/warbler/traits/jar.rb +56 -0
  28. data/lib/warbler/traits/merb.rb +35 -0
  29. data/lib/warbler/traits/nogemspec.rb +42 -0
  30. data/lib/warbler/traits/rack.rb +33 -0
  31. data/lib/warbler/traits/rails.rb +62 -0
  32. data/lib/warbler/traits/war.rb +197 -0
  33. data/lib/warbler/version.rb +10 -0
  34. data/lib/warbler/war.rb +8 -0
  35. data/lib/warbler_jar.jar +0 -0
  36. data/spec/drb_helper.rb +41 -0
  37. data/spec/sample_bundler/Gemfile.lock +10 -0
  38. data/spec/sample_bundler/config.ru +0 -0
  39. data/spec/sample_jar/History.txt +6 -0
  40. data/spec/sample_jar/Manifest.txt +8 -0
  41. data/spec/sample_jar/README.txt +30 -0
  42. data/spec/sample_jar/lib/sample_jar.rb +6 -0
  43. data/spec/sample_jar/sample_jar.gemspec +40 -0
  44. data/spec/sample_jar/test/test_sample_jar.rb +8 -0
  45. data/spec/sample_war/app/controllers/application.rb +15 -0
  46. data/spec/sample_war/app/helpers/application_helper.rb +3 -0
  47. data/spec/sample_war/config/boot.rb +109 -0
  48. data/spec/sample_war/config/database.yml +19 -0
  49. data/spec/sample_war/config/environment.rb +67 -0
  50. data/spec/sample_war/config/environments/development.rb +17 -0
  51. data/spec/sample_war/config/environments/production.rb +25 -0
  52. data/spec/sample_war/config/environments/test.rb +22 -0
  53. data/spec/sample_war/config/initializers/inflections.rb +10 -0
  54. data/spec/sample_war/config/initializers/mime_types.rb +5 -0
  55. data/spec/sample_war/config/initializers/new_rails_defaults.rb +15 -0
  56. data/spec/sample_war/config/routes.rb +41 -0
  57. data/spec/sample_war/lib/tasks/utils.rake +0 -0
  58. data/spec/sample_war/public/404.html +30 -0
  59. data/spec/sample_war/public/422.html +30 -0
  60. data/spec/sample_war/public/500.html +30 -0
  61. data/spec/sample_war/public/favicon.ico +0 -0
  62. data/spec/sample_war/public/index.html +274 -0
  63. data/spec/sample_war/public/robots.txt +5 -0
  64. data/spec/spec_helper.rb +112 -0
  65. data/spec/warbler/application_spec.rb +95 -0
  66. data/spec/warbler/bundler_spec.rb +136 -0
  67. data/spec/warbler/config_spec.rb +130 -0
  68. data/spec/warbler/gems_spec.rb +40 -0
  69. data/spec/warbler/jar_spec.rb +718 -0
  70. data/spec/warbler/task_spec.rb +170 -0
  71. data/spec/warbler/traits_spec.rb +17 -0
  72. data/spec/warbler/war_spec.rb +14 -0
  73. data/warble.rb +142 -0
  74. data/web.xml.erb +32 -0
  75. metadata +198 -0
@@ -0,0 +1,40 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Engine Yard, Inc.
3
+ # Copyright (c) 2007-2009 Sun Microsystems, Inc.
4
+ # This source code is available under the MIT license.
5
+ # See the file LICENSE.txt for details.
6
+ #++
7
+
8
+ require File.expand_path('../../spec_helper', __FILE__)
9
+
10
+ describe Warbler::Gems do
11
+ it "should accept a hash for initialization" do
12
+ gems = Warbler::Gems.new({"actionpack" => "1.2.3"})
13
+ gems.should have_key("actionpack")
14
+ gems["actionpack"].should == "1.2.3"
15
+ end
16
+
17
+ it "should accept an array for initialization" do
18
+ gems = Warbler::Gems.new ["activerecord"]
19
+ gems.should have_key("activerecord")
20
+ end
21
+
22
+ it "should allow gems with a version" do
23
+ gems = Warbler::Gems.new
24
+ gems["actionpack"] = "> 1.2.3"
25
+ gems["actionpack"].should == "> 1.2.3"
26
+ end
27
+
28
+ it "should allow gems without an explicit version" do
29
+ gems = Warbler::Gems.new
30
+ gems << "actionpack"
31
+ gems.should have_key("actionpack")
32
+ end
33
+
34
+ it "should allow to add gems" do
35
+ gems = Warbler::Gems.new
36
+ gems << "rails"
37
+ gems += ["activerecord-jdbcmysql-adapter", "jdbc-mysql", "jruby-openssl"]
38
+ ["rails", "activerecord-jdbcmysql-adapter", "jdbc-mysql", "jruby-openssl"].each {|g| gems.should have_key(g)}
39
+ end
40
+ end
@@ -0,0 +1,718 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Engine Yard, Inc.
3
+ # Copyright (c) 2007-2009 Sun Microsystems, Inc.
4
+ # This source code is available under the MIT license.
5
+ # See the file LICENSE.txt for details.
6
+ #++
7
+
8
+ require File.expand_path('../../spec_helper', __FILE__)
9
+
10
+ describe Warbler::Jar do
11
+ use_fresh_rake_application
12
+ use_fresh_environment
13
+
14
+ def file_list(regex)
15
+ jar.files.keys.select {|f| f =~ regex }
16
+ end
17
+
18
+ def use_config(&block)
19
+ @extra_config = block
20
+ end
21
+
22
+ def apply_extra_config(config)
23
+ @extra_config.call(config) if @extra_config
24
+ end
25
+
26
+ let(:config) { Warbler::Config.new {|c| apply_extra_config(c) } }
27
+ let(:jar) { Warbler::Jar.new }
28
+
29
+ context "in a jar project" do
30
+ run_in_directory "spec/sample_jar"
31
+ cleanup_temp_files
32
+
33
+ it "detects a Jar trait" do
34
+ config.traits.should include(Warbler::Traits::Jar)
35
+ end
36
+
37
+ it "collects java libraries" do
38
+ jar.apply(config)
39
+ file_list(%r{^META-INF/lib/jruby-.*\.jar$}).should_not be_empty
40
+ end
41
+
42
+ it "adds a JarMain class" do
43
+ jar.apply(config)
44
+ file_list(%r{^JarMain\.class$}).should_not be_empty
45
+ end
46
+
47
+ it "adds an init.rb" do
48
+ jar.apply(config)
49
+ file_list(%r{^META-INF/init.rb$}).should_not be_empty
50
+ end
51
+
52
+ it "requires 'rubygems' in init.rb" do
53
+ jar.add_init_file(config)
54
+ contents = jar.contents('META-INF/init.rb')
55
+ contents.split("\n").grep(/require 'rubygems'/).should_not be_empty
56
+ end
57
+
58
+ it "adds ENV['GEM_HOME'] to init.rb" do
59
+ jar.add_init_file(config)
60
+ contents = jar.contents('META-INF/init.rb')
61
+ contents.should =~ /ENV\['GEM_HOME'\]/
62
+ end
63
+
64
+ it "adds a main.rb" do
65
+ jar.apply(config)
66
+ file_list(%r{^META-INF/main.rb$}).should_not be_empty
67
+ end
68
+
69
+ it "accepts a custom manifest file" do
70
+ touch 'manifest'
71
+ use_config do |config|
72
+ config.manifest_file = 'manifest'
73
+ end
74
+ jar.apply(config)
75
+ jar.files['META-INF/MANIFEST.MF'].should == "manifest"
76
+ end
77
+
78
+ it "accepts a MANIFEST.MF file if it exists in the project root" do
79
+ touch 'MANIFEST.MF'
80
+ jar.apply(config)
81
+ jar.files['META-INF/MANIFEST.MF'].should == "MANIFEST.MF"
82
+ end
83
+
84
+ it "does not add a manifest if one already exists" do
85
+ jar.files['META-INF/MANIFEST.MF'] = 'manifest'
86
+ jar.add_manifest(config)
87
+ jar.files['META-INF/MANIFEST.MF'].should == "manifest"
88
+ end
89
+
90
+ context "with a .gemspec" do
91
+ it "detects a Gemspec trait" do
92
+ config.traits.should include(Warbler::Traits::Gemspec)
93
+ end
94
+
95
+ it "detects gem dependencies" do
96
+ jar.apply(config)
97
+ file_list(%r{^gems/rubyzip.*/lib/zip/zip.rb}).should_not be_empty
98
+ file_list(%r{^specifications/rubyzip.*\.gemspec}).should_not be_empty
99
+ end
100
+
101
+ it "sets load paths in init.rb" do
102
+ jar.add_init_file(config)
103
+ contents = jar.contents('META-INF/init.rb')
104
+ contents.split("\n").grep(/LOAD_PATH\.unshift.*sample_jar\/lib/).should_not be_empty
105
+ end
106
+
107
+ it "loads the default executable in main.rb" do
108
+ jar.apply(config)
109
+ contents = jar.contents('META-INF/main.rb')
110
+ contents.split("\n").grep(/load.*sample_jar\/bin\/sample_jar/).should_not be_empty
111
+ end
112
+
113
+ it "includes compiled .rb and .class files" do
114
+ config.compiled_ruby_files = %w(lib/sample_jar.rb)
115
+ jar.compile(config)
116
+ jar.apply(config)
117
+ file_list(%r{^sample_jar/lib/sample_jar\.class$}).should_not be_empty
118
+ jar.contents('sample_jar/lib/sample_jar.rb').should =~ /require __FILE__\.sub/
119
+ end
120
+ end
121
+
122
+ context "with a gemspec without a default executable" do
123
+ before :each do
124
+ Dir['*.gemspec'].each do |f|
125
+ cp f, "#{f}.tmp"
126
+ lines = IO.readlines(f)
127
+ File.open(f, 'w') do |io|
128
+ lines.each do |line|
129
+ next if line =~ /executable/
130
+ io << line
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ after :each do
137
+ Dir['*.gemspec.tmp'].each {|f| mv f, "#{f.sub /\.tmp$/, ''}"}
138
+ end
139
+
140
+ it "loads the first bin/executable in main.rb" do
141
+ silence { jar.apply(config) }
142
+ contents = jar.contents('META-INF/main.rb')
143
+ contents.split("\n").grep(/load.*sample_jar\/bin\/sample_jar/).should_not be_empty
144
+ end
145
+ end
146
+
147
+ context "without a .gemspec" do
148
+ before :each do
149
+ Dir['*.gemspec'].each {|f| mv f, "#{f}.tmp"}
150
+ end
151
+
152
+ after :each do
153
+ Dir['*.gemspec.tmp'].each {|f| mv f, "#{f.sub /\.tmp$/, ''}"}
154
+ end
155
+
156
+ it "detects a NoGemspec trait" do
157
+ config.traits.should include(Warbler::Traits::NoGemspec)
158
+ end
159
+
160
+ it "collects gem files from configuration" do
161
+ use_config do |config|
162
+ config.gems << "rake"
163
+ end
164
+ jar.apply(config)
165
+ file_list(%r{^gems/rake.*/lib/rake.rb}).should_not be_empty
166
+ file_list(%r{^specifications/rake.*\.gemspec}).should_not be_empty
167
+ end
168
+
169
+ it "collects all project files in the directory" do
170
+ touch "extra.foobar"
171
+ jar.apply(config)
172
+ file_list(%r{^sample_jar/bin$}).should_not be_empty
173
+ file_list(%r{^sample_jar/test$}).should_not be_empty
174
+ file_list(%r{^sample_jar/lib/sample_jar.rb$}).should_not be_empty
175
+ file_list(%r{^sample_jar/extra\.foobar$}).should_not be_empty
176
+ end
177
+
178
+ it "sets load paths in init.rb" do
179
+ jar.add_init_file(config)
180
+ contents = jar.contents('META-INF/init.rb')
181
+ contents.split("\n").grep(/LOAD_PATH\.unshift.*sample_jar\/lib/).should_not be_empty
182
+ end
183
+
184
+ it "loads the first bin/executable in main.rb" do
185
+ jar.apply(config)
186
+ contents = jar.contents('META-INF/main.rb')
187
+ contents.split("\n").grep(/load.*sample_jar\/bin\/sample_jar/).should_not be_empty
188
+ end
189
+ end
190
+ end
191
+
192
+ context "in a war project" do
193
+ run_in_directory "spec/sample_war"
194
+ cleanup_temp_files
195
+
196
+ before(:each) do
197
+ mkdir_p "log"
198
+ touch "log/test.log"
199
+ end
200
+
201
+ it "detects a War trait" do
202
+ config.traits.should include(Warbler::Traits::War)
203
+ end
204
+
205
+ it "collects files in public" do
206
+ jar.apply(config)
207
+ file_list(%r{^index\.html}).should_not be_empty
208
+ end
209
+
210
+ it "collects gem files" do
211
+ use_config do |config|
212
+ config.gems << "rake"
213
+ end
214
+ jar.apply(config)
215
+ file_list(%r{WEB-INF/gems/gems/rake.*/lib/rake.rb}).should_not be_empty
216
+ file_list(%r{WEB-INF/gems/specifications/rake.*\.gemspec}).should_not be_empty
217
+ end
218
+
219
+ it "adds ENV['GEM_HOME'] to init.rb" do
220
+ jar.add_init_file(config)
221
+ contents = jar.contents('META-INF/init.rb')
222
+ contents.should =~ /ENV\['GEM_HOME'\]/
223
+ contents.should =~ /WEB-INF\/gems/
224
+ end
225
+
226
+ it "does not include log files by default" do
227
+ jar.apply(config)
228
+ file_list(%r{WEB-INF/log}).should_not be_empty
229
+ file_list(%r{WEB-INF/log/.*\.log}).should be_empty
230
+ end
231
+
232
+ def expand_webxml
233
+ jar.apply(config)
234
+ jar.files.should include("WEB-INF/web.xml")
235
+ require 'rexml/document'
236
+ REXML::Document.new(jar.files["WEB-INF/web.xml"]).root.elements
237
+ end
238
+
239
+ it "creates a web.xml file" do
240
+ use_config do |config|
241
+ config.webxml.jruby.max.runtimes = 5
242
+ end
243
+ elements = expand_webxml
244
+ elements.to_a(
245
+ "context-param/param-name[text()='jruby.max.runtimes']"
246
+ ).should_not be_empty
247
+ elements.to_a(
248
+ "context-param/param-name[text()='jruby.max.runtimes']/../param-value"
249
+ ).first.text.should == "5"
250
+ end
251
+
252
+ it "includes custom context parameters in web.xml" do
253
+ use_config do |config|
254
+ config.webxml.some.custom.config = "myconfig"
255
+ end
256
+ elements = expand_webxml
257
+ elements.to_a(
258
+ "context-param/param-name[text()='some.custom.config']"
259
+ ).should_not be_empty
260
+ elements.to_a(
261
+ "context-param/param-name[text()='some.custom.config']/../param-value"
262
+ ).first.text.should == "myconfig"
263
+ end
264
+
265
+ it "allows one jndi resource to be included" do
266
+ use_config do |config|
267
+ config.webxml.jndi = 'jndi/rails'
268
+ end
269
+ elements = expand_webxml
270
+ elements.to_a(
271
+ "resource-ref/res-ref-name[text()='jndi/rails']"
272
+ ).should_not be_empty
273
+ end
274
+
275
+ it "allows multiple jndi resources to be included" do
276
+ use_config do |config|
277
+ config.webxml.jndi = ['jndi/rails1', 'jndi/rails2']
278
+ end
279
+ elements = expand_webxml
280
+ elements.to_a(
281
+ "resource-ref/res-ref-name[text()='jndi/rails1']"
282
+ ).should_not be_empty
283
+ elements.to_a(
284
+ "resource-ref/res-ref-name[text()='jndi/rails2']"
285
+ ).should_not be_empty
286
+ end
287
+
288
+ it "does not include any ignored context parameters" do
289
+ use_config do |config|
290
+ config.webxml.foo = "bar"
291
+ config.webxml.ignored << "foo"
292
+ end
293
+ elements = expand_webxml
294
+ elements.to_a(
295
+ "context-param/param-name[text()='foo']"
296
+ ).should be_empty
297
+ elements.to_a(
298
+ "context-param/param-name[text()='ignored']"
299
+ ).should be_empty
300
+ elements.to_a(
301
+ "context-param/param-name[text()='jndi']"
302
+ ).should be_empty
303
+ end
304
+
305
+ it "uses a config/web.xml if it exists" do
306
+ mkdir_p "config"
307
+ touch "config/web.xml"
308
+ jar.apply(config)
309
+ jar.files["WEB-INF/web.xml"].should == "config/web.xml"
310
+ end
311
+
312
+ it "uses a config/web.xml.erb if it exists" do
313
+ mkdir_p "config"
314
+ File.open("config/web.xml.erb", "w") {|f| f << "Hi <%= webxml.public.root %>" }
315
+ jar.apply(config)
316
+ jar.files["WEB-INF/web.xml"].should_not be_nil
317
+ jar.files["WEB-INF/web.xml"].read.should == "Hi /"
318
+ end
319
+
320
+ it "collects java libraries" do
321
+ jar.apply(config)
322
+ file_list(%r{WEB-INF/lib/jruby-.*\.jar$}).should_not be_empty
323
+ end
324
+
325
+ it "collects application files" do
326
+ jar.apply(config)
327
+ file_list(%r{WEB-INF/app$}).should_not be_empty
328
+ file_list(%r{WEB-INF/config$}).should_not be_empty
329
+ file_list(%r{WEB-INF/lib$}).should_not be_empty
330
+ end
331
+
332
+ it "accepts an autodeploy directory where the war should be created" do
333
+ require 'tmpdir'
334
+ use_config do |config|
335
+ config.jar_name = 'warbler'
336
+ config.autodeploy_dir = Dir::tmpdir
337
+ end
338
+ touch "file.txt"
339
+ jar.files["file.txt"] = "file.txt"
340
+ silence { jar.create(config) }
341
+ File.exist?(File.join("#{Dir::tmpdir}","warbler.war")).should == true
342
+ end
343
+
344
+ it "allows the jar extension to be customized" do
345
+ use_config do |config|
346
+ config.jar_name = 'warbler'
347
+ config.jar_extension = 'foobar'
348
+ end
349
+ touch "file.txt"
350
+ jar.files["file.txt"] = "file.txt"
351
+ silence { jar.create(config) }
352
+ File.exist?("warbler.foobar").should == true
353
+ end
354
+
355
+ it "can exclude files from the .war" do
356
+ use_config do |config|
357
+ config.excludes += FileList['lib/tasks/utils.rake']
358
+ end
359
+ jar.apply(config)
360
+ file_list(%r{lib/tasks/utils.rake}).should be_empty
361
+ end
362
+
363
+ it "can exclude public files from the .war" do
364
+ use_config do |config|
365
+ config.excludes += FileList['public/robots.txt']
366
+ end
367
+ jar.apply(config)
368
+ file_list(%r{robots.txt}).should be_empty
369
+ end
370
+
371
+ it "reads configuration from #{Warbler::Config::FILE}" do
372
+ mkdir_p "config"
373
+ File.open(Warbler::Config::FILE, "w") do |dest|
374
+ contents =
375
+ File.open("#{Warbler::WARBLER_HOME}/warble.rb") do |src|
376
+ src.read
377
+ end
378
+ dest << contents.sub(/# config\.jar_name/, 'config.jar_name'
379
+ ).sub(/# config.gems << "tzinfo"/, 'config.gems = []')
380
+ end
381
+ t = Warbler::Task.new "warble"
382
+ t.config.jar_name.should == "mywar"
383
+ end
384
+
385
+ it "fails if a gem is requested that is not installed" do
386
+ use_config do |config|
387
+ config.gems = ["nonexistent-gem"]
388
+ end
389
+ lambda {
390
+ Warbler::Task.new "warble", config
391
+ jar.apply(config)
392
+ }.should raise_error
393
+ end
394
+
395
+ it "allows specification of dependency by Gem::Dependency" do
396
+ spec = mock "gem spec"
397
+ spec.stub!(:name).and_return "hpricot"
398
+ spec.stub!(:full_name).and_return "hpricot-0.6.157"
399
+ spec.stub!(:full_gem_path).and_return "hpricot-0.6.157"
400
+ spec.stub!(:loaded_from).and_return "hpricot.gemspec"
401
+ spec.stub!(:files).and_return ["Rakefile"]
402
+ spec.stub!(:dependencies).and_return []
403
+ Gem.source_index.should_receive(:search).and_return do |gem|
404
+ gem.name.should == "hpricot"
405
+ [spec]
406
+ end
407
+ use_config do |config|
408
+ config.gems = [Gem::Dependency.new("hpricot", "> 0.6")]
409
+ end
410
+ silence { jar.apply(config) }
411
+ end
412
+
413
+ it "copies loose java classes to WEB-INF/classes" do
414
+ use_config do |config|
415
+ config.java_classes = FileList["Rakefile"]
416
+ end
417
+ jar.apply(config)
418
+ file_list(%r{WEB-INF/classes/Rakefile$}).should_not be_empty
419
+ end
420
+
421
+ it "does not try to autodetect frameworks when Warbler.framework_detection is false" do
422
+ begin
423
+ Warbler.framework_detection = false
424
+ task :environment
425
+ config.webxml.booter.should_not == :rails
426
+ t = Rake::Task['environment']
427
+ class << t; public :instance_variable_get; end
428
+ t.instance_variable_get("@already_invoked").should == false
429
+ ensure
430
+ Warbler.framework_detection = true
431
+ end
432
+ end
433
+
434
+ context "with the executable feature" do
435
+ it "adds a WarMain class" do
436
+ use_config do |config|
437
+ config.features << "executable"
438
+ end
439
+ jar.apply(config)
440
+ file_list(%r{^WarMain\.class$}).should_not be_empty
441
+ end
442
+ end
443
+
444
+ context "in a Rails application" do
445
+ before :each do
446
+ @rails = nil
447
+ task :environment do
448
+ @rails = mock_rails_module
449
+ end
450
+ end
451
+
452
+ def mock_rails_module
453
+ rails = Module.new
454
+ Object.const_set("Rails", rails)
455
+ version = Module.new
456
+ rails.const_set("VERSION", version)
457
+ version.const_set("STRING", "2.1.0")
458
+ rails
459
+ end
460
+
461
+ it "detects a Rails trait" do
462
+ config.traits.should include(Warbler::Traits::Rails)
463
+ end
464
+
465
+ it "auto-detects a Rails application" do
466
+ config.webxml.booter.should == :rails
467
+ config.gems["rails"].should == "2.1.0"
468
+ end
469
+
470
+ it "provides Rails gems by default, unless vendor/rails is present" do
471
+ config.gems.should have_key("rails")
472
+
473
+ mkdir_p "vendor/rails"
474
+ config = Warbler::Config.new
475
+ config.gems.should be_empty
476
+
477
+ rm_rf "vendor/rails"
478
+ @rails.stub!(:vendor_rails?).and_return true
479
+ config = Warbler::Config.new
480
+ config.gems.should be_empty
481
+ end
482
+
483
+ it "automatically adds Rails.configuration.gems to the list of gems" do
484
+ task :environment do
485
+ config = mock "config"
486
+ @rails.stub!(:configuration).and_return(config)
487
+ gem = mock "gem"
488
+ gem.stub!(:name).and_return "hpricot"
489
+ gem.stub!(:requirement).and_return Gem::Requirement.new("=0.6")
490
+ config.stub!(:gems).and_return [gem]
491
+ end
492
+
493
+ config.webxml.booter.should == :rails
494
+ config.gems.keys.should include(Gem::Dependency.new("hpricot", Gem::Requirement.new("=0.6")))
495
+ end
496
+
497
+ context "with threadsafe! enabled" do
498
+ before :each do
499
+ cp "config/environments/production.rb", "config/environments/production.rb.orig"
500
+ File.open("config/environments/production.rb", "a") {|f| f.puts "", "config.threadsafe!" }
501
+ end
502
+
503
+ after :each do
504
+ mv "config/environments/production.rb.orig", "config/environments/production.rb"
505
+ end
506
+
507
+ it "sets the jruby min and max runtimes to 1" do
508
+ config.webxml.booter.should == :rails
509
+ config.webxml.jruby.min.runtimes.should == 1
510
+ config.webxml.jruby.max.runtimes.should == 1
511
+ end
512
+
513
+ it "doesn't override already configured runtime numbers" do
514
+ use_config do |config|
515
+ config.webxml.jruby.min.runtimes = 2
516
+ config.webxml.jruby.max.runtimes = 2
517
+ end
518
+ config.webxml.jruby.min.runtimes.should == 2
519
+ config.webxml.jruby.max.runtimes.should == 2
520
+ end
521
+ end
522
+
523
+ it "adds RAILS_ENV to init.rb" do
524
+ use_config do |config|
525
+ config.webxml.booter = :rails
526
+ end
527
+ jar.add_init_file(config)
528
+ contents = jar.contents('META-INF/init.rb')
529
+ contents.should =~ /ENV\['RAILS_ENV'\]/
530
+ contents.should =~ /'production'/
531
+ end
532
+ end
533
+
534
+ context "in a Merb application" do
535
+ before :each do
536
+ touch "config/init.rb"
537
+ @merb = nil
538
+ task :merb_env do
539
+ @merb = mock_merb_module
540
+ end
541
+ end
542
+
543
+ after :each do
544
+ rm_f "config/init.rb"
545
+ end
546
+
547
+ def mock_merb_module
548
+ merb = Module.new
549
+ silence { Object.const_set("Merb", merb) }
550
+ boot_loader = Module.new
551
+ merb.const_set("BootLoader", boot_loader)
552
+ merb.const_set("VERSION", "1.0")
553
+ dependencies = Class.new do
554
+ @dependencies = []
555
+ def self.dependencies
556
+ @dependencies
557
+ end
558
+ def self.dependencies=(deps)
559
+ @dependencies = deps
560
+ end
561
+ end
562
+ boot_loader.const_set("Dependencies", dependencies)
563
+ dependencies
564
+ end
565
+
566
+ it "detects a Merb trait" do
567
+ config.traits.should include(Warbler::Traits::Merb)
568
+ end
569
+
570
+ it "auto-detects a Merb application" do
571
+ config.webxml.booter.should == :merb
572
+ config.gems.keys.should_not include("rails")
573
+ end
574
+
575
+ it "automatically adds Merb::BootLoader::Dependencies.dependencies to the list of gems" do
576
+ task :merb_env do
577
+ @merb.dependencies = [Gem::Dependency.new("merb-core", ">= 1.0.6.1")]
578
+ end
579
+ config.gems.keys.should include(Gem::Dependency.new("merb-core", ">= 1.0.6.1"))
580
+ end
581
+
582
+ it "skips Merb development dependencies" do
583
+ task :merb_env do
584
+ @merb.dependencies = [Gem::Dependency.new("rake", "= #{RAKEVERSION}", :development)]
585
+ end
586
+ jar.apply(config)
587
+ file_list(/rake-#{RAKEVERSION}/).should be_empty
588
+ end
589
+
590
+ it "warns about using Merb < 1.0" do
591
+ task :merb_env do
592
+ silence { Object.const_set("Merb", Module.new) }
593
+ end
594
+ silence { config.webxml.booter.should == :merb }
595
+ end
596
+ end
597
+
598
+ context "in a Rack application" do
599
+ before :each do
600
+ Dir.chdir('tmp')
601
+ rackup = "run Proc.new {|env| [200, {}, ['Hello World']]}"
602
+ File.open("config.ru", "w") {|f| f << rackup }
603
+ end
604
+
605
+ it "detects a Rack trait" do
606
+ config.traits.should include(Warbler::Traits::Rack)
607
+ end
608
+
609
+ it "auto-detects a Rack application with a config.ru file" do
610
+ jar.apply(config)
611
+ jar.files['WEB-INF/config.ru'].should == 'config.ru'
612
+ end
613
+
614
+ it "adds RACK_ENV to init.rb" do
615
+ jar.add_init_file(config)
616
+ contents = jar.contents('META-INF/init.rb')
617
+ contents.should =~ /ENV\['RACK_ENV'\]/
618
+ contents.should =~ /'production'/
619
+ end
620
+ end
621
+
622
+ it "skips directories that don't exist in config.dirs and print a warning" do
623
+ use_config do |config|
624
+ config.dirs = %w(lib notexist)
625
+ end
626
+ silence { jar.apply(config) }
627
+ file_list(%r{WEB-INF/lib}).should_not be_empty
628
+ file_list(%r{WEB-INF/notexist}).should be_empty
629
+ end
630
+
631
+ it "excludes Warbler's old tmp/war directory by default" do
632
+ mkdir_p "tmp/war"
633
+ touch "tmp/war/index.html"
634
+ use_config do |config|
635
+ config.dirs += ["tmp"]
636
+ end
637
+ jar.apply(config)
638
+ file_list(%r{WEB-INF/tmp/war/index\.html}).should be_empty
639
+ end
640
+
641
+ it "writes gems to location specified by gem_path" do
642
+ use_config do |config|
643
+ config.gem_path = "/WEB-INF/jewels"
644
+ config.gems << 'rake'
645
+ end
646
+ elements = expand_webxml
647
+ file_list(%r{WEB-INF/jewels}).should_not be_empty
648
+ elements.to_a(
649
+ "context-param/param-name[text()='gem.path']"
650
+ ).should_not be_empty
651
+ elements.to_a(
652
+ "context-param/param-name[text()='gem.path']/../param-value"
653
+ ).first.text.should == "/WEB-INF/jewels"
654
+ end
655
+
656
+ it "allows adding additional WEB-INF files via config.webinf_files" do
657
+ File.open("myserver-web.xml", "w") do |f|
658
+ f << "<web-app></web-app>"
659
+ end
660
+ use_config do |config|
661
+ config.webinf_files = FileList['myserver-web.xml']
662
+ end
663
+ jar.apply(config)
664
+ file_list(%r{WEB-INF/myserver-web.xml}).should_not be_empty
665
+ end
666
+
667
+ it "allows expanding of additional WEB-INF files via config.webinf_files" do
668
+ File.open("myserver-web.xml.erb", "w") do |f|
669
+ f << "<web-app><%= webxml.rails.env %></web-app>"
670
+ end
671
+ use_config do |config|
672
+ config.webinf_files = FileList['myserver-web.xml.erb']
673
+ end
674
+ jar.apply(config)
675
+ file_list(%r{WEB-INF/myserver-web.xml}).should_not be_empty
676
+ jar.contents('WEB-INF/myserver-web.xml').should =~ /web-app.*production/
677
+ end
678
+
679
+ it "excludes test files in gems according to config.gem_excludes" do
680
+ use_config do |config|
681
+ config.gem_excludes += [/^(test|spec)\//]
682
+ end
683
+ jar.apply(config)
684
+ file_list(%r{WEB-INF/gems/gems/rake([^/]+)/test/test_rake.rb}).should be_empty
685
+ end
686
+
687
+ it "creates a META-INF/init.rb file with startup config" do
688
+ jar.apply(config)
689
+ file_list(%r{META-INF/init.rb}).should_not be_empty
690
+ end
691
+
692
+ it "allows adjusting the init file location in the war" do
693
+ use_config do |config|
694
+ config.init_filename = 'WEB-INF/init.rb'
695
+ end
696
+ jar.add_init_file(config)
697
+ file_list(%r{WEB-INF/init.rb}).should_not be_empty
698
+ end
699
+
700
+ it "allows adding custom files' contents to init.rb" do
701
+ use_config do |config|
702
+ config.init_contents << "Rakefile"
703
+ end
704
+ jar.add_init_file(config)
705
+ contents = jar.contents('META-INF/init.rb')
706
+ contents.should =~ /require 'rake'/
707
+ end
708
+
709
+ it "does not have escaped HTML in WARBLER_CONFIG" do
710
+ use_config do |config|
711
+ config.webxml.dummy = '<dummy/>'
712
+ end
713
+ jar.apply(config)
714
+ jar.contents('META-INF/init.rb').should =~ /<dummy\/>/
715
+ end
716
+ end
717
+ end
718
+