/
task_spec.rb
146 lines (126 loc) · 5.3 KB
/
task_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
#--
# Copyright (c) 2010 Engine Yard, Inc.
# Copyright (c) 2007-2009 Sun Microsystems, Inc.
# This source code is available under the MIT license.
# See the file LICENSE.txt for details.
#++
require File.dirname(__FILE__) + '/../spec_helper'
describe Warbler::Task do
before(:each) do
@rake = Rake::Application.new
Rake.application = @rake
verbose(false)
@pwd = Dir.getwd
Dir.chdir("spec/sample")
mkdir_p "log"
touch "log/test.log"
@config = Warbler::Config.new do |config|
config.war_name = "warbler"
config.gems = ["rake"]
config.webxml.jruby.max.runtimes = 5
end
@task = Warbler::Task.new "warble", @config
end
after(:each) do
Rake::Task["warble:clean"].invoke
rm_rf "log"
rm_f FileList["config.ru", "*web.xml", "config/web.xml*", "config/warble.rb",
"config/special.txt", "config/link.txt", "tmp/gems.jar",
"file.txt", 'Gemfile', 'lib/rakelib']
Dir.chdir(@pwd)
end
it "should define a clean task for removing the war file" do
war_file = "#{@config.war_name}.war"
touch war_file
Rake::Task["warble:clean"].invoke
File.exist?(war_file).should == false
end
it "should define a make_gemjar task for storing gems in a jar file" do
silence { Rake::Task["warble:make_gemjar"].invoke }
File.exist?("tmp/gems.jar").should == true
@task.war.files.keys.should_not include(%r{WEB-INF\/gems})
@task.war.files.keys.should include("WEB-INF/lib/gems.jar")
end
it "should define a war task for bundling up everything" do
files_ran = false; task "warble:files" do; files_ran = true; end
jar_ran = false; task "warble:jar" do; jar_ran = true; end
silence { Rake::Task["warble"].invoke }
files_ran.should == true
jar_ran.should == true
end
it "should define a jar task for creating the .war" do
touch "file.txt"
@task.war.files["file.txt"] = "file.txt"
silence { Rake::Task["warble:jar"].invoke }
File.exist?("#{@config.war_name}.war").should == true
end
it "should invoke feature tasks configured in config.features" do
@config.features << "gemjar"
silence { Rake::Task["warble"].invoke }
@task.war.files.keys.should include("WEB-INF/lib/gems.jar")
end
it "should warn and skip unknown features configured in config.features" do
@config.features << "bogus"
capture { Rake::Task["warble"].invoke }.should =~ /unknown feature `bogus'/
end
it "should define an executable task for embedding a server in the war file" do
silence { Rake::Task["warble:executable"].invoke }
@task.war.files.keys.should include('WEB-INF/winstone.jar')
end
it "should be able to define all tasks successfully" do
Warbler::Task.new "warble", @config
end
it "should compile any ruby files specified" do
@config.compiled_ruby_files = FileList["app/helpers/application_helper.rb"]
silence { Rake::Task["warble"].invoke }
java_class_magic_number = [0xCA,0xFE,0xBA,0xBE].map { |magic_char| magic_char.chr }.join
Zip::ZipFile.open("#{@config.war_name}.war") do |zf|
java_class_header = zf.get_input_stream('WEB-INF/app/helpers/application_helper.class') {|io| io.read }[0..3]
ruby_class_definition = zf.get_input_stream('WEB-INF/app/helpers/application_helper.rb') {|io| io.read }
java_class_header.should == java_class_magic_number
ruby_class_definition.should == %{require __FILE__.sub(/.rb$/, '.class')}
end
end
it "should process symlinks by storing a file in the archive that has the same contents as the source" do
File.open("config/special.txt", "wb") {|f| f << "special"}
Dir.chdir("config") { ln_s "special.txt", "link.txt" }
silence { Rake::Task["warble"].invoke }
Zip::ZipFile.open("#{@config.war_name}.war") do |zf|
special = zf.get_input_stream('WEB-INF/config/special.txt') {|io| io.read }
link = zf.get_input_stream('WEB-INF/config/link.txt') {|io| io.read }
link.should == special
end
end
it "should process directory symlinks by copying the whole subdirectory" do
Dir.chdir("lib") { ln_s "tasks", "rakelib" }
silence { Rake::Task["warble"].invoke }
Zip::ZipFile.open("#{@config.war_name}.war") do |zf|
zf.find_entry("WEB-INF/lib/tasks/utils.rake").should_not be_nil
zf.find_entry("WEB-INF/lib/rakelib/").should_not be_nil
zf.find_entry("WEB-INF/lib/rakelib/utils.rake").should_not be_nil if defined?(JRUBY_VERSION)
end
end
it "should use a Bundler Gemfile to include gems" do
File.open("Gemfile", "w") {|f| f << "gem 'rspec'"}
@config.bundler = true
@config.send(:detect_bundler_gems)
silence { Rake::Task["warble"].invoke }
Zip::ZipFile.open("#{@config.war_name}.war") do |zf|
rspec_version = @config.gems.keys.detect {|k| k.name == 'rspec'}.version
zf.find_entry("WEB-INF/gems/specifications/rspec-#{rspec_version}.gemspec").should_not be_nil
end
end
end
describe "Debug targets" do
before(:each) do
@rake = Rake::Application.new
Rake.application = @rake
verbose(false)
silence { Warbler::Task.new :war, Object.new }
end
it "should print out lists of files" do
capture { Rake::Task["war:debug:includes"].invoke }.should =~ /include/
capture { Rake::Task["war:debug:excludes"].invoke }.should =~ /exclude/
capture { Rake::Task["war:debug"].invoke }.should =~ /Config/
end
end