From e75abdda47586a52026f363454de6f977fdb3639 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 23 Jan 2012 11:58:20 -0500 Subject: [PATCH] simple sprockets work --- .gitignore | 1 + Rakefile | 2 + features/process_with_sprockets.feature | 18 +++++++ .../step_definitions/given/i_have_file.rb | 2 + .../handler_should_have_files_in_order.rb | 6 +++ .../when/i_inspect_sprockets_asset.rb | 4 ++ .../when/i_instantiate_sprockets_handler.rb | 4 ++ features/support/env.rb | 6 +++ lib/flowerbox/delivery.rb | 1 + lib/flowerbox/delivery/sprockets_handler.rb | 29 +++++++++++ .../delivery/sprockets_handler_spec.rb | 48 +++++++++++++++++++ 11 files changed, 121 insertions(+) create mode 100644 features/process_with_sprockets.feature create mode 100644 features/step_definitions/then/handler_should_have_files_in_order.rb create mode 100644 features/step_definitions/when/i_inspect_sprockets_asset.rb create mode 100644 features/step_definitions/when/i_instantiate_sprockets_handler.rb create mode 100644 lib/flowerbox/delivery/sprockets_handler.rb create mode 100644 spec/flowerbox/delivery/sprockets_handler_spec.rb diff --git a/.gitignore b/.gitignore index d87d4be..76e1c9c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ spec/reports test/tmp test/version_tmp tmp +rerun.txt diff --git a/Rakefile b/Rakefile index f57ae68..c7615c2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,4 @@ #!/usr/bin/env rake require "bundler/gem_tasks" + +task :default => [ :rspec, :cucumber ] diff --git a/features/process_with_sprockets.feature b/features/process_with_sprockets.feature new file mode 100644 index 0000000..3ae262a --- /dev/null +++ b/features/process_with_sprockets.feature @@ -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 | + diff --git a/features/step_definitions/given/i_have_file.rb b/features/step_definitions/given/i_have_file.rb index 8bd77dc..9748479 100644 --- a/features/step_definitions/given/i_have_file.rb +++ b/features/step_definitions/given/i_have_file.rb @@ -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 diff --git a/features/step_definitions/then/handler_should_have_files_in_order.rb b/features/step_definitions/then/handler_should_have_files_in_order.rb new file mode 100644 index 0000000..ec5daf6 --- /dev/null +++ b/features/step_definitions/then/handler_should_have_files_in_order.rb @@ -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 + diff --git a/features/step_definitions/when/i_inspect_sprockets_asset.rb b/features/step_definitions/when/i_inspect_sprockets_asset.rb new file mode 100644 index 0000000..8cc3891 --- /dev/null +++ b/features/step_definitions/when/i_inspect_sprockets_asset.rb @@ -0,0 +1,4 @@ +When /^I work with the Sprockets asset "([^"]*)"$/ do |asset| + @sprockets.add(asset) +end + diff --git a/features/step_definitions/when/i_instantiate_sprockets_handler.rb b/features/step_definitions/when/i_instantiate_sprockets_handler.rb new file mode 100644 index 0000000..a3b4e72 --- /dev/null +++ b/features/step_definitions/when/i_instantiate_sprockets_handler.rb @@ -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 + diff --git a/features/support/env.rb b/features/support/env.rb index 9870f87..f580d64 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -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 diff --git a/lib/flowerbox/delivery.rb b/lib/flowerbox/delivery.rb index af537cc..2174765 100644 --- a/lib/flowerbox/delivery.rb +++ b/lib/flowerbox/delivery.rb @@ -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 diff --git a/lib/flowerbox/delivery/sprockets_handler.rb b/lib/flowerbox/delivery/sprockets_handler.rb new file mode 100644 index 0000000..a4e29c6 --- /dev/null +++ b/lib/flowerbox/delivery/sprockets_handler.rb @@ -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 diff --git a/spec/flowerbox/delivery/sprockets_handler_spec.rb b/spec/flowerbox/delivery/sprockets_handler_spec.rb new file mode 100644 index 0000000..160ca11 --- /dev/null +++ b/spec/flowerbox/delivery/sprockets_handler_spec.rb @@ -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 +