File: projects_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 (173 lines) | stat: -rwxr-xr-x 5,182 bytes parent folder | download
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
#! /usr/bin/env ruby
test_directory = File.expand_path(File.dirname(__FILE__))
$: << test_directory unless $:.include? test_directory
require 'test_helper'
require 'fileutils'
require 'compass/core'
require 'sass/plugin'
require 'pathname'
require 'true'
require 'timecop'

class ProjectsTest < Test::Unit::TestCase
  def setup
    Compass.reset_configuration!
  end

  def verbose?
    false
  end

  def test_compass
    compile_project("compass", &method(:standard_checks))
  end

  def test_busted_font_urls
    compile_project("busted_font_urls", {:style => :compact}, &method(:standard_checks))
  end

  def test_relative
    compile_project("relative", {:style => :compact}, &method(:standard_checks))
  end

  def test_valid
    compile_project("valid", {:line_comments => true, :style => :expanded}, &method(:standard_checks))
  end

  def test_busted_image_urls
    compile_project("busted_image_urls", {:style => :compact}, &method(:standard_checks))
  end

  def test_envtest
    Timecop.travel(Time.local(2013, "nov", 4, 12, 0)) do
      compile_project("envtest", &method(:standard_checks))
    end
  end

  def test_image_urls
    compile_project("image_urls", {:style => :compact}, &method(:standard_checks))
  end

  def test_uses_only_stylesheets_ext
    compile_project("uses_only_stylesheets_ext", {:style => :expanded}, &method(:standard_checks))
  end

private

  def standard_checks(compiler)
    compiler.on_updated_stylesheet do |sass, css, map|
      sass_file = Pathname.new(sass).relative_path_from(
                    Pathname.new(template_path(@current_project))).to_s
      if verbose?
        puts "sass_file = #{sass_file} (#{(((Time.now - @last_compile) - (@last_compile - @diff_start)) * 1000).round}ms)"
      end
      @diff_start = Time.now
      assert_renders_correctly sass_file, :ignore_charset => true
      @last_compile = Time.now
    end
    compiler.on_compilation_error do |error, sass, css|
      raise error
    end
  end

  def assert_renders_correctly(*arguments)
    options = arguments.last.is_a?(Hash) ? arguments.pop : {}
    for name in arguments
      actual_result_file = "#{tempfile_path(@current_project)}/#{name}".gsub(/s[ac]ss/, "css")
      expected_result_file = "#{result_path(@current_project)}/#{name}".gsub(/s[ac]ss/, "css")
      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 #{actual_result_file}:#{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 compile_project(project_name, options = {})
    @last_compile = Time.now
    @diff_start = Time.now
    @current_project = project_name
    options = {
      :always_update => true,
      :load_paths => []
    }.merge(options)
    options.update(
      :css_location => tempfile_path(project_name),
      :template_location => template_path(project_name)
    )
    options[:load_paths] = (load_paths + options[:load_paths] + sass_path_env).uniq
    compiler = Sass::Plugin::Compiler.new(options)
    yield compiler
    compiler.update_stylesheets
  rescue
    save_output(project_name)
    raise
  ensure
    @current_project = nil
  end

  def sass_path_env
    (ENV['SASSPATH'] || "").split(File::PATH_SEPARATOR).select {|d| File.directory?(d)}
  end

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

  def load_paths
    [Compass::Core.base_directory("stylesheets")]
  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|
      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 projects
    Dir.glob(File.join(projects_path, "*")).map do |project_path|
      File.dirname(project_path)
    end
  end

  def projects_path
    File.join(File.dirname(__FILE__), "projects")
  end

  def project_path(project_name)
    File.join(projects_path, project_name)
  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
end