diff --git a/Gemfile b/Gemfile index 7740bcd..5d44ae5 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,4 @@ gem 'guard-shell' gem 'guard-coffeescript' gem 'growl' gem 'rake', '0.8.7' +gem 'mocha' diff --git a/Guardfile b/Guardfile index 99417c6..f3a837a 100644 --- a/Guardfile +++ b/Guardfile @@ -9,7 +9,7 @@ end # A sample Guardfile # More info at https://github.com/guard/guard#readme -guard 'rspec', :version => 2 do +guard 'rspec', :version => 2, :all_on_start => false do watch(%r{^spec/.+_spec\.rb}) watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch(%r{^bin/(.+)}) { |m| "spec/bin/#{m[1]}_spec.rb" } diff --git a/bin/jasmine-headless-webkit b/bin/jasmine-headless-webkit index 61e23c4..57cacdc 100755 --- a/bin/jasmine-headless-webkit +++ b/bin/jasmine-headless-webkit @@ -17,6 +17,7 @@ require 'coffee-script' require 'rainbow' require 'jasmine/cli' +require 'jasmine/files_list' include Jasmine::CLI if !File.file?(File.join(gem_dir, RUNNER)) @@ -58,50 +59,15 @@ options = { read_defaults_files! opts.each(&@process_options) -@spec_filter = ARGV.dup - -data = YAML.load_file(options[:jasmine_config]) puts "Running Jasmine specs..." -files = %w{jasmine jasmine-html}.collect { |name| File.join(Jasmine.root, "lib/#{name}.js") } -files << File.join(gem_dir, 'jasmine/jasmine.headless-reporter.js') +files_list = Jasmine::FilesList.new( + :config => load_config(options[:jasmine_config]), + :only => ARGV.dup +) -[ [ 'src_files', 'src_dir' ], [ 'stylesheets', 'src_dir' ], [ 'helpers', 'spec_dir' ], [ 'spec_files', 'spec_dir' ] ].each do |searches, root| - if data[searches] ||= DEFAULTS[searches] - data[root] ||= DEFAULTS[root] - - data[searches].collect do |search| - path = search - path = File.join(data[root], path) if data[root] - found_files = Dir[path] - if searches == 'spec_files' - found_files = found_files.find_all { |file| use_spec?(file) } - end - files += found_files - end - end -end - -files = files.collect { |file| - case File.extname(file) - when '.js' - %{} - when '.coffee' - begin - %{} - rescue CoffeeScript::CompilationError => e - puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), e.message.to_s.color(:white) ] - exit 1 - ensure - fh.close - end - when '.css' - %{} - end -} - -output = jasmine_html_template(files) +output = jasmine_html_template(files_list.files_to_html) File.open(target = "specrunner.#{$$}.html", 'w') { |fh| fh.print output } system jasmine_command(options, target) diff --git a/lib/jasmine/cli.rb b/lib/jasmine/cli.rb index 2a58cbb..419e493 100644 --- a/lib/jasmine/cli.rb +++ b/lib/jasmine/cli.rb @@ -13,6 +13,10 @@ module Jasmine DEFAULTS_FILE = '.jasmine-headless-webkit' GLOBAL_DEFAULTS_FILE = File.expand_path("~/#{DEFAULTS_FILE}") + def load_config(file) + process_jasmine_config(YAML.load_file(file)) + end + def process_jasmine_config(overrides = {}) DEFAULTS.merge(overrides) end @@ -25,10 +29,6 @@ module Jasmine end end - def use_spec?(file) - @spec_filter.empty? || @spec_filter.include?(file) - end - def jasmine_html_template(files) <<-HTML diff --git a/lib/jasmine/files_list.rb b/lib/jasmine/files_list.rb new file mode 100644 index 0000000..477add2 --- /dev/null +++ b/lib/jasmine/files_list.rb @@ -0,0 +1,75 @@ +require 'jasmine' + +module Jasmine + class FilesList + attr_reader :files, :filtered_files + + DEFAULT_FILES = [ + File.join(Jasmine.root, "lib/jasmine.js"), + File.join(Jasmine.root, "lib/jasmine-html.js"), + File.expand_path('../../../jasmine/jasmine.headless-reporter.js', __FILE__) + ] + + def initialize(options = {}) + @options = options + @files = DEFAULT_FILES + use_config! if config? + end + + def use_spec?(file) + spec_filter.empty? || spec_filter.include?(file) + end + + def files_to_html + files.collect { |file| + case File.extname(file) + when '.js' + %{} + when '.coffee' + begin + %{} + rescue CoffeeScript::CompilationError => e + puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), e.message.to_s.color(:white) ] + exit 1 + ensure + fh.close + end + when '.css' + %{} + end + } + end + + private + def spec_filter + @options[:only] || [] + end + + def use_config! + @filtered_files = @files.dup + + data = @options[:config].dup + [ [ 'src_files', 'src_dir' ], [ 'stylesheets', 'src_dir' ], [ 'helpers', 'spec_dir' ], [ 'spec_files', 'spec_dir' ] ].each do |searches, root| + if data[searches] + data[searches].collect do |search| + path = search + path = File.join(data[root], path) if data[root] + found_files = Dir[path] + + @files += found_files + + if searches == 'spec_files' + found_files = found_files.find_all { |file| use_spec?(file) } + end + @filtered_files += found_files + end + end + end + end + + def config? + @options[:config] + end + end +end + diff --git a/spec/lib/jasmine/cli_spec.rb b/spec/lib/jasmine/cli_spec.rb index 8415b5e..8759092 100644 --- a/spec/lib/jasmine/cli_spec.rb +++ b/spec/lib/jasmine/cli_spec.rb @@ -58,32 +58,4 @@ describe Jasmine::CLI do all_data.should == [ global_test_data, test_data ] end end - - describe '#use_spec?' do - let(:spec_file) { 'my/spec.js' } - - context 'no filter provided' do - before do - @spec_filter = [] - end - - it "should allow the spec" do - use_spec?(spec_file).should be_true - end - end - - context 'filter provided' do - before do - @spec_filter = [ spec_file ] - end - - it "should use the spec" do - use_spec?(spec_file).should be_true - end - - it "should not use the spec" do - use_spec?('other/file').should be_false - end - end - end end diff --git a/spec/lib/jasmine/files_list_spec.rb b/spec/lib/jasmine/files_list_spec.rb new file mode 100644 index 0000000..7efc90f --- /dev/null +++ b/spec/lib/jasmine/files_list_spec.rb @@ -0,0 +1,141 @@ +require 'spec_helper' +require 'jasmine/files_list' +require 'fakefs/spec_helpers' +require 'coffee-script' + +describe Jasmine::FilesList do + let(:files_list) { Jasmine::FilesList.new } + + describe '#initialize' do + it "should have default files" do + files_list.files.should == [ + File.join(Jasmine.root, "lib/jasmine.js"), + File.join(Jasmine.root, "lib/jasmine-html.js"), + File.expand_path('jasmine/jasmine.headless-reporter.js') + ] + end + end + + describe '#use_config' do + let(:files_list) { Jasmine::FilesList.new(:config => config) } + + include FakeFS::SpecHelpers + + let(:src_dir) { 'src' } + let(:spec_dir) { 'spec' } + + 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 + [ src_file, spec_file, helper_file, stylesheet_file ].each do |file| + File.open(file, 'w') + end + end + + let(:config) { { + 'src_dir' => src_dir, + 'spec_dir' => spec_dir, + 'src_files' => [ 'js/*.js' ], + 'spec_files' => [ '*_spec.js' ], + 'helpers' => [ 'helper/*.js' ], + 'stylesheets' => [ 'stylesheet/*.css' ] + } } + + it 'should read the data from the jasmine.yml file and add the files' do + files_list.files.should == Jasmine::FilesList::DEFAULT_FILES + [ + File.expand_path(src_file), + File.expand_path(stylesheet_file), + File.expand_path(helper_file), + File.expand_path(spec_file) + ] + end + end + + describe '#use_spec?' do + let(:spec_file) { 'my/spec.js' } + + let(:files_list) { Jasmine::FilesList.new(:only => filter) } + + context 'no filter provided' do + let(:filter) { [] } + + it "should allow the spec" do + files_list.use_spec?(spec_file).should be_true + end + end + + context 'filter provided' do + let(:filter) { [ spec_file ] } + + it "should use the spec" do + files_list.use_spec?(spec_file).should be_true + end + + it "should not use the spec" do + files_list.use_spec?('other/file').should be_false + end + end + end + + context 'with filtered specs' do + let(:files_list) { Jasmine::FilesList.new(:only => filter, :config => config) } + let(:spec_dir) { 'spec' } + + include FakeFS::SpecHelpers + + let(:config) { { + 'spec_files' => [ '*_spec.js' ], + 'spec_dir' => spec_dir + } } + + before do + %w{one_spec.js two_spec.js}.each do |file| + File.open(File.join(spec_dir, file), 'w') + end + end + + let(:filter) { 'spec/one_spec.js' } + + it 'should return all files for files' do + files_list.files.any? { |file| file['two_spec.js'] }.should be_true + end + + it 'should return only filtered files for filtered_files' do + files_list.filtered_files.any? { |file| file['two_spec.js'] }.should be_false + end + end + + describe '#.*files_to_html' do + include FakeFS::SpecHelpers + + before do + files_list.instance_variable_set(:@files, [ + 'test.js', + 'test.coffee', + 'test.css' + ]) + + File.open('test.coffee', 'w') + + CoffeeScript.stubs(:compile).returns("i compiled") + end + + describe '#files_to_html' do + it "should create the right HTML" do + files_list.files_to_html.should == [ +%{}, +%{}, +%{} + ] + end + end + end + + describe '#filtered_files_to_html' do + + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 64a326c..61648e4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,7 @@ +RSpec.configure do |c| + c.mock_with :mocha +end + specrunner = 'ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner' if !File.file?(specrunner)