From 537b2e437d8acdf47e659977c4cd96475a88f058 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 18 Nov 2011 11:50:48 -0500 Subject: [PATCH] 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