diff --git a/bin/flowerbox b/bin/flowerbox index 1ffc87b..459c9df 100755 --- a/bin/flowerbox +++ b/bin/flowerbox @@ -22,6 +22,17 @@ class Flowerbox::CLI < Thor end end + desc "transplant DIR", "Convert an existing Jasmine gem-style project to Flowerbox" + def transplant(dir) + Flowerbox.transplant(dir) + end + + desc "clean", "Clean the Sprockets cache" + def clean + FileUtils.rm_rf(Flowerbox::CACHE_DIR) + puts "Sprockets cache cleaned." + end + default_task :test no_tasks do diff --git a/lib/flowerbox.rb b/lib/flowerbox.rb index 1fa9453..8c9caa2 100644 --- a/lib/flowerbox.rb +++ b/lib/flowerbox.rb @@ -17,6 +17,12 @@ module Flowerbox require 'flowerbox/reporter' + if defined?(Rails::Engine) + require 'flowerbox/rails/engine' + end + + CACHE_DIR = '.tmp-sprockets' + class << self attr_writer :reporters attr_accessor :port @@ -103,6 +109,12 @@ module Flowerbox @browsers = {} end + + def transplant(dir) + Flowerbox::TestEnvironment.transplantable_environments.each do |env| + break if env.transplant(dir) + end + end end end diff --git a/lib/flowerbox/rack.rb b/lib/flowerbox/rack.rb index 7d6f496..060bf80 100644 --- a/lib/flowerbox/rack.rb +++ b/lib/flowerbox/rack.rb @@ -66,6 +66,8 @@ module Flowerbox get %r{^/__F__/(.*)$} do |file| asset = sprockets.asset_for(file, :bundle => false) + halt(404) if !asset + content_type asset.content_type asset.body end diff --git a/lib/flowerbox/rails/engine.rb b/lib/flowerbox/rails/engine.rb new file mode 100644 index 0000000..37d4c11 --- /dev/null +++ b/lib/flowerbox/rails/engine.rb @@ -0,0 +1,15 @@ +require 'rails/engine' + +module Flowerbox + module Rails + class Engine < ::Rails::Engine + rake_tasks do + require 'flowerbox/task' + + Flowerbox::Task.create("flowerbox:spec", :dir => "spec/javascripts") + Flowerbox::Task.create("flowerbox:integration", :dir => "js-features") + end + end + end +end + diff --git a/lib/flowerbox/runner/node.rb b/lib/flowerbox/runner/node.rb index f67a5e4..f0fc59b 100644 --- a/lib/flowerbox/runner/node.rb +++ b/lib/flowerbox/runner/node.rb @@ -22,7 +22,7 @@ module Flowerbox def configured? File.directory?(File.join(Dir.pwd, 'node_modules/jsdom')) && - File.directory?(File.join(Dir.pwd, 'node_modules/XMLHttpRequest')) + File.directory?(File.join(Dir.pwd, 'node_modules/xmlhttprequest')) end def cleanup ; end diff --git a/lib/flowerbox/runner/selenium.rb b/lib/flowerbox/runner/selenium.rb index e45334b..770389c 100644 --- a/lib/flowerbox/runner/selenium.rb +++ b/lib/flowerbox/runner/selenium.rb @@ -71,7 +71,7 @@ HTML end def template_files - sprockets.files.collect { |file| %{} } + sprockets.files.collect { |file| %{} } end end end diff --git a/lib/flowerbox/sprockets_handler.rb b/lib/flowerbox/sprockets_handler.rb index ab03391..a0bb219 100644 --- a/lib/flowerbox/sprockets_handler.rb +++ b/lib/flowerbox/sprockets_handler.rb @@ -20,7 +20,7 @@ module Flowerbox require 'flowerbox/unique_asset_list' - @files = Flowerbox::UniqueAssetList.new + @files = Flowerbox::UniqueAssetList.new(self) end def add(asset) @@ -39,7 +39,7 @@ module Flowerbox return @environment if @environment @environment = Sprockets::Environment.new - @environment.cache = Sprockets::Cache::FileStore.new(".tmp") + @environment.cache = Sprockets::Cache::FileStore.new(Flowerbox::CACHE_DIR) self.class.gem_asset_paths.each { |path| append_path(path) } options[:asset_paths].each { |path| append_path(path) } @@ -54,6 +54,18 @@ module Flowerbox def add_paths_for_compiled_asset(path) asset_for(path, :bundle => false).to_a.each { |file_path| @files.add(file_path) } end + + def logical_path_for(asset) + asset_path = asset.pathname.to_s + + environment.paths.each do |path| + if asset_path[%r{^#{path}}] + return asset_path.gsub(%r{^#{path}/}, '') + end + end + + raise StandardError.new("Could not find logical path for #{asset_path}") + end end end diff --git a/lib/flowerbox/test_environment.rb b/lib/flowerbox/test_environment.rb index 33de4f7..5362178 100644 --- a/lib/flowerbox/test_environment.rb +++ b/lib/flowerbox/test_environment.rb @@ -2,12 +2,18 @@ module Flowerbox module TestEnvironment extend Flowerbox::CoreExt::Module - class << self - def for(env) - require "flowerbox/test_environment/#{env}" + def self.for(env) + require "flowerbox/test_environment/#{env}" - find_constant(env).new + find_constant(env).new + end + + def self.transplantable_environments + Dir[File.expand_path('../test_environment/*.rb', __FILE__)].each do |file| + require file end + + constants.collect { |k| const_get(k) }.find_all(&:transplantable?) end end end diff --git a/lib/flowerbox/test_environment/base.rb b/lib/flowerbox/test_environment/base.rb index 26871e8..b361d14 100644 --- a/lib/flowerbox/test_environment/base.rb +++ b/lib/flowerbox/test_environment/base.rb @@ -13,6 +13,10 @@ module Flowerbox @reporters ||= [] end + def self.transplantable? + respond_to?(:transplant) + end + def set_additional_options(opts = nil) @options = {} diff --git a/lib/flowerbox/test_environment/jasmine.rb b/lib/flowerbox/test_environment/jasmine.rb index 729a83b..e171e12 100644 --- a/lib/flowerbox/test_environment/jasmine.rb +++ b/lib/flowerbox/test_environment/jasmine.rb @@ -1,9 +1,56 @@ require 'flowerbox/test_environment/base' require 'jasmine-core' +require 'yaml' module Flowerbox module TestEnvironment class Jasmine < Base + def self.transplant(dir) + if File.file?(jasmine_yml = File.join(dir, 'support/jasmine.yml')) + puts "Transplanting #{jasmine_yml} into Flowerbox..." + + config = [ + %{f.test_with :jasmine}, + %{f.run_with :firefox}, + %{f.report_with :verbose} + ] + + YAML.load_file(jasmine_yml).each do |key, value| + case key + when 'src_dir' + [ value ].flatten.each do |dir| + config << %{f.asset_paths << "#{dir}"} + end + when 'src_files' + [ value ].flatten.each do |file| + config << %{f.additional_files << "#{file}"} + end + when 'helpers' + [ value ].flatten.each do |pattern| + Dir[File.join(dir, pattern)].each do |file| + config << %{f.additional_files << "#{file.gsub("#{dir}/", '')}"} + end + end + when 'spec_files' + [ value ].flatten.each do |pattern| + config << %{f.spec_patterns << "#{pattern}"} + end + end + end + + File.open(target = File.join(dir, 'spec_helper.rb'), 'wb') do |fh| + fh.puts "Flowerbox.configure do |f|" + config.each do |line| + fh.puts " #{line}" + end + fh.puts "end" + end + + puts "#{target} created. Run your tests with:" + puts " flowerbox test #{dir}" + end + end + def inject_into(sprockets) sprockets.append_path(::Jasmine::Core.path) diff --git a/lib/flowerbox/unique_asset_list.rb b/lib/flowerbox/unique_asset_list.rb index 8011b9d..03353a7 100644 --- a/lib/flowerbox/unique_asset_list.rb +++ b/lib/flowerbox/unique_asset_list.rb @@ -1,11 +1,19 @@ module Flowerbox class UniqueAssetList < ::Array + attr_reader :sprockets + + def initialize(sprockets) + super([]) + + @sprockets = sprockets + end + def add(files) [ files ].flatten.each { |file| self << file if !include?(file) } end def to_json - collect(&:logical_path) + collect { |file| sprockets.logical_path_for(file) } end private