ymdp 0.1.6 → 0.1.7

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.
@@ -34,16 +34,22 @@ describe "Compiler" do
34
34
  end
35
35
  end
36
36
 
37
+ describe "log" do
38
+ it "should return output formatted with time" do
39
+ @compiler.log("Hello").should match(/Hello$/)
40
+ end
41
+ end
42
+
37
43
  describe "process" do
38
44
  before(:each) do
39
45
  YMDP::ApplicationView.stub!(:supported_languages).and_return([])
40
-
41
- @view_template = mock('view_template').as_null_object
42
- YMDP::Compiler::Template::View.stub!(:new).and_return(@view_template)
43
- @js_template = mock('js_template').as_null_object
44
- YMDP::Compiler::Template::JavaScript.stub!(:new).and_return(@js_template)
45
- @yrb_template = mock('yrb_template').as_null_object
46
- YMDP::Compiler::Template::YRB.stub!(:new).and_return(@yrb_template)
46
+
47
+ # @view_template = mock('view_template').as_null_object
48
+ # YMDP::Compiler::Template::View.stub!(:new).and_return(@view_template)
49
+ # @js_template = mock('js_template').as_null_object
50
+ # YMDP::Compiler::Template::JavaScript.stub!(:new).and_return(@js_template)
51
+ # @yrb_template = mock('yrb_template').as_null_object
52
+ # YMDP::Compiler::Template::YRB.stub!(:new).and_return(@yrb_template)
47
53
 
48
54
  Dir.stub!(:[]).and_return([])
49
55
  end
@@ -52,9 +58,36 @@ describe "Compiler" do
52
58
  File.should_receive(:exists?).with("#{@base_path}/servers/#{@domain}")
53
59
  @compiler.process_all
54
60
  end
61
+
62
+ describe "copy_images" do
63
+ it "should remove images folder" do
64
+ FileUtils.should_receive(:rm_rf).with("./app/servers/#{@domain}/assets/images")
65
+ @compiler.process_all
66
+ end
67
+
68
+ it "should create images folder" do
69
+ FileUtils.should_receive(:mkdir_p).with("./app/servers/#{@domain}/assets")
70
+ @compiler.process_all
71
+ end
72
+
73
+ it "should copy images from app to server" do
74
+ FileUtils.should_receive(:cp_r).with("./app/assets/images/", "./app/servers/#{@domain}/assets")
75
+ @compiler.process_all
76
+ end
77
+
78
+ it "should log in verbose mode" do
79
+ @compiler = YMDP::Compiler::Base.new(@domain, @git_hash, :base_path => @base_path, :server => "staging", :verbose => true)
80
+ $stdout.should_receive(:puts).with(/Moving images/)
81
+ @compiler.process_all
82
+ end
83
+ end
55
84
 
56
85
  describe "translations" do
57
86
  before(:each) do
87
+
88
+ @yrb_template = mock('yrb_template').as_null_object
89
+ YMDP::Compiler::Template::YRB.stub!(:new).and_return(@yrb_template)
90
+
58
91
  @supported_languages = ["en-US", "de-DE"]
59
92
  YMDP::ApplicationView.stub!(:supported_languages).and_return(@supported_languages)
60
93
  end
@@ -102,6 +135,9 @@ describe "Compiler" do
102
135
  before(:each) do
103
136
  @files = ["./app/views/view.html.haml"]
104
137
  Dir.stub!(:[]).with("./app/views/**/*").and_return(@files)
138
+
139
+ @view_template = mock('view_template').as_null_object
140
+ YMDP::Compiler::Template::View.stub!(:new).and_return(@view_template)
105
141
  end
106
142
 
107
143
  it "should run on all views in the path" do
@@ -125,6 +161,9 @@ describe "Compiler" do
125
161
  before(:each) do
126
162
  @files = ["./app/views/view.html.erb"]
