From 4150dd1828bbc36b38ff6d7aea4014d57cd9b05f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 16 Nov 2011 15:28:02 -0500 Subject: [PATCH 01/20] working on stuff, broken --- Gemfile | 2 + bin/jasmine-headless-webkit | 6 ++- jasmine-headless-webkit.gemspec | 1 + lib/jasmine-headless-webkit.rb | 4 -- lib/jasmine/headless.rb | 2 + lib/jasmine/{ => headless}/files_list.rb | 9 +++- lib/jasmine/headless/runner.rb | 2 +- lib/jasmine/headless/test_file.rb | 9 ++++ spec/bin/jasmine-headless-webkit_spec.rb | 16 ++++++ .../assets/application.js | 2 + .../with_sprockets_includes/assets/code.js | 2 + .../assets/required.js | 0 .../assets/subcode/more_code.js | 2 + .../with_sprockets_includes/spec/code_spec.js | 9 ++++ .../spec/spec_helper.js | 2 + .../with_sprockets_includes.yml | 12 +++++ .../jasmine/{ => headless}/files_list_spec.rb | 7 ++- spec/lib/jasmine/headless/test_file_spec.rb | 52 +++++++++++++++++++ 18 files changed, 126 insertions(+), 13 deletions(-) rename lib/jasmine/{ => headless}/files_list.rb (95%) create mode 100644 lib/jasmine/headless/test_file.rb create mode 100644 spec/jasmine/with_sprockets_includes/assets/application.js create mode 100644 spec/jasmine/with_sprockets_includes/assets/code.js create mode 100644 spec/jasmine/with_sprockets_includes/assets/required.js create mode 100644 spec/jasmine/with_sprockets_includes/assets/subcode/more_code.js create mode 100644 spec/jasmine/with_sprockets_includes/spec/code_spec.js create mode 100644 spec/jasmine/with_sprockets_includes/spec/spec_helper.js create mode 100644 spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml rename spec/lib/jasmine/{ => headless}/files_list_spec.rb (97%) create mode 100644 spec/lib/jasmine/headless/test_file_spec.rb diff --git a/Gemfile b/Gemfile index d6151f4..1679aed 100644 --- a/Gemfile +++ b/Gemfile @@ -15,3 +15,5 @@ gem 'mocha', '0.9.12' gem 'guard-jasmine-headless-webkit', :git => 'git://github.com/johnbintz/guard-jasmine-headless-webkit.git' gem 'facter' +gem 'jquery-rails' + diff --git a/bin/jasmine-headless-webkit b/bin/jasmine-headless-webkit index 5495d33..3796599 100755 --- a/bin/jasmine-headless-webkit +++ b/bin/jasmine-headless-webkit @@ -1,21 +1,23 @@ #!/usr/bin/env ruby require 'rubygems' -require 'rainbow' def gem_dir File.expand_path('../..', __FILE__) end $:.unshift(File.join(gem_dir, 'lib')) + require 'jasmine-headless-webkit' +require 'coffee-script' +require 'rainbow' begin options = Jasmine::Headless::Options.from_command_line runner = Jasmine::Headless::Runner.new(options) if options[:do_list] - files_list = Jasmine::FilesList.new(:config => runner.jasmine_config) + files_list = Jasmine::Headless::FilesList.new(:config => runner.jasmine_config) files_list.files.each { |file| puts file } else exit runner.run diff --git a/jasmine-headless-webkit.gemspec b/jasmine-headless-webkit.gemspec index 834f207..627b159 100644 --- a/jasmine-headless-webkit.gemspec +++ b/jasmine-headless-webkit.gemspec @@ -24,4 +24,5 @@ Gem::Specification.new do |s| s.add_dependency 'coffee-script', '>= 2.2' s.add_dependency 'rainbow' s.add_dependency 'multi_json' + s.add_dependency 'sprockets', '~> 2.0' end diff --git a/lib/jasmine-headless-webkit.rb b/lib/jasmine-headless-webkit.rb index c279e93..556c370 100644 --- a/lib/jasmine-headless-webkit.rb +++ b/lib/jasmine-headless-webkit.rb @@ -1,7 +1,3 @@ -module Jasmine - autoload :FilesList, 'jasmine/files_list' -end - require 'jasmine/headless' require 'jasmine/headless/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index b35ff93..664ccc0 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -8,6 +8,8 @@ module Jasmine::Headless autoload :Runner, 'jasmine/headless/runner' autoload :Options, 'jasmine/headless/options' autoload :Task, 'jasmine/headless/task' + autoload :FilesList, 'jasmine/headless/files_list' + autoload :TestFile, 'jasmine/headless/test_file' autoload :TemplateWriter, 'jasmine/headless/template_writer' diff --git a/lib/jasmine/files_list.rb b/lib/jasmine/headless/files_list.rb similarity index 95% rename from lib/jasmine/files_list.rb rename to lib/jasmine/headless/files_list.rb index 828637d..97b05d3 100644 --- a/lib/jasmine/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -2,7 +2,7 @@ require 'jasmine-core' require 'time' require 'multi_json' -module Jasmine +module Jasmine::Headless class FilesList attr_reader :files, :spec_files, :filtered_files, :spec_outside_scope @@ -60,7 +60,7 @@ module Jasmine def spec_file_line_numbers @spec_file_line_numbers ||= Hash[@spec_files.collect { |file| - if File.exist?(file) + if ::File.exist?(file) if !(lines = Jasmine::Headless::SpecFileAnalyzer.for(file)).empty? [ file, lines ] end @@ -82,6 +82,8 @@ module Jasmine source = nil + next file.to_html + result = case File.extname(file) when '.coffee' begin @@ -162,6 +164,9 @@ module Jasmine end end end + + @files.collect! { |file| Jasmine::Headless::TestFile.new(file) } + @filtered_files.collect! { |file| Jasmine::Headless::TestFile.new(file) } end def config? diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index c82a30d..d742cd4 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -66,7 +66,7 @@ module Jasmine def run Jasmine::Headless::CacheableAction.enabled = @options[:enable_cache] - files_list = Jasmine::FilesList.new( + files_list = Jasmine::Headless::FilesList.new( :config => jasmine_config, :only => @options[:files] ) diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb new file mode 100644 index 0000000..afaeac3 --- /dev/null +++ b/lib/jasmine/headless/test_file.rb @@ -0,0 +1,9 @@ +module Jasmine::Headless + class TestFile + attr_reader :path + + def initialize(path) + @path = path + end + end +end diff --git a/spec/bin/jasmine-headless-webkit_spec.rb b/spec/bin/jasmine-headless-webkit_spec.rb index 72845c0..9105391 100644 --- a/spec/bin/jasmine-headless-webkit_spec.rb +++ b/spec/bin/jasmine-headless-webkit_spec.rb @@ -138,5 +138,21 @@ describe "jasmine-headless-webkit" do File.size(runner_path.path).should_not == 0 end end + + describe 'sprockets' do + it 'should pull in the code via sprockets' do + files = %x{bin/jasmine-headless-webkit -l -j spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml} + $?.exitstatus.should == 0 + + puts files + + files.lines.to_a.should include('vendor/assets/javascripts/jquery.js') + files.lines.to_a.should include('assets/code.js') + files.lines.to_a.should include('assets/required.js') + files.lines.to_a.should include('assets/subcode/more_code.js') + files.lines.to_a.should include('spec/spec_helper.js') + files.lines.to_a.should include('spec/code_spec.js') + end + end end diff --git a/spec/jasmine/with_sprockets_includes/assets/application.js b/spec/jasmine/with_sprockets_includes/assets/application.js new file mode 100644 index 0000000..5d11335 --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/assets/application.js @@ -0,0 +1,2 @@ +//= require 'jquery' +//= require_tree '.' diff --git a/spec/jasmine/with_sprockets_includes/assets/code.js b/spec/jasmine/with_sprockets_includes/assets/code.js new file mode 100644 index 0000000..bd8cb0d --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/assets/code.js @@ -0,0 +1,2 @@ +window.a = '1'; + diff --git a/spec/jasmine/with_sprockets_includes/assets/required.js b/spec/jasmine/with_sprockets_includes/assets/required.js new file mode 100644 index 0000000..e69de29 diff --git a/spec/jasmine/with_sprockets_includes/assets/subcode/more_code.js b/spec/jasmine/with_sprockets_includes/assets/subcode/more_code.js new file mode 100644 index 0000000..fbe3c2f --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/assets/subcode/more_code.js @@ -0,0 +1,2 @@ +//= require 'required' + diff --git a/spec/jasmine/with_sprockets_includes/spec/code_spec.js b/spec/jasmine/with_sprockets_includes/spec/code_spec.js new file mode 100644 index 0000000..d60428c --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/spec/code_spec.js @@ -0,0 +1,9 @@ +//= require 'code' + +describe('code', function() { + it('should equal 1', function() { + expect(window.a).toEqual(1) + expect(jQuery).not.toBeUndefined() + }); +}); + diff --git a/spec/jasmine/with_sprockets_includes/spec/spec_helper.js b/spec/jasmine/with_sprockets_includes/spec/spec_helper.js new file mode 100644 index 0000000..18241c9 --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/spec/spec_helper.js @@ -0,0 +1,2 @@ +//= require 'jquery' + diff --git a/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml b/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml new file mode 100644 index 0000000..3b859d0 --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml @@ -0,0 +1,12 @@ +src_dir: spec/jasmine/with_sprockets_includes/assets +spec_dir: spec/jasmine/with_sprockets_includes/spec + +spec_files: + - "*_spec.js" + +src_files: + - "*.js" + +helpers: + - "spec_helper.js" + diff --git a/spec/lib/jasmine/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb similarity index 97% rename from spec/lib/jasmine/files_list_spec.rb rename to spec/lib/jasmine/headless/files_list_spec.rb index bb39811..b5674bc 100644 --- a/spec/lib/jasmine/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -1,11 +1,10 @@ # encoding: UTF-8 require 'spec_helper' -require 'jasmine/files_list' require 'fakefs/spec_helpers' require 'coffee-script' -describe Jasmine::FilesList do +describe Jasmine::Headless::FilesList do let(:files_list) { described_class.new } describe '#initialize' do @@ -47,7 +46,7 @@ describe Jasmine::FilesList do shared_examples_for :reading_data do it 'should read the data from the jasmine.yml file and add the files' do - files_list.files.should == Jasmine::FilesList::DEFAULT_FILES + [ + files_list.files.should == Jasmine::Headless::FilesList::DEFAULT_FILES + [ File.expand_path(first_file), File.expand_path(src_file), File.expand_path(stylesheet_file), @@ -114,7 +113,7 @@ describe Jasmine::FilesList do end context 'with filtered specs' do - let(:files_list) { Jasmine::FilesList.new(:only => filter, :config => config) } + let(:files_list) { Jasmine::Headless::FilesList.new(:only => filter, :config => config) } let(:spec_dir) { 'spec' } include FakeFS::SpecHelpers diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb new file mode 100644 index 0000000..8a25edc --- /dev/null +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper' + +describe Jasmine::Headless::TestFile do + let(:file) { described_class.new(path) } + let(:path) { 'path' } + + subject { file } + + its(:path) { should == path } + + describe '#to_html' do + subject { file.to_html } + + context '.js' do + let(:path) { 'path.js' } + + it { should == %{} } + end + + context '.css' do + let(:path) { 'path.css' } + + it { should == %{} } + end + + context '.coffee' do + let(:path) { 'path.coffee' } + + let(:handle_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle) } + + context 'compilation error' do + let(:error) { CoffeeScript::CompilationError.new("fail") } + + before do + handle_exception.raises(error) + end + + it 'should pass along the error' do + expect { subject }.to raise_error(error) + end + end + + context 'compiles fine' do + let(:cached_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return) } + + before do + + end + end + end + end +end From 1583807c14febda985b1a01c11a345af7ab693cd Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 17 Nov 2011 16:18:25 -0500 Subject: [PATCH 02/20] still reworking things, broken --- lib/jasmine/headless/files_list.rb | 175 ++++++++++-------- lib/jasmine/headless/test_file.rb | 34 ++++ spec/lib/jasmine/headless/files_list_spec.rb | 52 +++++- .../jasmine/headless/template_writer_spec.rb | 4 +- spec/lib/jasmine/headless/test_file_spec.rb | 28 ++- 5 files changed, 212 insertions(+), 81 deletions(-) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 97b05d3..5c8f8be 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -1,10 +1,12 @@ require 'jasmine-core' require 'time' require 'multi_json' +require 'set' +require 'sprockets/directive_processor' module Jasmine::Headless class FilesList - attr_reader :files, :spec_files, :filtered_files, :spec_outside_scope + attr_reader :spec_outside_scope class << self def find_vendored_asset_paths(*names) @@ -23,25 +25,35 @@ module Jasmine::Headless end end - DEFAULT_FILES = [ - File.join(Jasmine::Core.path, "jasmine.js"), - File.join(Jasmine::Core.path, "jasmine-html.js"), - File.join(Jasmine::Core.path, "jasmine.css") - ] + %w{jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html}.collect { |name| - Jasmine::Headless.root.join("vendor/assets/javascripts/#{name}.js").to_s - } + DEFAULT_FILES = + %w{jasmine.js jasmine-html.js jasmine.css}.collect { |name| File.join(Jasmine::Core.path, name) } + + %w{jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html}.collect { |name| + Jasmine::Headless.root.join("vendor/assets/javascripts/#{name}.js").to_s + } PLEASE_WAIT_IM_WORKING_TIME = 2 def initialize(options = {}) @options = options - @files = DEFAULT_FILES.dup + @files = Set.new(DEFAULT_FILES.dup) @filtered_files = @files.dup @spec_outside_scope = false - @spec_files = [] + @spec_files = Set.new use_config! if config? end + def files + @files.to_a + end + + def filtered_files + @filtered_files.to_a + end + + def spec_files + @spec_files.to_a + end + def has_spec_outside_scope? @spec_outside_scope end @@ -60,7 +72,7 @@ module Jasmine::Headless def spec_file_line_numbers @spec_file_line_numbers ||= Hash[@spec_files.collect { |file| - if ::File.exist?(file) + if File.exist?(file) if !(lines = Jasmine::Headless::SpecFileAnalyzer.for(file)).empty? [ file, lines ] end @@ -70,6 +82,18 @@ module Jasmine::Headless }.compact] end + def add_dependencies(file) + if File.file?(file) + processor = Sprockets::DirectiveProcessor.new(file) + processor.directives.each do |line, type, name| + case type + when 'require' + find_vendored(name) + end + end + end + end + private def to_html(files) alert_time = Time.now + PLEASE_WAIT_IM_WORKING_TIME @@ -80,37 +104,7 @@ module Jasmine::Headless alert_time = nil end - source = nil - - next file.to_html - - result = case File.extname(file) - when '.coffee' - 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.to_s.color(:white) ] - raise ne - rescue StandardError => e - puts "[%s] Error in compiling one of the followng: %s" % [ 'coffeescript'.color(:red), files.join(' ').color(:yellow) ] - raise e - end - when '.js' - %{} - when '.css' - %{} - end - - result + Jasmine::Headless::TestFile.new(file).to_html }.flatten.compact.reject(&:empty?) end @@ -126,47 +120,53 @@ module Jasmine::Headless end end + SEARCH_ROOTS = { + 'src_files' => 'src_dir', + 'stylesheets' => 'src_dir', + 'helpers' => 'spec_dir', + 'spec_files' => 'spec_dir' + } + def use_config! @filtered_files = @files.dup - data = @options[:config].dup - [ [ 'src_files', 'src_dir' ], [ 'stylesheets', 'src_dir' ], [ 'vendored_helpers' ], [ 'helpers', 'spec_dir' ], [ 'spec_files', 'spec_dir' ] ].each do |searches, root| - if data[searches] - case searches - when 'vendored_helpers' - data[searches].each do |name| - found_files = self.class.find_vendored_asset_path(name) + @config = @options[:config].dup - @files += found_files - @filtered_files += found_files - end + %w{src_files stylesheets vendored_helpers helpers spec_files}.each do |searches| + if data = @config[searches] + if self.respond_to?("add_#{searches}_files", true) + send("add_#{searches}_files", data.flatten) else - data[searches].flatten.collect do |search| - path = search - path = File.join(data[root], path) if data[root] - found_files = expanded_dir(path) - @files - - @files += found_files - - if searches == 'spec_files' - @spec_files += spec_filter.empty? ? found_files : (found_files & spec_filter) - end - - @filtered_files += begin - if searches == 'spec_files' - @spec_outside_scope = ((spec_filter | found_files).sort != found_files.sort) - spec_filter.empty? ? found_files : (spec_filter || found_files) - else - found_files - end - end - end + add_files(data.flatten, searches) end end end + end - @files.collect! { |file| Jasmine::Headless::TestFile.new(file) } - @filtered_files.collect! { |file| Jasmine::Headless::TestFile.new(file) } + def add_vendored_helpers_files(searches) + searches.each do |name| + self.class.find_vendored_asset_path(name).each do |file| + add_file(file) + end + end + end + + def add_files(searches, type) + searches.each do |search| + path = search + path = File.join(@config[SEARCH_ROOTS[type]], path) if @config[SEARCH_ROOTS[type]] + found_files = expanded_dir(path) - files + + found_files.each do |file| + type == 'spec_files' ? add_spec_file(file) : add_file(file) + end + end + + if type == 'spec_files' + spec_filter.each do |file| + @spec_outside_scope ||= add_spec_file(file) + end + end end def config? @@ -176,6 +176,33 @@ module Jasmine::Headless def expanded_dir(path) Dir[path].collect { |file| File.expand_path(file) } end + + def add_file(file) + add_dependencies(file) + + @files << file + @filtered_files << file + end + + def add_spec_file(file) + add_dependencies(file) + + if !@files.include?(file) + @files << file + + if include_spec_file?(file) + @filtered_files << file + @spec_files << file if spec_filter.empty? || spec_filter.include?(file) + end + + true + end + end + + def include_spec_file?(file) + spec_filter.empty? || spec_filter.include?(file) + end + end end diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index afaeac3..cf12667 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -1,3 +1,5 @@ +require 'rainbow' + module Jasmine::Headless class TestFile attr_reader :path @@ -5,5 +7,37 @@ module Jasmine::Headless def initialize(path) @path = path end + + def ==(other) + self.path == other.path + end + + def to_html + case File.extname(path) + when '.coffee' + begin + cache = Jasmine::Headless::CoffeeScriptCache.new(path) + source = cache.handle + if cache.cached? + %{ + } + else + %{} + end + rescue CoffeeScript::CompilationError => ne + puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), path.color(:yellow), ne.message.to_s.color(:white) ] + raise ne + rescue StandardError => e + puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), path.color(:yellow) ] + raise e + end + when '.js' + %{} + when '.css' + %{} + end + end end end diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index b5674bc..2bf78ea 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -45,14 +45,18 @@ describe Jasmine::Headless::FilesList do end shared_examples_for :reading_data do - it 'should read the data from the jasmine.yml file and add the files' do - files_list.files.should == Jasmine::Headless::FilesList::DEFAULT_FILES + [ + let(:expected_files) do + Jasmine::Headless::FilesList::DEFAULT_FILES + [ File.expand_path(first_file), File.expand_path(src_file), File.expand_path(stylesheet_file), File.expand_path(helper_file), File.expand_path(spec_file) ] + end + + it 'should read the data from the jasmine.yml file and add the files' do + files_list.files.should == expected_files files_list.spec_files.should == [ File.expand_path(spec_file) ] end @@ -243,5 +247,49 @@ describe Jasmine::Headless::FilesList do } end end + + describe '#add_dependencies' do + include FakeFS::SpecHelpers + + let(:file) { 'file.js' } + + before do + File.open(file, 'wb') { |fh| fh.print data } + end + + subject { files_list.add_dependencies(file) } + + context 'no requires' do + let(:data) { 'javascript' } + + before do + files_list.expects(:add_file).never + end + + it 'should succeed' do + subject + end + end + + context 'require' do + let(:data) { %{//= require 'other'\njavascript} } + + before do + File.open(other, 'wb') + end + + context 'with js' do + let(:other) { 'other.js' } + + before do + files_list.expects(:add_file).with(other) + end + + it 'should succeed' do + subject + end + end + end + end end diff --git a/spec/lib/jasmine/headless/template_writer_spec.rb b/spec/lib/jasmine/headless/template_writer_spec.rb index 25a9cc6..a6e8579 100644 --- a/spec/lib/jasmine/headless/template_writer_spec.rb +++ b/spec/lib/jasmine/headless/template_writer_spec.rb @@ -53,7 +53,7 @@ describe Jasmine::Headless::TemplateWriter do runner.stubs(:runner_filename).returns(false) end - let(:files_list) { Jasmine::FilesList.new } + let(:files_list) { Jasmine::Headless::FilesList.new } before do files_list.files << 'file.js' @@ -70,7 +70,7 @@ describe Jasmine::Headless::TemplateWriter do context 'filtered files' do before do - files_list.files << 'file2.js' + files_list.instance_variable_get(:@files) << 'file2.js' end it 'should write two files' do diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index 8a25edc..f916d9a 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -20,7 +20,7 @@ describe Jasmine::Headless::TestFile do context '.css' do let(:path) { 'path.css' } - it { should == %{} } + it { should == %{} } end context '.coffee' do @@ -32,7 +32,7 @@ describe Jasmine::Headless::TestFile do let(:error) { CoffeeScript::CompilationError.new("fail") } before do - handle_exception.raises(error) + handle_expectation.raises(error) end it 'should pass along the error' do @@ -41,10 +41,32 @@ describe Jasmine::Headless::TestFile do end context 'compiles fine' do - let(:cached_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return) } + 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 From a2e3ea90c97c7fb496504a9107d0776022d0959e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 17 Nov 2011 22:16:04 -0500 Subject: [PATCH 03/20] require works, whoa --- lib/jasmine/headless/files_list.rb | 134 +++++++++------- lib/jasmine/headless/test_file.rb | 23 ++- spec/bin/jasmine-headless-webkit_spec.rb | 16 +- .../with_sprockets_includes/assets/code.js | 2 - .../assets/required.js | 0 .../assets/things/code.js | 4 + .../assets/things/required.js | 2 + .../assets/{ => things}/subcode/more_code.js | 0 .../spec/{ => things}/code_spec.js | 0 .../with_sprockets_includes.yml | 4 +- spec/lib/jasmine/headless/files_list_spec.rb | 149 ++++++++++++------ spec/lib/jasmine/headless/test_file_spec.rb | 35 +++- spec/spec_helper.rb | 18 +++ 13 files changed, 264 insertions(+), 123 deletions(-) delete mode 100644 spec/jasmine/with_sprockets_includes/assets/code.js delete mode 100644 spec/jasmine/with_sprockets_includes/assets/required.js create mode 100644 spec/jasmine/with_sprockets_includes/assets/things/code.js create mode 100644 spec/jasmine/with_sprockets_includes/assets/things/required.js rename spec/jasmine/with_sprockets_includes/assets/{ => things}/subcode/more_code.js (100%) rename spec/jasmine/with_sprockets_includes/spec/{ => things}/code_spec.js (100%) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 5c8f8be..ed37c43 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -1,44 +1,42 @@ require 'jasmine-core' require 'time' require 'multi_json' -require 'set' -require 'sprockets/directive_processor' module Jasmine::Headless class FilesList - attr_reader :spec_outside_scope - class << self - def find_vendored_asset_paths(*names) + def vendor_asset_paths + return @vendor_asset_paths if @vendor_asset_paths + require 'rubygems' raise StandardError.new("A newer version of Rubygems is required to use vendored assets. Please upgrade.") if !Gem::Specification.respond_to?(:map) - all_spec_files.find_all do |file| - names.any? { |name| file["/#{name}.js"] } - end - end - def all_spec_files - @all_spec_files ||= Gem::Specification.map { |spec| spec.files.find_all { |file| - file["vendor/assets/javascripts"] - }.compact.collect { |file| File.join(spec.gem_dir, file) } }.flatten + @vendor_asset_paths = [] + + Gem::Specification.map { |spec| + path = File.join(spec.gem_dir, 'vendor/assets/javascripts') + + File.directory?(path) ? path : nil + }.compact end end - DEFAULT_FILES = - %w{jasmine.js jasmine-html.js jasmine.css}.collect { |name| File.join(Jasmine::Core.path, name) } + - %w{jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html}.collect { |name| - Jasmine::Headless.root.join("vendor/assets/javascripts/#{name}.js").to_s - } + DEFAULT_FILES = %w{jasmine.js jasmine-html jasmine.css jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html} PLEASE_WAIT_IM_WORKING_TIME = 2 def initialize(options = {}) @options = options - @files = Set.new(DEFAULT_FILES.dup) - @filtered_files = @files.dup + + @files = [] + @filtered_files = [] + + DEFAULT_FILES.each { |file| add_dependency('require', file) } + @spec_outside_scope = false - @spec_files = Set.new + @spec_files = [] + use_config! if config? end @@ -54,6 +52,10 @@ module Jasmine::Headless @spec_files.to_a end + def search_paths + @search_paths ||= [ Jasmine::Core.path, src_dir, spec_dir ] + self.class.vendor_asset_paths + end + def has_spec_outside_scope? @spec_outside_scope end @@ -82,16 +84,35 @@ module Jasmine::Headless }.compact] end - def add_dependencies(file) - if File.file?(file) - processor = Sprockets::DirectiveProcessor.new(file) - processor.directives.each do |line, type, name| - case type - when 'require' - find_vendored(name) + def add_dependencies(file, source_root) + TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name) } + end + + def add_dependency(type, file) + if result = find_dependency(file) + path, source_root = result + + case type + when 'require' + add_file(path, source_root) + end + end + end + + def find_dependency(file) + search_paths.each do |dir| + if file[%r{\.(js|css|coffee)$}] + if File.file?(path = File.join(dir, file)) + return [ File.expand_path(path), dir ] + end + else + if path = Dir[File.join(dir, "#{file}.*")].first + return [ File.expand_path(path), dir ] end end end + + false end private @@ -132,33 +153,23 @@ module Jasmine::Headless @config = @options[:config].dup - %w{src_files stylesheets vendored_helpers helpers spec_files}.each do |searches| + %w{src_files stylesheets helpers spec_files}.each do |searches| if data = @config[searches] - if self.respond_to?("add_#{searches}_files", true) - send("add_#{searches}_files", data.flatten) - else - add_files(data.flatten, searches) - end - end - end - end - - def add_vendored_helpers_files(searches) - searches.each do |name| - self.class.find_vendored_asset_path(name).each do |file| - add_file(file) + add_files(data.flatten, searches) end end end def add_files(searches, type) searches.each do |search| - path = search - path = File.join(@config[SEARCH_ROOTS[type]], path) if @config[SEARCH_ROOTS[type]] + dir = @config[SEARCH_ROOTS[type]] || Dir.pwd + + path = File.expand_path(File.join(dir, search)) + found_files = expanded_dir(path) - files found_files.each do |file| - type == 'spec_files' ? add_spec_file(file) : add_file(file) + type == 'spec_files' ? add_spec_file(file) : add_file(file, dir) end end @@ -177,22 +188,22 @@ module Jasmine::Headless Dir[path].collect { |file| File.expand_path(file) } end - def add_file(file) - add_dependencies(file) + def add_file(file, source_root) + add_dependencies(file, source_root) - @files << file - @filtered_files << file + @files << file if !@files.include?(file) + @filtered_files << file if !@filtered_files.include?(file) end def add_spec_file(file) - add_dependencies(file) + add_dependencies(file, spec_dir) if !@files.include?(file) - @files << file + @files << file if !@files.include?(file) if include_spec_file?(file) - @filtered_files << file - @spec_files << file if spec_filter.empty? || spec_filter.include?(file) + @filtered_files << file if !@filtered_files.include?(file) + @spec_files << file if !@spec_files.include?(file) && spec_filter.empty? || spec_filter.include?(file) end true @@ -203,6 +214,23 @@ module Jasmine::Headless spec_filter.empty? || spec_filter.include?(file) end + def src_dir + config_dir_or_pwd('src_dir') + end + + def spec_dir + config_dir_or_pwd('spec_dir') + end + + def config_dir_or_pwd(dir) + found_dir = Dir.pwd + + if @options[:config] + found_dir = @options[:config][dir] || found_dir + end + + found_dir + end end end diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index cf12667..9605d2b 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -1,11 +1,12 @@ require 'rainbow' +require 'sprockets/directive_processor' module Jasmine::Headless class TestFile - attr_reader :path + attr_reader :path, :source_root - def initialize(path) - @path = path + def initialize(path, source_root = nil) + @path, @source_root = path, source_root end def ==(other) @@ -27,7 +28,7 @@ module Jasmine::Headless %{} end rescue CoffeeScript::CompilationError => ne - puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), path.color(:yellow), ne.message.to_s.color(:white) ] + puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), path.color(:yellow), ne.message.dup.to_s.color(:white) ] raise ne rescue StandardError => e puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), path.color(:yellow) ] @@ -39,5 +40,19 @@ module Jasmine::Headless %{} end end + + def dependencies + return @dependencies if @dependencies + + processor = Sprockets::DirectiveProcessor.new(path) + @dependencies = processor.directives.collect do |_, type, name| + if name[%r{^./}] + name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') + end + + [ type, name ] + end + end end end + diff --git a/spec/bin/jasmine-headless-webkit_spec.rb b/spec/bin/jasmine-headless-webkit_spec.rb index 9105391..e8fcbf9 100644 --- a/spec/bin/jasmine-headless-webkit_spec.rb +++ b/spec/bin/jasmine-headless-webkit_spec.rb @@ -144,14 +144,14 @@ describe "jasmine-headless-webkit" do files = %x{bin/jasmine-headless-webkit -l -j spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml} $?.exitstatus.should == 0 - puts files - - files.lines.to_a.should include('vendor/assets/javascripts/jquery.js') - files.lines.to_a.should include('assets/code.js') - files.lines.to_a.should include('assets/required.js') - files.lines.to_a.should include('assets/subcode/more_code.js') - files.lines.to_a.should include('spec/spec_helper.js') - files.lines.to_a.should include('spec/code_spec.js') + files.lines.to_a.should contain_in_order_in_file_list( + 'vendor/assets/javascripts/jquery.js', + 'assets/things/required.js', + 'assets/things/code.js', + 'assets/things/subcode/more_code.js', + 'spec_helper.js', + 'spec/things/code_spec.js' + ) end end end diff --git a/spec/jasmine/with_sprockets_includes/assets/code.js b/spec/jasmine/with_sprockets_includes/assets/code.js deleted file mode 100644 index bd8cb0d..0000000 --- a/spec/jasmine/with_sprockets_includes/assets/code.js +++ /dev/null @@ -1,2 +0,0 @@ -window.a = '1'; - diff --git a/spec/jasmine/with_sprockets_includes/assets/required.js b/spec/jasmine/with_sprockets_includes/assets/required.js deleted file mode 100644 index e69de29..0000000 diff --git a/spec/jasmine/with_sprockets_includes/assets/things/code.js b/spec/jasmine/with_sprockets_includes/assets/things/code.js new file mode 100644 index 0000000..bb2e8ae --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/assets/things/code.js @@ -0,0 +1,4 @@ +//= require 'things/required' + +window.a = '1'; + diff --git a/spec/jasmine/with_sprockets_includes/assets/things/required.js b/spec/jasmine/with_sprockets_includes/assets/things/required.js new file mode 100644 index 0000000..18241c9 --- /dev/null +++ b/spec/jasmine/with_sprockets_includes/assets/things/required.js @@ -0,0 +1,2 @@ +//= require 'jquery' + diff --git a/spec/jasmine/with_sprockets_includes/assets/subcode/more_code.js b/spec/jasmine/with_sprockets_includes/assets/things/subcode/more_code.js similarity index 100% rename from spec/jasmine/with_sprockets_includes/assets/subcode/more_code.js rename to spec/jasmine/with_sprockets_includes/assets/things/subcode/more_code.js diff --git a/spec/jasmine/with_sprockets_includes/spec/code_spec.js b/spec/jasmine/with_sprockets_includes/spec/things/code_spec.js similarity index 100% rename from spec/jasmine/with_sprockets_includes/spec/code_spec.js rename to spec/jasmine/with_sprockets_includes/spec/things/code_spec.js diff --git a/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml b/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml index 3b859d0..f5943f8 100644 --- a/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml +++ b/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml @@ -2,10 +2,10 @@ src_dir: spec/jasmine/with_sprockets_includes/assets spec_dir: spec/jasmine/with_sprockets_includes/spec spec_files: - - "*_spec.js" + - "**/*_spec.js" src_files: - - "*.js" + - "things/**/*.js" helpers: - "spec_helper.js" diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 2bf78ea..ec53257 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -46,7 +46,7 @@ describe Jasmine::Headless::FilesList do shared_examples_for :reading_data do let(:expected_files) do - Jasmine::Headless::FilesList::DEFAULT_FILES + [ + [ File.expand_path(first_file), File.expand_path(src_file), File.expand_path(stylesheet_file), @@ -87,33 +87,6 @@ describe Jasmine::Headless::FilesList do it_should_behave_like :reading_data end - - context 'with vendored helpers' do - let(:config) { { - 'src_dir' => src_dir, - 'spec_dir' => spec_dir, - 'src_files' => [ 'js/first_file.js', 'js/*.js' ], - 'spec_files' => [ '*_spec.js' ], - 'helpers' => [], - 'stylesheets' => [ 'stylesheet/*.css' ], - 'vendored_helpers' => [ 'one', 'two' ] - } } - - let(:helper_file) { "path/one.js" } - let(:other_helper_file) { "path/two.js" } - - before do - described_class.expects(:find_vendored_asset_path).with('one').returns([ helper_file ]) - described_class.expects(:find_vendored_asset_path).with('two').returns([ other_helper_file ]) - end - - it 'should find the vendored file' do - files_list.files.should include(helper_file) - files_list.files.should include(other_helper_file) - - files_list.files.index(helper_file).should be < files_list.files.index(other_helper_file) - end - end end context 'with filtered specs' do @@ -248,46 +221,120 @@ describe Jasmine::Headless::FilesList do end end - describe '#add_dependencies' do - include FakeFS::SpecHelpers - - let(:file) { 'file.js' } + describe '#add_dependency' do + let(:file) { 'file' } + let(:other_file) { 'other' } + let(:path) { 'path' } before do - File.open(file, 'wb') { |fh| fh.print data } + files_list.stubs(:find_dependency).with(file).returns(path) + files_list.stubs(:find_dependency).with(other_file).returns(false) end - subject { files_list.add_dependencies(file) } - - context 'no requires' do - let(:data) { 'javascript' } - + context 'not found' do before do files_list.expects(:add_file).never end - it 'should succeed' do - subject + it 'should do nothing' do + files_list.add_dependency('', other_file) end end context 'require' do - let(:data) { %{//= require 'other'\njavascript} } - before do - File.open(other, 'wb') + files_list.expects(:add_file).with(path, nil) end - context 'with js' do - let(:other) { 'other.js' } + it 'should add the file to the front' do + files_list.add_dependency('require', file) + end + end + end - before do - files_list.expects(:add_file).with(other) - end + describe '#search_paths' do + let(:files_list) { described_class.new(:config => config) } - it 'should succeed' do - subject - end + let(:config) { { + 'src_dir' => src_dir, + 'spec_dir' => spec_dir + } } + + let(:src_dir) { 'src dir' } + let(:spec_dir) { 'spec dir' } + let(:path) { 'path' } + + context 'no vendored gem paths' do + before do + Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([]) + end + + it 'should take the src dir and spec dirs' do + files_list.search_paths.should == [ Jasmine::Core.path, src_dir, spec_dir ] + end + end + + context 'vendored gem paths' do + before do + Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([ path ]) + end + + it 'should add the vendor gem paths to the list' do + files_list.search_paths.should == [ Jasmine::Core.path, src_dir, spec_dir, path ] + end + end + end + + describe '.vendor_asset_paths' do + include FakeFS::SpecHelpers + + let(:dir_one) { 'dir_one' } + let(:dir_two) { 'dir_two' } + + let(:gem_one) { stub(:gem_dir => dir_one) } + let(:gem_two) { stub(:gem_dir => dir_two) } + + before do + described_class.instance_variable_set(:@vendor_asset_paths, nil) + + FileUtils.mkdir_p File.join(dir_two, 'vendor/assets/javascripts') + + Gem::Specification.stubs(:_all).returns([gem_one, gem_two]) + end + + it 'should return all matching gems with vendor/assets/javascripts directories' do + described_class.vendor_asset_paths.should == [ File.join(dir_two, 'vendor/assets/javascripts') ] + end + end + + describe '#find_dependency' do + include FakeFS::SpecHelpers + + let(:dir) { File.expand_path('dir') } + let(:filename) { 'file' } + let(:file) { "#{filename}.js" } + + before do + files_list.stubs(:search_paths).returns([ dir ]) + + FileUtils.mkdir_p dir + end + + context 'does not exist' do + it 'should not be found' do + files_list.find_dependency(file).should be_false + end + end + + context 'exists' do + let(:path) { File.join(dir, file) } + + before do + File.open(path, 'wb') + end + + it 'should be found' do + files_list.find_dependency(filename).should == [ File.expand_path(path), dir ] end end end diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index f916d9a..8758ba0 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -1,8 +1,10 @@ require 'spec_helper' describe Jasmine::Headless::TestFile do - let(:file) { described_class.new(path) } - let(:path) { 'path' } + let(:source_root) { File.expand_path('source_root') } + let(:path) { File.join(source_root, 'path.js') } + + let(:file) { described_class.new(path, source_root) } subject { file } @@ -36,7 +38,7 @@ describe Jasmine::Headless::TestFile do end it 'should pass along the error' do - expect { subject }.to raise_error(error) + expect { subject }.to raise_error(CoffeeScript::CompilationError) end end @@ -71,4 +73,31 @@ describe Jasmine::Headless::TestFile do end end end + + describe '#dependencies' do + include FakeFS::SpecHelpers + + before do + FileUtils.mkdir_p File.dirname(path) + File.open(path, 'wb') { |fh| fh.print "//= require '#{req}'\njavascript" } + end + + context 'absolute' do + let(:req) { 'test' } + + subject { file.dependencies } + + it { should == [ [ 'require', req ] ] } + end + + context 'relative' do + let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') } + + let(:req) { './test' } + + subject { file.dependencies } + + it { should == [ [ 'require', 'subdir/subsubdir/test' ] ] } + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2348573..0066121 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,6 +6,7 @@ RSpec.configure do |c| c.before(:each) do Jasmine::Headless::CacheableAction.enabled = false + Jasmine::Headless::FilesList.instance_variable_set(:@vendor_asset_paths, nil) end end @@ -51,4 +52,21 @@ module RSpec::Matchers File.file?(file) end end + + define :contain_in_order_in_file_list do |*files| + match do |lines| + file_list = files.dup + + lines.each do |line| + next if !file_list.first + + if line[file_list.first] + file_list.shift + end + end + + file_list.length == 0 + end + end end + From 537b2e437d8acdf47e659977c4cd96475a88f058 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 18 Nov 2011 11:50:48 -0500 Subject: [PATCH 04/20] more work on making sprockets support better, oh yeah --- lib/jasmine/headless/files_list.rb | 31 +++++++++++++------- lib/jasmine/headless/runner.rb | 2 ++ lib/jasmine/headless/test_file.rb | 2 +- spec/lib/jasmine/headless/files_list_spec.rb | 26 ++++++++++++++-- spec/lib/jasmine/headless/test_file_spec.rb | 10 +++++++ spec/spec_helper.rb | 6 +++- 6 files changed, 62 insertions(+), 15 deletions(-) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index ed37c43..86199fc 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -20,6 +20,10 @@ module Jasmine::Headless File.directory?(path) ? path : nil }.compact end + + def reset! + @vendor_asset_paths = nil + end end DEFAULT_FILES = %w{jasmine.js jasmine-html jasmine.css jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html} @@ -32,7 +36,7 @@ module Jasmine::Headless @files = [] @filtered_files = [] - DEFAULT_FILES.each { |file| add_dependency('require', file) } + DEFAULT_FILES.each { |file| add_dependency('require', file, nil) } @spec_outside_scope = false @spec_files = [] @@ -85,16 +89,20 @@ module Jasmine::Headless end def add_dependencies(file, source_root) - TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name) } + TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) } end - def add_dependency(type, file) - if result = find_dependency(file) - path, source_root = result - - case type - when 'require' - add_file(path, source_root) + def add_dependency(type, file, source_root) + case type + when 'require' + if result = find_dependency(file) + add_file(*result) + end + when 'require_tree' + Dir[File.join(source_root, file, '**/*.{js,css,coffee}')].each do |tree_path| + if result = find_dependency(tree_path.gsub(%r{^#{source_root}/}, '')) + add_file(*result) + end end end end @@ -103,11 +111,11 @@ module Jasmine::Headless search_paths.each do |dir| if file[%r{\.(js|css|coffee)$}] if File.file?(path = File.join(dir, file)) - return [ File.expand_path(path), dir ] + return [ File.expand_path(path), File.expand_path(dir) ] end else if path = Dir[File.join(dir, "#{file}.*")].first - return [ File.expand_path(path), dir ] + return [ File.expand_path(path), File.expand_path(dir) ] end end end @@ -163,6 +171,7 @@ module Jasmine::Headless def add_files(searches, type) searches.each do |search| dir = @config[SEARCH_ROOTS[type]] || Dir.pwd + dir = File.expand_path(dir) path = File.expand_path(File.join(dir, search)) diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index d742cd4..d8c14c2 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -66,6 +66,8 @@ module Jasmine def run Jasmine::Headless::CacheableAction.enabled = @options[:enable_cache] + Jasmine::Headless::FilesList.reset! + files_list = Jasmine::Headless::FilesList.new( :config => jasmine_config, :only => @options[:files] diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index 9605d2b..7d8ee10 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -46,7 +46,7 @@ module Jasmine::Headless processor = Sprockets::DirectiveProcessor.new(path) @dependencies = processor.directives.collect do |_, type, name| - if name[%r{^./}] + if name[%r{^\.}] name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') end diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index ec53257..4cb0742 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -237,7 +237,7 @@ describe Jasmine::Headless::FilesList do end it 'should do nothing' do - files_list.add_dependency('', other_file) + files_list.add_dependency('', other_file, nil) end end @@ -247,7 +247,29 @@ describe Jasmine::Headless::FilesList do end it 'should add the file to the front' do - files_list.add_dependency('require', file) + files_list.add_dependency('require', file, nil) + end + end + + context 'require_tree' do + include FakeFS::SpecHelpers + + let(:paths) { %w{one.js dir/two.coffee dir/three.css dir/subdir/four.js.erb other/five.css.erb} } + + before do + paths.each do |path| + FileUtils.mkdir_p File.dirname(path) + File.open(path, 'wb') + + if path[%r{\.(js|css|coffee)$}] + files_list.expects(:find_dependency).with(path).returns(other_file) + files_list.expects(:add_file).with(other_file, nil) + end + end + end + + it 'should add the file to the front' do + files_list.add_dependency('require_tree', '.', File.expand_path('.')) end end end diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index 8758ba0..0265286 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -99,5 +99,15 @@ describe Jasmine::Headless::TestFile do it { should == [ [ 'require', 'subdir/subsubdir/test' ] ] } end + + context 'dot' do + let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') } + + let(:req) { '.' } + + subject { file.dependencies } + + it { should == [ [ 'require', 'subdir/subsubdir' ] ] } + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0066121..d73f675 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,7 +6,7 @@ RSpec.configure do |c| c.before(:each) do Jasmine::Headless::CacheableAction.enabled = false - Jasmine::Headless::FilesList.instance_variable_set(:@vendor_asset_paths, nil) + Jasmine::Headless::FilesList.reset! end end @@ -67,6 +67,10 @@ module RSpec::Matchers file_list.length == 0 end + + failure_message_for_should do |lines| + %{expected\n#{lines.join("\n")}\nto contain the following files, in order:\n#{files.join("\n")}} + end end end From 8867d00ac80750198733e9872715d93a9088b08f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 18 Nov 2011 16:00:24 -0500 Subject: [PATCH 05/20] support sprockets-style jst templates --- Gemfile | 2 +- jasmine-headless-webkit.gemspec | 2 +- lib/jasmine/headless.rb | 2 + lib/jasmine/headless/files_list.rb | 16 ++++--- lib/jasmine/headless/test_file.rb | 29 +++++++++++- spec/bin/jasmine-headless-webkit_spec.rb | 2 + .../assets/things/code.js | 2 + .../assets/things/templates/that.jst.ejs | 0 .../assets/things/templates/this.jst | 0 .../spec/spec_helper.js | 1 - .../with_sprockets_includes.yml | 2 +- spec/lib/jasmine/headless/files_list_spec.rb | 44 +------------------ spec/lib/jasmine/headless/test_file_spec.rb | 32 ++++++++++++++ 13 files changed, 82 insertions(+), 52 deletions(-) create mode 100644 spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs create mode 100644 spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst diff --git a/Gemfile b/Gemfile index 1679aed..4896aec 100644 --- a/Gemfile +++ b/Gemfile @@ -16,4 +16,4 @@ gem 'guard-jasmine-headless-webkit', :git => 'git://github.com/johnbintz/guard-j gem 'facter' gem 'jquery-rails' - +gem 'ejs' diff --git a/jasmine-headless-webkit.gemspec b/jasmine-headless-webkit.gemspec index 627b159..e7be2ec 100644 --- a/jasmine-headless-webkit.gemspec +++ b/jasmine-headless-webkit.gemspec @@ -24,5 +24,5 @@ Gem::Specification.new do |s| s.add_dependency 'coffee-script', '>= 2.2' s.add_dependency 'rainbow' s.add_dependency 'multi_json' - s.add_dependency 'sprockets', '~> 2.0' + s.add_dependency 'sprockets', '>= 2.0' end diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 664ccc0..58c11af 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -1,4 +1,5 @@ require 'pathname' +require 'sprockets/engines' module Jasmine::Headless autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' @@ -24,3 +25,4 @@ module Jasmine::Headless end require 'jasmine/headless/errors' +Sprockets::Engines diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 86199fc..e577fef 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -57,7 +57,7 @@ module Jasmine::Headless end def search_paths - @search_paths ||= [ Jasmine::Core.path, src_dir, spec_dir ] + self.class.vendor_asset_paths + @search_paths ||= [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ] + self.class.vendor_asset_paths end def has_spec_outside_scope? @@ -92,6 +92,8 @@ module Jasmine::Headless TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) } end + EXTENSION_FILTER = %r{\.(js|css|coffee|jst.*)$} + def add_dependency(type, file, source_root) case type when 'require' @@ -99,7 +101,7 @@ module Jasmine::Headless add_file(*result) end when 'require_tree' - Dir[File.join(source_root, file, '**/*.{js,css,coffee}')].each do |tree_path| + Dir[File.join(source_root, file, '**/*')].find_all { |path| File.file?(path) && path[EXTENSION_FILTER] }.sort.each do |tree_path| if result = find_dependency(tree_path.gsub(%r{^#{source_root}/}, '')) add_file(*result) end @@ -109,7 +111,7 @@ module Jasmine::Headless def find_dependency(file) search_paths.each do |dir| - if file[%r{\.(js|css|coffee)$}] + if file[EXTENSION_FILTER] if File.file?(path = File.join(dir, file)) return [ File.expand_path(path), File.expand_path(dir) ] end @@ -133,7 +135,11 @@ module Jasmine::Headless alert_time = nil end - Jasmine::Headless::TestFile.new(file).to_html + search_paths.collect do |path| + if file[path] + Jasmine::Headless::TestFile.new(file, path).to_html + end + end.compact.first }.flatten.compact.reject(&:empty?) end @@ -194,7 +200,7 @@ module Jasmine::Headless end def expanded_dir(path) - Dir[path].collect { |file| File.expand_path(file) } + Dir[path].collect { |file| File.expand_path(file) }.find_all { |path| File.file?(path) && path[EXTENSION_FILTER] } end def add_file(file, source_root) diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index 7d8ee10..52167f8 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -1,5 +1,12 @@ require 'rainbow' -require 'sprockets/directive_processor' +require 'sprockets' + +%w{haml-sprockets}.each do |library| + begin + require library + rescue LoadError + end +end module Jasmine::Headless class TestFile @@ -38,6 +45,13 @@ module Jasmine::Headless %{} when '.css' %{} + when '.jst' + to_jst(read) + else + case path + when %r{\.jst(\..*)$} + to_jst(Sprockets.engines($1).new { read }.evaluate(self, {})) + end end end @@ -53,6 +67,19 @@ module Jasmine::Headless [ type, name ] end end + + def logical_path + path.gsub(%r{^#{source_root}/}, '').gsub(%r{\..+$}, '') + end + + private + def to_jst(data) + %{} + end + + def read + File.read(path) + end end end diff --git a/spec/bin/jasmine-headless-webkit_spec.rb b/spec/bin/jasmine-headless-webkit_spec.rb index e8fcbf9..cd665d7 100644 --- a/spec/bin/jasmine-headless-webkit_spec.rb +++ b/spec/bin/jasmine-headless-webkit_spec.rb @@ -146,6 +146,8 @@ describe "jasmine-headless-webkit" do files.lines.to_a.should contain_in_order_in_file_list( 'vendor/assets/javascripts/jquery.js', + 'templates/that.jst.ejs', + 'templates/this.jst', 'assets/things/required.js', 'assets/things/code.js', 'assets/things/subcode/more_code.js', diff --git a/spec/jasmine/with_sprockets_includes/assets/things/code.js b/spec/jasmine/with_sprockets_includes/assets/things/code.js index bb2e8ae..89bab5e 100644 --- a/spec/jasmine/with_sprockets_includes/assets/things/code.js +++ b/spec/jasmine/with_sprockets_includes/assets/things/code.js @@ -1,3 +1,5 @@ +//= require 'jquery' +//= require_tree 'things/templates' //= require 'things/required' window.a = '1'; diff --git a/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs b/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs new file mode 100644 index 0000000..e69de29 diff --git a/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst b/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst new file mode 100644 index 0000000..e69de29 diff --git a/spec/jasmine/with_sprockets_includes/spec/spec_helper.js b/spec/jasmine/with_sprockets_includes/spec/spec_helper.js index 18241c9..4817b36 100644 --- a/spec/jasmine/with_sprockets_includes/spec/spec_helper.js +++ b/spec/jasmine/with_sprockets_includes/spec/spec_helper.js @@ -1,2 +1 @@ //= require 'jquery' - diff --git a/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml b/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml index f5943f8..99cd320 100644 --- a/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml +++ b/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml @@ -5,7 +5,7 @@ spec_files: - "**/*_spec.js" src_files: - - "things/**/*.js" + - "things/**/*" helpers: - "spec_helper.js" diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 4cb0742..82f36a8 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -161,46 +161,6 @@ describe Jasmine::Headless::FilesList do end end - describe '#.*files_to_html' do - include FakeFS::SpecHelpers - - before do - files_list.instance_variable_set(:@files, [ - 'test.js', - 'test.coffee', - 'test.whatever', - 'test.css' - ]) - - files_list.instance_variable_set(:@filtered_files, [ - 'test.js', - 'test.coffee' - ]) - - File.stubs(:read) - Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle).returns("i compiled") - end - - context '#files_to_html' do - it "should create the right HTML" do - files_list.files_to_html.should == [ - %{}, - %{}, - %{} - ] - end - end - - context '#filtered_files_to_html' do - it "should create the right HTML" do - files_list.filtered_files_to_html.should == [ - %{}, - %{} - ] - end - end - end - describe '#spec_file_line_numbers' do include FakeFS::SpecHelpers @@ -292,7 +252,7 @@ describe Jasmine::Headless::FilesList do end it 'should take the src dir and spec dirs' do - files_list.search_paths.should == [ Jasmine::Core.path, src_dir, spec_dir ] + files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ] end end @@ -302,7 +262,7 @@ describe Jasmine::Headless::FilesList do end it 'should add the vendor gem paths to the list' do - files_list.search_paths.should == [ Jasmine::Core.path, src_dir, spec_dir, path ] + files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir), path ] end end end diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index 0265286..14f053b 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -72,6 +72,38 @@ describe Jasmine::Headless::TestFile do end end end + + context '.jst' do + include FakeFS::SpecHelpers + + let(:path) { 'file.jst' } + let(:content) { 'content' } + + before do + File.open(path, 'wb') { |fh| fh.print content } + end + + it 'should use the JST template processor to get the processed file' do + subject.should include('JST["file"]') + subject.should include(content) + end + end + + context '.jst.*' do + include FakeFS::SpecHelpers + + let(:path) { 'file.jst.ejs' } + let(:content) { 'content' } + + before do + File.open(path, 'wb') { |fh| fh.print content } + end + + it 'should use the JST template processor to get the processed file' do + subject.should include('JST["file"]') + subject.should include(content) + end + end end describe '#dependencies' do From e04d692d26a6890cb47305545be8152adf9517ac Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 19 Nov 2011 12:45:40 -0500 Subject: [PATCH 06/20] reowrk a bunch of the tilt stuff, only use extensions we know --- lib/jasmine/headless.rb | 23 ++++- lib/jasmine/headless/coffee_template.rb | 29 ++++++ lib/jasmine/headless/files_list.rb | 2 +- lib/jasmine/headless/test_file.rb | 63 +++++-------- .../jasmine/headless/coffee_template_spec.rb | 55 ++++++++++++ spec/lib/jasmine/headless/test_file_spec.rb | 90 ++++++------------- spec/spec_helper.rb | 1 + 7 files changed, 153 insertions(+), 110 deletions(-) create mode 100644 lib/jasmine/headless/coffee_template.rb create mode 100644 spec/lib/jasmine/headless/coffee_template_spec.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 58c11af..45ddf9d 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -1,5 +1,5 @@ require 'pathname' -require 'sprockets/engines' +require 'sprockets' module Jasmine::Headless autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' @@ -14,6 +14,8 @@ module Jasmine::Headless autoload :TemplateWriter, 'jasmine/headless/template_writer' + autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' + autoload :Report, 'jasmine/headless/report' autoload :ReportMessage, 'jasmine/headless/report_message' @@ -25,4 +27,21 @@ module Jasmine::Headless end require 'jasmine/headless/errors' -Sprockets::Engines + +# register haml-sprockets if it's available... +%w{haml-sprockets}.each do |library| + begin + require library + rescue LoadError + end +end + +# ...and unregister ones we don't want/need +module Sprockets + %w{less sass scss erb str}.each do |extension| + @engines.delete(".#{extension}") + end + + register_engine '.coffee', Jasmine::Headless::CoffeeTemplate +end + diff --git a/lib/jasmine/headless/coffee_template.rb b/lib/jasmine/headless/coffee_template.rb new file mode 100644 index 0000000..fbc8c95 --- /dev/null +++ b/lib/jasmine/headless/coffee_template.rb @@ -0,0 +1,29 @@ +require 'tilt/template' + +module Jasmine::Headless + class CoffeeTemplate < Tilt::Template + def prepare ; end + + def evaluate(scope, locals, &block) + 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/files_list.rb b/lib/jasmine/headless/files_list.rb index e577fef..b1311db 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -92,7 +92,7 @@ module Jasmine::Headless TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) } end - EXTENSION_FILTER = %r{\.(js|css|coffee|jst.*)$} + EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} def add_dependency(type, file, source_root) case type diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index 52167f8..528acd4 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -1,13 +1,6 @@ require 'rainbow' require 'sprockets' -%w{haml-sprockets}.each do |library| - begin - require library - rescue LoadError - end -end - module Jasmine::Headless class TestFile attr_reader :path, :source_root @@ -21,38 +14,7 @@ module Jasmine::Headless end def to_html - case File.extname(path) - when '.coffee' - begin - cache = Jasmine::Headless::CoffeeScriptCache.new(path) - source = cache.handle - if cache.cached? - %{ - } - else - %{} - end - rescue CoffeeScript::CompilationError => ne - puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), path.color(:yellow), ne.message.dup.to_s.color(:white) ] - raise ne - rescue StandardError => e - puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), path.color(:yellow) ] - raise e - end - when '.js' - %{} - when '.css' - %{} - when '.jst' - to_jst(read) - else - case path - when %r{\.jst(\..*)$} - to_jst(Sprockets.engines($1).new { read }.evaluate(self, {})) - end - end + process_data_by_filename(path) end def dependencies @@ -73,13 +35,28 @@ module Jasmine::Headless end private - def to_jst(data) - %{} - end - def read File.read(path) end + + def process_data_by_filename(path, data = nil) + case extension = File.extname(path) + when '' + data || '' + when '.js' + data || %{} + when '.css' + data || %{} + else + if engine = Sprockets.engines(extension) + data = engine.new(path) { data || read }.render(self) + + process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data) + else + data || '' + end + end + end end end diff --git a/spec/lib/jasmine/headless/coffee_template_spec.rb b/spec/lib/jasmine/headless/coffee_template_spec.rb new file mode 100644 index 0000000..f07e769 --- /dev/null +++ b/spec/lib/jasmine/headless/coffee_template_spec.rb @@ -0,0 +1,55 @@ +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/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index 14f053b..b19ed21 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -25,83 +25,45 @@ describe Jasmine::Headless::TestFile do it { should == %{} } end - context '.coffee' do - let(:path) { 'path.coffee' } - - 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 - - context '.jst' do + context 'with tilt template' do include FakeFS::SpecHelpers - let(:path) { 'file.jst' } let(:content) { 'content' } before do File.open(path, 'wb') { |fh| fh.print content } end - it 'should use the JST template processor to get the processed file' do - subject.should include('JST["file"]') - subject.should include(content) + let(:klass) do + Class.new(Tilt::Template) do + def prepare ; end + + def evaluate(scope, locals, &block) + "#{file} made it #{data}" + end + end end - end - - context '.jst.*' do - include FakeFS::SpecHelpers - - let(:path) { 'file.jst.ejs' } - let(:content) { 'content' } before do - File.open(path, 'wb') { |fh| fh.print content } + Sprockets.stubs(:engines).with('.tilt').returns(klass) end - it 'should use the JST template processor to get the processed file' do - subject.should include('JST["file"]') - subject.should include(content) + context '.tilt' do + let(:path) { 'path.tilt' } + + it { should == "#{path} made it #{content}" } + end + + context '.tilt.tilt' do + let(:path) { 'path.tilt.tilt' } + + it { should == "path.tilt made it #{path} made it #{content}" } + end + + context '.js.tilt' do + let(:path) { 'path.js.tilt' } + + it { should == "#{path} made it #{content}" } end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d73f675..a95be66 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ require 'fakefs/spec_helpers' RSpec.configure do |c| c.mock_with :mocha + c.backtrace_clean_patterns = [] c.before(:each) do Jasmine::Headless::CacheableAction.enabled = false From 61c8ed8828a88e6d434884c8e03798bab72afb7d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 19 Nov 2011 19:15:38 -0500 Subject: [PATCH 07/20] more sprockets improvements --- bin/jasmine-headless-webkit | 26 +------ jasmine-headless-webkit.gemspec | 10 +-- lib/jasmine/headless.rb | 2 + lib/jasmine/headless/command_line.rb | 29 ++++++++ lib/jasmine/headless/files_list.rb | 37 +++++----- lib/jasmine/headless/task.rb | 2 + lib/jasmine/headless/test_file.rb | 1 + .../assets/things/templates/that.jst.ejs | 1 + .../assets/things/templates/this.jst | 2 + spec/lib/jasmine/headless/files_list_spec.rb | 67 ++++++++++++++----- spec/lib/jasmine/headless/test_file_spec.rb | 21 ++++-- .../javascripts/headless_reporter_result.js | 19 ++++-- vendor/assets/javascripts/intense.js | 3 + .../assets/javascripts/jasmine-extensions.js | 3 + .../jasmine.HeadlessConsoleReporter.js | 26 ++++--- vendor/assets/javascripts/prolog.js | 8 ++- 16 files changed, 170 insertions(+), 87 deletions(-) create mode 100644 lib/jasmine/headless/command_line.rb diff --git a/bin/jasmine-headless-webkit b/bin/jasmine-headless-webkit index 3796599..530cdf1 100755 --- a/bin/jasmine-headless-webkit +++ b/bin/jasmine-headless-webkit @@ -2,31 +2,9 @@ require 'rubygems' -def gem_dir - File.expand_path('../..', __FILE__) -end - -$:.unshift(File.join(gem_dir, 'lib')) +$: << File.expand_path('../../lib', __FILE__) require 'jasmine-headless-webkit' -require 'coffee-script' -require 'rainbow' -begin - options = Jasmine::Headless::Options.from_command_line - runner = Jasmine::Headless::Runner.new(options) - - if options[:do_list] - files_list = Jasmine::Headless::FilesList.new(:config => runner.jasmine_config) - files_list.files.each { |file| puts file } - else - exit runner.run - end -rescue CoffeeScript::CompilationError - exit 1 -rescue StandardError => e - $stderr.puts "[%s] %s (%s)" % [ "jasmine-headless-webkit".color(:red), e.message.color(:white), e.class.name.color(:yellow) ] - $stderr.puts e.backtrace.collect { |line| " #{line}" }.join("\n") - exit 1 -end +Jasmine::Headless::CommandLine.run! diff --git a/jasmine-headless-webkit.gemspec b/jasmine-headless-webkit.gemspec index e7be2ec..2a6c27e 100644 --- a/jasmine-headless-webkit.gemspec +++ b/jasmine-headless-webkit.gemspec @@ -20,9 +20,9 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - s.add_dependency 'jasmine-core', '~>1.1.beta' - s.add_dependency 'coffee-script', '>= 2.2' - s.add_dependency 'rainbow' - s.add_dependency 'multi_json' - s.add_dependency 'sprockets', '>= 2.0' + s.add_runtime_dependency 'jasmine-core', '~> 1.1' + s.add_runtime_dependency 'coffee-script' + s.add_runtime_dependency 'rainbow' + s.add_runtime_dependency 'multi_json' + s.add_runtime_dependency 'sprockets', '~> 2' end diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 45ddf9d..bb88225 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -2,6 +2,8 @@ require 'pathname' require 'sprockets' module Jasmine::Headless + autoload :CommandLine, 'jasmine/headless/command_line' + autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' autoload :SpecFileAnalyzer, 'jasmine/headless/spec_file_analyzer' autoload :CacheableAction, 'jasmine/headless/cacheable_action' diff --git a/lib/jasmine/headless/command_line.rb b/lib/jasmine/headless/command_line.rb new file mode 100644 index 0000000..a746247 --- /dev/null +++ b/lib/jasmine/headless/command_line.rb @@ -0,0 +1,29 @@ +module Jasmine::Headless + class CommandLine + class << self + def run! + require 'coffee-script' + require 'rainbow' + + begin + options = Options.from_command_line + runner = Runner.new(options) + + if options[:do_list] + files_list = FilesList.new(:config => runner.jasmine_config) + files_list.files.each { |file| puts file } + else + exit runner.run + end + rescue CoffeeScript::CompilationError + exit 1 + rescue StandardError => e + $stderr.puts "[%s] %s (%s)" % [ "jasmine-headless-webkit".color(:red), e.message.color(:white), e.class.name.color(:yellow) ] + $stderr.puts e.backtrace.collect { |line| " #{line}" }.join("\n") + exit 1 + end + end + end + end +end + diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index b1311db..4d45d51 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -57,7 +57,14 @@ module Jasmine::Headless end def search_paths - @search_paths ||= [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ] + self.class.vendor_asset_paths + return @search_paths if @search_paths + + @search_paths = [ Jasmine::Core.path ] + @search_paths += [ src_dir ].flatten.collect { |dir| File.expand_path(dir) } + @search_paths << File.expand_path(spec_dir) + @search_paths += self.class.vendor_asset_paths + + @search_paths end def has_spec_outside_scope? @@ -111,14 +118,13 @@ module Jasmine::Headless def find_dependency(file) search_paths.each do |dir| - if file[EXTENSION_FILTER] - if File.file?(path = File.join(dir, file)) - return [ File.expand_path(path), File.expand_path(dir) ] - end - else - if path = Dir[File.join(dir, "#{file}.*")].first - return [ File.expand_path(path), File.expand_path(dir) ] - end + Dir[File.join(dir, "#{file}*")].find_all { |path| File.file?(path) }.each do |path| + root = path.gsub(%r{^#{dir}/}, '') + + ok = (root == file) + ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[EXTENSION_FILTER] } + + return [ File.expand_path(path), File.expand_path(dir) ] if ok end end @@ -176,15 +182,16 @@ module Jasmine::Headless def add_files(searches, type) searches.each do |search| - dir = @config[SEARCH_ROOTS[type]] || Dir.pwd - dir = File.expand_path(dir) + [ @config[SEARCH_ROOTS[type]] || Dir.pwd ].flatten.each do |dir| + dir = File.expand_path(dir) - path = File.expand_path(File.join(dir, search)) + path = File.expand_path(File.join(dir, search)) - found_files = expanded_dir(path) - files + found_files = expanded_dir(path) - files - found_files.each do |file| - type == 'spec_files' ? add_spec_file(file) : add_file(file, dir) + found_files.each do |file| + type == 'spec_files' ? add_spec_file(file) : add_file(file, dir) + end end end diff --git a/lib/jasmine/headless/task.rb b/lib/jasmine/headless/task.rb index d6456a2..78eec46 100644 --- a/lib/jasmine/headless/task.rb +++ b/lib/jasmine/headless/task.rb @@ -43,6 +43,8 @@ module Jasmine if Rails.respond_to?(:version) && Rails.version >= "3.1.0" desc 'Force generate static assets without an MD5 hash, all assets end with -test.' task 'assets:precompile:for_testing' => :environment do + $stderr.puts "This task is deprecated and will be removed after 2012-01-01" + Rails.application.assets.digest_class = Digest::JasmineTest Rake::Task['assets:precompile'].invoke diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index 528acd4..03fbda6 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -50,6 +50,7 @@ module Jasmine::Headless else if engine = Sprockets.engines(extension) data = engine.new(path) { data || read }.render(self) + data = %{} if extension == '.jst' process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data) else diff --git a/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs b/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs index e69de29..30f1577 100644 --- a/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs +++ b/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs @@ -0,0 +1 @@ +'hello' diff --git a/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst b/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst index e69de29..9424e78 100644 --- a/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst +++ b/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst @@ -0,0 +1,2 @@ +'hello' + diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 82f36a8..7efc57d 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -87,6 +87,19 @@ describe Jasmine::Headless::FilesList do it_should_behave_like :reading_data end + + context 'with multidimensional src dir' do + let(:config) { { + 'src_dir' => [ src_dir ], + 'spec_dir' => spec_dir, + 'src_files' => [ [ 'js/first_file.js', 'js/*.js' ] ], + 'spec_files' => [ '*_spec.js' ], + 'helpers' => [ 'helper/*.js' ], + 'stylesheets' => [ 'stylesheet/*.css' ] + } } + + it_should_behave_like :reading_data + end end context 'with filtered specs' do @@ -246,11 +259,11 @@ describe Jasmine::Headless::FilesList do let(:spec_dir) { 'spec dir' } let(:path) { 'path' } - context 'no vendored gem paths' do - before do - Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([]) - end + before do + Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([]) + end + context 'no vendored gem paths' do it 'should take the src dir and spec dirs' do files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ] end @@ -265,6 +278,17 @@ describe Jasmine::Headless::FilesList do files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir), path ] end end + + context 'src_dir is an array' do + let(:dir_1) { 'dir 1' } + let(:dir_2) { 'dir 2' } + + let(:src_dir) { [ dir_1, dir_2 ] } + + it 'should take the src dir and spec dirs' do + files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(dir_1), File.expand_path(dir_2), File.expand_path(spec_dir) ] + end + end end describe '.vendor_asset_paths' do @@ -293,31 +317,38 @@ describe Jasmine::Headless::FilesList do include FakeFS::SpecHelpers let(:dir) { File.expand_path('dir') } + let(:filename) { 'file' } let(:file) { "#{filename}.js" } before do - files_list.stubs(:search_paths).returns([ dir ]) - FileUtils.mkdir_p dir + + %w{file.sub.js file.js.coffee}.each do |file| + File.open(File.join(dir, file), 'wb') + end + + files_list.stubs(:search_paths).returns([ dir ]) end - context 'does not exist' do - it 'should not be found' do - files_list.find_dependency(file).should be_false - end + subject { files_list.find_dependency(search) } + + context 'bad' do + let(:search) { 'bad' } + + it { should be_false } end - context 'exists' do - let(:path) { File.join(dir, file) } + context 'file' do + let(:search) { 'file' } - before do - File.open(path, 'wb') - end + it { should == [ File.join(dir, 'file.js.coffee'), dir ] } + end - it 'should be found' do - files_list.find_dependency(filename).should == [ File.expand_path(path), dir ] - end + context 'file.sub' do + let(:search) { 'file.sub' } + + it { should == [ File.join(dir, 'file.sub.js'), dir ] } end end end diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index b19ed21..6302fb2 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -44,26 +44,37 @@ describe Jasmine::Headless::TestFile do end end + let(:other_klass) do + Class.new(Tilt::Template) do + def prepare ; end + + def evaluate(scope, locals, &block) + data + end + end + end + before do Sprockets.stubs(:engines).with('.tilt').returns(klass) + Sprockets.stubs(:engines).with('.jst').returns(other_klass) end context '.tilt' do let(:path) { 'path.tilt' } - it { should == "#{path} made it #{content}" } + it { should == %{#{path} made it #{content}} } end context '.tilt.tilt' do let(:path) { 'path.tilt.tilt' } - it { should == "path.tilt made it #{path} made it #{content}" } + it { should == %{path.tilt made it #{path} made it #{content}} } end - context '.js.tilt' do - let(:path) { 'path.js.tilt' } + context '.jst.tilt' do + let(:path) { 'path.jst.tilt' } - it { should == "#{path} made it #{content}" } + it { should == %{} } end end end diff --git a/vendor/assets/javascripts/headless_reporter_result.js b/vendor/assets/javascripts/headless_reporter_result.js index 14ba347..92ec7b9 100644 --- a/vendor/assets/javascripts/headless_reporter_result.js +++ b/vendor/assets/javascripts/headless_reporter_result.js @@ -1,13 +1,16 @@ -(function() { + window.HeadlessReporterResult = (function() { + function HeadlessReporterResult(name, splitName) { this.name = name; this.splitName = splitName; this.results = []; } + HeadlessReporterResult.prototype.addResult = function(message) { return this.results.push(message); }; + HeadlessReporterResult.prototype.print = function() { var bestChoice, output, result, _i, _len, _ref, _results; output = this.name.foreground('red'); @@ -25,10 +28,15 @@ output += (" (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")").foreground('red').bright(); } JHW.stdout.puts(" " + output); - _results.push(result.line != null ? JHW.stdout.puts((" " + result.line).foreground('yellow')) : void 0); + if (result.line != null) { + _results.push(JHW.stdout.puts((" " + result.line).foreground('yellow'))); + } else { + _results.push(void 0); + } } return _results; }; + HeadlessReporterResult.findSpecLine = function(splitName) { var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref; bestChoice = { @@ -49,9 +57,7 @@ for (_i = 0, _len = newLineNumberInfo.length; _i < _len; _i++) { line = newLineNumberInfo[_i]; lastLine = line; - if (line > lineNumber) { - break; - } + if (line > lineNumber) break; } lineNumber = lastLine; } @@ -67,6 +73,7 @@ } return bestChoice; }; + return HeadlessReporterResult; + })(); -}).call(this); diff --git a/vendor/assets/javascripts/intense.js b/vendor/assets/javascripts/intense.js index 2e5fffd..6d885a1 100644 --- a/vendor/assets/javascripts/intense.js +++ b/vendor/assets/javascripts/intense.js @@ -1,5 +1,6 @@ (function() { var code, method, _ref; + window.Intense = { colors: { black: 0, @@ -29,9 +30,11 @@ }, useColors: true }; + _ref = Intense.methods; for (method in _ref) { code = _ref[method]; String.prototype[method] = code; } + }).call(this); diff --git a/vendor/assets/javascripts/jasmine-extensions.js b/vendor/assets/javascripts/jasmine-extensions.js index 6464dd3..c9b4a93 100644 --- a/vendor/assets/javascripts/jasmine-extensions.js +++ b/vendor/assets/javascripts/jasmine-extensions.js @@ -1,8 +1,10 @@ (function() { var getSplitName, pauseAndRun; + if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not laoded!"); } + if (window.JHW) { getSplitName = function(parts) { parts.push(String(this.description).replace(/[\n\r]/g, ' ')); @@ -98,4 +100,5 @@ }; } } + }).call(this); diff --git a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js index be8f10b..ea1d365 100644 --- a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js +++ b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js @@ -1,19 +1,20 @@ -(function() { + if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not loaded!"); } + jasmine.HeadlessConsoleReporter = (function() { + function HeadlessConsoleReporter(callback) { this.callback = callback != null ? callback : null; this.results = []; this.failedCount = 0; this.length = 0; } + HeadlessConsoleReporter.prototype.reportRunnerResults = function(runner) { var output, result, resultLine, runtime, _i, _len, _ref; - if (this.hasError()) { - return; - } + if (this.hasError()) return; runtime = (new Date() - this.startTime) / 1000.0; JHW.stdout.print("\n"); resultLine = this._formatResultLine(runtime); @@ -30,20 +31,18 @@ result = _ref[_i]; result.print(); } - if (window.JHW) { - window.onbeforeunload = null; - } + if (window.JHW) window.onbeforeunload = null; return JHW.finishSuite(); }; + HeadlessConsoleReporter.prototype.reportRunnerStarting = function(runner) { this.startTime = new Date(); return JHW.stdout.puts("\nRunning Jasmine specs...".bright()); }; + HeadlessConsoleReporter.prototype.reportSpecResults = function(spec) { var failureResult, foundLine, result, results, testCount, _i, _len, _ref; - if (this.hasError()) { - return; - } + if (this.hasError()) return; JHW.ping(); results = spec.results(); this.length++; @@ -70,16 +69,20 @@ return this.results.push(failureResult); } }; + HeadlessConsoleReporter.prototype.reportSpecStarting = function(spec) { if (this.hasError()) { spec.finish(); return spec.suite.finish(); } }; + HeadlessConsoleReporter.prototype.reportSuiteResults = function(suite) {}; + HeadlessConsoleReporter.prototype.hasError = function() { return JHW._hasErrors; }; + HeadlessConsoleReporter.prototype._formatResultLine = function(runtime) { var line; line = []; @@ -91,6 +94,7 @@ line.push((runtime === 1.0 ? "sec" : "secs") + '.'); return line.join(' '); }; + return HeadlessConsoleReporter; + })(); -}).call(this); diff --git a/vendor/assets/javascripts/prolog.js b/vendor/assets/javascripts/prolog.js index 2fd358b..eb2b69f 100644 --- a/vendor/assets/javascripts/prolog.js +++ b/vendor/assets/javascripts/prolog.js @@ -1,5 +1,6 @@ (function() { var createHandle, handle, _i, _len, _ref; + if (window.JHW) { window.console = { log: function(data) { @@ -40,9 +41,7 @@ e = e || window.event; JHW.hasError(); JHW.stdout.puts('The code tried to leave the test page. Check for unhandled form submits and link clicks.'); - if (e) { - e.returnValue = 'string'; - } + if (e) e.returnValue = 'string'; return 'string'; }; window.confirm = function(message) { @@ -84,6 +83,9 @@ return JHW.stdout.puts(msg); }; } + window.CoffeeScriptToFilename = {}; + window.CSTF = window.CoffeeScriptToFilename; + }).call(this); From ca8c655f0032649d1487648d77356282c49f39df Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 20 Nov 2011 11:56:25 -0500 Subject: [PATCH 08/20] more sprockets improvements --- lib/jasmine/headless/files_list.rb | 15 +++++++++++++-- lib/jasmine/headless/test_file.rb | 2 ++ .../assets/things/code.js | 2 +- spec/lib/jasmine/headless/test_file_spec.rb | 16 ++++++++++++++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 4d45d51..8b4f51b 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -1,6 +1,7 @@ require 'jasmine-core' require 'time' require 'multi_json' +require 'set' module Jasmine::Headless class FilesList @@ -35,6 +36,7 @@ module Jasmine::Headless @files = [] @filtered_files = [] + @checked_dependency = Set.new DEFAULT_FILES.each { |file| add_dependency('require', file, nil) } @@ -96,7 +98,12 @@ module Jasmine::Headless end def add_dependencies(file, source_root) - TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) } + TestFile.new(file, source_root).dependencies.each do |type, name| + if !@checked_dependency.include?(name) + @checked_dependency << name + add_dependency(type, name, source_root) + end + end end EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} @@ -124,7 +131,11 @@ module Jasmine::Headless ok = (root == file) ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[EXTENSION_FILTER] } - return [ File.expand_path(path), File.expand_path(dir) ] if ok + expanded_path = File.expand_path(path) + + if ok + return [ expanded_path, File.expand_path(dir) ] + end end end diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index 03fbda6..6e3bbb9 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -24,6 +24,8 @@ module Jasmine::Headless @dependencies = processor.directives.collect do |_, type, name| if name[%r{^\.}] name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') + else + raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree' end [ type, name ] diff --git a/spec/jasmine/with_sprockets_includes/assets/things/code.js b/spec/jasmine/with_sprockets_includes/assets/things/code.js index 89bab5e..d437925 100644 --- a/spec/jasmine/with_sprockets_includes/assets/things/code.js +++ b/spec/jasmine/with_sprockets_includes/assets/things/code.js @@ -1,5 +1,5 @@ //= require 'jquery' -//= require_tree 'things/templates' +//= require_tree './templates' //= require 'things/required' window.a = '1'; diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index 6302fb2..9c47a20 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -82,9 +82,11 @@ describe Jasmine::Headless::TestFile do describe '#dependencies' do include FakeFS::SpecHelpers + let(:directive) { 'require' } + before do FileUtils.mkdir_p File.dirname(path) - File.open(path, 'wb') { |fh| fh.print "//= require '#{req}'\njavascript" } + File.open(path, 'wb') { |fh| fh.print "//= #{directive} '#{req}'\njavascript" } end context 'absolute' do @@ -92,7 +94,17 @@ describe Jasmine::Headless::TestFile do subject { file.dependencies } - it { should == [ [ 'require', req ] ] } + context 'require' do + it { should == [ [ 'require', req ] ] } + end + + context 'require_tree' do + let(:directive) { 'require_tree' } + + it 'should raise an error' do + expect { subject }.to raise_error(Sprockets::ArgumentError, /relative/) + end + end end context 'relative' do From d214674620350d4378020fc916666247b3336564 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 20 Nov 2011 13:35:30 -0500 Subject: [PATCH 09/20] fixing things up and cleaning more things --- lib/jasmine/headless.rb | 19 -------- lib/jasmine/headless/files_list.rb | 42 ++++++++++------ lib/jasmine/headless/runner.rb | 30 ++++++++++-- spec/lib/jasmine/headless/files_list_spec.rb | 50 ++++++++++++++++---- spec/spec_helper.rb | 1 + 5 files changed, 97 insertions(+), 45 deletions(-) diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index bb88225..6947c0d 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -1,5 +1,4 @@ require 'pathname' -require 'sprockets' module Jasmine::Headless autoload :CommandLine, 'jasmine/headless/command_line' @@ -29,21 +28,3 @@ module Jasmine::Headless end require 'jasmine/headless/errors' - -# register haml-sprockets if it's available... -%w{haml-sprockets}.each do |library| - begin - require library - rescue LoadError - end -end - -# ...and unregister ones we don't want/need -module Sprockets - %w{less sass scss erb str}.each do |extension| - @engines.delete(".#{extension}") - end - - register_engine '.coffee', Jasmine::Headless::CoffeeTemplate -end - diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 8b4f51b..f81896a 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -2,6 +2,7 @@ require 'jasmine-core' require 'time' require 'multi_json' require 'set' +require 'sprockets' module Jasmine::Headless class FilesList @@ -99,26 +100,35 @@ module Jasmine::Headless def add_dependencies(file, source_root) TestFile.new(file, source_root).dependencies.each do |type, name| - if !@checked_dependency.include?(name) - @checked_dependency << name - add_dependency(type, name, source_root) - end + add_dependency(type, name, source_root) end end - EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} + def extension_filter + %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} + end def add_dependency(type, file, source_root) - case type + files = case type when 'require' - if result = find_dependency(file) - add_file(*result) + if !@checked_dependency.include?(file) + @checked_dependency << file + + [ file ] + else + [] end when 'require_tree' - Dir[File.join(source_root, file, '**/*')].find_all { |path| File.file?(path) && path[EXTENSION_FILTER] }.sort.each do |tree_path| - if result = find_dependency(tree_path.gsub(%r{^#{source_root}/}, '')) - add_file(*result) - end + Dir[File.join(source_root, file, '**/*')].find_all { |path| + File.file?(path) && path[extension_filter] + }.sort.collect { |path| path.gsub(%r{^#{source_root}/}, '') } + else + [] + end + + files.each do |file| + if result = find_dependency(file) + add_file(result[0], result[1], false) end end end @@ -129,7 +139,7 @@ module Jasmine::Headless root = path.gsub(%r{^#{dir}/}, '') ok = (root == file) - ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[EXTENSION_FILTER] } + ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[extension_filter] } expanded_path = File.expand_path(path) @@ -218,10 +228,12 @@ module Jasmine::Headless end def expanded_dir(path) - Dir[path].collect { |file| File.expand_path(file) }.find_all { |path| File.file?(path) && path[EXTENSION_FILTER] } + Dir[path].collect { |file| File.expand_path(file) }.find_all { |path| File.file?(path) && path[extension_filter] } end - def add_file(file, source_root) + def add_file(file, source_root, clear_dependencies = true) + @checked_dependency = Set.new if clear_dependencies + add_dependencies(file, source_root) @files << file if !@files.include?(file) diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index d8c14c2..c39f34d 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -4,6 +4,8 @@ require 'coffee-script' require 'rainbow' require 'yaml' +require 'sprockets' + module Jasmine module Headless @@ -23,9 +25,30 @@ module Jasmine attr_reader :options - def self.run(options = {}) - options = Options.new(options) if !options.kind_of?(Options) - new(options).run + class << self + def reset! + # register haml-sprockets if it's available... + %w{haml-sprockets}.each do |library| + begin + require library + rescue LoadError + end + end + + # ...and unregister ones we don't want/need + Sprockets.instance_eval do + %w{less sass scss erb str}.each do |extension| + @engines.delete(".#{extension}") + end + + register_engine '.coffee', Jasmine::Headless::CoffeeTemplate + end + end + + def run(options = {}) + options = Options.new(options) if !options.kind_of?(Options) + new(options).run + end end def initialize(options) @@ -67,6 +90,7 @@ module Jasmine Jasmine::Headless::CacheableAction.enabled = @options[:enable_cache] Jasmine::Headless::FilesList.reset! + self.class.reset! files_list = Jasmine::Headless::FilesList.new( :config => jasmine_config, diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 7efc57d..50bffcf 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -199,9 +199,12 @@ describe Jasmine::Headless::FilesList do let(:other_file) { 'other' } let(:path) { 'path' } + let(:set) { Set.new } + before do - files_list.stubs(:find_dependency).with(file).returns(path) + files_list.stubs(:find_dependency).with(file).returns([ path, nil ]) files_list.stubs(:find_dependency).with(other_file).returns(false) + files_list.instance_variable_set(:@checked_dependency, set) end context 'not found' do @@ -211,16 +214,20 @@ describe Jasmine::Headless::FilesList do it 'should do nothing' do files_list.add_dependency('', other_file, nil) + set.should be_empty end end context 'require' do - before do - files_list.expects(:add_file).with(path, nil) - end + context 'not already added' do + before do + files_list.expects(:add_file).with(path, nil, false) + end - it 'should add the file to the front' do - files_list.add_dependency('require', file, nil) + it 'should add the file to the front' do + files_list.add_dependency('require', file, nil) + set.should include(file) + end end end @@ -235,14 +242,15 @@ describe Jasmine::Headless::FilesList do File.open(path, 'wb') if path[%r{\.(js|css|coffee)$}] - files_list.expects(:find_dependency).with(path).returns(other_file) - files_list.expects(:add_file).with(other_file, nil) + files_list.expects(:find_dependency).with(path).returns([ other_file, nil ]) + files_list.expects(:add_file).with(other_file, nil, false) end end end it 'should add the file to the front' do files_list.add_dependency('require_tree', '.', File.expand_path('.')) + set.should be_empty end end end @@ -351,5 +359,31 @@ describe Jasmine::Headless::FilesList do it { should == [ File.join(dir, 'file.sub.js'), dir ] } end end + + describe '#add_file' do + let(:set) { Set.new([ :one ]) } + + before do + files_list.instance_variable_set(:@checked_dependency, set) + + files_list.stubs(:add_dependencies) + end + + context 'clear dependencies' do + it 'should clear dependency checking' do + files_list.send(:add_file, 'file', 'root') + + files_list.instance_variable_get(:@checked_dependency).should == Set.new + end + end + + context 'do not clear dependencies' do + it 'should clear dependency checking' do + files_list.send(:add_file, 'file', 'root', false) + + files_list.instance_variable_get(:@checked_dependency).should == set + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a95be66..7a0d1a5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,7 @@ RSpec.configure do |c| c.before(:each) do Jasmine::Headless::CacheableAction.enabled = false Jasmine::Headless::FilesList.reset! + Jasmine::Headless::Runner.reset! end end From fde8bc3b7bd3c11d923e1618f47febf9b01f8530 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 21 Nov 2011 10:32:49 -0500 Subject: [PATCH 10/20] a ton of changes --- lib/jasmine/headless.rb | 3 +- lib/jasmine/headless/command_line.rb | 2 + lib/jasmine/headless/files_list.rb | 212 ++++++++--------- lib/jasmine/headless/path_searcher.rb | 34 +++ .../{test_file.rb => required_file.rb} | 52 ++++- lib/jasmine/headless/runner.rb | 22 -- .../assets/things/subcode/more_code.js | 2 +- .../spec/things/code_spec.js | 2 +- spec/lib/jasmine/headless/files_list_spec.rb | 178 ++++++--------- .../jasmine/headless/path_searcher_spec.rb | 40 ++++ .../jasmine/headless/required_file_spec.rb | 213 ++++++++++++++++++ .../jasmine/headless/template_writer_spec.rb | 8 +- spec/lib/jasmine/headless/test_file_spec.rb | 130 ----------- spec/spec_helper.rb | 11 +- 14 files changed, 515 insertions(+), 394 deletions(-) create mode 100644 lib/jasmine/headless/path_searcher.rb rename lib/jasmine/headless/{test_file.rb => required_file.rb} (51%) create mode 100644 spec/lib/jasmine/headless/path_searcher_spec.rb create mode 100644 spec/lib/jasmine/headless/required_file_spec.rb delete mode 100644 spec/lib/jasmine/headless/test_file_spec.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 6947c0d..5675b3f 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -11,7 +11,8 @@ module Jasmine::Headless autoload :Options, 'jasmine/headless/options' autoload :Task, 'jasmine/headless/task' autoload :FilesList, 'jasmine/headless/files_list' - autoload :TestFile, 'jasmine/headless/test_file' + autoload :RequiredFile, 'jasmine/headless/required_file' + autoload :PathSearcher, 'jasmine/headless/path_searcher' autoload :TemplateWriter, 'jasmine/headless/template_writer' diff --git a/lib/jasmine/headless/command_line.rb b/lib/jasmine/headless/command_line.rb index a746247..94e1362 100644 --- a/lib/jasmine/headless/command_line.rb +++ b/lib/jasmine/headless/command_line.rb @@ -5,6 +5,8 @@ module Jasmine::Headless require 'coffee-script' require 'rainbow' + FilesList.reset! + begin options = Options.from_command_line runner = Runner.new(options) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index f81896a..7291154 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -3,6 +3,7 @@ require 'time' require 'multi_json' require 'set' require 'sprockets' +require 'sprockets/engines' module Jasmine::Headless class FilesList @@ -25,53 +26,83 @@ module Jasmine::Headless def reset! @vendor_asset_paths = nil + + # register haml-sprockets if it's available... + %w{haml-sprockets}.each do |library| + begin + require library + rescue LoadError + end + end + + # ...and unregister ones we don't want/need + Sprockets.instance_eval do + %w{less sass scss erb str}.each do |extension| + @engines.delete(".#{extension}") + end + + register_engine '.coffee', Jasmine::Headless::CoffeeTemplate + end + end + + def default_files + %w{jasmine.js jasmine-html jasmine.css jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html} end end - DEFAULT_FILES = %w{jasmine.js jasmine-html jasmine.css jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html} - PLEASE_WAIT_IM_WORKING_TIME = 2 + attr_reader :required_files, :potential_files_to_filter + def initialize(options = {}) @options = options - @files = [] - @filtered_files = [] - @checked_dependency = Set.new + @required_files = [] + @potential_files_to_filter = [] - DEFAULT_FILES.each { |file| add_dependency('require', file, nil) } - - @spec_outside_scope = false - @spec_files = [] + self.class.default_files.each do |file| + @required_files << RequiredFile.new(*path_searcher.find(file.dup), self) + end use_config! if config? end def files - @files.to_a - end - - def filtered_files - @filtered_files.to_a + required_files.collect { |file| file.file_paths }.flatten.uniq end def spec_files - @spec_files.to_a + filter_for_requested_specs(required_files.find_all(&:spec_file?).collect(&:path)) + end + + def filtered_files + filter_for_requested_specs(files) end def search_paths return @search_paths if @search_paths @search_paths = [ Jasmine::Core.path ] - @search_paths += [ src_dir ].flatten.collect { |dir| File.expand_path(dir) } - @search_paths << File.expand_path(spec_dir) + @search_paths += src_dir.collect { |dir| File.expand_path(dir) } + @search_paths += spec_dir.collect { |dir| File.expand_path(dir) } @search_paths += self.class.vendor_asset_paths @search_paths end + def path_searcher + @path_searcher ||= PathSearcher.new(self) + end + def has_spec_outside_scope? - @spec_outside_scope + if is_outside_scope = !spec_filter.empty? + is_outside_scope = spec_dir.any? do |dir| + spec_file_searches.any? do |search| + !spec_files.any? { |file| File.fnmatch?(File.join(dir, search), file) } + end + end + end + is_outside_scope end def filtered? @@ -87,7 +118,7 @@ module Jasmine::Headless end def spec_file_line_numbers - @spec_file_line_numbers ||= Hash[@spec_files.collect { |file| + @spec_file_line_numbers ||= Hash[spec_files.collect { |file| if File.exist?(file) if !(lines = Jasmine::Headless::SpecFileAnalyzer.for(file)).empty? [ file, lines ] @@ -98,76 +129,22 @@ module Jasmine::Headless }.compact] end - def add_dependencies(file, source_root) - TestFile.new(file, source_root).dependencies.each do |type, name| - add_dependency(type, name, source_root) - end - end - - def extension_filter - %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} - end - - def add_dependency(type, file, source_root) - files = case type - when 'require' - if !@checked_dependency.include?(file) - @checked_dependency << file - - [ file ] - else - [] - end - when 'require_tree' - Dir[File.join(source_root, file, '**/*')].find_all { |path| - File.file?(path) && path[extension_filter] - }.sort.collect { |path| path.gsub(%r{^#{source_root}/}, '') } - else - [] - end - - files.each do |file| - if result = find_dependency(file) - add_file(result[0], result[1], false) - end - end - end - - def find_dependency(file) - search_paths.each do |dir| - Dir[File.join(dir, "#{file}*")].find_all { |path| File.file?(path) }.each do |path| - root = path.gsub(%r{^#{dir}/}, '') - - ok = (root == file) - ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[extension_filter] } - - expanded_path = File.expand_path(path) - - if ok - return [ expanded_path, File.expand_path(dir) ] - end - end - end - - false - end - private def to_html(files) alert_time = Time.now + PLEASE_WAIT_IM_WORKING_TIME - files.collect { |file| + files.collect do |file| if alert_time && alert_time < Time.now puts "Rebuilding cache, please wait..." alert_time = nil end search_paths.collect do |path| - if file[path] - Jasmine::Headless::TestFile.new(file, path).to_html + if file[%r{^#{path}}] + Jasmine::Headless::RequiredFile.new(file, path, self).to_html end end.compact.first - }.flatten.compact.reject(&:empty?) + end.flatten.compact.reject(&:empty?) end def spec_filter @@ -190,37 +167,45 @@ module Jasmine::Headless } def use_config! - @filtered_files = @files.dup - @config = @options[:config].dup + @searches = {} + @potential_files_to_filter = [] - %w{src_files stylesheets helpers spec_files}.each do |searches| - if data = @config[searches] - add_files(data.flatten, searches) + %w{src_files stylesheets helpers spec_files}.each do |type| + if data = @config[type] + dirs = send(SEARCH_ROOTS[type]) + + add_files(@searches[type] = data.flatten, type, dirs) end end end - def add_files(searches, type) - searches.each do |search| - [ @config[SEARCH_ROOTS[type]] || Dir.pwd ].flatten.each do |dir| - dir = File.expand_path(dir) + def add_files(patterns, type, dirs) + dirs.each do |dir| + patterns.each do |search| + search = File.expand_path(File.join(dir, search)) - path = File.expand_path(File.join(dir, search)) + Dir[search].find_all { |file| file[extension_filter] }.each do |path| + @required_files << (file = RequiredFile.new(path, dir, self)) - found_files = expanded_dir(path) - files - - found_files.each do |file| - type == 'spec_files' ? add_spec_file(file) : add_file(file, dir) + if type == 'spec_files' + file.spec_file = true + @potential_files_to_filter << path + end end end end if type == 'spec_files' - spec_filter.each do |file| - @spec_outside_scope ||= add_spec_file(file) + spec_filter.each do |path| + @required_files << (file = RequiredFile.new(path, nil, self)) + + file.spec_file = true + @potential_files_to_filter << path end end + + @required_files.uniq!(&:path) end def config? @@ -231,28 +216,12 @@ module Jasmine::Headless Dir[path].collect { |file| File.expand_path(file) }.find_all { |path| File.file?(path) && path[extension_filter] } end - def add_file(file, source_root, clear_dependencies = true) - @checked_dependency = Set.new if clear_dependencies - - add_dependencies(file, source_root) - - @files << file if !@files.include?(file) - @filtered_files << file if !@filtered_files.include?(file) + def extension_filter + %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} end - def add_spec_file(file) - add_dependencies(file, spec_dir) - - if !@files.include?(file) - @files << file if !@files.include?(file) - - if include_spec_file?(file) - @filtered_files << file if !@filtered_files.include?(file) - @spec_files << file if !@spec_files.include?(file) && spec_filter.empty? || spec_filter.include?(file) - end - - true - end + def add_file(file) + @files << file end def include_spec_file?(file) @@ -267,6 +236,10 @@ module Jasmine::Headless config_dir_or_pwd('spec_dir') end + def spec_file_searches + @searches['spec_files'] + end + def config_dir_or_pwd(dir) found_dir = Dir.pwd @@ -274,8 +247,17 @@ module Jasmine::Headless found_dir = @options[:config][dir] || found_dir end - found_dir + [ found_dir ].flatten.collect { |dir| File.expand_path(dir) } + end + + def filter_for_requested_specs(files) + files.find_all do |file| + if potential_files_to_filter.include?(file) + spec_filter.empty? || spec_filter.any? { |pattern| File.fnmatch?(pattern, file) } + else + true + end + end end end end - diff --git a/lib/jasmine/headless/path_searcher.rb b/lib/jasmine/headless/path_searcher.rb new file mode 100644 index 0000000..b4c78fa --- /dev/null +++ b/lib/jasmine/headless/path_searcher.rb @@ -0,0 +1,34 @@ +require 'sprockets' +require 'forwardable' + +module Jasmine::Headless + class PathSearcher + extend Forwardable + + def_delegators :source, :search_paths, :extension_filter + + attr_reader :source + + def initialize(source) + @source = source + end + + def find(file) + search_paths.each do |dir| + Dir[File.join(dir, "#{file}*")].find_all { |path| File.file?(path) }.each do |path| + root = path.gsub(%r{^#{dir}/}, '') + + ok = (root == file) + ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[extension_filter] } + + if ok + return [ File.expand_path(path), dir ] + end + end + end + + false + end + end +end + diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/required_file.rb similarity index 51% rename from lib/jasmine/headless/test_file.rb rename to lib/jasmine/headless/required_file.rb index 6e3bbb9..c20ab09 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/required_file.rb @@ -1,12 +1,23 @@ require 'rainbow' require 'sprockets' +require 'forwardable' module Jasmine::Headless - class TestFile - attr_reader :path, :source_root + class RequiredFile + extend Forwardable - def initialize(path, source_root = nil) - @path, @source_root = path, source_root + def_delegators :parent, :path_searcher, :extension_filter + + attr_reader :path, :source_root, :parent + attr_writer :spec_file + + def initialize(path, source_root, parent) + @path, @source_root, @parent = path, source_root, parent + @spec_file = false + end + + def spec_file? + @spec_file end def ==(other) @@ -17,19 +28,46 @@ module Jasmine::Headless process_data_by_filename(path) end + def has_dependencies? + !dependencies.empty? + end + + def includes?(path) + @path == path || dependencies.any? { |dependency| dependency.includes?(path) } + end + + def file_paths + (dependencies.collect(&:file_paths) + [ path ]).flatten + end + def dependencies return @dependencies if @dependencies processor = Sprockets::DirectiveProcessor.new(path) - @dependencies = processor.directives.collect do |_, type, name| + @dependencies = processor.directives.collect do |line, type, name| if name[%r{^\.}] name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') else raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree' end - [ type, name ] - end + files = case type + when 'require' + [ name ] + when 'require_tree' + Dir[File.join(source_root, name, '**/*')].find_all { |path| + File.file?(path) && path[extension_filter] + }.sort.collect { |path| path.gsub(%r{^#{source_root}/}, '') } + end + + files.collect do |file| + if result = path_searcher.find(file) + self.class.new(*result, self) + else + raise Sprockets::FileNotFound.new("Could not find #{file}, referenced from #{path}:#{line}") + end + end + end.flatten end def logical_path diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index c39f34d..cc225c9 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -26,25 +26,6 @@ module Jasmine attr_reader :options class << self - def reset! - # register haml-sprockets if it's available... - %w{haml-sprockets}.each do |library| - begin - require library - rescue LoadError - end - end - - # ...and unregister ones we don't want/need - Sprockets.instance_eval do - %w{less sass scss erb str}.each do |extension| - @engines.delete(".#{extension}") - end - - register_engine '.coffee', Jasmine::Headless::CoffeeTemplate - end - end - def run(options = {}) options = Options.new(options) if !options.kind_of?(Options) new(options).run @@ -89,9 +70,6 @@ module Jasmine def run Jasmine::Headless::CacheableAction.enabled = @options[:enable_cache] - Jasmine::Headless::FilesList.reset! - self.class.reset! - files_list = Jasmine::Headless::FilesList.new( :config => jasmine_config, :only => @options[:files] diff --git a/spec/jasmine/with_sprockets_includes/assets/things/subcode/more_code.js b/spec/jasmine/with_sprockets_includes/assets/things/subcode/more_code.js index fbe3c2f..d40811b 100644 --- a/spec/jasmine/with_sprockets_includes/assets/things/subcode/more_code.js +++ b/spec/jasmine/with_sprockets_includes/assets/things/subcode/more_code.js @@ -1,2 +1,2 @@ -//= require 'required' +//= require 'things/required' diff --git a/spec/jasmine/with_sprockets_includes/spec/things/code_spec.js b/spec/jasmine/with_sprockets_includes/spec/things/code_spec.js index d60428c..8eed82a 100644 --- a/spec/jasmine/with_sprockets_includes/spec/things/code_spec.js +++ b/spec/jasmine/with_sprockets_includes/spec/things/code_spec.js @@ -1,4 +1,4 @@ -//= require 'code' +//= require 'things/code' describe('code', function() { it('should equal 1', function() { diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 50bffcf..a47e957 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -1,5 +1,3 @@ -# encoding: UTF-8 - require 'spec_helper' require 'fakefs/spec_helpers' require 'coffee-script' @@ -23,11 +21,19 @@ describe Jasmine::Headless::FilesList do end end + def self.no_default_files! + before do + described_class.stubs(:default_files).returns([]) + end + end + describe '#use_config' do let(:files_list) { described_class.new(:config => config) } include FakeFS::SpecHelpers + no_default_files! + let(:src_dir) { 'src' } let(:spec_dir) { 'spec' } @@ -108,6 +114,8 @@ describe Jasmine::Headless::FilesList do include FakeFS::SpecHelpers + no_default_files! + let(:config) { { 'spec_files' => [ '*_spec.js' ], 'spec_dir' => spec_dir @@ -177,11 +185,10 @@ describe Jasmine::Headless::FilesList do describe '#spec_file_line_numbers' do include FakeFS::SpecHelpers + no_default_files! + before do - files_list.instance_variable_set(:@spec_files, [ - 'test.coffee', - 'test2.coffee' - ]) + files_list.stubs(:spec_files).returns(['test.coffee', 'test2.coffee']) File.open('test.coffee', 'w') { |fh| fh.print "describe('cat')\ndescribe('cat')" } File.open('test2.coffee', 'w') { |fh| fh.print "no matches" } @@ -194,68 +201,9 @@ describe Jasmine::Headless::FilesList do end end - describe '#add_dependency' do - let(:file) { 'file' } - let(:other_file) { 'other' } - let(:path) { 'path' } - - let(:set) { Set.new } - - before do - files_list.stubs(:find_dependency).with(file).returns([ path, nil ]) - files_list.stubs(:find_dependency).with(other_file).returns(false) - files_list.instance_variable_set(:@checked_dependency, set) - end - - context 'not found' do - before do - files_list.expects(:add_file).never - end - - it 'should do nothing' do - files_list.add_dependency('', other_file, nil) - set.should be_empty - end - end - - context 'require' do - context 'not already added' do - before do - files_list.expects(:add_file).with(path, nil, false) - end - - it 'should add the file to the front' do - files_list.add_dependency('require', file, nil) - set.should include(file) - end - end - end - - context 'require_tree' do - include FakeFS::SpecHelpers - - let(:paths) { %w{one.js dir/two.coffee dir/three.css dir/subdir/four.js.erb other/five.css.erb} } - - before do - paths.each do |path| - FileUtils.mkdir_p File.dirname(path) - File.open(path, 'wb') - - if path[%r{\.(js|css|coffee)$}] - files_list.expects(:find_dependency).with(path).returns([ other_file, nil ]) - files_list.expects(:add_file).with(other_file, nil, false) - end - end - end - - it 'should add the file to the front' do - files_list.add_dependency('require_tree', '.', File.expand_path('.')) - set.should be_empty - end - end - end - describe '#search_paths' do + no_default_files! + let(:files_list) { described_class.new(:config => config) } let(:config) { { @@ -321,68 +269,74 @@ describe Jasmine::Headless::FilesList do end end - describe '#find_dependency' do - include FakeFS::SpecHelpers + describe '#files' do + let(:path_one) { 'one' } + let(:path_two) { 'two' } + let(:path_three) { 'three' } - let(:dir) { File.expand_path('dir') } - - let(:filename) { 'file' } - let(:file) { "#{filename}.js" } + let(:file_one) { stub(:file_paths => [ path_one, path_two ] ) } + let(:file_two) { stub(:file_paths => [ path_two, path_three ] ) } before do - FileUtils.mkdir_p dir + files_list.stubs(:required_files).returns([ file_one, file_two ]) + end - %w{file.sub.js file.js.coffee}.each do |file| - File.open(File.join(dir, file), 'wb') + subject { files_list.files } + + it { should == [ path_one, path_two, path_three ] } + end + + describe '#filtered_files' do + let(:spec_dir) { 'spec' } + + let(:file_one) { "#{spec_dir}/one" } + let(:file_two) { "#{spec_dir}/two" } + let(:file_three) { "#{spec_dir}/three" } + let(:file_four) { 'other/four' } + + before do + files_list.stubs(:files).returns([ + file_one, + file_two, + file_three, + file_four + ]) + + files_list.stubs(:potential_files_to_filter).returns([ file_one, file_two, file_three ]) + end + + subject { files_list.filtered_files } + + context 'empty filter' do + before do + files_list.stubs(:spec_filter).returns([]) end - files_list.stubs(:search_paths).returns([ dir ]) + it { should == [ file_one, file_two, file_three, file_four ] } end - subject { files_list.find_dependency(search) } + context 'with filter' do + before do + files_list.stubs(:spec_filter).returns([ "#{spec_dir}/one", '**/tw*' ]) + end - context 'bad' do - let(:search) { 'bad' } - - it { should be_false } - end - - context 'file' do - let(:search) { 'file' } - - it { should == [ File.join(dir, 'file.js.coffee'), dir ] } - end - - context 'file.sub' do - let(:search) { 'file.sub' } - - it { should == [ File.join(dir, 'file.sub.js'), dir ] } + it { should == [ file_one, file_two, file_four ] } end end - describe '#add_file' do - let(:set) { Set.new([ :one ]) } + describe '#files_to_html' do + let(:file_one) { 'path/one' } + let(:file_two) { 'path/two' } before do - files_list.instance_variable_set(:@checked_dependency, set) + files_list.stubs(:files).returns([ file_one, file_two ]) + files_list.stubs(:search_paths).returns([ 'path' ]) - files_list.stubs(:add_dependencies) + Jasmine::Headless::RequiredFile.any_instance.stubs(:to_html).returns('made it') end - context 'clear dependencies' do - it 'should clear dependency checking' do - files_list.send(:add_file, 'file', 'root') - - files_list.instance_variable_get(:@checked_dependency).should == Set.new - end - end - - context 'do not clear dependencies' do - it 'should clear dependency checking' do - files_list.send(:add_file, 'file', 'root', false) - - files_list.instance_variable_get(:@checked_dependency).should == set - end + it 'should render all the files' do + files_list.files_to_html.should == [ 'made it', 'made it' ] end end end diff --git a/spec/lib/jasmine/headless/path_searcher_spec.rb b/spec/lib/jasmine/headless/path_searcher_spec.rb new file mode 100644 index 0000000..9c7f4fd --- /dev/null +++ b/spec/lib/jasmine/headless/path_searcher_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe Jasmine::Headless::PathSearcher do + include FakeFS::SpecHelpers + + let(:path) { File.expand_path('path') } + let(:paths) { [ path ] } + let(:source) { stub(:search_paths => paths, :extension_filter => %r{.*}) } + let(:path_searcher) { described_class.new(source) } + + let(:filename) { 'file.js' } + + let(:file) { File.join(path, filename) } + + describe '#find' do + subject { path_searcher.find(search) } + + before do + FileUtils.mkdir_p path + File.open(file, 'wb') + end + + context 'found file' do + let(:search) { 'file' } + + it 'should find the file' do + subject.should == [ File.expand_path(file), path ] + end + end + + context 'not found file' do + let(:search) { 'other' } + + it 'should not find the file' do + subject.should be_false + end + end + end +end + diff --git a/spec/lib/jasmine/headless/required_file_spec.rb b/spec/lib/jasmine/headless/required_file_spec.rb new file mode 100644 index 0000000..6820173 --- /dev/null +++ b/spec/lib/jasmine/headless/required_file_spec.rb @@ -0,0 +1,213 @@ +require 'spec_helper' + +describe Jasmine::Headless::RequiredFile do + let(:source_root) { File.expand_path('source_root') } + let(:path) { File.join(source_root, 'path.js') } + + let(:file) { described_class.new(path, source_root, files_list) } + + let(:paths) { [ source_root ] } + let(:path_searcher) { stub } + let(:files_list) { stub(:path_searcher => path_searcher) } + + subject { file } + + its(:path) { should == path } + its(:source_root) { should == source_root } + its(:parent) { should == files_list } + + describe '#has_dependencies?' do + it 'should have dependencies' do + file.instance_variable_set(:@dependencies, [ 1 ]) + + file.should have_dependencies + end + + it 'should not have dependencies' do + file.instance_variable_set(:@dependencies, []) + + file.should_not have_dependencies + end + end + + describe '#includes?' do + it 'includes itself' do + file.includes?(path).should be_true + end + + context 'with dependencies' do + let(:other_file) { stub } + let(:other_path) { 'other path' } + let(:third_path) { 'third path' } + + before do + other_file.stubs(:includes?).with(other_path).returns(true) + other_file.stubs(:includes?).with(third_path).returns(false) + + file.stubs(:dependencies).returns([ other_file ]) + end + + it 'checks dependencies' do + file.includes?(third_path).should be_false + file.includes?(other_path).should be_true + end + end + end + + describe '#to_html' do + subject { file.to_html } + + context '.js' do + let(:path) { 'path.js' } + + it { should == %{} } + end + + context '.css' do + let(:path) { 'path.css' } + + it { should == %{} } + end + + context 'with tilt template' do + include FakeFS::SpecHelpers + + let(:content) { 'content' } + + before do + File.open(path, 'wb') { |fh| fh.print content } + end + + let(:klass) do + Class.new(Tilt::Template) do + def prepare ; end + + def evaluate(scope, locals, &block) + "#{file} made it #{data}" + end + end + end + + let(:other_klass) do + Class.new(Tilt::Template) do + def prepare ; end + + def evaluate(scope, locals, &block) + data + end + end + end + + before do + Sprockets.stubs(:engines).with('.tilt').returns(klass) + Sprockets.stubs(:engines).with('.jst').returns(other_klass) + end + + context '.tilt' do + let(:path) { 'path.tilt' } + + it { should == %{#{path} made it #{content}} } + end + + context '.tilt.tilt' do + let(:path) { 'path.tilt.tilt' } + + it { should == %{path.tilt made it #{path} made it #{content}} } + end + + context '.jst.tilt' do + let(:path) { 'path.jst.tilt' } + + it { should == %{} } + end + end + end + + describe '#dependencies' do + include FakeFS::SpecHelpers + + let(:directive) { 'require' } + + let(:dirname) { 'subdir/subsubdir' } + let(:dir) { File.join(source_root, dirname) } + + let(:path) { path_file } + let(:path_file) { File.join(dir, 'path.js') } + let(:req_file) { File.join(dir, "req.js") } + + before do + FileUtils.mkdir_p dir + File.open(path_file, 'wb') { |fh| fh.print "//= #{directive} '#{req_name}'\njavascript" } + File.open(req_file, 'wb') + end + + subject { file.dependencies } + + context 'absolute' do + context 'require' do + context 'file exists' do + let(:req_name) { File.join(dirname, 'req') } + + before do + path_searcher.expects(:find).with(File.join(dirname, 'req')).returns([ req_file, source_root ]) + end + + it { should == [ described_class.new(req_file, source_root, file) ] } + end + + context 'file does not exist' do + let(:req_name) { File.join(dirname, 'bad') } + + before do + path_searcher.expects(:find).with(File.join(dirname, 'bad')).returns(false) + end + + it 'should raise an exception' do + expect { subject }.to raise_error(Sprockets::FileNotFound) + end + end + end + end + + context 'relative' do + context 'require' do + context 'file exists' do + let(:req_name) { './req' } + + before do + path_searcher.expects(:find).with(File.join(dirname, 'req')).returns([ req_file, source_root ]) + end + + it { should == [ described_class.new(req_file, source_root, file) ] } + end + + context 'file does not exist' do + let(:req_name) { './bad' } + + before do + path_searcher.expects(:find).with(File.join(dirname, 'bad')).returns(false) + end + + it 'should raise an exception' do + expect { subject }.to raise_error(Sprockets::FileNotFound) + end + end + end + end + end + + describe '#file_paths' do + let(:other_path) { File.join(source_root, 'other_path.js') } + let(:other_file) { described_class.new(other_path, source_root, file) } + + before do + file.stubs(:dependencies).returns([ other_file ]) + other_file.stubs(:dependencies).returns([]) + end + + it 'should flatten all the paths in itself and descendents' do + file.file_paths.should == [ other_path, path ] + end + end +end + diff --git a/spec/lib/jasmine/headless/template_writer_spec.rb b/spec/lib/jasmine/headless/template_writer_spec.rb index a6e8579..3b4ad0d 100644 --- a/spec/lib/jasmine/headless/template_writer_spec.rb +++ b/spec/lib/jasmine/headless/template_writer_spec.rb @@ -47,6 +47,8 @@ describe Jasmine::Headless::TemplateWriter do include FakeFS::SpecHelpers before do + Jasmine::Headless::FilesList.stubs(:default_files).returns([]) + File.stubs(:read).returns(nil) runner.stubs(:keep_runner).returns(true) @@ -56,8 +58,8 @@ describe Jasmine::Headless::TemplateWriter do let(:files_list) { Jasmine::Headless::FilesList.new } before do - files_list.files << 'file.js' - files_list.filtered_files << 'file.js' + files_list.stubs(:files).returns([ 'file.js' ]) + files_list.stubs(:filtered_files).returns([ 'file.js' ]) end context 'no filter' do @@ -70,7 +72,7 @@ describe Jasmine::Headless::TemplateWriter do context 'filtered files' do before do - files_list.instance_variable_get(:@files) << 'file2.js' + files_list.stubs(:files).returns([ 'file.js', 'file2.js' ]) end it 'should write two files' do diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb deleted file mode 100644 index 9c47a20..0000000 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ /dev/null @@ -1,130 +0,0 @@ -require 'spec_helper' - -describe Jasmine::Headless::TestFile do - let(:source_root) { File.expand_path('source_root') } - let(:path) { File.join(source_root, 'path.js') } - - let(:file) { described_class.new(path, source_root) } - - subject { file } - - its(:path) { should == path } - - describe '#to_html' do - subject { file.to_html } - - context '.js' do - let(:path) { 'path.js' } - - it { should == %{} } - end - - context '.css' do - let(:path) { 'path.css' } - - it { should == %{} } - end - - context 'with tilt template' do - include FakeFS::SpecHelpers - - let(:content) { 'content' } - - before do - File.open(path, 'wb') { |fh| fh.print content } - end - - let(:klass) do - Class.new(Tilt::Template) do - def prepare ; end - - def evaluate(scope, locals, &block) - "#{file} made it #{data}" - end - end - end - - let(:other_klass) do - Class.new(Tilt::Template) do - def prepare ; end - - def evaluate(scope, locals, &block) - data - end - end - end - - before do - Sprockets.stubs(:engines).with('.tilt').returns(klass) - Sprockets.stubs(:engines).with('.jst').returns(other_klass) - end - - context '.tilt' do - let(:path) { 'path.tilt' } - - it { should == %{#{path} made it #{content}} } - end - - context '.tilt.tilt' do - let(:path) { 'path.tilt.tilt' } - - it { should == %{path.tilt made it #{path} made it #{content}} } - end - - context '.jst.tilt' do - let(:path) { 'path.jst.tilt' } - - it { should == %{} } - end - end - end - - describe '#dependencies' do - include FakeFS::SpecHelpers - - let(:directive) { 'require' } - - before do - FileUtils.mkdir_p File.dirname(path) - File.open(path, 'wb') { |fh| fh.print "//= #{directive} '#{req}'\njavascript" } - end - - context 'absolute' do - let(:req) { 'test' } - - subject { file.dependencies } - - context 'require' do - it { should == [ [ 'require', req ] ] } - end - - context 'require_tree' do - let(:directive) { 'require_tree' } - - it 'should raise an error' do - expect { subject }.to raise_error(Sprockets::ArgumentError, /relative/) - end - end - end - - context 'relative' do - let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') } - - let(:req) { './test' } - - subject { file.dependencies } - - it { should == [ [ 'require', 'subdir/subsubdir/test' ] ] } - end - - context 'dot' do - let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') } - - let(:req) { '.' } - - subject { file.dependencies } - - it { should == [ [ 'require', 'subdir/subsubdir' ] ] } - end - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7a0d1a5..94aff13 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,12 +3,11 @@ require 'fakefs/spec_helpers' RSpec.configure do |c| c.mock_with :mocha - c.backtrace_clean_patterns = [] + #c.backtrace_clean_patterns = [] c.before(:each) do Jasmine::Headless::CacheableAction.enabled = false Jasmine::Headless::FilesList.reset! - Jasmine::Headless::Runner.reset! end end @@ -20,6 +19,14 @@ if !File.file?(specrunner) end end +class FakeFS::File + class << self + def fnmatch?(pattern, file) + RealFile.fnmatch?(pattern, file) + end + end +end + module RSpec::Matchers define :be_a_report_containing do |total, failed, used_console| match do |filename| From 9b5729b8086b6dcfc46cb9eb498d22f9e8ba806d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 21 Nov 2011 10:55:37 -0500 Subject: [PATCH 11/20] yet more changes, getting really close --- Guardfile | 2 ++ lib/jasmine/headless/command_line.rb | 2 -- lib/jasmine/headless/files_list.rb | 12 ++++++++++-- lib/jasmine/headless/required_file.rb | 2 +- lib/jasmine/headless/runner.rb | 1 + .../assets/coffeescripts/jasmine-extensions.coffee | 4 ++++ 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Guardfile b/Guardfile index 38c2dc6..512beef 100644 --- a/Guardfile +++ b/Guardfile @@ -3,6 +3,7 @@ # watch('file/path') { `command(s)` } # +if false guard 'coffeescript', :input => 'vendor/assets/coffeescripts', :output => 'vendor/assets/javascripts' guard 'shell' do @@ -21,6 +22,7 @@ guard 'rspec', :version => 2, :all_on_start => false do watch(%r{^bin/(.+)}) { |m| "spec/bin/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end +end guard 'jasmine-headless-webkit', :all_on_start => false do watch(%r{^spec/javascripts/.+_spec\.coffee}) diff --git a/lib/jasmine/headless/command_line.rb b/lib/jasmine/headless/command_line.rb index 94e1362..a746247 100644 --- a/lib/jasmine/headless/command_line.rb +++ b/lib/jasmine/headless/command_line.rb @@ -5,8 +5,6 @@ module Jasmine::Headless require 'coffee-script' require 'rainbow' - FilesList.reset! - begin options = Options.from_command_line runner = Runner.new(options) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 7291154..404e407 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -61,7 +61,7 @@ module Jasmine::Headless @potential_files_to_filter = [] self.class.default_files.each do |file| - @required_files << RequiredFile.new(*path_searcher.find(file.dup), self) + @required_files << RequiredFile.new(*[ path_searcher.find(file.dup), self ].flatten) end use_config! if config? @@ -205,7 +205,15 @@ module Jasmine::Headless end end - @required_files.uniq!(&:path) + filtered_required_files = [] + + @required_files.each do |file| + if !filtered_required_files.any? { |other_file| other_file == file } + filtered_required_files << file + end + end + + @required_files = filtered_required_files end def config? diff --git a/lib/jasmine/headless/required_file.rb b/lib/jasmine/headless/required_file.rb index c20ab09..dd28b82 100644 --- a/lib/jasmine/headless/required_file.rb +++ b/lib/jasmine/headless/required_file.rb @@ -62,7 +62,7 @@ module Jasmine::Headless files.collect do |file| if result = path_searcher.find(file) - self.class.new(*result, self) + self.class.new(*[ result, self ].flatten) else raise Sprockets::FileNotFound.new("Could not find #{file}, referenced from #{path}:#{line}") end diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index cc225c9..33de5c9 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -69,6 +69,7 @@ module Jasmine def run Jasmine::Headless::CacheableAction.enabled = @options[:enable_cache] + FilesList.reset! files_list = Jasmine::Headless::FilesList.new( :config => jasmine_config, diff --git a/vendor/assets/coffeescripts/jasmine-extensions.coffee b/vendor/assets/coffeescripts/jasmine-extensions.coffee index a06150e..561cf9c 100644 --- a/vendor/assets/coffeescripts/jasmine-extensions.coffee +++ b/vendor/assets/coffeescripts/jasmine-extensions.coffee @@ -73,7 +73,11 @@ if window.JHW pauseAndRun = (onComplete) -> JHW.timerPause() + if this.env.reporter.reportSpecWaiting + this.env.reporter.reportSpecWaiting(this) this._execute -> + if this.env.reporter.reportSpecRunning + this.env.reporter.reportSpecRunning(this) JHW.timerDone() onComplete() From 4727ad5b7bf94c89355574d78101368ac1271791 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 21 Nov 2011 11:33:51 -0500 Subject: [PATCH 12/20] show an indicator when wait tests are running, so it doesn't look like the program crashed --- Guardfile | 2 -- vendor/assets/coffeescripts/intense.coffee | 1 + .../coffeescripts/jasmine-extensions.coffee | 17 +++++++--- .../jasmine.HeadlessConsoleReporter.coffee | 30 ++++++++++++++++ vendor/assets/javascripts/intense.js | 6 +++- .../assets/javascripts/jasmine-extensions.js | 27 ++++++++++++++- .../jasmine.HeadlessConsoleReporter.js | 34 +++++++++++++++++++ 7 files changed, 109 insertions(+), 8 deletions(-) diff --git a/Guardfile b/Guardfile index 512beef..38c2dc6 100644 --- a/Guardfile +++ b/Guardfile @@ -3,7 +3,6 @@ # watch('file/path') { `command(s)` } # -if false guard 'coffeescript', :input => 'vendor/assets/coffeescripts', :output => 'vendor/assets/javascripts' guard 'shell' do @@ -22,7 +21,6 @@ guard 'rspec', :version => 2, :all_on_start => false do watch(%r{^bin/(.+)}) { |m| "spec/bin/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end -end guard 'jasmine-headless-webkit', :all_on_start => false do watch(%r{^spec/javascripts/.+_spec\.coffee}) diff --git a/vendor/assets/coffeescripts/intense.coffee b/vendor/assets/coffeescripts/intense.coffee index f24d136..db00e5c 100644 --- a/vendor/assets/coffeescripts/intense.coffee +++ b/vendor/assets/coffeescripts/intense.coffee @@ -20,6 +20,7 @@ window.Intense = { else this useColors: true + moveBack: (count = 1) -> "\033[#{count}D" } for method, code of Intense.methods diff --git a/vendor/assets/coffeescripts/jasmine-extensions.coffee b/vendor/assets/coffeescripts/jasmine-extensions.coffee index 561cf9c..92dd70b 100644 --- a/vendor/assets/coffeescripts/jasmine-extensions.coffee +++ b/vendor/assets/coffeescripts/jasmine-extensions.coffee @@ -73,11 +73,10 @@ if window.JHW pauseAndRun = (onComplete) -> JHW.timerPause() - if this.env.reporter.reportSpecWaiting - this.env.reporter.reportSpecWaiting(this) + jasmine.getEnv().reporter.reportSpecWaiting() + this._execute -> - if this.env.reporter.reportSpecRunning - this.env.reporter.reportSpecRunning(this) + jasmine.getEnv().reporter.reportSpecRunning() JHW.timerDone() onComplete() @@ -92,3 +91,13 @@ if window.JHW result.expectations = jasmine.NestedResults.parseAndStore(arguments.callee.caller.caller.caller.toString()) this.addResult_(result) + + for method in [ "reportSpecWaiting", "reportSpecRunning" ] + generator = (method) -> + (args...) -> + for reporter in @subReporters_ + if reporter[method]? + reporter[method](args...) + + jasmine.MultiReporter.prototype[method] = generator(method) + diff --git a/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee b/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee index 069760b..3bdedd0 100644 --- a/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee +++ b/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee @@ -6,6 +6,9 @@ class jasmine.HeadlessConsoleReporter @results = [] @failedCount = 0 @length = 0 + @timer = null + @position = 0 + @positions = "|/-\\" reportRunnerResults: (runner) -> return if this.hasError() @@ -67,6 +70,33 @@ class jasmine.HeadlessConsoleReporter spec.finish() spec.suite.finish() + reportSpecWaiting: -> + runner = null + + if !@timer + @timer = true + first = true + + runner = => + @timer = setTimeout( + => + if @timer + JHW.stdout.print(Intense.moveBack()) if !first + JHW.stdout.print(@positions.substr(@position, 1).foreground('yellow')) + @position += 1 + @position %= @positions.length + first = false + runner() + , 750 + ) + runner() + + reportSpecRunning: -> + if @timer + clearTimeout(@timer) + @timer = null + JHW.stdout.print(Intense.moveBack()) + reportSuiteResults: (suite) -> hasError: -> JHW._hasErrors diff --git a/vendor/assets/javascripts/intense.js b/vendor/assets/javascripts/intense.js index 6d885a1..8341721 100644 --- a/vendor/assets/javascripts/intense.js +++ b/vendor/assets/javascripts/intense.js @@ -28,7 +28,11 @@ } } }, - useColors: true + useColors: true, + moveBack: function(count) { + if (count == null) count = 1; + return "\033[" + count + "D"; + } }; _ref = Intense.methods; diff --git a/vendor/assets/javascripts/jasmine-extensions.js b/vendor/assets/javascripts/jasmine-extensions.js index c9b4a93..da5394c 100644 --- a/vendor/assets/javascripts/jasmine-extensions.js +++ b/vendor/assets/javascripts/jasmine-extensions.js @@ -1,5 +1,6 @@ (function() { - var getSplitName, pauseAndRun; + var generator, getSplitName, method, pauseAndRun, _i, _len, _ref; + var __slice = Array.prototype.slice; if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not laoded!"); @@ -85,7 +86,9 @@ jasmine.WaitsForBlock.prototype._execute = jasmine.WaitsForBlock.prototype.execute; pauseAndRun = function(onComplete) { JHW.timerPause(); + jasmine.getEnv().reporter.reportSpecWaiting(); return this._execute(function() { + jasmine.getEnv().reporter.reportSpecRunning(); JHW.timerDone(); return onComplete(); }); @@ -98,6 +101,28 @@ result.expectations = jasmine.NestedResults.parseAndStore(arguments.callee.caller.caller.caller.toString()); return this.addResult_(result); }; + _ref = ["reportSpecWaiting", "reportSpecRunning"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + method = _ref[_i]; + generator = function(method) { + return function() { + var args, reporter, _j, _len2, _ref2, _results; + args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + _ref2 = this.subReporters_; + _results = []; + for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { + reporter = _ref2[_j]; + if (reporter[method] != null) { + _results.push(reporter[method].apply(reporter, args)); + } else { + _results.push(void 0); + } + } + return _results; + }; + }; + jasmine.MultiReporter.prototype[method] = generator(method); + } } } diff --git a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js index ea1d365..e96bbb7 100644 --- a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js +++ b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js @@ -10,6 +10,9 @@ this.results = []; this.failedCount = 0; this.length = 0; + this.timer = null; + this.position = 0; + this.positions = "|/-\\"; } HeadlessConsoleReporter.prototype.reportRunnerResults = function(runner) { @@ -77,6 +80,37 @@ } }; + HeadlessConsoleReporter.prototype.reportSpecWaiting = function() { + var first, runner; + var _this = this; + runner = null; + if (!this.timer) { + this.timer = true; + first = true; + runner = function() { + return _this.timer = setTimeout(function() { + if (_this.timer) { + if (!first) JHW.stdout.print(Intense.moveBack()); + JHW.stdout.print(_this.positions.substr(_this.position, 1).foreground('yellow')); + _this.position += 1; + _this.position %= _this.positions.length; + first = false; + return runner(); + } + }, 750); + }; + return runner(); + } + }; + + HeadlessConsoleReporter.prototype.reportSpecRunning = function() { + if (this.timer) { + clearTimeout(this.timer); + this.timer = null; + return JHW.stdout.print(Intense.moveBack()); + } + }; + HeadlessConsoleReporter.prototype.reportSuiteResults = function(suite) {}; HeadlessConsoleReporter.prototype.hasError = function() { From 7ba77ea1f90d5cb4dc72ec4f2d9f4a21532f4ce6 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 21 Nov 2011 16:10:15 -0500 Subject: [PATCH 13/20] dont show the running message if there are compile errors --- .../jasmine.HeadlessConsoleReporter.coffee | 2 +- .../javascripts/headless_reporter_result.js | 19 +++---- vendor/assets/javascripts/intense.js | 7 ++- .../assets/javascripts/jasmine-extensions.js | 9 +--- .../jasmine.HeadlessConsoleReporter.js | 54 +++++++++---------- vendor/assets/javascripts/prolog.js | 8 ++- 6 files changed, 40 insertions(+), 59 deletions(-) diff --git a/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee b/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee index 3bdedd0..aee9087 100644 --- a/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee +++ b/vendor/assets/coffeescripts/jasmine.HeadlessConsoleReporter.coffee @@ -37,7 +37,7 @@ class jasmine.HeadlessConsoleReporter reportRunnerStarting: (runner) -> @startTime = new Date() - JHW.stdout.puts("\nRunning Jasmine specs...".bright()) + JHW.stdout.puts("\nRunning Jasmine specs...".bright()) if !this.hasError() reportSpecResults: (spec) -> return if this.hasError() diff --git a/vendor/assets/javascripts/headless_reporter_result.js b/vendor/assets/javascripts/headless_reporter_result.js index 92ec7b9..14ba347 100644 --- a/vendor/assets/javascripts/headless_reporter_result.js +++ b/vendor/assets/javascripts/headless_reporter_result.js @@ -1,16 +1,13 @@ - +(function() { window.HeadlessReporterResult = (function() { - function HeadlessReporterResult(name, splitName) { this.name = name; this.splitName = splitName; this.results = []; } - HeadlessReporterResult.prototype.addResult = function(message) { return this.results.push(message); }; - HeadlessReporterResult.prototype.print = function() { var bestChoice, output, result, _i, _len, _ref, _results; output = this.name.foreground('red'); @@ -28,15 +25,10 @@ output += (" (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")").foreground('red').bright(); } JHW.stdout.puts(" " + output); - if (result.line != null) { - _results.push(JHW.stdout.puts((" " + result.line).foreground('yellow'))); - } else { - _results.push(void 0); - } + _results.push(result.line != null ? JHW.stdout.puts((" " + result.line).foreground('yellow')) : void 0); } return _results; }; - HeadlessReporterResult.findSpecLine = function(splitName) { var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref; bestChoice = { @@ -57,7 +49,9 @@ for (_i = 0, _len = newLineNumberInfo.length; _i < _len; _i++) { line = newLineNumberInfo[_i]; lastLine = line; - if (line > lineNumber) break; + if (line > lineNumber) { + break; + } } lineNumber = lastLine; } @@ -73,7 +67,6 @@ } return bestChoice; }; - return HeadlessReporterResult; - })(); +}).call(this); diff --git a/vendor/assets/javascripts/intense.js b/vendor/assets/javascripts/intense.js index 8341721..73b1556 100644 --- a/vendor/assets/javascripts/intense.js +++ b/vendor/assets/javascripts/intense.js @@ -1,6 +1,5 @@ (function() { var code, method, _ref; - window.Intense = { colors: { black: 0, @@ -30,15 +29,15 @@ }, useColors: true, moveBack: function(count) { - if (count == null) count = 1; + if (count == null) { + count = 1; + } return "\033[" + count + "D"; } }; - _ref = Intense.methods; for (method in _ref) { code = _ref[method]; String.prototype[method] = code; } - }).call(this); diff --git a/vendor/assets/javascripts/jasmine-extensions.js b/vendor/assets/javascripts/jasmine-extensions.js index da5394c..c45e757 100644 --- a/vendor/assets/javascripts/jasmine-extensions.js +++ b/vendor/assets/javascripts/jasmine-extensions.js @@ -1,11 +1,9 @@ (function() { var generator, getSplitName, method, pauseAndRun, _i, _len, _ref; var __slice = Array.prototype.slice; - if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not laoded!"); } - if (window.JHW) { getSplitName = function(parts) { parts.push(String(this.description).replace(/[\n\r]/g, ' ')); @@ -112,11 +110,7 @@ _results = []; for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { reporter = _ref2[_j]; - if (reporter[method] != null) { - _results.push(reporter[method].apply(reporter, args)); - } else { - _results.push(void 0); - } + _results.push(reporter[method] != null ? reporter[method].apply(reporter, args) : void 0); } return _results; }; @@ -125,5 +119,4 @@ } } } - }).call(this); diff --git a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js index e96bbb7..e81a776 100644 --- a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js +++ b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js @@ -1,10 +1,9 @@ - +(function() { + var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not loaded!"); } - jasmine.HeadlessConsoleReporter = (function() { - function HeadlessConsoleReporter(callback) { this.callback = callback != null ? callback : null; this.results = []; @@ -14,10 +13,11 @@ this.position = 0; this.positions = "|/-\\"; } - HeadlessConsoleReporter.prototype.reportRunnerResults = function(runner) { var output, result, resultLine, runtime, _i, _len, _ref; - if (this.hasError()) return; + if (this.hasError()) { + return; + } runtime = (new Date() - this.startTime) / 1000.0; JHW.stdout.print("\n"); resultLine = this._formatResultLine(runtime); @@ -34,18 +34,22 @@ result = _ref[_i]; result.print(); } - if (window.JHW) window.onbeforeunload = null; + if (window.JHW) { + window.onbeforeunload = null; + } return JHW.finishSuite(); }; - HeadlessConsoleReporter.prototype.reportRunnerStarting = function(runner) { this.startTime = new Date(); - return JHW.stdout.puts("\nRunning Jasmine specs...".bright()); + if (!this.hasError()) { + return JHW.stdout.puts("\nRunning Jasmine specs...".bright()); + } }; - HeadlessConsoleReporter.prototype.reportSpecResults = function(spec) { var failureResult, foundLine, result, results, testCount, _i, _len, _ref; - if (this.hasError()) return; + if (this.hasError()) { + return; + } JHW.ping(); results = spec.results(); this.length++; @@ -72,37 +76,35 @@ return this.results.push(failureResult); } }; - HeadlessConsoleReporter.prototype.reportSpecStarting = function(spec) { if (this.hasError()) { spec.finish(); return spec.suite.finish(); } }; - HeadlessConsoleReporter.prototype.reportSpecWaiting = function() { var first, runner; - var _this = this; runner = null; if (!this.timer) { this.timer = true; first = true; - runner = function() { - return _this.timer = setTimeout(function() { - if (_this.timer) { - if (!first) JHW.stdout.print(Intense.moveBack()); - JHW.stdout.print(_this.positions.substr(_this.position, 1).foreground('yellow')); - _this.position += 1; - _this.position %= _this.positions.length; + runner = __bind(function() { + return this.timer = setTimeout(__bind(function() { + if (this.timer) { + if (!first) { + JHW.stdout.print(Intense.moveBack()); + } + JHW.stdout.print(this.positions.substr(this.position, 1).foreground('yellow')); + this.position += 1; + this.position %= this.positions.length; first = false; return runner(); } - }, 750); - }; + }, this), 750); + }, this); return runner(); } }; - HeadlessConsoleReporter.prototype.reportSpecRunning = function() { if (this.timer) { clearTimeout(this.timer); @@ -110,13 +112,10 @@ return JHW.stdout.print(Intense.moveBack()); } }; - HeadlessConsoleReporter.prototype.reportSuiteResults = function(suite) {}; - HeadlessConsoleReporter.prototype.hasError = function() { return JHW._hasErrors; }; - HeadlessConsoleReporter.prototype._formatResultLine = function(runtime) { var line; line = []; @@ -128,7 +127,6 @@ line.push((runtime === 1.0 ? "sec" : "secs") + '.'); return line.join(' '); }; - return HeadlessConsoleReporter; - })(); +}).call(this); diff --git a/vendor/assets/javascripts/prolog.js b/vendor/assets/javascripts/prolog.js index eb2b69f..2fd358b 100644 --- a/vendor/assets/javascripts/prolog.js +++ b/vendor/assets/javascripts/prolog.js @@ -1,6 +1,5 @@ (function() { var createHandle, handle, _i, _len, _ref; - if (window.JHW) { window.console = { log: function(data) { @@ -41,7 +40,9 @@ e = e || window.event; JHW.hasError(); JHW.stdout.puts('The code tried to leave the test page. Check for unhandled form submits and link clicks.'); - if (e) e.returnValue = 'string'; + if (e) { + e.returnValue = 'string'; + } return 'string'; }; window.confirm = function(message) { @@ -83,9 +84,6 @@ return JHW.stdout.puts(msg); }; } - window.CoffeeScriptToFilename = {}; - window.CSTF = window.CoffeeScriptToFilename; - }).call(this); From a9cc872cc8ed6e53fc3dfbfa6b5b36f980676a59 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 21 Nov 2011 17:03:44 -0500 Subject: [PATCH 14/20] some cleanup --- skel/template.html.erb | 10 +++++++++- .../coffeescripts/headless_reporter_result.coffee | 2 +- vendor/assets/javascripts/headless_reporter_result.js | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/skel/template.html.erb b/skel/template.html.erb index 05f7038..4da2057 100644 --- a/skel/template.html.erb +++ b/skel/template.html.erb @@ -23,7 +23,15 @@ if (window.JHW) { jasmine.getEnv().addReporter(new jasmine.HeadlessConsoleReporter()); } else { - jasmine.getEnv().addReporter(new jasmine.HtmlReporter()); + types = [ 'HtmlReporter', 'TrivialReporter' ]; + + for (var i = 0, j = types.length; i < j; ++i) { + var type = jasmine[types[i]] + if (type) { + jasmine.getEnv().addReporter(new type()); + break; + } + } } jasmine.getEnv().execute(); diff --git a/vendor/assets/coffeescripts/headless_reporter_result.coffee b/vendor/assets/coffeescripts/headless_reporter_result.coffee index 077b571..0c8e752 100644 --- a/vendor/assets/coffeescripts/headless_reporter_result.coffee +++ b/vendor/assets/coffeescripts/headless_reporter_result.coffee @@ -11,7 +11,7 @@ class window.HeadlessReporterResult bestChoice = HeadlessReporterResult.findSpecLine(@splitName) output += " (#{bestChoice.file}:#{bestChoice.lineNumber})".foreground('blue') if bestChoice.file - JHW.stdout.puts "\n\n#{output}" + JHW.stdout.puts "\n#{output}" for result in @results output = result.message.foreground('red') if result.lineNumber diff --git a/vendor/assets/javascripts/headless_reporter_result.js b/vendor/assets/javascripts/headless_reporter_result.js index 14ba347..5498284 100644 --- a/vendor/assets/javascripts/headless_reporter_result.js +++ b/vendor/assets/javascripts/headless_reporter_result.js @@ -15,7 +15,7 @@ if (bestChoice.file) { output += (" (" + bestChoice.file + ":" + bestChoice.lineNumber + ")").foreground('blue'); } - JHW.stdout.puts("\n\n" + output); + JHW.stdout.puts("\n" + output); _ref = this.results; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { From 7206341768e4a958688bab21fa85106a42c083d7 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 21 Nov 2011 20:18:11 -0500 Subject: [PATCH 15/20] require_self --- lib/jasmine/headless/required_file.rb | 28 ++++++++++++++---- .../jasmine/headless/required_file_spec.rb | 29 ++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/lib/jasmine/headless/required_file.rb b/lib/jasmine/headless/required_file.rb index dd28b82..8b6401b 100644 --- a/lib/jasmine/headless/required_file.rb +++ b/lib/jasmine/headless/required_file.rb @@ -37,15 +37,26 @@ module Jasmine::Headless end def file_paths - (dependencies.collect(&:file_paths) + [ path ]).flatten + paths = dependencies.collect(&:file_paths).flatten + + if @insert_after + paths.insert(paths.index(@insert_after) + 1, path) + else + paths << path + end + + paths end def dependencies return @dependencies if @dependencies processor = Sprockets::DirectiveProcessor.new(path) + + last_file_added = nil + @dependencies = processor.directives.collect do |line, type, name| - if name[%r{^\.}] + if name && name[%r{^\.}] name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') else raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree' @@ -55,14 +66,21 @@ module Jasmine::Headless when 'require' [ name ] when 'require_tree' - Dir[File.join(source_root, name, '**/*')].find_all { |path| - File.file?(path) && path[extension_filter] + Dir[File.join(source_root, name, '**/*')].find_all { |found_path| + found_path != path && File.file?(found_path) && found_path[extension_filter] }.sort.collect { |path| path.gsub(%r{^#{source_root}/}, '') } + when 'require_self' + @insert_after = last_file_added + [] + else + [] end files.collect do |file| if result = path_searcher.find(file) - self.class.new(*[ result, self ].flatten) + new_file = self.class.new(*[ result, self ].flatten) + last_file_added = new_file.path + new_file else raise Sprockets::FileNotFound.new("Could not find #{file}, referenced from #{path}:#{line}") end diff --git a/spec/lib/jasmine/headless/required_file_spec.rb b/spec/lib/jasmine/headless/required_file_spec.rb index 6820173..ea98ea1 100644 --- a/spec/lib/jasmine/headless/required_file_spec.rb +++ b/spec/lib/jasmine/headless/required_file_spec.rb @@ -134,11 +134,15 @@ describe Jasmine::Headless::RequiredFile do let(:path) { path_file } let(:path_file) { File.join(dir, 'path.js') } let(:req_file) { File.join(dir, "req.js") } + let(:other_file) { File.join(dir, "other.js") } + + let(:content) { "//= #{directive} '#{req_name}'\njavascript" } before do FileUtils.mkdir_p dir - File.open(path_file, 'wb') { |fh| fh.print "//= #{directive} '#{req_name}'\njavascript" } + File.open(path_file, 'wb') { |fh| fh.print content } File.open(req_file, 'wb') + File.open(other_file, 'wb') end subject { file.dependencies } @@ -194,6 +198,29 @@ describe Jasmine::Headless::RequiredFile do end end end + + context 'require_self' do + subject { file.file_paths } + + let(:content) do + <<-ENDTXT +//= require #{dirname}/req +//= require_self +//= require #{dirname}/other +ENDTXT + end + + before do + path_searcher.expects(:find).with(File.join(dirname, 'req')).returns([ req_file, source_root ]) + path_searcher.expects(:find).with(File.join(dirname, 'other')).returns([ other_file, source_root ]) + end + + it { should == [ + req_file, + path_file, + other_file + ] } + end end describe '#file_paths' do From 7d75b5466f652bcecb21f5f41c467dab62c0f99b Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 22 Nov 2011 10:36:42 -0500 Subject: [PATCH 16/20] whoa actually just plug in sprockets, need to backfill tests later since i have real work to do --- lib/jasmine/headless.rb | 4 +- lib/jasmine/headless/coffee_template.rb | 2 + lib/jasmine/headless/css_template.rb | 14 + lib/jasmine/headless/files_list.rb | 76 +++--- lib/jasmine/headless/js_template.rb | 14 + lib/jasmine/headless/path_searcher.rb | 34 --- lib/jasmine/headless/required_file.rb | 121 --------- lib/jasmine/headless/spec_file_analyzer.rb | 2 +- spec/lib/jasmine/headless/files_list_spec.rb | 180 +------------ .../jasmine/headless/path_searcher_spec.rb | 40 --- .../jasmine/headless/required_file_spec.rb | 240 ------------------ .../javascripts/headless_reporter_result.js | 19 +- vendor/assets/javascripts/intense.js | 7 +- .../assets/javascripts/jasmine-extensions.js | 9 +- .../jasmine.HeadlessConsoleReporter.js | 50 ++-- vendor/assets/javascripts/prolog.js | 8 +- 16 files changed, 141 insertions(+), 679 deletions(-) create mode 100644 lib/jasmine/headless/css_template.rb create mode 100644 lib/jasmine/headless/js_template.rb delete mode 100644 lib/jasmine/headless/path_searcher.rb delete mode 100644 lib/jasmine/headless/required_file.rb delete mode 100644 spec/lib/jasmine/headless/path_searcher_spec.rb delete mode 100644 spec/lib/jasmine/headless/required_file_spec.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 5675b3f..05076ac 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -11,12 +11,12 @@ module Jasmine::Headless autoload :Options, 'jasmine/headless/options' autoload :Task, 'jasmine/headless/task' autoload :FilesList, 'jasmine/headless/files_list' - autoload :RequiredFile, 'jasmine/headless/required_file' - autoload :PathSearcher, 'jasmine/headless/path_searcher' autoload :TemplateWriter, 'jasmine/headless/template_writer' autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' + autoload :JSTemplate, 'jasmine/headless/js_template' + autoload :CSSTemplate, 'jasmine/headless/css_template' autoload :Report, 'jasmine/headless/report' autoload :ReportMessage, 'jasmine/headless/report_message' diff --git a/lib/jasmine/headless/coffee_template.rb b/lib/jasmine/headless/coffee_template.rb index fbc8c95..0cc7d95 100644 --- a/lib/jasmine/headless/coffee_template.rb +++ b/lib/jasmine/headless/coffee_template.rb @@ -2,6 +2,8 @@ require 'tilt/template' module Jasmine::Headless class CoffeeTemplate < Tilt::Template + self.default_mime_type = 'application/javascript' + def prepare ; end def evaluate(scope, locals, &block) diff --git a/lib/jasmine/headless/css_template.rb b/lib/jasmine/headless/css_template.rb new file mode 100644 index 0000000..7829548 --- /dev/null +++ b/lib/jasmine/headless/css_template.rb @@ -0,0 +1,14 @@ +require 'tilt/template' + +module Jasmine::Headless + class CSSTemplate < Tilt::Template + self.default_mime_type = 'text/css' + + def prepare ; end + + def evaluate(scope, locals, &block) + file ? %{} : data + end + end +end + diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 404e407..9130ab2 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -42,6 +42,8 @@ module Jasmine::Headless end register_engine '.coffee', Jasmine::Headless::CoffeeTemplate + register_engine '.js', Jasmine::Headless::JSTemplate + register_engine '.css', Jasmine::Headless::CSSTemplate end end @@ -61,18 +63,20 @@ module Jasmine::Headless @potential_files_to_filter = [] self.class.default_files.each do |file| - @required_files << RequiredFile.new(*[ path_searcher.find(file.dup), self ].flatten) + @required_files << sprockets_environment.find_asset(file, :bundle => false) end use_config! if config? end def files - required_files.collect { |file| file.file_paths }.flatten.uniq + required_files.collect { |file| file.send(:required_assets).collect { |asset| asset.pathname.to_s } }.flatten.uniq end def spec_files - filter_for_requested_specs(required_files.find_all(&:spec_file?).collect(&:path)) + filter_for_requested_specs( + files.find_all { |file| spec_dir.any? { |dir| file[dir] } } + ) end def filtered_files @@ -90,6 +94,17 @@ module Jasmine::Headless @search_paths end + def sprockets_environment + return @sprockets_environment if @sprockets_environment + + @sprockets_environment = Sprockets::Environment.new + search_paths.each do |path| + @sprockets_environment.append_path(path) + end + + @sprockets_environment + end + def path_searcher @path_searcher ||= PathSearcher.new(self) end @@ -98,10 +113,13 @@ module Jasmine::Headless if is_outside_scope = !spec_filter.empty? is_outside_scope = spec_dir.any? do |dir| spec_file_searches.any? do |search| - !spec_files.any? { |file| File.fnmatch?(File.join(dir, search), file) } + !spec_files.any? { |file| + File.fnmatch?(File.join(dir, search), file) + } end end end + is_outside_scope end @@ -139,11 +157,7 @@ module Jasmine::Headless alert_time = nil end - search_paths.collect do |path| - if file[%r{^#{path}}] - Jasmine::Headless::RequiredFile.new(file, path, self).to_html - end - end.compact.first + sprockets_environment.find_asset(file, :bundle => false).to_s end.flatten.compact.reject(&:empty?) end @@ -178,6 +192,16 @@ module Jasmine::Headless add_files(@searches[type] = data.flatten, type, dirs) end end + + filtered_required_files = [] + + @required_files.each do |file| + if !filtered_required_files.any? { |other_file| other_file.logical_path == file.logical_path } + filtered_required_files << file + end + end + + @required_files = filtered_required_files end def add_files(patterns, type, dirs) @@ -186,34 +210,14 @@ module Jasmine::Headless search = File.expand_path(File.join(dir, search)) Dir[search].find_all { |file| file[extension_filter] }.each do |path| - @required_files << (file = RequiredFile.new(path, dir, self)) - - if type == 'spec_files' - file.spec_file = true - @potential_files_to_filter << path - end + add_path(path, type) if File.file?(path) end end end if type == 'spec_files' - spec_filter.each do |path| - @required_files << (file = RequiredFile.new(path, nil, self)) - - file.spec_file = true - @potential_files_to_filter << path - end + spec_filter.each { |path| add_path(path, type) } end - - filtered_required_files = [] - - @required_files.each do |file| - if !filtered_required_files.any? { |other_file| other_file == file } - filtered_required_files << file - end - end - - @required_files = filtered_required_files end def config? @@ -228,8 +232,14 @@ module Jasmine::Headless %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} end - def add_file(file) - @files << file + def add_path(path, type) + asset = sprockets_environment.find_asset(path, :bundle => false) + + @required_files << asset + + if type == 'spec_files' + @potential_files_to_filter << path + end end def include_spec_file?(file) diff --git a/lib/jasmine/headless/js_template.rb b/lib/jasmine/headless/js_template.rb new file mode 100644 index 0000000..81a2fb9 --- /dev/null +++ b/lib/jasmine/headless/js_template.rb @@ -0,0 +1,14 @@ +require 'tilt/template' + +module Jasmine::Headless + class JSTemplate < Tilt::Template + self.default_mime_type = 'application/javascript' + + def prepare ; end + + def evaluate(scope, locals, &block) + file ? %{} : data + end + end +end + diff --git a/lib/jasmine/headless/path_searcher.rb b/lib/jasmine/headless/path_searcher.rb deleted file mode 100644 index b4c78fa..0000000 --- a/lib/jasmine/headless/path_searcher.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'sprockets' -require 'forwardable' - -module Jasmine::Headless - class PathSearcher - extend Forwardable - - def_delegators :source, :search_paths, :extension_filter - - attr_reader :source - - def initialize(source) - @source = source - end - - def find(file) - search_paths.each do |dir| - Dir[File.join(dir, "#{file}*")].find_all { |path| File.file?(path) }.each do |path| - root = path.gsub(%r{^#{dir}/}, '') - - ok = (root == file) - ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[extension_filter] } - - if ok - return [ File.expand_path(path), dir ] - end - end - end - - false - end - end -end - diff --git a/lib/jasmine/headless/required_file.rb b/lib/jasmine/headless/required_file.rb deleted file mode 100644 index 8b6401b..0000000 --- a/lib/jasmine/headless/required_file.rb +++ /dev/null @@ -1,121 +0,0 @@ -require 'rainbow' -require 'sprockets' -require 'forwardable' - -module Jasmine::Headless - class RequiredFile - extend Forwardable - - def_delegators :parent, :path_searcher, :extension_filter - - attr_reader :path, :source_root, :parent - attr_writer :spec_file - - def initialize(path, source_root, parent) - @path, @source_root, @parent = path, source_root, parent - @spec_file = false - end - - def spec_file? - @spec_file - end - - def ==(other) - self.path == other.path - end - - def to_html - process_data_by_filename(path) - end - - def has_dependencies? - !dependencies.empty? - end - - def includes?(path) - @path == path || dependencies.any? { |dependency| dependency.includes?(path) } - end - - def file_paths - paths = dependencies.collect(&:file_paths).flatten - - if @insert_after - paths.insert(paths.index(@insert_after) + 1, path) - else - paths << path - end - - paths - end - - def dependencies - return @dependencies if @dependencies - - processor = Sprockets::DirectiveProcessor.new(path) - - last_file_added = nil - - @dependencies = processor.directives.collect do |line, type, name| - if name && name[%r{^\.}] - name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') - else - raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree' - end - - files = case type - when 'require' - [ name ] - when 'require_tree' - Dir[File.join(source_root, name, '**/*')].find_all { |found_path| - found_path != path && File.file?(found_path) && found_path[extension_filter] - }.sort.collect { |path| path.gsub(%r{^#{source_root}/}, '') } - when 'require_self' - @insert_after = last_file_added - [] - else - [] - end - - files.collect do |file| - if result = path_searcher.find(file) - new_file = self.class.new(*[ result, self ].flatten) - last_file_added = new_file.path - new_file - else - raise Sprockets::FileNotFound.new("Could not find #{file}, referenced from #{path}:#{line}") - end - end - end.flatten - end - - def logical_path - path.gsub(%r{^#{source_root}/}, '').gsub(%r{\..+$}, '') - end - - private - def read - File.read(path) - end - - def process_data_by_filename(path, data = nil) - case extension = File.extname(path) - when '' - data || '' - when '.js' - data || %{} - when '.css' - data || %{} - else - if engine = Sprockets.engines(extension) - data = engine.new(path) { data || read }.render(self) - data = %{} if extension == '.jst' - - process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data) - else - data || '' - end - end - end - end -end - diff --git a/lib/jasmine/headless/spec_file_analyzer.rb b/lib/jasmine/headless/spec_file_analyzer.rb index 0ac0899..f32308d 100644 --- a/lib/jasmine/headless/spec_file_analyzer.rb +++ b/lib/jasmine/headless/spec_file_analyzer.rb @@ -14,7 +14,7 @@ module Jasmine::Headless data = File.read(file) if data.respond_to?(:encode) - data.encode!('US-ASCII', 'UTF-8', :invalid => :replace) + data.encode!('US-ASCII', 'UTF-8', :invalid => :replace, :undef => :replace) else require 'iconv' ic = Iconv.new('UTF-8//IGNORE', 'US-ASCII') diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index a47e957..8e9d571 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -27,160 +27,8 @@ describe Jasmine::Headless::FilesList do end end - describe '#use_config' do - let(:files_list) { described_class.new(:config => config) } - - include FakeFS::SpecHelpers - - no_default_files! - - let(:src_dir) { 'src' } - let(:spec_dir) { 'spec' } - - let(:first_file) { File.join(src_dir, 'js/first_file.js') } - let(:src_file) { File.join(src_dir, 'js/src_file.js') } - let(:spec_file) { File.join(spec_dir, 'spec_file_spec.js') } - let(:helper_file) { File.join(spec_dir, 'helper/helper_file.js') } - let(:stylesheet_file) { File.join(src_dir, 'stylesheet/blah.css') } - - before do - [ first_file, src_file, spec_file, helper_file, stylesheet_file ].each do |file| - FileUtils.mkdir_p File.split(file).first - File.open(file, 'w') - end - end - - shared_examples_for :reading_data do - let(:expected_files) do - [ - File.expand_path(first_file), - File.expand_path(src_file), - File.expand_path(stylesheet_file), - File.expand_path(helper_file), - File.expand_path(spec_file) - ] - end - - it 'should read the data from the jasmine.yml file and add the files' do - files_list.files.should == expected_files - - files_list.spec_files.should == [ File.expand_path(spec_file) ] - end - end - - context 'with normal list' do - let(:config) { { - 'src_dir' => src_dir, - 'spec_dir' => spec_dir, - 'src_files' => [ 'js/first_file.js', 'js/*.js' ], - 'spec_files' => [ '*_spec.js' ], - 'helpers' => [ 'helper/*.js' ], - 'stylesheets' => [ 'stylesheet/*.css' ] - } } - - it_should_behave_like :reading_data - end - - context 'with multidimensional list' do - let(:config) { { - 'src_dir' => src_dir, - 'spec_dir' => spec_dir, - 'src_files' => [ [ 'js/first_file.js', 'js/*.js' ] ], - 'spec_files' => [ '*_spec.js' ], - 'helpers' => [ 'helper/*.js' ], - 'stylesheets' => [ 'stylesheet/*.css' ] - } } - - it_should_behave_like :reading_data - end - - context 'with multidimensional src dir' do - let(:config) { { - 'src_dir' => [ src_dir ], - 'spec_dir' => spec_dir, - 'src_files' => [ [ 'js/first_file.js', 'js/*.js' ] ], - 'spec_files' => [ '*_spec.js' ], - 'helpers' => [ 'helper/*.js' ], - 'stylesheets' => [ 'stylesheet/*.css' ] - } } - - it_should_behave_like :reading_data - end - end - - context 'with filtered specs' do - let(:files_list) { Jasmine::Headless::FilesList.new(:only => filter, :config => config) } - let(:spec_dir) { 'spec' } - - include FakeFS::SpecHelpers - - no_default_files! - - let(:config) { { - 'spec_files' => [ '*_spec.js' ], - 'spec_dir' => spec_dir - } } - - let(:spec_files) { %w{one_spec.js two_spec.js whatever.js} } - - before do - spec_files.each do |file| - FileUtils.mkdir_p spec_dir - File.open(File.join(spec_dir, file), 'w') - end - end - - context 'empty filter' do - let(:filter) { [] } - - it 'should return all files for filtered and all files' do - files_list.files.any? { |file| file['two_spec.js'] }.should be_true - files_list.filtered?.should be_false - files_list.should_not have_spec_outside_scope - files_list.spec_files.sort.should == %w{one_spec.js two_spec.js}.sort.collect { |file| File.expand_path(File.join(spec_dir, file)) } - end - end - - context 'filter with a file that is matchable' do - let(:filter) { [ File.expand_path('spec/one_spec.js') ] } - - it 'should return all files for files' do - files_list.files.any? { |file| file['two_spec.js'] }.should be_true - files_list.filtered?.should be_true - files_list.should_not have_spec_outside_scope - files_list.spec_files.should == filter - end - - it 'should return only filtered files for filtered_files' do - files_list.filtered_files.any? { |file| file['two_spec.js'] }.should be_false - files_list.should_not have_spec_outside_scope - end - end - - context 'filter with a glob' do - let(:filter) { [ File.expand_path('spec/one*') ] } - - it 'should return all files for files' do - files_list.files.any? { |file| file['two_spec.js'] }.should be_true - files_list.filtered?.should be_true - files_list.should_not have_spec_outside_scope - end - - it 'should return only filtered files for filtered_files' do - files_list.filtered_files.any? { |file| file['two_spec.js'] }.should be_false - files_list.should_not have_spec_outside_scope - end - end - - context 'filter with a file that is not even there' do - let(:filter) { [ File.expand_path('spec/whatever.js') ] } - - it 'should use the provided file' do - files_list.filtered_files.any? { |file| file['whatever.js'] }.should be_true - files_list.should have_spec_outside_scope - end - end - end + it 'should have tests for #use_config!' + it 'should have tests for #add_files' describe '#spec_file_line_numbers' do include FakeFS::SpecHelpers @@ -274,8 +122,12 @@ describe Jasmine::Headless::FilesList do let(:path_two) { 'two' } let(:path_three) { 'three' } - let(:file_one) { stub(:file_paths => [ path_one, path_two ] ) } - let(:file_two) { stub(:file_paths => [ path_two, path_three ] ) } + let(:file_one) { stub(:required_assets => [ asset_one, asset_two ] ) } + let(:file_two) { stub(:required_assets => [ asset_two, asset_three ] ) } + + let(:asset_one) { stub(:pathname => Pathname(path_one)) } + let(:asset_two) { stub(:pathname => Pathname(path_two)) } + let(:asset_three) { stub(:pathname => Pathname(path_three)) } before do files_list.stubs(:required_files).returns([ file_one, file_two ]) @@ -323,21 +175,5 @@ describe Jasmine::Headless::FilesList do it { should == [ file_one, file_two, file_four ] } end end - - describe '#files_to_html' do - let(:file_one) { 'path/one' } - let(:file_two) { 'path/two' } - - before do - files_list.stubs(:files).returns([ file_one, file_two ]) - files_list.stubs(:search_paths).returns([ 'path' ]) - - Jasmine::Headless::RequiredFile.any_instance.stubs(:to_html).returns('made it') - end - - it 'should render all the files' do - files_list.files_to_html.should == [ 'made it', 'made it' ] - end - end end diff --git a/spec/lib/jasmine/headless/path_searcher_spec.rb b/spec/lib/jasmine/headless/path_searcher_spec.rb deleted file mode 100644 index 9c7f4fd..0000000 --- a/spec/lib/jasmine/headless/path_searcher_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'spec_helper' - -describe Jasmine::Headless::PathSearcher do - include FakeFS::SpecHelpers - - let(:path) { File.expand_path('path') } - let(:paths) { [ path ] } - let(:source) { stub(:search_paths => paths, :extension_filter => %r{.*}) } - let(:path_searcher) { described_class.new(source) } - - let(:filename) { 'file.js' } - - let(:file) { File.join(path, filename) } - - describe '#find' do - subject { path_searcher.find(search) } - - before do - FileUtils.mkdir_p path - File.open(file, 'wb') - end - - context 'found file' do - let(:search) { 'file' } - - it 'should find the file' do - subject.should == [ File.expand_path(file), path ] - end - end - - context 'not found file' do - let(:search) { 'other' } - - it 'should not find the file' do - subject.should be_false - end - end - end -end - diff --git a/spec/lib/jasmine/headless/required_file_spec.rb b/spec/lib/jasmine/headless/required_file_spec.rb deleted file mode 100644 index ea98ea1..0000000 --- a/spec/lib/jasmine/headless/required_file_spec.rb +++ /dev/null @@ -1,240 +0,0 @@ -require 'spec_helper' - -describe Jasmine::Headless::RequiredFile do - let(:source_root) { File.expand_path('source_root') } - let(:path) { File.join(source_root, 'path.js') } - - let(:file) { described_class.new(path, source_root, files_list) } - - let(:paths) { [ source_root ] } - let(:path_searcher) { stub } - let(:files_list) { stub(:path_searcher => path_searcher) } - - subject { file } - - its(:path) { should == path } - its(:source_root) { should == source_root } - its(:parent) { should == files_list } - - describe '#has_dependencies?' do - it 'should have dependencies' do - file.instance_variable_set(:@dependencies, [ 1 ]) - - file.should have_dependencies - end - - it 'should not have dependencies' do - file.instance_variable_set(:@dependencies, []) - - file.should_not have_dependencies - end - end - - describe '#includes?' do - it 'includes itself' do - file.includes?(path).should be_true - end - - context 'with dependencies' do - let(:other_file) { stub } - let(:other_path) { 'other path' } - let(:third_path) { 'third path' } - - before do - other_file.stubs(:includes?).with(other_path).returns(true) - other_file.stubs(:includes?).with(third_path).returns(false) - - file.stubs(:dependencies).returns([ other_file ]) - end - - it 'checks dependencies' do - file.includes?(third_path).should be_false - file.includes?(other_path).should be_true - end - end - end - - describe '#to_html' do - subject { file.to_html } - - context '.js' do - let(:path) { 'path.js' } - - it { should == %{} } - end - - context '.css' do - let(:path) { 'path.css' } - - it { should == %{} } - end - - context 'with tilt template' do - include FakeFS::SpecHelpers - - let(:content) { 'content' } - - before do - File.open(path, 'wb') { |fh| fh.print content } - end - - let(:klass) do - Class.new(Tilt::Template) do - def prepare ; end - - def evaluate(scope, locals, &block) - "#{file} made it #{data}" - end - end - end - - let(:other_klass) do - Class.new(Tilt::Template) do - def prepare ; end - - def evaluate(scope, locals, &block) - data - end - end - end - - before do - Sprockets.stubs(:engines).with('.tilt').returns(klass) - Sprockets.stubs(:engines).with('.jst').returns(other_klass) - end - - context '.tilt' do - let(:path) { 'path.tilt' } - - it { should == %{#{path} made it #{content}} } - end - - context '.tilt.tilt' do - let(:path) { 'path.tilt.tilt' } - - it { should == %{path.tilt made it #{path} made it #{content}} } - end - - context '.jst.tilt' do - let(:path) { 'path.jst.tilt' } - - it { should == %{} } - end - end - end - - describe '#dependencies' do - include FakeFS::SpecHelpers - - let(:directive) { 'require' } - - let(:dirname) { 'subdir/subsubdir' } - let(:dir) { File.join(source_root, dirname) } - - let(:path) { path_file } - let(:path_file) { File.join(dir, 'path.js') } - let(:req_file) { File.join(dir, "req.js") } - let(:other_file) { File.join(dir, "other.js") } - - let(:content) { "//= #{directive} '#{req_name}'\njavascript" } - - before do - FileUtils.mkdir_p dir - File.open(path_file, 'wb') { |fh| fh.print content } - File.open(req_file, 'wb') - File.open(other_file, 'wb') - end - - subject { file.dependencies } - - context 'absolute' do - context 'require' do - context 'file exists' do - let(:req_name) { File.join(dirname, 'req') } - - before do - path_searcher.expects(:find).with(File.join(dirname, 'req')).returns([ req_file, source_root ]) - end - - it { should == [ described_class.new(req_file, source_root, file) ] } - end - - context 'file does not exist' do - let(:req_name) { File.join(dirname, 'bad') } - - before do - path_searcher.expects(:find).with(File.join(dirname, 'bad')).returns(false) - end - - it 'should raise an exception' do - expect { subject }.to raise_error(Sprockets::FileNotFound) - end - end - end - end - - context 'relative' do - context 'require' do - context 'file exists' do - let(:req_name) { './req' } - - before do - path_searcher.expects(:find).with(File.join(dirname, 'req')).returns([ req_file, source_root ]) - end - - it { should == [ described_class.new(req_file, source_root, file) ] } - end - - context 'file does not exist' do - let(:req_name) { './bad' } - - before do - path_searcher.expects(:find).with(File.join(dirname, 'bad')).returns(false) - end - - it 'should raise an exception' do - expect { subject }.to raise_error(Sprockets::FileNotFound) - end - end - end - end - - context 'require_self' do - subject { file.file_paths } - - let(:content) do - <<-ENDTXT -//= require #{dirname}/req -//= require_self -//= require #{dirname}/other -ENDTXT - end - - before do - path_searcher.expects(:find).with(File.join(dirname, 'req')).returns([ req_file, source_root ]) - path_searcher.expects(:find).with(File.join(dirname, 'other')).returns([ other_file, source_root ]) - end - - it { should == [ - req_file, - path_file, - other_file - ] } - end - end - - describe '#file_paths' do - let(:other_path) { File.join(source_root, 'other_path.js') } - let(:other_file) { described_class.new(other_path, source_root, file) } - - before do - file.stubs(:dependencies).returns([ other_file ]) - other_file.stubs(:dependencies).returns([]) - end - - it 'should flatten all the paths in itself and descendents' do - file.file_paths.should == [ other_path, path ] - end - end -end - diff --git a/vendor/assets/javascripts/headless_reporter_result.js b/vendor/assets/javascripts/headless_reporter_result.js index 5498284..3ffc20f 100644 --- a/vendor/assets/javascripts/headless_reporter_result.js +++ b/vendor/assets/javascripts/headless_reporter_result.js @@ -1,13 +1,16 @@ -(function() { + window.HeadlessReporterResult = (function() { + function HeadlessReporterResult(name, splitName) { this.name = name; this.splitName = splitName; this.results = []; } + HeadlessReporterResult.prototype.addResult = function(message) { return this.results.push(message); }; + HeadlessReporterResult.prototype.print = function() { var bestChoice, output, result, _i, _len, _ref, _results; output = this.name.foreground('red'); @@ -25,10 +28,15 @@ output += (" (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")").foreground('red').bright(); } JHW.stdout.puts(" " + output); - _results.push(result.line != null ? JHW.stdout.puts((" " + result.line).foreground('yellow')) : void 0); + if (result.line != null) { + _results.push(JHW.stdout.puts((" " + result.line).foreground('yellow'))); + } else { + _results.push(void 0); + } } return _results; }; + HeadlessReporterResult.findSpecLine = function(splitName) { var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref; bestChoice = { @@ -49,9 +57,7 @@ for (_i = 0, _len = newLineNumberInfo.length; _i < _len; _i++) { line = newLineNumberInfo[_i]; lastLine = line; - if (line > lineNumber) { - break; - } + if (line > lineNumber) break; } lineNumber = lastLine; } @@ -67,6 +73,7 @@ } return bestChoice; }; + return HeadlessReporterResult; + })(); -}).call(this); diff --git a/vendor/assets/javascripts/intense.js b/vendor/assets/javascripts/intense.js index 73b1556..8341721 100644 --- a/vendor/assets/javascripts/intense.js +++ b/vendor/assets/javascripts/intense.js @@ -1,5 +1,6 @@ (function() { var code, method, _ref; + window.Intense = { colors: { black: 0, @@ -29,15 +30,15 @@ }, useColors: true, moveBack: function(count) { - if (count == null) { - count = 1; - } + if (count == null) count = 1; return "\033[" + count + "D"; } }; + _ref = Intense.methods; for (method in _ref) { code = _ref[method]; String.prototype[method] = code; } + }).call(this); diff --git a/vendor/assets/javascripts/jasmine-extensions.js b/vendor/assets/javascripts/jasmine-extensions.js index c45e757..da5394c 100644 --- a/vendor/assets/javascripts/jasmine-extensions.js +++ b/vendor/assets/javascripts/jasmine-extensions.js @@ -1,9 +1,11 @@ (function() { var generator, getSplitName, method, pauseAndRun, _i, _len, _ref; var __slice = Array.prototype.slice; + if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not laoded!"); } + if (window.JHW) { getSplitName = function(parts) { parts.push(String(this.description).replace(/[\n\r]/g, ' ')); @@ -110,7 +112,11 @@ _results = []; for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { reporter = _ref2[_j]; - _results.push(reporter[method] != null ? reporter[method].apply(reporter, args) : void 0); + if (reporter[method] != null) { + _results.push(reporter[method].apply(reporter, args)); + } else { + _results.push(void 0); + } } return _results; }; @@ -119,4 +125,5 @@ } } } + }).call(this); diff --git a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js index e81a776..70f0800 100644 --- a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js +++ b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js @@ -1,9 +1,10 @@ -(function() { - var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not loaded!"); } + jasmine.HeadlessConsoleReporter = (function() { + function HeadlessConsoleReporter(callback) { this.callback = callback != null ? callback : null; this.results = []; @@ -13,11 +14,10 @@ this.position = 0; this.positions = "|/-\\"; } + HeadlessConsoleReporter.prototype.reportRunnerResults = function(runner) { var output, result, resultLine, runtime, _i, _len, _ref; - if (this.hasError()) { - return; - } + if (this.hasError()) return; runtime = (new Date() - this.startTime) / 1000.0; JHW.stdout.print("\n"); resultLine = this._formatResultLine(runtime); @@ -34,22 +34,20 @@ result = _ref[_i]; result.print(); } - if (window.JHW) { - window.onbeforeunload = null; - } + if (window.JHW) window.onbeforeunload = null; return JHW.finishSuite(); }; + HeadlessConsoleReporter.prototype.reportRunnerStarting = function(runner) { this.startTime = new Date(); if (!this.hasError()) { return JHW.stdout.puts("\nRunning Jasmine specs...".bright()); } }; + HeadlessConsoleReporter.prototype.reportSpecResults = function(spec) { var failureResult, foundLine, result, results, testCount, _i, _len, _ref; - if (this.hasError()) { - return; - } + if (this.hasError()) return; JHW.ping(); results = spec.results(); this.length++; @@ -76,35 +74,37 @@ return this.results.push(failureResult); } }; + HeadlessConsoleReporter.prototype.reportSpecStarting = function(spec) { if (this.hasError()) { spec.finish(); return spec.suite.finish(); } }; + HeadlessConsoleReporter.prototype.reportSpecWaiting = function() { var first, runner; + var _this = this; runner = null; if (!this.timer) { this.timer = true; first = true; - runner = __bind(function() { - return this.timer = setTimeout(__bind(function() { - if (this.timer) { - if (!first) { - JHW.stdout.print(Intense.moveBack()); - } - JHW.stdout.print(this.positions.substr(this.position, 1).foreground('yellow')); - this.position += 1; - this.position %= this.positions.length; + runner = function() { + return _this.timer = setTimeout(function() { + if (_this.timer) { + if (!first) JHW.stdout.print(Intense.moveBack()); + JHW.stdout.print(_this.positions.substr(_this.position, 1).foreground('yellow')); + _this.position += 1; + _this.position %= _this.positions.length; first = false; return runner(); } - }, this), 750); - }, this); + }, 750); + }; return runner(); } }; + HeadlessConsoleReporter.prototype.reportSpecRunning = function() { if (this.timer) { clearTimeout(this.timer); @@ -112,10 +112,13 @@ return JHW.stdout.print(Intense.moveBack()); } }; + HeadlessConsoleReporter.prototype.reportSuiteResults = function(suite) {}; + HeadlessConsoleReporter.prototype.hasError = function() { return JHW._hasErrors; }; + HeadlessConsoleReporter.prototype._formatResultLine = function(runtime) { var line; line = []; @@ -127,6 +130,7 @@ line.push((runtime === 1.0 ? "sec" : "secs") + '.'); return line.join(' '); }; + return HeadlessConsoleReporter; + })(); -}).call(this); diff --git a/vendor/assets/javascripts/prolog.js b/vendor/assets/javascripts/prolog.js index 2fd358b..eb2b69f 100644 --- a/vendor/assets/javascripts/prolog.js +++ b/vendor/assets/javascripts/prolog.js @@ -1,5 +1,6 @@ (function() { var createHandle, handle, _i, _len, _ref; + if (window.JHW) { window.console = { log: function(data) { @@ -40,9 +41,7 @@ e = e || window.event; JHW.hasError(); JHW.stdout.puts('The code tried to leave the test page. Check for unhandled form submits and link clicks.'); - if (e) { - e.returnValue = 'string'; - } + if (e) e.returnValue = 'string'; return 'string'; }; window.confirm = function(message) { @@ -84,6 +83,9 @@ return JHW.stdout.puts(msg); }; } + window.CoffeeScriptToFilename = {}; + window.CSTF = window.CoffeeScriptToFilename; + }).call(this); From 2316041580c16b30fb994cecf01292842e83945f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 22 Nov 2011 10:51:46 -0500 Subject: [PATCH 17/20] ok i actually want a bundled asset --- lib/jasmine/headless/files_list.rb | 4 ++-- spec/lib/jasmine/headless/files_list_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 9130ab2..f34fb38 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -70,7 +70,7 @@ module Jasmine::Headless end def files - required_files.collect { |file| file.send(:required_assets).collect { |asset| asset.pathname.to_s } }.flatten.uniq + required_files.collect { |file| file.to_a.collect { |asset| asset.pathname.to_s } }.flatten.uniq end def spec_files @@ -233,7 +233,7 @@ module Jasmine::Headless end def add_path(path, type) - asset = sprockets_environment.find_asset(path, :bundle => false) + asset = sprockets_environment.find_asset(path) @required_files << asset diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 8e9d571..219f5b3 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -122,8 +122,8 @@ describe Jasmine::Headless::FilesList do let(:path_two) { 'two' } let(:path_three) { 'three' } - let(:file_one) { stub(:required_assets => [ asset_one, asset_two ] ) } - let(:file_two) { stub(:required_assets => [ asset_two, asset_three ] ) } + let(:file_one) { stub(:to_a => [ asset_one, asset_two ] ) } + let(:file_two) { stub(:to_a => [ asset_two, asset_three ] ) } let(:asset_one) { stub(:pathname => Pathname(path_one)) } let(:asset_two) { stub(:pathname => Pathname(path_two)) } From 38327e6a95e6dad18c4a861d3019547d4732856d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 22 Nov 2011 12:14:00 -0500 Subject: [PATCH 18/20] more cleanups for things i found that are broken --- lib/jasmine/headless.rb | 1 + lib/jasmine/headless/coffee_template.rb | 8 +++----- lib/jasmine/headless/command_line.rb | 2 ++ lib/jasmine/headless/files_list.rb | 4 +++- lib/jasmine/headless/js_template.rb | 6 +++++- lib/jasmine/headless/jst_template.rb | 10 ++++++++++ 6 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 lib/jasmine/headless/jst_template.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 05076ac..76d96ea 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -16,6 +16,7 @@ module Jasmine::Headless 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 :Report, 'jasmine/headless/report' diff --git a/lib/jasmine/headless/coffee_template.rb b/lib/jasmine/headless/coffee_template.rb index 0cc7d95..b6764e6 100644 --- a/lib/jasmine/headless/coffee_template.rb +++ b/lib/jasmine/headless/coffee_template.rb @@ -11,12 +11,10 @@ module Jasmine::Headless 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) ] diff --git a/lib/jasmine/headless/command_line.rb b/lib/jasmine/headless/command_line.rb index a746247..f1181a9 100644 --- a/lib/jasmine/headless/command_line.rb +++ b/lib/jasmine/headless/command_line.rb @@ -10,6 +10,8 @@ module Jasmine::Headless runner = Runner.new(options) if options[:do_list] + FilesList.reset! + files_list = FilesList.new(:config => runner.jasmine_config) files_list.files.each { |file| puts file } else diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index f34fb38..37bc782 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -44,6 +44,7 @@ module Jasmine::Headless 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 end @@ -102,6 +103,7 @@ module Jasmine::Headless @sprockets_environment.append_path(path) end + @sprockets_environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons) @sprockets_environment end @@ -157,7 +159,7 @@ module Jasmine::Headless alert_time = nil end - sprockets_environment.find_asset(file, :bundle => false).to_s + sprockets_environment.find_asset(file, :bundle => false).body end.flatten.compact.reject(&:empty?) end diff --git a/lib/jasmine/headless/js_template.rb b/lib/jasmine/headless/js_template.rb index 81a2fb9..e4a134f 100644 --- a/lib/jasmine/headless/js_template.rb +++ b/lib/jasmine/headless/js_template.rb @@ -7,7 +7,11 @@ module Jasmine::Headless def prepare ; end def evaluate(scope, locals, &block) - file ? %{} : data + if data['from="jhw"'] + data + else + file ? %{} : data + end end end end diff --git a/lib/jasmine/headless/jst_template.rb b/lib/jasmine/headless/jst_template.rb new file mode 100644 index 0000000..f350686 --- /dev/null +++ b/lib/jasmine/headless/jst_template.rb @@ -0,0 +1,10 @@ +require 'sprockets/jst_processor' + +module Jasmine::Headless + class JSTTemplate < Sprockets::JstProcessor + def evaluate(*args) + %{} + end + end +end + From 634f60ffe6799723fd9f6d0c5f4e95cccc2d4a69 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 22 Nov 2011 14:01:05 -0500 Subject: [PATCH 19/20] fix tests --- lib/jasmine/headless/coffee_template.rb | 1 + spec/lib/jasmine/headless/coffee_template_spec.rb | 4 ++-- spec/lib/jasmine/headless/template_writer_spec.rb | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/jasmine/headless/coffee_template.rb b/lib/jasmine/headless/coffee_template.rb index b6764e6..5ad8918 100644 --- a/lib/jasmine/headless/coffee_template.rb +++ b/lib/jasmine/headless/coffee_template.rb @@ -1,4 +1,5 @@ require 'tilt/template' +require 'rainbow' module Jasmine::Headless class CoffeeTemplate < Tilt::Template diff --git a/spec/lib/jasmine/headless/coffee_template_spec.rb b/spec/lib/jasmine/headless/coffee_template_spec.rb index f07e769..c4abdea 100644 --- a/spec/lib/jasmine/headless/coffee_template_spec.rb +++ b/spec/lib/jasmine/headless/coffee_template_spec.rb @@ -39,7 +39,7 @@ describe Jasmine::Headless::CoffeeTemplate do end it 'should return the cached file' do - subject.should include(%{}) + subject.should include(%{}) end end @@ -47,7 +47,7 @@ describe Jasmine::Headless::CoffeeTemplate do let(:cache_return) { false } it 'should return the generated js' do - subject.should include(%{}) + subject.should include(%{}) end end end diff --git a/spec/lib/jasmine/headless/template_writer_spec.rb b/spec/lib/jasmine/headless/template_writer_spec.rb index 3b4ad0d..2a9c380 100644 --- a/spec/lib/jasmine/headless/template_writer_spec.rb +++ b/spec/lib/jasmine/headless/template_writer_spec.rb @@ -53,6 +53,8 @@ describe Jasmine::Headless::TemplateWriter do runner.stubs(:keep_runner).returns(true) runner.stubs(:runner_filename).returns(false) + + Sprockets::Environment.any_instance.stubs(:find_asset).returns(stub(:body => '')) end let(:files_list) { Jasmine::Headless::FilesList.new } From edf5a49f27c9889d87d6f2af51e54d807152806d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 23 Nov 2011 09:35:44 -0500 Subject: [PATCH 20/20] test coverage and cleanup in anticipation of merge --- .gitignore | 1 + Gemfile | 2 ++ lib/digest/jasmine_test.rb | 20 ++++++++++++ lib/jasmine-headless-webkit.rb | 4 +++ lib/jasmine/headless/files_list.rb | 8 ----- lib/jasmine/headless/task.rb | 21 ------------- .../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 +++++++++++++ spec/spec_helper.rb | 5 +++ 10 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 lib/digest/jasmine_test.rb create mode 100644 spec/lib/jasmine/headless/css_template_spec.rb create mode 100644 spec/lib/jasmine/headless/js_template_spec.rb create mode 100644 spec/lib/jasmine/headless/jst_template_spec.rb diff --git a/.gitignore b/.gitignore index 420aafa..f301dd4 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ jhw-test .jhw-cache/ _site/ jhw.*.html +coverage/ diff --git a/Gemfile b/Gemfile index 4896aec..59c357c 100644 --- a/Gemfile +++ b/Gemfile @@ -17,3 +17,5 @@ gem 'facter' gem 'jquery-rails' gem 'ejs' + +gem 'simplecov' diff --git a/lib/digest/jasmine_test.rb b/lib/digest/jasmine_test.rb new file mode 100644 index 0000000..f29e207 --- /dev/null +++ b/lib/digest/jasmine_test.rb @@ -0,0 +1,20 @@ +module Digest + class JasmineTest + def self.file(file) + new + end + + def file(file) + self + end + + def hexdigest + 'test' + end + + def update(prefix) + self + end + end +end + diff --git a/lib/jasmine-headless-webkit.rb b/lib/jasmine-headless-webkit.rb index 556c370..a99ab37 100644 --- a/lib/jasmine-headless-webkit.rb +++ b/lib/jasmine-headless-webkit.rb @@ -1,3 +1,7 @@ require 'jasmine/headless' require 'jasmine/headless/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 +module Digest + autoload :JasmineTest, 'digest/jasmine_test' +end + diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 37bc782..f533218 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -107,10 +107,6 @@ module Jasmine::Headless @sprockets_environment end - def path_searcher - @path_searcher ||= PathSearcher.new(self) - end - def has_spec_outside_scope? if is_outside_scope = !spec_filter.empty? is_outside_scope = spec_dir.any? do |dir| @@ -244,10 +240,6 @@ module Jasmine::Headless end end - def include_spec_file?(file) - spec_filter.empty? || spec_filter.include?(file) - end - def src_dir config_dir_or_pwd('src_dir') end diff --git a/lib/jasmine/headless/task.rb b/lib/jasmine/headless/task.rb index 78eec46..3368c6b 100644 --- a/lib/jasmine/headless/task.rb +++ b/lib/jasmine/headless/task.rb @@ -1,23 +1,3 @@ -module Digest - class JasmineTest - def self.file(file) - new - end - - def file(file) - self - end - - def hexdigest - 'test' - end - - def update(prefix) - self - end - end -end - module Jasmine module Headless class Task @@ -41,7 +21,6 @@ module Jasmine private def create_rails_compliant_task if Rails.respond_to?(:version) && Rails.version >= "3.1.0" - desc 'Force generate static assets without an MD5 hash, all assets end with -test.' task 'assets:precompile:for_testing' => :environment do $stderr.puts "This task is deprecated and will be removed after 2012-01-01" diff --git a/spec/lib/jasmine/headless/css_template_spec.rb b/spec/lib/jasmine/headless/css_template_spec.rb new file mode 100644 index 0000000..ca180e8 --- /dev/null +++ b/spec/lib/jasmine/headless/css_template_spec.rb @@ -0,0 +1,25 @@ +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 new file mode 100644 index 0000000..2f75b28 --- /dev/null +++ b/spec/lib/jasmine/headless/js_template_spec.rb @@ -0,0 +1,31 @@ +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 'jhw content' do + let(:data) { 'from="jhw"' } + + it { should == data } + end +end diff --git a/spec/lib/jasmine/headless/jst_template_spec.rb b/spec/lib/jasmine/headless/jst_template_spec.rb new file mode 100644 index 0000000..943a7e8 --- /dev/null +++ b/spec/lib/jasmine/headless/jst_template_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Jasmine::Headless::JSTTemplate do + include FakeFS::SpecHelpers + + let(:template) { described_class.new(file) } + let(:file) { 'file' } + let(:data) { 'data' } + + let(:context) { stub(:logical_path => 'path') } + + before do + File.open(file, 'wb') { |fh| fh.print data } + end + + subject { template.render(context) } + + it { should include(%{