From 16f867af098a0bd8c64b241413e72dda13239a17 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 25 Nov 2011 16:18:06 -0500 Subject: [PATCH] a little cleanup work --- lib/jasmine/headless.rb | 2 + lib/jasmine/headless/files_list.rb | 66 +++++-------------- lib/jasmine/headless/unique_asset_list.rb | 14 ++++ spec/bin/jasmine-headless-webkit_spec.rb | 18 ----- spec/integration/sprockets_spec.rb | 19 ++++++ .../{application.js => application.js.erb} | 0 .../with_sprockets_includes.yml | 2 +- spec/lib/jasmine/headless/files_list_spec.rb | 10 +-- .../headless/unique_asset_list_spec.rb | 22 +++++++ spec/spec_helper.rb | 12 ++++ 10 files changed, 93 insertions(+), 72 deletions(-) create mode 100644 lib/jasmine/headless/unique_asset_list.rb create mode 100644 spec/integration/sprockets_spec.rb rename spec/jasmine/with_sprockets_includes/assets/{application.js => application.js.erb} (100%) create mode 100644 spec/lib/jasmine/headless/unique_asset_list_spec.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 76d96ea..4bf9cbc 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -10,7 +10,9 @@ 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 :UniqueAssetList, 'jasmine/headless/unique_asset_list' autoload :TemplateWriter, 'jasmine/headless/template_writer' diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index f533218..53ab9cd 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -13,15 +13,17 @@ module Jasmine::Headless require 'rubygems' - raise StandardError.new("A newer version of Rubygems is required to use vendored assets. Please upgrade.") if !Gem::Specification.respond_to?(:map) + raise StandardError.new("A newer version of Rubygems is required to use vendored assets. Please upgrade.") if !Gem::Specification.respond_to?(:each) @vendor_asset_paths = [] - Gem::Specification.map { |spec| + Gem::Specification.each do |spec| path = File.join(spec.gem_dir, 'vendor/assets/javascripts') - File.directory?(path) ? path : nil - }.compact + @vendor_asset_paths << path if File.directory?(path) + end + + @vendor_asset_paths end def reset! @@ -60,7 +62,7 @@ module Jasmine::Headless def initialize(options = {}) @options = options - @required_files = [] + @required_files = UniqueAssetList.new @potential_files_to_filter = [] self.class.default_files.each do |file| @@ -71,7 +73,7 @@ module Jasmine::Headless end def files - required_files.collect { |file| file.to_a.collect { |asset| asset.pathname.to_s } }.flatten.uniq + required_files.flatten.collect { |asset| asset.pathname.to_s }.uniq end def spec_files @@ -88,9 +90,9 @@ module Jasmine::Headless return @search_paths if @search_paths @search_paths = [ Jasmine::Core.path ] + @search_paths += self.class.vendor_asset_paths @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 @@ -99,9 +101,7 @@ module Jasmine::Headless return @sprockets_environment if @sprockets_environment @sprockets_environment = Sprockets::Environment.new - search_paths.each do |path| - @sprockets_environment.append_path(path) - end + search_paths.each { |path| @sprockets_environment.append_path(path) } @sprockets_environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons) @sprockets_environment @@ -111,9 +111,7 @@ 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 @@ -156,19 +154,11 @@ module Jasmine::Headless end sprockets_environment.find_asset(file, :bundle => false).body - end.flatten.compact.reject(&:empty?) + end.compact.reject(&:empty?) end def spec_filter - return @spec_filter if @spec_filter - - @spec_filter = begin - if @options[:only] - @options[:only].collect { |path| expanded_dir(path) }.flatten - else - [] - end - end + @spec_filter ||= (@options[:only] && @options[:only].collect { |path| expanded_dir(path) }.flatten) || [] end SEARCH_ROOTS = { @@ -185,31 +175,15 @@ module Jasmine::Headless %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) + add_files(@searches[type] = data.flatten, type, send(SEARCH_ROOTS[type])) 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) - dirs.each do |dir| - patterns.each do |search| - search = File.expand_path(File.join(dir, search)) - - Dir[search].find_all { |file| file[extension_filter] }.each do |path| - add_path(path, type) if File.file?(path) - end + dirs.product(patterns).each do |search| + Dir[File.join(*search)].find_all { |file| file[extension_filter] }.each do |path| + add_path(path, type) if File.file?(path) end end @@ -253,11 +227,7 @@ module Jasmine::Headless end def config_dir_or_pwd(dir) - found_dir = Dir.pwd - - if @options[:config] - found_dir = @options[:config][dir] || found_dir - end + found_dir = (@options[:config] && @options[:config][dir]) || Dir.pwd [ found_dir ].flatten.collect { |dir| File.expand_path(dir) } end diff --git a/lib/jasmine/headless/unique_asset_list.rb b/lib/jasmine/headless/unique_asset_list.rb new file mode 100644 index 0000000..6e27d4e --- /dev/null +++ b/lib/jasmine/headless/unique_asset_list.rb @@ -0,0 +1,14 @@ +module Jasmine::Headless + class UniqueAssetList < ::Array + def <<(asset) + raise StandardError.new("Not an asset") if !asset.respond_to?(:logical_path) + + super if !self.any? { |other| asset.logical_path == other.logical_path } + end + + def flatten + self.collect(&:to_a).flatten + end + end +end + diff --git a/spec/bin/jasmine-headless-webkit_spec.rb b/spec/bin/jasmine-headless-webkit_spec.rb index cd665d7..72845c0 100644 --- a/spec/bin/jasmine-headless-webkit_spec.rb +++ b/spec/bin/jasmine-headless-webkit_spec.rb @@ -138,23 +138,5 @@ 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 - - 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', - 'spec_helper.js', - 'spec/things/code_spec.js' - ) - end - end end diff --git a/spec/integration/sprockets_spec.rb b/spec/integration/sprockets_spec.rb new file mode 100644 index 0000000..792d488 --- /dev/null +++ b/spec/integration/sprockets_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +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 + + 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', + 'spec_helper.js', + 'spec/things/code_spec.js' + ) + end +end diff --git a/spec/jasmine/with_sprockets_includes/assets/application.js b/spec/jasmine/with_sprockets_includes/assets/application.js.erb similarity index 100% rename from spec/jasmine/with_sprockets_includes/assets/application.js rename to spec/jasmine/with_sprockets_includes/assets/application.js.erb diff --git a/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml b/spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml index 99cd320..cf685ca 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/**/*" + - "**/*" 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 219f5b3..5087399 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -79,7 +79,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, File.expand_path(src_dir), File.expand_path(spec_dir), path ] + files_list.search_paths.should == [ Jasmine::Core.path, path, File.expand_path(src_dir), File.expand_path(spec_dir) ] end end @@ -125,12 +125,12 @@ describe Jasmine::Headless::FilesList do 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)) } - let(:asset_three) { stub(:pathname => Pathname(path_three)) } + let(:asset_one) { stub(:pathname => Pathname(path_one), :to_ary => nil) } + let(:asset_two) { stub(:pathname => Pathname(path_two), :to_ary => nil) } + let(:asset_three) { stub(:pathname => Pathname(path_three), :to_ary => nil) } before do - files_list.stubs(:required_files).returns([ file_one, file_two ]) + files_list.stubs(:required_files).returns(Jasmine::Headless::UniqueAssetList.new([ file_one, file_two ])) end subject { files_list.files } diff --git a/spec/lib/jasmine/headless/unique_asset_list_spec.rb b/spec/lib/jasmine/headless/unique_asset_list_spec.rb new file mode 100644 index 0000000..4759292 --- /dev/null +++ b/spec/lib/jasmine/headless/unique_asset_list_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe Jasmine::Headless::UniqueAssetList do + let(:list) { described_class.new } + + let(:first) { stub(:logical_path => 'one') } + let(:second) { stub(:logical_path => 'two') } + let(:third) { stub(:logical_path => 'two') } + + it 'should raise an exception on a non-asset' do + expect { list << "whatever" }.to raise_error(StandardError) + end + + it 'should not add the same asset with the same logical path twice' do + list << first + list << second + list << third + + list.to_a.should == [ first, second ] + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5e67198..6529b58 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,6 +14,18 @@ RSpec.configure do |c| Jasmine::Headless::CacheableAction.enabled = false Jasmine::Headless::FilesList.reset! end + + c.before(:each, :type => :integration) do + let(:report) { 'spec/report.txt' } + + before do + FileUtils.rm_f report + end + + after do + FileUtils.rm_f report + end + end end specrunner = 'ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner'