class Sass::Rails::TestCase

Protected Instance Methods

assert_file_exists(filename) click to toggle source
# File test/support/sass_rails_test_case.rb, line 45
def assert_file_exists(filename)
  assert File.exists?(filename), "could not find #{filename}. PWD=#{Dir.pwd}\nDid find: #{Dir.glob(File.dirname(filename)+"/*").join(", ")}"
end
assert_line_count(count) click to toggle source
# File test/support/sass_rails_test_case.rb, line 57
def assert_line_count(count)
  last_count = $last_ouput.lines.count
  assert last_count == count, "Wrong line count, expected: #{count} but got: #{last_count}"
end
assert_not_output(match) click to toggle source
# File test/support/sass_rails_test_case.rb, line 49
def assert_not_output(match)
  assert_no_match match, $last_ouput
end
assert_output(match) click to toggle source
# File test/support/sass_rails_test_case.rb, line 53
def assert_output(match)
  assert $last_ouput.to_s =~ match, "#{match} was not found in #{$last_ouput.inspect}"
end
fixture_path(path) click to toggle source
# File test/support/sass_rails_test_case.rb, line 31
def fixture_path(path)
  File.expand_path("../../fixtures/#{path}", __FILE__)
end
gem_entry(gemname, options) click to toggle source
# File test/support/sass_rails_test_case.rb, line 107
def gem_entry(gemname, options)
  entry = %Q{gem "#{gemname}", "~> #{options[:version]}"}
  entry += ", :path => #{options[:path].inspect}" if options[:path]
  entry
end
modify_gem_entry(gemname, options, gemfile = "Gemfile") click to toggle source
# File test/support/sass_rails_test_case.rb, line 90
def modify_gem_entry(gemname, options, gemfile = "Gemfile")
  found = false
  process_gemfile(gemfile) do |line|
    if line =~ /gem *(["'])#{Regexp.escape(gemname)}\1/
      found = true
      gem_entry(gemname, options) + "\n"
    else
      line
    end
  end
  unless found
    File.open(gemfile, "a") do |f|
      f.print("\n#{gem_entry(gemname, options)}\n")
    end
  end
end
process_gemfile(gemfile = "Gemfile", &blk) click to toggle source
# File test/support/sass_rails_test_case.rb, line 81
def process_gemfile(gemfile = "Gemfile", &blk)
  gem_contents = File.readlines(gemfile)
  gem_contents.map!(&blk)
  gem_contents.compact!
  File.open(gemfile, "w") do |f|
    f.print(gem_contents.join(""))
  end
end
remove_gem(gemname) click to toggle source
# File test/support/sass_rails_test_case.rb, line 113
def remove_gem(gemname)
  process_gemfile(gemfile) do |line|
    line unless line =~ /gem *(["'])#{Regexp.escape(gemname)}\1/
  end
end
runcmd(cmd, working_directory = Dir.pwd, clean_env = true, gemfile = "Gemfile", env = {}) click to toggle source

executes a system command raises an error if it does not complete successfully returns the output as a string if it does complete successfully

# File test/support/sass_rails_test_case.rb, line 140
def runcmd(cmd, working_directory = Dir.pwd, clean_env = true, gemfile = "Gemfile", env = {})
  # There's a bug in bundler where with_clean_env doesn't clear out the BUNDLE_GEMFILE environment setting
  # https://github.com/carlhuda/bundler/issues/1133
  env["BUNDLE_GEMFILE"] = "#{working_directory}/#{gemfile}" if clean_env
  todo = Proc.new do
    r, w = IO.pipe
    pid = Kernel.spawn(env, cmd, :out =>w , :err => w, :chdir => working_directory)
    w.close
    Process.wait
    output = r.read
    r.close
    unless $?.exitstatus == 0
      raise ExecutionError, "Command failed with exit status #{$?.exitstatus}: #{cmd}", output
    end
    $last_ouput = output
  end
  if clean_env
    Bundler.with_clean_env(&todo)
  else
    todo.call
  end
end
silently() { || ... } click to toggle source
# File test/support/sass_rails_test_case.rb, line 119
def silently
  output = StringIO.new
  $stderr, old_stderr = output, $stderr
  $stdout, old_stdout = output, $stdout
  begin
    yield
  rescue ExecutionError => e
    raise
  rescue => e
    e.extend(SilentError)
    e.output = output.string
    raise
  end
ensure
  $stderr = old_stderr
  $stdout = old_stdout
end
sprockets_render(project, filename) click to toggle source
# File test/support/sass_rails_test_case.rb, line 39
def sprockets_render(project, filename)
  within_rails_app(project) do
    runcmd "ruby script/rails runner 'puts Rails.application.assets[#{filename.inspect}]'"
  end
end
within_rails_app(name, without_gems = [], gem_options = $gem_options) { |tmpdir| ... } click to toggle source

Copies a rails app fixture to a temp directory and changes to that directory during the yield.

Automatically changes back to the working directory and removes the temp directory when done.

# File test/support/sass_rails_test_case.rb, line 66
def within_rails_app(name, without_gems = [], gem_options = $gem_options)
  sourcedir = File.expand_path("../../fixtures/#{name}", __FILE__)
  Dir.mktmpdir do |tmpdir|
    FileUtils.cp_r "#{sourcedir}/.", tmpdir
    Dir.chdir(tmpdir) do
      gem_options.each {|name, options| modify_gem_entry name, options}
      without_gems.each {|name| remove_gem name}
      FileUtils.rm("Gemfile.lock") if File.exist?("Gemfile.lock")
      runcmd "bundle install --verbose"
      runcmd "bundle exec rake db:create --trace"
      yield tmpdir
    end
  end
end