From 0987d6acc9dd81649702f8699fd02400492b2703 Mon Sep 17 00:00:00 2001 From: Marcus Mateus Date: Tue, 12 Jun 2012 21:47:45 -0500 Subject: [PATCH 1/5] Exposed sprockets_environment from Jasmine::Headless * Allows for manipulation of the Sprockets::Environment for things like adding/removing pre/postprocessors from spec_helper.rb * Also, fixed specs that were failing on files_list.rb when run standalone due to missing require in file_checker.rb --- Gemfile | 2 +- lib/jasmine/headless/file_checker.rb | 2 ++ lib/jasmine/headless/files_list.rb | 10 +++++++++- spec/lib/jasmine/headless/files_list_spec.rb | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index b736b0d..c663190 100644 --- a/Gemfile +++ b/Gemfile @@ -23,7 +23,7 @@ gem 'mocha' gem 'cucumber' -gem 'jquery-rails', '~> 1.0.0' +gem 'jquery-rails', '> 1.0' gem 'ejs' gem 'guard-jasmine-headless-webkit', :git => 'git://github.com/johnbintz/guard-jasmine-headless-webkit.git' diff --git a/lib/jasmine/headless/file_checker.rb b/lib/jasmine/headless/file_checker.rb index 6795a5b..6011bc0 100644 --- a/lib/jasmine/headless/file_checker.rb +++ b/lib/jasmine/headless/file_checker.rb @@ -1,3 +1,5 @@ +require 'rainbow' + module Jasmine::Headless::FileChecker def excluded_formats ::Jasmine::Headless::EXCLUDED_FORMATS diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index aad2d62..a97f11b 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -39,6 +39,10 @@ module Jasmine::Headless @sprockets_environment = nil end + def sprockets_environment + @sprockets_environment ||= Sprockets::Environment.new + end + def registered_engines @registered_engines ||= {} end @@ -138,7 +142,7 @@ module Jasmine::Headless def sprockets_environment return @sprockets_environment if @sprockets_environment - @sprockets_environment = Sprockets::Environment.new + @sprockets_environment = self.class.sprockets_environment #|| Sprockets::Environment.new search_paths.each { |path| @sprockets_environment.append_path(path) } @sprockets_environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons) @@ -326,6 +330,10 @@ end module Jasmine::Headless extend self + def sprockets_environment + Jasmine::Headless::FilesList.sprockets_environment + end + def register_engine(file_extension, template_class) Jasmine::Headless::FilesList.register_engine(file_extension, template_class) end diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 542e8f8..d9ca10f 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -189,6 +189,20 @@ describe Jasmine::Headless::FilesList do end end + + describe "#sprockets_environment" do + + before(:each) do + Jasmine::Headless::FilesList.reset! + end + + it "should return shared class-level sprockets environment that will be used when tests are run" do + processor = Object.new + described_class.sprockets_environment.register_postprocessor "application/javascript", processor + described_class.new.sprockets_environment.postprocessors["application/javascript"].should include(processor) + end + + end end end -- 2.45.2 From 99cef1c9a67c558e9ca91cdd5d82cc74ed14e29a Mon Sep 17 00:00:00 2001 From: Marcus Mateus Date: Tue, 12 Jun 2012 22:17:26 -0500 Subject: [PATCH 2/5] Refactored and simplified sprockets asset caching * Done primarily so postprocessors would work properly (i.e. sprockets-commonjs) * Potentially eliminates need for all the JHW specific template engines, except NilTemplate --- lib/jasmine/headless/files_list.rb | 31 +++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index a97f11b..a26c560 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -153,10 +153,10 @@ module Jasmine::Headless register_engine ".#{extension}", Jasmine::Headless::NilTemplate end - register_engine '.coffee', Jasmine::Headless::CoffeeTemplate - register_engine '.js', Jasmine::Headless::JSTemplate - register_engine '.css', Jasmine::Headless::CSSTemplate - register_engine '.jst', Jasmine::Headless::JSTTemplate + # register_engine '.coffee', Jasmine::Headless::CoffeeTemplate + # register_engine '.js', Jasmine::Headless::JSTemplate + # register_engine '.css', Jasmine::Headless::CSSTemplate + # register_engine '.jst', Jasmine::Headless::JSTTemplate end @sprockets_environment @@ -211,7 +211,28 @@ module Jasmine::Headless alert_time = nil end - sprockets_environment.find_asset(file, :bundle => false).body + asset = sprockets_environment.find_asset(file, :bundle => false) + cache_file = File.join('.jhw-cache', 'code', asset.logical_path) + # Process & cache the asset only if needed + unless File.exist?(cache_file) && (File.mtime(file) > File.mtime(cache_file)) + FileUtils.mkdir_p File.dirname(cache_file) + asset.write_to(cache_file) + end + + html = '' + case asset.content_type + when 'application/javascript' + html = %{} + # NOTE: In future sprockets versions below could be simplified w/ asset.extensions.include?('.coffee') + if asset.pathname.basename.to_s.scan(/\.[^.]+/).include?('.coffee') + html += %{\n} + end + when 'text/css' + html = %{} + else + html = asset.body + end + html end.compact.reject(&:empty?) end -- 2.45.2 From 1557bfbc5572b71721451369dab8d0eae7fbe89b Mon Sep 17 00:00:00 2001 From: Marcus Mateus Date: Tue, 12 Jun 2012 22:54:15 -0500 Subject: [PATCH 3/5] Ouch! Corrected logic mistake in calculation of cache_file freshness --- lib/jasmine/headless/files_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index a26c560..8dc338e 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -214,7 +214,7 @@ module Jasmine::Headless asset = sprockets_environment.find_asset(file, :bundle => false) cache_file = File.join('.jhw-cache', 'code', asset.logical_path) # Process & cache the asset only if needed - unless File.exist?(cache_file) && (File.mtime(file) > File.mtime(cache_file)) + unless File.exist?(cache_file) && (File.mtime(file) < File.mtime(cache_file)) FileUtils.mkdir_p File.dirname(cache_file) asset.write_to(cache_file) end -- 2.45.2 From 5ff1a9d2552db12a33ffc3ddf6061749211ec19b Mon Sep 17 00:00:00 2001 From: Marcus Mateus Date: Tue, 12 Jun 2012 23:03:09 -0500 Subject: [PATCH 4/5] Removed now superfluous JHW specific Sprockets template engine files --- lib/jasmine/headless.rb | 5 -- lib/jasmine/headless/coffee_script_cache.rb | 20 ------- lib/jasmine/headless/coffee_template.rb | 36 ------------ lib/jasmine/headless/css_template.rb | 19 ------- lib/jasmine/headless/files_list.rb | 4 -- lib/jasmine/headless/js_template.rb | 23 -------- lib/jasmine/headless/jst_template.rb | 15 ----- .../headless/coffee_script_cache_spec.rb | 21 ------- .../jasmine/headless/coffee_template_spec.rb | 55 ------------------- .../lib/jasmine/headless/css_template_spec.rb | 25 --------- spec/lib/jasmine/headless/js_template_spec.rb | 31 ----------- .../lib/jasmine/headless/jst_template_spec.rb | 21 ------- 12 files changed, 275 deletions(-) delete mode 100644 lib/jasmine/headless/coffee_script_cache.rb delete mode 100644 lib/jasmine/headless/coffee_template.rb delete mode 100644 lib/jasmine/headless/css_template.rb delete mode 100644 lib/jasmine/headless/js_template.rb delete mode 100644 lib/jasmine/headless/jst_template.rb delete mode 100644 spec/lib/jasmine/headless/coffee_script_cache_spec.rb delete mode 100644 spec/lib/jasmine/headless/coffee_template_spec.rb delete mode 100644 spec/lib/jasmine/headless/css_template_spec.rb delete mode 100644 spec/lib/jasmine/headless/js_template_spec.rb delete mode 100644 spec/lib/jasmine/headless/jst_template_spec.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 0a60efa..97998d1 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -21,11 +21,6 @@ module Jasmine autoload :TemplateWriter, 'jasmine/headless/template_writer' autoload :FileChecker, 'jasmine/headless/file_checker' - - autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' - autoload :JSTemplate, 'jasmine/headless/js_template' - autoload :JSTTemplate, 'jasmine/headless/jst_template' - autoload :CSSTemplate, 'jasmine/headless/css_template' autoload :NilTemplate, 'jasmine/headless/nil_template' autoload :Report, 'jasmine/headless/report' diff --git a/lib/jasmine/headless/coffee_script_cache.rb b/lib/jasmine/headless/coffee_script_cache.rb deleted file mode 100644 index 5ae5efe..0000000 --- a/lib/jasmine/headless/coffee_script_cache.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'coffee_script' -require 'digest/sha1' -require 'fileutils' - -module Jasmine - module Headless - class CoffeeScriptCache < CacheableAction - class << self - def cache_type - "coffee_script" - end - end - - def action - CoffeeScript.compile(File.read(file)) - end - end - end -end - diff --git a/lib/jasmine/headless/coffee_template.rb b/lib/jasmine/headless/coffee_template.rb deleted file mode 100644 index cfaaec2..0000000 --- a/lib/jasmine/headless/coffee_template.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'tilt/template' -require 'rainbow' - -module Jasmine::Headless - class CoffeeTemplate < Tilt::Template - include Jasmine::Headless::FileChecker - - self.default_mime_type = 'application/javascript' - - def prepare ; end - - def evaluate(scope, locals, &block) - if bad_format?(file) - alert_bad_format(file) - return '' - end - begin - cache = Jasmine::Headless::CoffeeScriptCache.new(file) - source = cache.handle - if cache.cached? - %{ - } - else - %{} - end - rescue CoffeeScript::CompilationError => ne - puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), "#{ne.message}".color(:white) ] - raise ne - rescue StandardError => e - puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), file.color(:yellow) ] - raise e - end - end - end -end - diff --git a/lib/jasmine/headless/css_template.rb b/lib/jasmine/headless/css_template.rb deleted file mode 100644 index 7053772..0000000 --- a/lib/jasmine/headless/css_template.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'tilt/template' - -module Jasmine::Headless - class CSSTemplate < Tilt::Template - include Jasmine::Headless::FileChecker - self.default_mime_type = 'text/css' - - def prepare ; end - - def evaluate(scope, locals, &block) - if bad_format?(file) - alert_bad_format(file) - return '' - end - file ? %{} : data - end - end -end - diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 8dc338e..4deeef9 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -153,10 +153,6 @@ module Jasmine::Headless register_engine ".#{extension}", Jasmine::Headless::NilTemplate end - # register_engine '.coffee', Jasmine::Headless::CoffeeTemplate - # register_engine '.js', Jasmine::Headless::JSTemplate - # register_engine '.css', Jasmine::Headless::CSSTemplate - # register_engine '.jst', Jasmine::Headless::JSTTemplate end @sprockets_environment diff --git a/lib/jasmine/headless/js_template.rb b/lib/jasmine/headless/js_template.rb deleted file mode 100644 index 49eb5b6..0000000 --- a/lib/jasmine/headless/js_template.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'tilt/template' - -module Jasmine::Headless - class JSTemplate < Tilt::Template - include Jasmine::Headless::FileChecker - self.default_mime_type = 'application/javascript' - - def prepare ; end - - def evaluate(scope, locals, &block) - if bad_format?(file) - alert_bad_format(file) - return '' - end - if data[%r{^} : data - end - end - end -end - diff --git a/lib/jasmine/headless/jst_template.rb b/lib/jasmine/headless/jst_template.rb deleted file mode 100644 index b6d8f1c..0000000 --- a/lib/jasmine/headless/jst_template.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'sprockets/jst_processor' - -module Jasmine::Headless - class JSTTemplate < Sprockets::JstProcessor - include Jasmine::Headless::FileChecker - def evaluate(*args) - if bad_format?(file) - alert_bad_format(file) - return '' - end - %{} - end - end -end - diff --git a/spec/lib/jasmine/headless/coffee_script_cache_spec.rb b/spec/lib/jasmine/headless/coffee_script_cache_spec.rb deleted file mode 100644 index 9952270..0000000 --- a/spec/lib/jasmine/headless/coffee_script_cache_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'spec_helper' - -describe Jasmine::Headless::CoffeeScriptCache do - include FakeFS::SpecHelpers - - describe '#action' do - let(:file) { 'file' } - let(:data) { 'data' } - let(:compiled) { 'compiled' } - - before do - CoffeeScript.expects(:compile).with(data).returns(compiled) - File.open(file, 'wb') { |fh| fh.print(data) } - end - - it 'should compile coffeescript' do - described_class.new(file).action.should == compiled - end - end -end - diff --git a/spec/lib/jasmine/headless/coffee_template_spec.rb b/spec/lib/jasmine/headless/coffee_template_spec.rb deleted file mode 100644 index f07e769..0000000 --- a/spec/lib/jasmine/headless/coffee_template_spec.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'spec_helper' - -describe Jasmine::Headless::CoffeeTemplate do - let(:data) { 'data' } - let(:path) { 'path.coffee' } - - let(:template) { described_class.new(path) { data } } - - subject { template.render } - - let(:handle_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle) } - - context 'compilation error' do - let(:error) { CoffeeScript::CompilationError.new("fail") } - - before do - handle_expectation.raises(error) - end - - it 'should pass along the error' do - expect { subject }.to raise_error(CoffeeScript::CompilationError) - end - end - - context 'compiles fine' do - let(:source) { 'source' } - - before do - Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return) - handle_expectation.returns(source) - end - - context 'cached' do - let(:file_path) { 'dir/file.js' } - let(:cache_return) { true } - - before do - Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cache_file).returns(file_path) - end - - it 'should return the cached file' do - subject.should include(%{}) - end - end - - context 'not cached' do - let(:cache_return) { false } - - it 'should return the generated js' do - subject.should include(%{}) - end - end - end -end - diff --git a/spec/lib/jasmine/headless/css_template_spec.rb b/spec/lib/jasmine/headless/css_template_spec.rb deleted file mode 100644 index ca180e8..0000000 --- a/spec/lib/jasmine/headless/css_template_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'spec_helper' - -describe Jasmine::Headless::CSSTemplate do - include FakeFS::SpecHelpers - - let(:template) { described_class.new(file) { data } } - let(:file) { 'file' } - let(:data) { 'data' } - - subject { template.render } - - before do - File.open(file, 'wb') if file - end - - context "no file'" do - let(:file) { nil } - - it { should == data } - end - - context 'file' do - it { should == %{} } - end -end diff --git a/spec/lib/jasmine/headless/js_template_spec.rb b/spec/lib/jasmine/headless/js_template_spec.rb deleted file mode 100644 index 3e07ca7..0000000 --- a/spec/lib/jasmine/headless/js_template_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'spec_helper' - -describe Jasmine::Headless::JSTemplate do - include FakeFS::SpecHelpers - - let(:template) { described_class.new(file) { data } } - let(:file) { 'file' } - let(:data) { 'data' } - - subject { template.render } - - before do - File.open(file, 'wb') if file - end - - context "no file'" do - let(:file) { nil } - - it { should == data } - end - - context 'file' do - it { should == %{} } - end - - context 'script as first thing' do - let(:data) { '