finnlabs-warbler 0.9.14

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,39 @@
1
+ #--
2
+ # (c) Copyright 2007-2008 Sun Microsystems, Inc.
3
+ # See the file LICENSES.txt included with the distribution for
4
+ # software license details.
5
+ #++
6
+
7
+ require File.dirname(__FILE__) + '/../spec_helper'
8
+
9
+ describe Warbler::Gems do
10
+ it "should accept a hash for initialization" do
11
+ gems = Warbler::Gems.new({"actionpack" => "1.2.3"})
12
+ gems.should have_key("actionpack")
13
+ gems["actionpack"].should == "1.2.3"
14
+ end
15
+
16
+ it "should accept an array for initialization" do
17
+ gems = Warbler::Gems.new ["activerecord"]
18
+ gems.should have_key("activerecord")
19
+ end
20
+
21
+ it "should allow gems with a version" do
22
+ gems = Warbler::Gems.new
23
+ gems["actionpack"] = "> 1.2.3"
24
+ gems["actionpack"].should == "> 1.2.3"
25
+ end
26
+
27
+ it "should allow gems without an explicit version" do
28
+ gems = Warbler::Gems.new
29
+ gems << "actionpack"
30
+ gems.should have_key("actionpack")
31
+ end
32
+
33
+ it "should allow to add gems" do
34
+ gems = Warbler::Gems.new
35
+ gems << "rails"
36
+ gems += ["activerecord-jdbcmysql-adapter", "jdbc-mysql", "jruby-openssl"]
37
+ ["rails", "activerecord-jdbcmysql-adapter", "jdbc-mysql", "jruby-openssl"].each {|g| gems.should have_key(g)}
38
+ end
39
+ end
@@ -0,0 +1,486 @@
1
+ #--
2
+ # (c) Copyright 2007-2009 Sun Microsystems, Inc.
3
+ # See the file LICENSES.txt included with the distribution for
4
+ # software license details.
5
+ #++
6
+
7
+ require File.dirname(__FILE__) + '/../spec_helper'
8
+
9
+ describe Warbler::Task do
10
+ before(:each) do
11
+ @rake = Rake::Application.new
12
+ Rake.application = @rake
13
+ verbose(false)
14
+ @pwd = Dir.getwd
15
+ Dir.chdir("spec/sample")
16
+ mkdir_p "log"
17
+ touch "log/test.log"
18
+ @config = Warbler::Config.new do |config|
19
+ config.staging_dir = "tmp/war"
20
+ config.war_name = "warbler"
21
+ config.gems = ["rake"]
22
+ config.webxml.jruby.max.runtimes = 5
23
+ end
24
+ end
25
+
26
+ after(:each) do
27
+ define_tasks "clean"
28
+ Rake::Task["warble:clean"].invoke
29
+ rm_rf "log"
30
+ rm_f FileList["config.ru", "*web.xml", "config/web.xml*", "config/warble.rb"]
31
+ Dir.chdir(@pwd)
32
+ end
33
+
34
+ def define_tasks(*tasks)
35
+ options = tasks.last.kind_of?(Hash) ? tasks.pop : {}
36
+ @defined_tasks ||= []
37
+ tasks.each do |task|
38
+ unless @defined_tasks.include?(task)
39
+ meth = "define_#{task}_task"
40
+ meth = "define_#{task}_tasks" unless Warbler::Task.private_instance_methods.include?(meth)
41
+ Warbler::Task.new "warble", @config, meth.to_sym do |t|
42
+ options.each {|k,v| t.send "#{k}=", v }
43
+ end
44
+ @defined_tasks << task
45
+ end
46
+ end
47
+ end
48
+
49
+ def file_list(regex)
50
+ FileList["#{@config.staging_dir}/**/*"].select {|f| f =~ regex }
51
+ end
52
+
53
+ it "should define a clean task for removing the staging directory" do
54
+ define_tasks "clean"
55
+ mkdir_p @config.staging_dir
56
+ Rake::Task["warble:clean"].invoke
57
+ File.exist?(@config.staging_dir).should == false
58
+ end
59
+
60
+ it "should define a public task for copying the public files" do
61
+ define_tasks "public"
62
+ Rake::Task["warble:public"].invoke
63
+ file_list(%r{^#{@config.staging_dir}/index\.html}).should_not be_empty
64
+ end
65
+
66
+ it "should define a gems task for unpacking gems" do
67
+ @config.gems << "rails"
68
+ define_tasks "gems"
69
+ Rake::Task["warble:gems"].invoke
70
+ file_list(%r{WEB-INF/gems/gems/rake.*/lib/rake.rb}).should_not be_empty
71
+ file_list(%r{WEB-INF/gems/specifications/rake.*\.gemspec}).should_not be_empty
72
+ end
73
+
74
+ it "should define a app task for copying application files" do
75
+ define_tasks "app", "gems"
76
+ Rake::Task["warble:app"].invoke
77
+ file_list(%r{WEB-INF/log}).should_not be_empty
78
+ file_list(%r{WEB-INF/log/*.log}).should be_empty
79
+ end
80
+
81
+ def expand_webxml
82
+ define_tasks "webxml"
83
+ Rake::Task["warble:webxml"].invoke
84
+ require 'rexml/document'
85
+ File.open("#{@config.staging_dir}/WEB-INF/web.xml") do |f|
86
+ REXML::Document.new(f).root.elements
87
+ end
88
+ end
89
+
90
+ it "should define a webxml task for creating web.xml" do
91
+ elements = expand_webxml
92
+ elements.to_a(
93
+ "context-param/param-name[text()='jruby.max.runtimes']"
94
+ ).should_not be_empty
95
+ elements.to_a(
96
+ "context-param/param-name[text()='jruby.max.runtimes']/../param-value"
97
+ ).first.text.should == "5"
98
+ end
99
+
100
+ it "should include custom context parameters" do
101
+ @config.webxml.some.custom.config = "myconfig"
102
+ elements = expand_webxml
103
+ elements.to_a(
104
+ "context-param/param-name[text()='some.custom.config']"
105
+ ).should_not be_empty
106
+ elements.to_a(
107
+ "context-param/param-name[text()='some.custom.config']/../param-value"
108
+ ).first.text.should == "myconfig"
109
+ end
110
+
111
+ it "should allow one jndi resource to be included" do
112
+ @config.webxml.jndi = 'jndi/rails'
113
+ elements = expand_webxml
114
+ elements.to_a(
115
+ "resource-ref/res-ref-name[text()='jndi/rails']"
116
+ ).should_not be_empty
117
+ end
118
+
119
+ it "should allow multiple jndi resources to be included" do
120
+ @config.webxml.jndi = ['jndi/rails1', 'jndi/rails2']
121
+ elements = expand_webxml
122
+ elements.to_a(
123
+ "resource-ref/res-ref-name[text()='jndi/rails1']"
124
+ ).should_not be_empty
125
+ elements.to_a(
126
+ "resource-ref/res-ref-name[text()='jndi/rails2']"
127
+ ).should_not be_empty
128
+ end
129
+
130
+ it "should not include any ignored context parameters" do
131
+ @config.webxml.foo = "bar"
132
+ @config.webxml.ignored << "foo"
133
+ elements = expand_webxml
134
+ elements.to_a(
135
+ "context-param/param-name[text()='foo']"
136
+ ).should be_empty
137
+ elements.to_a(
138
+ "context-param/param-name[text()='ignored']"
139
+ ).should be_empty
140
+ elements.to_a(
141
+ "context-param/param-name[text()='jndi']"
142
+ ).should be_empty
143
+ end
144
+
145
+ it "should use a config/web.xml if it exists" do
146
+ define_tasks "webxml"
147
+ mkdir_p "config"
148
+ File.open("config/web.xml", "w") {|f| f << "Hi there" }
149
+ Rake::Task["warble:webxml"].invoke
150
+ files = file_list(%r{WEB-INF/web.xml$})
151
+ files.should_not be_empty
152
+ File.open(files.first) {|f| f.read}.should == "Hi there"
153
+ end
154
+
155
+ it "should use a config/web.xml.erb if it exists" do
156
+ define_tasks "webxml"
157
+ mkdir_p "config"
158
+ File.open("config/web.xml.erb", "w") {|f| f << "Hi <%= webxml.public.root %>" }
159
+ Rake::Task["warble:webxml"].invoke
160
+ files = file_list(%r{WEB-INF/web.xml$})
161
+ files.should_not be_empty
162
+ File.open(files.first) {|f| f.read}.should == "Hi /"
163
+ end
164
+
165
+ it "should define a java_libs task for copying java libraries" do
166
+ define_tasks "java_libs"
167
+ Rake::Task["warble:java_libs"].invoke
168
+ file_list(%r{WEB-INF/lib/jruby-complete.*\.jar$}).should_not be_empty
169
+ end
170
+
171
+ it "should define an app task for copying application files" do
172
+ gems_ran = false
173
+ task "warble:gems" do
174
+ gems_ran = true
175
+ end
176
+ define_tasks "app"
177
+ Rake::Task["warble:app"].invoke
178
+ file_list(%r{WEB-INF/app$}).should_not be_empty
179
+ file_list(%r{WEB-INF/config$}).should_not be_empty
180
+ file_list(%r{WEB-INF/lib$}).should_not be_empty
181
+ gems_ran.should == true
182
+ end
183
+
184
+ it "should define a jar task for creating the .war" do
185
+ define_tasks "jar"
186
+ mkdir_p @config.staging_dir
187
+ touch "#{@config.staging_dir}/file.txt"
188
+ Rake::Task["warble:jar"].invoke
189
+ File.exist?("warbler.war").should == true
190
+ end
191
+
192
+ it "should define an exploded task for creating an exploded Rails app" do
193
+ @config.java_classes = ["Rakefile"]
194
+ @config.java_libs = []
195
+ define_tasks "webxml", "exploded", "java_classes", "gems"
196
+ Rake::Task['warble:exploded'].invoke
197
+ File.exist?("web.xml").should == true
198
+ File.exist?("sun-web.xml").should == true
199
+ File.symlink?("gems").should == true
200
+ File.symlink?("public/WEB-INF").should == true
201
+ Rake::Task['warble:clean:exploded'].invoke
202
+ end
203
+
204
+ it "should accept an autodeploy directory where the war should be created" do
205
+ define_tasks "jar"
206
+ require 'tempfile'
207
+ @config.autodeploy_dir = Dir::tmpdir
208
+ mkdir_p @config.staging_dir
209
+ touch "#{@config.staging_dir}/file.txt"
210
+ Rake::Task["warble:jar"].invoke
211
+ File.exist?(File.join("#{Dir::tmpdir}","warbler.war")).should == true
212
+ end
213
+
214
+ it "should define a war task for bundling up everything" do
215
+ app_ran = false; task "warble:app" do; app_ran = true; end
216
+ public_ran = false; task "warble:public" do; public_ran = true; end
217
+ jar_ran = false; task "warble:jar" do; jar_ran = true; end
218
+ webxml_ran = false; task "warble:webxml" do; webxml_ran = true; end
219
+ define_tasks "main"
220
+ Rake::Task["warble"].invoke
221
+ app_ran.should == true
222
+ public_ran.should == true
223
+ jar_ran.should == true
224
+ webxml_ran.should == true
225
+ end
226
+
227
+ it "should be able to exclude files from the .war" do
228
+ @config.excludes += FileList['lib/tasks/utils.rake']
229
+ task "warble:gems" do; end
230
+ define_tasks "app"
231
+ Rake::Task["warble:app"].invoke
232
+ file_list(%r{lib/tasks/utils.rake}).should be_empty
233
+ end
234
+
235
+ it "should be able to define all tasks successfully" do
236
+ Warbler::Task.new "warble", @config
237
+ end
238
+
239
+ it "should read configuration from #{Warbler::Config::FILE}" do
240
+ mkdir_p "config"
241
+ File.open(Warbler::Config::FILE, "w") do |dest|
242
+ contents =
243
+ File.open("#{Warbler::WARBLER_HOME}/generators/warble/templates/warble.rb") do |src|
244
+ src.read
245
+ end
246
+ dest << contents.sub(/# config\.war_name/, 'config.war_name'
247
+ ).sub(/# config.gems << "tzinfo"/, 'config.gems = []')
248
+ end
249
+ t = Warbler::Task.new "warble"
250
+ t.config.war_name.should == "mywar"
251
+ end
252
+
253
+ it "should fail if a gem is requested that is not installed" do
254
+ @config.gems = ["nonexistent-gem"]
255
+ lambda {
256
+ Warbler::Task.new "warble", @config
257
+ }.should raise_error
258
+ end
259
+
260
+ it "should handle platform-specific gems" do
261
+ spec = mock "gem spec"
262
+ spec.stub!(:name).and_return "hpricot"
263
+ spec.stub!(:version).and_return "0.6.157"
264
+ spec.stub!(:platform).and_return "java"
265
+ spec.stub!(:original_platform).and_return "java"
266
+ spec.stub!(:loaded_from).and_return "hpricot.gemspec"
267
+ spec.stub!(:dependencies).and_return []
268
+ Gem.source_index.should_receive(:search).and_return do |gem|
269
+ gem.name.should == "hpricot"
270
+ [spec]
271
+ end
272
+ File.should_receive(:exist?).with(File.join(Gem.dir, 'cache', "hpricot-0.6.157-java.gem")).and_return true
273
+ @config.gems = ["hpricot"]
274
+ define_tasks "gems"
275
+ end
276
+
277
+ it "should allow specification of dependency by Gem::Dependency" do
278
+ spec = mock "gem spec"
279
+ spec.stub!(:name).and_return "hpricot"
280
+ spec.stub!(:version).and_return "0.6.157"
281
+ spec.stub!(:platform).and_return "java"
282
+ spec.stub!(:original_platform).and_return "java"
283
+ spec.stub!(:loaded_from).and_return "hpricot.gemspec"
284
+ spec.stub!(:dependencies).and_return []
285
+ Gem.source_index.should_receive(:search).and_return do |gem|
286
+ gem.name.should == "hpricot"
287
+ [spec]
288
+ end
289
+ File.should_receive(:exist?).with(File.join(Gem.dir, 'cache', "hpricot-0.6.157-java.gem")).and_return true
290
+ @config.gems = [Gem::Dependency.new("hpricot", "> 0.6")]
291
+ define_tasks "gems"
292
+ end
293
+
294
+ it "should define a java_classes task for copying loose java classes" do
295
+ @config.java_classes = FileList["Rakefile"]
296
+ define_tasks "java_classes"
297
+ Rake::Task["warble:java_classes"].invoke
298
+ file_list(%r{WEB-INF/classes/Rakefile$}).should_not be_empty
299
+ end
300
+
301
+ def mock_rails_module
302
+ rails = Module.new
303
+ Object.const_set("Rails", rails)
304
+ version = Module.new
305
+ rails.const_set("VERSION", version)
306
+ version.const_set("STRING", "2.1.0")
307
+ rails
308
+ end
309
+
310
+ def mock_merb_module
311
+ merb = Module.new
312
+ Object.const_set("Merb", merb)
313
+ boot_loader = Module.new
314
+ merb.const_set("BootLoader", boot_loader)
315
+ merb.const_set("VERSION", "1.0")
316
+ dependencies = Class.new do
317
+ @@dependencies = []
318
+ def self.dependencies
319
+ @@dependencies
320
+ end
321
+ def self.dependencies=(deps)
322
+ @@dependencies = deps
323
+ end
324
+ end
325
+ boot_loader.const_set("Dependencies", dependencies)
326
+ dependencies
327
+ end
328
+
329
+ it "should auto-detect a Rails application" do
330
+ task :environment do
331
+ mock_rails_module
332
+ end
333
+ @config = Warbler::Config.new
334
+ @config.webxml.booter.should == :rails
335
+ @config.gems["rails"].should == "2.1.0"
336
+ end
337
+
338
+ it "should provide Rails gems by default, unless vendor/rails is present" do
339
+ rails = nil
340
+ task :environment do
341
+ rails = mock_rails_module
342
+ end
343
+
344
+ config = Warbler::Config.new
345
+ config.gems.should have_key("rails")
346
+
347
+ mkdir_p "vendor/rails"
348
+ config = Warbler::Config.new
349
+ config.gems.should be_empty
350
+
351
+ rm_rf "vendor/rails"
352
+ rails.stub!(:vendor_rails?).and_return true
353
+ config = Warbler::Config.new
354
+ config.gems.should be_empty
355
+ end
356
+
357
+ it "should not try to autodetect frameworks when Warbler.framework_detection is false" do
358
+ begin
359
+ Warbler.framework_detection = false
360
+ task :environment
361
+ config = Warbler::Config.new
362
+ config.webxml.booter.should_not == :rails
363
+ t = Rake::Task['environment']
364
+ class << t; public :instance_variable_get; end
365
+ t.instance_variable_get("@already_invoked").should == false
366
+ ensure
367
+ Warbler.framework_detection = true
368
+ end
369
+ end
370
+
371
+ it "should auto-detect a Merb application" do
372
+ task :merb_env do
373
+ mock_merb_module
374
+ end
375
+ @config = Warbler::Config.new
376
+ @config.webxml.booter.should == :merb
377
+ @config.gems.keys.should_not include("rails")
378
+ end
379
+
380
+ it "should auto-detect a Rack application with a config.ru file" do
381
+ rackup = "run Proc.new {|env| [200, {}, ['Hello World']]}"
382
+ File.open("config.ru", "w") {|f| f << rackup }
383
+ @config = Warbler::Config.new
384
+ @config.webxml.booter.should == :rack
385
+ @config.webxml.rackup.should == rackup
386
+ end
387
+
388
+ it "should automatically add Rails.configuration.gems to the list of gems" do
389
+ task :environment do
390
+ rails = mock_rails_module
391
+ config = mock "config"
392
+ rails.stub!(:configuration).and_return(config)
393
+ gem = mock "gem"
394
+ gem.stub!(:name).and_return "hpricot"
395
+ gem.stub!(:requirement).and_return Gem::Requirement.new("=0.6")
396
+ config.stub!(:gems).and_return [gem]
397
+ end
398
+
399
+ @config = Warbler::Config.new
400
+ @config.webxml.booter.should == :rails
401
+ @config.gems.keys.should include(Gem::Dependency.new("hpricot", Gem::Requirement.new("=0.6")))
402
+ end
403
+
404
+ it "should automatically add Merb::BootLoader::Dependencies.dependencies to the list of gems" do
405
+ task :merb_env do
406
+ deps = mock_merb_module
407
+ deps.dependencies = [Gem::Dependency.new("merb-core", ">= 1.0.6.1")]
408
+ end
409
+ @config = Warbler::Config.new
410
+ @config.webxml.booter.should == :merb
411
+ @config.gems.keys.should include(Gem::Dependency.new("merb-core", ">= 1.0.6.1"))
412
+ end
413
+
414
+ it "should skip Merb development dependencies" do
415
+ task :merb_env do
416
+ deps = mock_merb_module
417
+ deps.dependencies = [Gem::Dependency.new("rake", "= #{RAKEVERSION}", :development)]
418
+ end
419
+ @config = Warbler::Config.new
420
+ define_tasks "copy_gems"
421
+ Rake.application.lookup("gem:rake-#{RAKEVERSION}").should be_nil
422
+ end
423
+
424
+ it "should warn about using Merb < 1.0" do
425
+ task :merb_env do
426
+ Object.const_set("Merb", Module.new)
427
+ end
428
+ @config = Warbler::Config.new
429
+ @config.webxml.booter.should == :merb
430
+ end
431
+
432
+ it "should set the jruby max runtimes to 1 when MT Rails is detected" do
433
+ task :environment do
434
+ rails = mock_rails_module
435
+ config = mock "config"
436
+ rails.stub!(:configuration).and_return(config)
437
+ config.stub!(:threadsafe!)
438
+ end
439
+ @config = Warbler::Config.new
440
+ @config.webxml.booter.should == :rails
441
+ @config.webxml.jruby.max.runtimes.should == 1
442
+ end
443
+
444
+ it "should skip directories that don't exist in config.dirs and print a warning" do
445
+ @config = Warbler::Config.new
446
+ @config.dirs = %w(lib notexist)
447
+ define_tasks "webinf_file"
448
+ Rake.application.lookup("#{@config.staging_dir}/WEB-INF/lib").should_not be_nil
449
+ Rake.application.lookup("#{@config.staging_dir}/WEB-INF/notexist").should be_nil
450
+ end
451
+ end
452
+
453
+ describe "The warbler.rake file" do
454
+ it "should be able to list its contents" do
455
+ output = `#{FileUtils::RUBY} -S rake -f #{Warbler::WARBLER_HOME}/tasks/warbler.rake -T`
456
+ output.should =~ /war\s/
457
+ output.should =~ /war:exploded/
458
+ output.should =~ /war:app/
459
+ output.should =~ /war:clean/
460
+ output.should =~ /war:gems/
461
+ output.should =~ /war:jar/
462
+ output.should =~ /war:java_libs/
463
+ output.should =~ /war:java_classes/
464
+ output.should =~ /war:public/
465
+ end
466
+ end
467
+
468
+ describe "Debug targets" do
469
+ before(:each) do
470
+ @rake = Rake::Application.new
471
+ Rake.application = @rake
472
+ verbose(false)
473
+ silence { Warbler::Task.new :war, Object.new }
474
+ end
475
+
476
+ it "should print out lists of files" do
477
+ capture { Rake::Task["war:debug:public"].invoke }.should =~ /public/
478
+ capture { Rake::Task["war:debug:gems"].invoke }.should =~ /gems/
479
+ capture { Rake::Task["war:debug:java_libs"].invoke }.should =~ /java_libs/
480
+ capture { Rake::Task["war:debug:java_classes"].invoke }.should =~ /java_classes/
481
+ capture { Rake::Task["war:debug:app"].invoke }.should =~ /app/
482
+ capture { Rake::Task["war:debug:includes"].invoke }.should =~ /include/
483
+ capture { Rake::Task["war:debug:excludes"].invoke }.should =~ /exclude/
484
+ capture { Rake::Task["war:debug"].invoke }.should =~ /Config/
485
+ end
486
+ end