127
163
  Dir.stub!(:[]).with("./app/views/**/*").and_return(@files)
164
+
165
+ @view_template = mock('view_template').as_null_object
166
+ YMDP::Compiler::Template::View.stub!(:new).and_return(@view_template)
128
167
  end
129
168
 
130
169
  it "should run on all views in the path" do
@@ -146,6 +185,9 @@ describe "Compiler" do
146
185
  end
147
186
 
148
187
  it "should run on all views in the path" do
188
+ @js_template = mock('js_template').as_null_object
189
+ YMDP::Compiler::Template::JavaScript.stub!(:new).and_return(@js_template)
190
+
149
191
  files = ["./app/assets/javascripts/view.js"]
150
192
  Dir.should_receive(:[]).with("./app/assets/**/*").and_return(files)
151
193
  @compiler.process_all
@@ -3,23 +3,99 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  require 'compiler/base'
4
4
  require 'compiler/template'
5
5
 
6
-
7
- describe "Compiler" do
6
+ describe "Template" do
8
7
  before(:each) do
8
+ stub_screen_io
9
+
9
10
  reset_constant(:YMDP_ENV, "build")
10
- reset_constant(:CONFIG, mock('config').as_null_object)
11
11
 
12
12
  Application.stub!(:current_view=)
13
13
 
14
- stub_io
14
+ @domain = "staging"
15
+ @filename = "filename.html.haml"
16
+ @git_hash = "asjkhfasjkfhjk"
17
+ @message = "This is a commit message."
18
+
19
+ stub_yrb_configuration
20
+
21
+ stub_config
15
22
  end
16
23
 
17
- describe "view" do
24
+ describe "javascript" do
25
+ before(:each) do
26
+ stub_file_utils
27
+
28
+ @filename = "filename.pres"
29
+
30
+ @params = {
31
+ :verbose => false,
32
+ :domain => @domain,
33
+ :file => @filename,
34
+ :git_hash => @git_hash,
35
+ :message => @message
36
+ }
37
+
38
+ @js_template = YMDP::Compiler::Template::YRB.new(@params)
39
+ end
40
+
41
+ it "should instantiate" do
42
+ @js_template.should_not be_nil
43
+ end
44
+
45
+ describe "build" do
46
+ before(:each) do
47
+ @file = mock('file', :write => true)
48
+ File.stub!(:open).with(/filename.json/, "w").and_return(@file)
49
+ F.stub!(:save_to_file)
50
+ end
51
+
52
+ it "should save json" do
53
+ File.stub!(:read).with("filename.pres").and_return("KEY=value")
54
+ @js_template.build.should == "{\"KEY\":\"value\"}"
55
+ end
56
+
57
+ it "should skip lines that aren't keys" do
58
+ File.stub!(:read).with("filename.pres").and_return("KEY=value\nnot a key\n")
59
+ @js_template.build.should == "{\"KEY\":\"value\"}"
60
+ end
61
+
62
+ it "should raise error for duplicate keys" do
63
+ File.stub!(:read).with("filename.pres").and_return("KEY=value\nKEY=value\n")
64
+ lambda {
65
+ @js_template.build
66
+ }.should raise_error("Duplicate key error")
67
+ end
68
+
69
+ describe "methods" do
70
+ before(:each) do
71
+ @file = mock('file', :write => true)
72
+ File.stub!(:open).with(/filename.json/, "w").and_return(@file)
73
+ F.stub!(:save_to_file)
74
+ File.stub!(:read).with("filename.pres").and_return("KEY=value")
75
+ end
76
+
77
+ it "should return json" do
78
+ @js_template.to_json.should == "{\"KEY\":\"value\"}"
79
+ end
80
+
81
+ it "should return yaml" do
82
+ @js_template.to_yaml.should == "--- \nkey: value\n"
83
+ end
84
+
85
+ it "should validate" do
86
+ YMDP::Validator::JSON.should_receive(:validate)
87
+ @js_template.validate
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+
94
+ describe "javascript" do
18
95
  before(:each) do
