forked from oliamb/guard-compass
/
compass_spec.rb
298 lines (245 loc) · 9.29 KB
/
compass_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
require 'fileutils'
require 'spec_helper'
describe Guard::Compass do
after do
::Compass.reset_configuration!
end
describe "In a standard project" do
before do
create_fixture(:compass_prj)
@guard = Guard::Compass.new
end
after do
remove_fixtures
::Compass.reset_configuration!
end
it "has a reporter" do
@guard.reporter.should_not be_nil
end
it "might be initialized with options" do
g = Guard::Compass.new(project_path: 'test', configuration_file: 'test_also')
g.options[:project_path].should eq 'test'
g.options[:configuration_file].should eq 'test_also'
end
describe "start" do
it "supports creation of the updater instance" do
@guard.updater.should be_nil
@guard.start.should be_true
@guard.updater.should_not be_nil
end
it "should not generate anything" do
File.exists?(@project_dir + "/stylesheets/screen.css").should be_false
end
end
describe "default options" do
it "should have a default path mathching the run location" do
@guard.root_path.should == @project_path
@guard.working_path.should == @project_path
@guard.start.should be_true
end
end
describe "after start" do
before do
@guard.start
end
after do
@guard.stop
::Compass.reset_configuration!
end
describe "the updater" do
it "should contains an options hashmap with config file values" do
::Compass.configuration.sass_path.should_not be_nil
::Compass.configuration.sass_path.should eq "#{@project_dir}/src"
end
end
describe "stop" do
it "Stop remove the updater" do
@guard.updater.should_not be_nil
@guard.stop.should be_true
@guard.updater.should be_nil
end
end
describe "run_on_change" do
it "rebuilds all scss files in compass path" do
File.exists?(@project_dir + "/src/screen.scss").should(be_true)
File.exists?(@project_dir + "/stylesheets/screen.css").should be_false
@guard.run_on_change(@project_dir + "/src/screen.scss").should(be_true) rescue raise inspect_configuration
File.exists?(@project_dir + "/stylesheets/screen.css").should be_true
end
end
describe "run all" do
it "rebuilds all scss files in compass path" do
File.exists?(@project_dir + "/src/screen.scss").should(be_true)
File.exists?(@project_dir + "/stylesheets/screen.css").should be_false
@guard.run_all.should be_true
File.exists?(@project_dir + "/stylesheets/screen.css").should be_true
end
end
end
end
describe "with custom configuration and locations" do
before do
create_fixture(:custom_config_file)
@guard = Guard::Compass.new
end
after do
remove_fixtures
@guard.stop
::Compass.reset_configuration!
end
it "configure Compass correctly with an absolute path" do
@guard.options[:configuration_file] = "#{@project_dir}/another_config_location/config.rb"
@guard.options.should_not be_include(:project_path)
Pathname.pwd.to_s.should eq @project_dir
@guard.start
::Compass.configuration.sass_path.should eq "#{@project_dir}/another_src_location"
end
it "configure Compass correctly with a path relative to the workdir" do
@guard.options[:configuration_file] = "./another_config_location/config.rb"
@guard.options[:project_path] = @project_dir
@guard.root_path.should eql @guard.working_path
@guard.start
::Compass.configuration.sass_path.should eql("#{@project_dir}/another_src_location"), inspect_configuration
::Compass.configuration.project_path.should eq @project_dir
@guard.options[:project_path].should eq @project_dir
@guard.options[:configuration_file].should eq "#{@project_dir}/another_config_location/config.rb"
end
it "rebuilds all scss files in compass path" do
@guard.options[:configuration_file] = "#{@project_dir}/another_config_location/config.rb"
@guard.start
File.exists?("#{@project_dir}/another_src_location/screen.scss").should(be_true)
File.exists?("#{@project_dir}/another_stylesheets_location/screen.css").should be_false
@guard.run_on_change(@project_dir + "/another_src_location/screen.scss").should be_true
File.exists?(@project_dir + "/another_stylesheets_location/screen.css").should be_true
end
end
describe "without config file" do
before do
::Compass.reset_configuration!
create_fixture(:no_config_file)
@guard = Guard::Compass.new
@guard.reporter.stub(:failure)
end
after do
remove_fixtures
@guard.stop
::Compass.reset_configuration!
end
it "fails to build sass" do
@guard.reporter.should_receive(:failure).with("Cannot find a Compass configuration file, please add information to your Guardfile guard 'compass' declaration.")
@guard.start
File.exists?(@project_dir + "/src/screen.scss").should(be_true)
File.exists?(@project_dir + "/stylesheets/screen.css").should be_false
@guard.run_on_change(@project_dir + "/bad_src/screen.scss")
end
end
describe "with a bad directory configuration" do
before do
create_fixture(:bad_src_directory)
@guard = Guard::Compass.new
@guard.reporter.stub(:failure).with("Sass files src directory not found: #{@project_dir}/src\nPlease check your Compass configuration.")
@guard.start
end
after do
remove_fixtures
@guard.stop
::Compass.reset_configuration!
end
it "fails to build sass" do
File.exists?(@project_dir + "/bad_src/screen.scss").should(be_true)
File.exists?(@project_dir + "/stylesheets/screen.css").should be_false
@guard.run_on_change(@project_dir + "/bad_src/screen.scss")
end
end
describe "with a bad configuration file parameter" do
before do
create_fixture(:custom_config_file)
@guard = Guard::Compass.new(configuration_file: 'invalid.rb')
end
after do
@guard.stop
remove_fixtures
::Compass.reset_configuration!
end
it "reports an error" do
@guard.options[:configuration_file].should eq 'invalid.rb'
@guard.reporter.stub(:failure)
@guard.reporter.should_receive(:failure).with("Compass configuration file not found: #{@project_dir}/invalid.rb\nPlease check Guard configuration.")
@guard.start
end
end
describe "Watchers creation" do
describe "in standard project" do
before do
create_fixture :compass_prj
@guard = Guard::Compass.new
end
after do
remove_fixtures
::Compass.reset_configuration!
end
it "should have some watchers" do
@guard.start
@guard.watchers.size.should(eql(2), @guard.watchers.inspect)
@guard.watchers.first.pattern.should eq %r{^src/.*}
@guard.watchers.last.pattern.should eq %r{^config.rb$}
end
end
describe "in customized project" do
before do
create_fixture :custom_config_file
@guard = Guard::Compass.new(project_path: "#{TMP_PATH}/custom_config_file", configuration_file: 'another_config_location/config.rb')
end
after do
remove_fixtures
::Compass.reset_configuration!
end
it "should have some watchers" do
@guard.start
@guard.watchers.size.should(eql(2), @guard.watchers.inspect)
@guard.watchers.first.pattern.should eq %r{^another_src_location/.*}
@guard.watchers.last.pattern.should eq %r{^another_config_location/config.rb$}
end
end
describe "with relative return in working_directory" do
before do
create_fixture :custom_config_file_2
@guard = Guard::Compass.new(project_path: "..", configuration_file: 'another_config_location/config.rb')
end
after do
remove_fixtures
::Compass.reset_configuration!
end
it "should have some watchers" do
@guard.reporter.should_not_receive(:failure)
@guard.options[:project_path].should eq ".."
@guard.options[:configuration_file].should eq 'another_config_location/config.rb'
@guard.start
@guard.options[:project_path].should eq ".."
@guard.options[:configuration_file].should eq "#{@project_path}/another_config_location/config.rb"
@guard.watchers.size.should eq(2), @guard.watchers.inspect
@guard.watchers.last.pattern.should eq %r{^another_config_location/config.rb$}
@guard.watchers.first.pattern.should eq(%r{^another_src_location/.*}), ::Compass.configuration.sass_dir
end
end
end
end
def create_fixture(name)
FileUtils.mkdir(TMP_PATH) unless File.exists?(TMP_PATH)
@project_dir = "#{TMP_PATH}/#{name}"
@project_path = Pathname.new(@project_dir)
FileUtils.cp_r("#{FIXTURES_PATH}/#{name}", TMP_PATH)
## Fake the current directory.
Dir.stub(:pwd).and_return(@project_dir)
Pathname.stub(:pwd).and_return(@project_path)
end
def remove_fixtures
FileUtils.rm_rf(TMP_PATH)
end
def inspect_configuration
result = "Compass Configuration:\n"
::Compass::Configuration::ATTRIBUTES.each do |a|
result << " * #{a.inspect} => #{::Compass.configuration.send(a).inspect}\n" rescue result << " ! error for #{a}"
end
return result
end