simple sprockets work

This commit is contained in:
John Bintz 2012-01-23 11:58:20 -05:00
parent 5b8a746e24
commit e75abdda47
11 changed files with 121 additions and 0 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
rerun.txt

View File

@ -1,2 +1,4 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
task :default => [ :rspec, :cucumber ]

View File

@ -0,0 +1,18 @@
Feature: Process files with Sprockets
Scenario: Simple Sprockets work
Given I have the file "dir/file.js" with the content:
"""
//= require other
a file
"""
And I have the file "dir/other.js" with the content:
"""
another file
"""
When I instantiate a Sprockets handler with the following asset directories:
| dir |
And I work with the Sprockets asset "file"
Then the handler should have the following files in order:
| dir/other.js |
| dir/file.js |

View File

@ -1,3 +1,5 @@
Given /^I have the file "([^"]*)" with the content:$/ do |filename, string|
FileUtils.mkdir_p File.dirname(filename)
File.open(filename, 'wb') { |fh| fh.print(string) }
end

View File

@ -0,0 +1,6 @@
Then /^the handler should have the following files in order:$/ do |table|
table.raw.collect(&:first).each_with_index do |path, index|
@sprockets.files[index].should == Pathname(File.expand_path(path))
end
end

View File

@ -0,0 +1,4 @@
When /^I work with the Sprockets asset "([^"]*)"$/ do |asset|
@sprockets.add(asset)
end

View File

@ -0,0 +1,4 @@
When /^I instantiate a Sprockets handler with the following asset directories:$/ do |table|
@sprockets = Flowerbox::Delivery::SprocketsHandler.new(:asset_paths => table.raw.collect(&:first))
end

View File

@ -5,6 +5,12 @@ require 'fakefs/safe'
World(Mocha::API)
class FakeFS::File::Stat
def file?
File.file?(@file)
end
end
Before do
mocha_setup

View File

@ -2,6 +2,7 @@ module Flowerbox
module Delivery
autoload :Server, 'flowerbox/delivery/server'
autoload :TemplateRenderer, 'flowerbox/delivery/template_renderer'
autoload :SprocketsHandler, 'flowerbox/delivery/sprockets_handler'
end
end

View File

@ -0,0 +1,29 @@
require 'sprockets'
module Flowerbox::Delivery
class SprocketsHandler
attr_reader :files, :options
def initialize(options)
@options = options
@files = []
end
def add(asset)
@files += paths_for(asset)
end
def paths_for(asset)
environment.find_asset(asset).to_a.collect(&:pathname)
end
def environment
return @environment if @environment
@environment = Sprockets::Environment.new
options[:asset_paths].each { |path| @environment.append_path(path) }
@environment
end
end
end

View File

@ -0,0 +1,48 @@
require 'spec_helper'
describe Flowerbox::Delivery::SprocketsHandler do
let(:sprockets_handler) { described_class.new(options) }
let(:options) { { :asset_paths => asset_paths } }
let(:asset_paths) { [ File.expand_path('asset path') ] }
describe '#add' do
let(:asset) { 'asset' }
let(:paths) { [ 'paths' ] }
before do
sprockets_handler.expects(:paths_for).with(asset).returns(paths)
end
it 'should add the asset to the list of ones to work with' do
sprockets_handler.add(asset)
sprockets_handler.files.should == paths
end
end
describe '#paths_for' do
subject { sprockets_handler.paths_for(asset) }
let(:asset) { 'asset' }
let(:environment) { stub }
let(:bundled_asset) { stub(:to_a => [ processed_asset ]) }
let(:processed_asset) { stub(:pathname => path) }
let(:path) { 'path' }
before do
sprockets_handler.stubs(:environment).returns(environment)
environment.expects(:find_asset).with(asset).returns(bundled_asset)
end
it { should == [ path ] }
end
describe '#environment' do
subject { sprockets_handler.environment }
it { should be_a_kind_of(Sprockets::Environment) }
its(:paths) { should == asset_paths }
end
end