19
- @domain = "staging"
20
- @filename = "filename.html.haml"
21
- @git_hash = "asjkhfasjkfhjk"
22
- @message = "This is a commit message."
96
+ stub_file_utils
97
+
98
+ @filename = "filename.js"
23
99
 
24
100
  @params = {
25
101
  :verbose => false,
@@ -28,12 +104,76 @@ describe "Compiler" do
28
104
  :git_hash => @git_hash,
29
105
  :message => @message
30
106
  }
31
- stub_yrb_configuration
32
107
 
108
+ @js_template = YMDP::Compiler::Template::JavaScript.new(@params)
109
+ end
110
+
111
+ it "should instantiate" do
112
+ @js_template.should_not be_nil
113
+ end
114
+
115
+ it "should build" do
116
+ File.stub!(:read).with("filename.js").and_return("unprocessed")
117
+
118
+ # process the contents of filename.html.erb through ERB
119
+ #
120
+ @erb = mock('erb', :result => "processed template output")
121
+ ERB.should_receive(:new).with("unprocessed", anything, anything).and_return(@erb)
122
+
123
+ F.stub!(:save_to_file).with("processed template output")
124
+
125
+ @js_template.build.should == "processed template output"
126
+ end
127
+
128
+ it "should compress" do
129
+ @config.stub!(:compress_embedded_js?).and_return(true)
130
+
131
+ # process the contents of filename.html.erb through ERB
132
+ #
133
+ @erb = mock('erb', :result => "processed template output")
134
+ ERB.should_receive(:new).with("unprocessed", anything, anything).and_return(@erb)
135
+
136
+ YMDP::Compressor::JavaScript.stub!(:compress).and_return("compressed template")
137
+ File.stub!(:read).with("filename.js").and_return("unprocessed")
138
+
139
+ F.stub!(:save_to_file)
140
+
141
+ @js_template.build.should == "compressed template"
142
+ end
143
+ end
144
+
145
+ describe "view" do
146
+ before(:each) do
147
+ @params = {
148
+ :verbose => false,
149
+ :domain => @domain,
150
+ :file => @filename,
151
+ :git_hash => @git_hash,
152
+ :message => @message
153
+ }
33
154
  @view_template = YMDP::Compiler::Template::View.new(@params)
34
155
  end
35
156
 
36
157
  describe "instantiation" do
158
+ it "should raise an error if called on base" do
159
+ lambda {
160
+ @view_template = YMDP::Compiler::Template::Base.new(@params)
161
+ }.should raise_error("Define in child")
162
+ end
163
+
164
+ it "should raise an error if process_template is called from base" do
165
+ class Thing < YMDP::Compiler::Template::Base
166
+ def base_filename(filename)
167
+ filename
168
+ end
169
+ end
170
+
171
+ @thing = Thing.new(@params)
172
+ lambda {
173
+ @thing.process_template("template")
174
+ }.should raise_error("Define in child")
175
+ end
176
+
37
177
  it "should instantiate" do
38
178
  @view_template.should_not be_nil
39
179
  end
@@ -65,11 +205,103 @@ describe "Compiler" do
65
205
  end
66
206
  end
67
207
 
