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|