File: compass_test.rb

package info (click to toggle)
ruby-compass 1.0.1~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,152 kB
  • ctags: 1,795
  • sloc: ruby: 12,901; makefile: 127; perl: 43; xml: 14; sh: 4
file content (259 lines) | stat: -rw-r--r-- 8,099 bytes parent folder | download | duplicates (2)
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
require 'test_helper'
require 'fileutils'
require 'compass'
require 'compass/logger'
require 'sass/plugin'

class CompassTest < Test::Unit::TestCase

  def setup
    Compass.reset_configuration!
  end

  def teardown
    Dir.glob(absolutize("fixtures/stylesheets/*")).each do |dir|
      project_name = File.basename(dir)
      ::FileUtils.rm_rf tempfile_path(project_name)
      ::FileUtils.rm_rf File.join(project_path(project_name), ".sass-cache")
    end
  end

  def test_on_stylesheet_saved_callback
    saved = false
    path = nil
    config = nil
    before_compile = Proc.new do |config|
      config.on_stylesheet_saved {|filepath| path = filepath; saved = true }
    end
    within_project(:compass, before_compile)
    assert saved, "Stylesheet callback didn't get called"
    assert path.is_a?(String), "Path is not a string. Got: #{path.class.name}"
  end

  # no project with errors exists to test aginst - leep of FAITH!
  # *chriseppstein flogs himself*
  def test_on_stylesheet_error_callback
      error = false
      file = nil
      before_compile = Proc.new do |config|
        config.on_stylesheet_error {|filename, message| file = filename; error = true }
      end
      within_project(:error, before_compile) rescue nil
      assert error, "Project did not throw a compile error"
      assert file.is_a?(String), "Filename was not a string"
    end

  def test_empty_project
    # With no sass files, we should have no css files.
    within_project(:empty) do |proj|
      return unless proj.css_path && File.exists?(proj.css_path)
      Dir.new(proj.css_path).each do |f|
        fail "This file should not have been generated: #{f}" unless f == "." || f == ".."
      end
    end
  end

  def test_compass
    within_project('compass') do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'compass'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file, :ignore_charset => true
      end
    end
  end

  def test_sourcemaps
    within_project('sourcemaps') do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'sourcemaps'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file, :ignore_charset => true
      end
    end
  end

  def test_env_in_development
    within_project('envtest', lambda {|c| c.environment = :development }) do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'envtest'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file, :ignore_charset => true, :environment => "development"
      end
    end
  end

  def test_env_in_production
    within_project('envtest', lambda {|c| c.environment = :production }) do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'envtest'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file, :ignore_charset => true, :environment => "production"
      end
    end
  end

  def test_busted_font_urls
    within_project('busted_font_urls') do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'busted_font_urls'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file
      end
    end
  end

  def test_busted_image_urls
    within_project('busted_image_urls') do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'busted_image_urls'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file
      end
    end
  end

  def test_with_sass_globbing
    within_project('with_sass_globbing') do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'with_sass_globbing'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file
      end
    end
  end

  def test_image_urls
    within_project('image_urls') do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'image_urls'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file
      end
    end
  end

  def test_relative
    within_project('relative') do |proj|
      each_css_file(proj.css_path) do |css_file|
        assert_no_errors css_file, 'relative'
      end
      each_sass_file do |sass_file|
        assert_renders_correctly sass_file
      end
    end
  end

private
  def assert_no_errors(css_file, project_name)
    file = css_file[(tempfile_path(project_name).size+1)..-1]
    msg = "Syntax Error found in #{file}. Results saved into #{save_path(project_name)}/#{file}"
    assert_equal 0, open(css_file).readlines.grep(/Sass::SyntaxError/).size, msg
  end

  def assert_renders_correctly(*arguments)
    options = arguments.last.is_a?(Hash) ? arguments.pop : {}
    for name in arguments
      @output_file = actual_result_file = "#{tempfile_path(@current_project)}/#{name}.css"
      expected_result_file = "#{result_path(@current_project)}/#{name}.css"
      @filename = expected_result_file.gsub('css', 'scss')
      actual_lines = File.read(actual_result_file)
      actual_lines.gsub!(/^@charset[^;]+;/,'') if options[:ignore_charset]
      actual_lines = actual_lines.split("\n").reject{|l| l=~/\A\Z/}
      expected_lines = ERB.new(File.read(expected_result_file)).result(binding)
      expected_lines.gsub!(/^@charset[^;]+;/,'') if options[:ignore_charset]
      expected_lines = expected_lines.split("\n").reject{|l| l=~/\A\Z/}
      expected_lines.zip(actual_lines).each_with_index do |pair, line|
        if pair.first == pair.last
          assert(true)
        else
          assert false, "Error in #{result_path(@current_project)}/#{name}.css:#{line + 1}\n"+diff_as_string(pair.first.inspect, pair.last.inspect)
        end
      end
      if expected_lines.size < actual_lines.size
        assert(false, "#{actual_lines.size - expected_lines.size} Trailing lines found in #{actual_result_file}: #{actual_lines[expected_lines.size..-1].join('\n')}")
      end
    end
  end

  def within_project(project_name, config_block = nil)
    @current_project = project_name
    Compass.add_configuration(configuration_file(project_name)) if File.exists?(configuration_file(project_name))
    Compass.configuration.project_path = project_path(project_name)
    Compass.configuration.environment = :production
    Compass.configuration.sourcemap = false unless Compass.configuration.sourcemap_set?

    if config_block
      config_block.call(Compass.configuration)
    end

    if Compass.configuration.sass_path && File.exists?(Compass.configuration.sass_path)
      compiler = Compass.sass_compiler
      compiler.logger = Compass::NullLogger.new
      compiler.clean!
      compiler.compile!
    end
    yield Compass.configuration if block_given?
  rescue
    save_output(project_name)
    raise
  end

  def each_css_file(dir, &block)
    Dir.glob("#{dir}/**/*.css").each(&block)
  end

  def each_sass_file(sass_dir = nil)
    sass_dir ||= template_path(@current_project)
    Dir.glob("#{sass_dir}/**/*.s[ac]ss").each do |sass_file|
      next if File.basename(sass_file).start_with?("_")
      yield sass_file[(sass_dir.length+1)..-6]
    end
  end

  def save_output(dir)
    FileUtils.rm_rf(save_path(dir))
    FileUtils.cp_r(tempfile_path(dir), save_path(dir)) if File.exists?(tempfile_path(dir))
  end

  def project_path(project_name)
    absolutize("fixtures/stylesheets/#{project_name}")
  end

  def configuration_file(project_name)
    File.join(project_path(project_name), "config.rb")
  end

  def tempfile_path(project_name)
    File.join(project_path(project_name), "tmp")
  end

  def template_path(project_name)
    File.join(project_path(project_name), "sass")
  end

  def result_path(project_name)
    File.join(project_path(project_name), "css")
  end

  def save_path(project_name)
    File.join(project_path(project_name), "saved")
  end

  def filename
    @filename
  end

  def output_file
    @output_file
  end

end