208
+ describe "build" do
209
+ before(:each) do
210
+ @processed_haml = "processed haml"
211
+ stub_haml_class
212
+
213
+ YMDP::Validator::HTML.stub!(:validate)
214
+ end
215
+
216
+ it "should not build if it's a partial" do
217
+ @view_template = YMDP::Compiler::Template::View.new(@params.merge({:file => "_partial.html.haml"}))
218
+ @view_template.build.should be_nil
219
+ end
220
+
221
+ it "should get Haml when no layout exists" do
222
+ stub_file_utils
223
+
224
+ # File.exists? will return false, so no layouts exist
225
+
226
+ # read the contents of filename.html.haml
227
+ #
228
+ File.stub!(:read).with("filename.html.haml").and_return("unprocessed")
229
+
230
+ # process the contents of filename.html.haml through Haml::Engine
231
+ #
232
+ @haml = mock('haml', :render => "processed_template output")
233
+ Haml::Engine.stub!(:new).with("unprocessed", :filename=>"filename.html.haml").and_return(@haml)
234
+
235
+ @view_template.build.should == "processed_template output"
236
+ end
237
+
238
+ it "should get ERB when no layout exists" do
239
+ stub_file_utils
240
+
241
+ # File.exists? will return false, so no layouts exist
242
+
243
+ # read the contents of filename.html.haml
244
+ #
245
+ File.stub!(:read).with("filename.html.erb").and_return("unprocessed")
246
+
247
+ # process the contents of filename.html.erb through ERB
248
+ #
249
+ @erb = mock('erb', :result => "processed_template output")
250
+ ERB.should_receive(:new).with("unprocessed", anything, anything).and_return(@erb)
251
+
252
+ @view_template = YMDP::Compiler::Template::View.new(@params.merge({:file => "filename.html.erb"}))
253
+ @view_template.build.should == "processed_template output"
254
+ end
255
+
256
+ describe "haml template exists" do
257
+ it "should return processed layout" do
258
+ stub_file_utils
259
+
260
+ File.stub!(:exists?).with(/application.html.haml/).and_return(true)
261
+
262
+ # read the contents of application.html.haml
263
+ #
264
+ File.stub!(:read).with(/application.html.haml/).and_return("unprocessed layout")
265
+
266
+ # process the contents of application.html.haml through Haml::Engine
267
+ #
268
+ @layout_haml = mock('layout_haml', :render => "processed layout")
269
+ Haml::Engine.stub!(:new).with("unprocessed layout", :filename=>"./base_path//app/views/layouts/application.html.haml").and_return(@layout_haml)
270
+
271
+ # read the contents of filename.html.haml
272
+ #
273
+ File.stub!(:read).with("filename.html.haml").and_return("unprocessed")
274
+
275
+ # process the contents of filename.html.haml through Haml::Engine
276
+ #
277
+ @haml = mock('haml', :render => "processed template output")
278
+ Haml::Engine.stub!(:new).with("unprocessed", :filename=>"filename.html.haml").and_return(@haml)
279
+
280
+ @view_template.build.should == "processed layout"
281
+ end
282
+ end
283
+ end
284
+
68
285
  describe "validation" do
69
286
  it "should raise an error without a server" do
70
287
  lambda {
71
288
  @view_template = YMDP::Compiler::Template::View.new(@params.merge({:domain => "nowhere"}))
72
- }.should raise_error
289
+ }.should raise_error("Server settings are required.")
290
+ end
291
+
292
+ it "should raise an error without a server" do
293
+ @invalid_servers = {
294
+ "staging" => {
295
+ "application_id" => "abcdefg_1"
296
+ }
297
+ }
298
+ YMDP::Base.configure do |config|
299
+ config.servers = @invalid_servers
300
+ end
301
+
302
+ lambda {
303
+ @view_template = YMDP::Compiler::Template::View.new(@params)
304
+ }.should raise_error("Server name does not exist in server settings.")
73
305
  end
74
306
  end
75
307
  end
