diff --git a/features/build_runner_html.feature b/features/build_runner_html.feature new file mode 100644 index 0000000..add7f51 --- /dev/null +++ b/features/build_runner_html.feature @@ -0,0 +1,16 @@ +Feature: Build Runner HTML + Scenario: All JS files + Given I have the file "template.erb" with the content: + """ + + <%= resource_tags %> + + """ + When I instantiate a template renderer with the template "template.erb" and the files: + | src/source.js | + | spec/spec.js | + And I render the template + Then I should have script tags for the following files in the template: + | src/source.js | + | spec/spec.js | + diff --git a/features/step_definitions/given/i_have_file.rb b/features/step_definitions/given/i_have_file.rb new file mode 100644 index 0000000..8bd77dc --- /dev/null +++ b/features/step_definitions/given/i_have_file.rb @@ -0,0 +1,3 @@ +Given /^I have the file "([^"]*)" with the content:$/ do |filename, string| + File.open(filename, 'wb') { |fh| fh.print(string) } +end diff --git a/features/step_definitions/then/i_should_have_script_tags.rb b/features/step_definitions/then/i_should_have_script_tags.rb new file mode 100644 index 0000000..85123e5 --- /dev/null +++ b/features/step_definitions/then/i_should_have_script_tags.rb @@ -0,0 +1,9 @@ +require 'nokogiri' + +Then /^I should have script tags for the following files in the template:$/ do |table| + doc = Nokogiri::XML(@result) + + table.raw.collect(&:first).each do |file| + doc.at_css("script[src='#{file}']").should_not be_nil + end +end diff --git a/features/step_definitions/when/i_instantiate_template_builder.rb b/features/step_definitions/when/i_instantiate_template_builder.rb new file mode 100644 index 0000000..a767bdf --- /dev/null +++ b/features/step_definitions/when/i_instantiate_template_builder.rb @@ -0,0 +1,3 @@ +When /^I instantiate a template renderer with the template "([^"]*)" and the files:$/ do |template, table| + @template_renderer = Flowerbox::Delivery::TemplateRenderer.new(:files => table.raw.collect(&:first), :template => template) +end diff --git a/features/step_definitions/when/i_render_template.rb b/features/step_definitions/when/i_render_template.rb new file mode 100644 index 0000000..5363ef1 --- /dev/null +++ b/features/step_definitions/when/i_render_template.rb @@ -0,0 +1,3 @@ +When /^I render the template$/ do + @result = @template_renderer.render +end diff --git a/features/support/env.rb b/features/support/env.rb index 06cbad9..9870f87 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,11 +1,14 @@ require 'flowerbox-delivery' require 'mocha' +require 'fakefs/safe' World(Mocha::API) Before do mocha_setup + + FakeFS.activate! end After do @@ -15,6 +18,8 @@ After do mocha_teardown end + FakeFS.deactivate! + if @running_server @running_server[:server].shutdown @running_server = nil diff --git a/flowerbox-delivery.gemspec b/flowerbox-delivery.gemspec index 2e01a64..d895de7 100644 --- a/flowerbox-delivery.gemspec +++ b/flowerbox-delivery.gemspec @@ -19,6 +19,8 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'rspec' gem.add_development_dependency 'jquery-rails', '~> 1.0.0' gem.add_development_dependency 'mocha' + gem.add_development_dependency 'fakefs' + gem.add_development_dependency 'nokogiri' gem.add_runtime_dependency 'rack' gem.add_runtime_dependency 'sprockets' diff --git a/lib/flowerbox/delivery.rb b/lib/flowerbox/delivery.rb index bc2e398..af537cc 100644 --- a/lib/flowerbox/delivery.rb +++ b/lib/flowerbox/delivery.rb @@ -1,6 +1,7 @@ module Flowerbox module Delivery autoload :Server, 'flowerbox/delivery/server' + autoload :TemplateRenderer, 'flowerbox/delivery/template_renderer' end end diff --git a/lib/flowerbox/delivery/server.rb b/lib/flowerbox/delivery/server.rb index b0b85b1..bda6b07 100644 --- a/lib/flowerbox/delivery/server.rb +++ b/lib/flowerbox/delivery/server.rb @@ -38,7 +38,7 @@ module Flowerbox def port return @port if @port ||= options[:port] - attempts = 100 + attempts = 20 begin attempts -= 1 diff --git a/lib/flowerbox/delivery/template_renderer.rb b/lib/flowerbox/delivery/template_renderer.rb new file mode 100644 index 0000000..2638512 --- /dev/null +++ b/lib/flowerbox/delivery/template_renderer.rb @@ -0,0 +1,35 @@ +require 'erb' + +module Flowerbox::Delivery + class TemplateRenderer + class FileTypeError < StandardError ; end + + attr_reader :options + + def initialize(options) + @options = options + end + + def render + ERB.new(template).result(binding) + end + + def template + File.read(options[:template]) + end + + def resource_tags + options[:files].collect do |file| + case File.extname(file) + when '.js' + %{} + when '.css' + %{} + else + raise FileTypeError.new("Unknown file type: #{File.extname(file)} for #{file}") + end + end.join + end + end +end + diff --git a/spec/flowerbox/delivery/server_spec.rb b/spec/flowerbox/delivery/server_spec.rb index 1333c93..5a73362 100644 --- a/spec/flowerbox/delivery/server_spec.rb +++ b/spec/flowerbox/delivery/server_spec.rb @@ -41,31 +41,59 @@ describe Flowerbox::Delivery::Server do describe '#port' do let(:interface) { '127.0.0.1' } + let(:base) { 25000 } + let(:initial) { base + @offset } before do server.stubs(:interface).returns(interface) + + @offset = 0 + ok = true + + begin + [ 0, 1 ].each do |index| + begin + TCPSocket.new(interface, base + @offset + index) + @offset += 1 + ok = false + rescue Errno::ECONNREFUSED => e + end + end + end while !ok end subject { server.port } context 'no running service' do before do - Kernel.stubs(:rand).returns(0) + Kernel.stubs(:rand).returns(@offset) end - it { should == 25000 } + it { should == initial } end context 'running service' do before do @server = Thread.new do - TCPServer.new(interface, 25000) + TCPServer.new(interface, initial) end - server.stubs(:random_port).returns(25000, 25001) + server.stubs(:random_port).returns(initial, initial + 1) + + while true + begin + TCPSocket.new(interface, initial) + break + rescue Errno::ECONNREFUSED + end + end end - it { should == 25001 } + it { should == initial + 1 } + + after do + @server.kill + end end end end diff --git a/spec/flowerbox/delivery/template_renderer_spec.rb b/spec/flowerbox/delivery/template_renderer_spec.rb new file mode 100644 index 0000000..7c790d0 --- /dev/null +++ b/spec/flowerbox/delivery/template_renderer_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe Flowerbox::Delivery::TemplateRenderer do + let(:template_renderer) { described_class.new(:template => template, :files => files) } + let(:template) { 'template' } + let(:files) { 'files' } + + let(:rendered_template) { 'rendered template' } + let(:erb_template) { "#{rendered_template} <%= resource_tags %>" } + + describe '#render' do + subject { template_renderer.render } + + let(:rendered_files) { 'with files' } + let(:result) { "#{rendered_template} #{rendered_files}" } + + before do + template_renderer.expects(:resource_tags).returns(rendered_files) + template_renderer.expects(:template).returns(erb_template) + end + + it { should == result } + end + + describe '#template' do + include FakeFS::SpecHelpers + + before do + File.open(template, 'wb') { |fh| fh.print erb_template } + end + + subject { template_renderer.template } + + it { should == erb_template } + end + + describe '#resource_tags' do + subject { template_renderer.resource_tags } + + context 'success' do + let(:files) { [ js, css ] } + let(:js) { 'file.js' } + let(:css) { 'file.css' } + + it { should == [ + %{}, + %{} + ].join } + end + + context 'failure' do + let(:files) { [ 'what.ever' ] } + + it 'should raise error' do + expect { subject }.to raise_error(Flowerbox::Delivery::TemplateRenderer::FileTypeError) + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6942209..fe4f8b5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ require 'flowerbox-delivery' require 'mocha' +require 'fakefs/spec_helpers' RSpec.configure do |c| c.mock_with :mocha