From d214674620350d4378020fc916666247b3336564 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 20 Nov 2011 13:35:30 -0500 Subject: [PATCH] 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