@@ -0,0 +1,123 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Compressor" do
4
+ before(:each) do
5
+ stub_io
6
+ end
7
+
8
+ describe "Base" do
9
+ it "should use the compressed file if it already exists" do
10
+ File.stub(:exists?).and_return(true)
11
+ @file = "compressed file"
12
+ File.stub(:read).and_return(@file)
13
+ YMDP::Compressor::Base.compress("file.js", "type" => "js").should == @file
14
+ end
15
+
16
+ describe "generate a compressed file if one doesn't exist" do
17
+ before(:each) do
18
+ File.stub!(:exists?).and_return(false, true)
19
+ end
20
+
21
+ it "should log what it's doing" do
22
+ $stdout.should_receive(:print).with(/file.js compressing . . ./)
23
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
24
+ end
25
+
26
+ it "should run the compressor" do
27
+ F.should_receive(:execute).with(/yuicompressor/, :return => true).and_return("")
28
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
29
+ end
30
+
31
+ describe "options" do
32
+ it "should set nomunge" do
33
+ F.should_receive(:execute).with(/nomunge/, :return => true).and_return("")
34
+ YMDP::Compressor::Base.compress("file.js", "type" => "js", "obfuscate" => true)
35
+ end
36
+
37
+ it "should not set nomunge" do
38
+ F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("")
39
+ F.should_not_receive(:execute).with(/nomunge/, :return => true).and_return("")
40
+ YMDP::Compressor::Base.compress("file.js", "type" => "js", "obfuscate" => false)
41
+ end
42
+
43
+ it "should set verbose" do
44
+ F.should_receive(:execute).with(/verbose/, :return => true).and_return("")
45
+ YMDP::Compressor::Base.compress("file.js", "type" => "js", "verbose" => true)
46
+ end
47
+
48
+ it "should not set verbose" do
49
+ F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("")
50
+ F.should_not_receive(:execute).with(/verbose/, :return => true).and_return("")
51
+ YMDP::Compressor::Base.compress("file.js", "type" => "js", "verbose" => false)
52
+ end
53
+
54
+ it "should set preserve-semi on javascript" do
55
+ F.should_receive(:execute).with(/preserve-semi/, :return => true).and_return("")
56
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
57
+ end
58
+
59
+ it "should not set preserve-semi on css" do
60
+ F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("")
61
+ F.should_not_receive(:execute).with(/preserve-semi/, :return => true).and_return("")
62
+ YMDP::Compressor::Base.compress("file.css", "type" => "css")
63
+ end
64
+ end
65
+
66
+ describe "on errors" do
67
+ before(:each) do
68
+ F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("[ERROR] 12:13: Too much fruzzlegump")
69
+ end
70
+
71
+ it "should raise an exception" do
72
+ lambda {
73
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
74
+ }.should raise_error(/JavaScript errors/)
75
+ end
76
+
77
+ it "should growl" do
78
+ @g.should_receive(:notify)
79
+ lambda {
80
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
81
+ }.should raise_error(/JavaScript errors/)
82
+ end
83
+
84
+ it "should show the source code" do
85
+ F.should_receive(:get_line_from_file).with("file.js", 12).and_return("")
86
+ lambda {
87
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
88
+ }.should raise_error(/JavaScript errors/)
89
+ end
90
+ end
91
+
92
+ it "should report OK" do
93
+ $stdout.should_receive(:puts).with("OK")
94
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
95
+ end
96
+
97
+ it "should raise an error if the compressed file doesn't exist" do
98
+ File.stub!(:exists?).and_return(false)
99
+ lambda {
100
+ YMDP::Compressor::Base.compress("file.js", "type" => "js")
101
+ }.should raise_error(/File does not exist/)
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "JavaScript" do
107
+ it "should call Base with type js" do
108
+ File.stub(:exists?).and_return(true)
109
+ @file = "compressed file"
110
+ File.stub(:read).and_return(@file)
111
+ YMDP::Compressor::JavaScript.compress("file.js").should == @file
112
+ end
113
+ end
114
+
115
+ describe "Stylesheet" do
116
+ it "should call Base with type css" do
117
+ File.stub(:exists?).and_return(true)
118
+ @file = "compressed file"
119
+ File.stub(:read).and_return(@file)
120
+ YMDP::Compressor::Stylesheet.compress("file.css").should == @file
121
+ end
122
+ end
123
+ end