more clean up work, break things apart for manageability/testing

This commit is contained in:
John Bintz 2011-12-30 11:34:30 -05:00
parent d31f628d91
commit d5b2239d0f
5 changed files with 166 additions and 185 deletions

View File

@ -24,5 +24,4 @@ gem 'jquery-rails'
gem 'ejs'
gem 'simplecov'
#gem 'perftools.rb'

View File

@ -72,8 +72,6 @@ module Jasmine::Headless
def default_files
%w{jasmine.js jasmine-html jasmine.css jasmine-extensions
intense headless_reporter_result jasmine.HeadlessReporter
jasmine.HeadlessReporter.File jasmine.HeadlessReporter.Console
jasmine.HeadlessReporter.Tap
jsDump beautify-html}
end
@ -91,20 +89,28 @@ module Jasmine::Headless
def initialize(options = {})
@options = options
Kernel.srand(@options[:seed]) if @options[:seed]
Kernel.srand(options[:seed]) if options[:seed]
@required_files = UniqueAssetList.new
@potential_files_to_filter = []
load_initial_assets
use_config if config?
end
def load_initial_assets
self.class.default_files.each do |file|
begin
@required_files << sprockets_environment.find_asset(file, :bundle => false)
add_path(file)
rescue InvalidUniqueAsset => e
raise StandardError.new("Not an asset: #{file}")
end
end
use_config! if config?
(options[:reporters] || []).each do |reporter, identifier, file|
add_path("jasmine.HeadlessReporter.#{reporter}")
end
end
def files
@ -207,7 +213,7 @@ module Jasmine::Headless
'spec_files' => 'spec_dir'
}
def use_config!
def use_config
@config = @options[:config].dup
@searches = {}
@potential_files_to_filter = []
@ -253,7 +259,7 @@ module Jasmine::Headless
self.class.extension_filter
end
def add_path(path, type)
def add_path(path, type = nil)
asset = sprockets_environment.find_asset(path)
@required_files << asset

View File

@ -103,8 +103,9 @@ module Jasmine
def files_list
@files_list ||= Jasmine::Headless::FilesList.new(
:config => jasmine_config,
:only => @options[:files],
:seed => @options[:seed]
:only => options[:files],
:seed => options[:seed],
:reporters => options.reporters
)
end

View File

@ -5,53 +5,6 @@ require 'coffee-script'
describe Jasmine::Headless::FilesList do
let(:files_list) { described_class.new }
describe '#initialize' do
it "should have default files" do
files_list.files.should == [
File.join(Jasmine::Core.path, "jasmine.js"),
File.join(Jasmine::Core.path, "jasmine-html.js"),
File.join(Jasmine::Core.path, "jasmine.css"),
File.expand_path('vendor/assets/javascripts/jasmine-extensions.js'),
File.expand_path('vendor/assets/javascripts/intense.js'),
File.expand_path('vendor/assets/javascripts/headless_reporter_result.js'),
File.expand_path('vendor/assets/javascripts/jasmine.HeadlessReporter.js'),
File.expand_path('vendor/assets/javascripts/jasmine.HeadlessReporter.File.js'),
File.expand_path('vendor/assets/javascripts/jasmine.HeadlessReporter.Console.js'),
File.expand_path('vendor/assets/javascripts/jasmine.HeadlessReporter.Tap.js'),
File.expand_path('vendor/assets/javascripts/jsDump.js'),
File.expand_path('vendor/assets/javascripts/beautify-html.js'),
]
end
end
def self.no_default_files!
before do
described_class.stubs(:default_files).returns([])
end
end
it 'should have tests for #use_config!'
it 'should have tests for #add_files'
describe '#spec_file_line_numbers' do
include FakeFS::SpecHelpers
no_default_files!
before do
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" }
end
it 'should generate filenames and line number info' do
files_list.spec_file_line_numbers.should == {
'test.coffee' => { 'cat' => [ 1, 2 ] }
}
end
end
describe '.get_paths_from_gemspec' do
include FakeFS::SpecHelpers
@ -75,67 +28,6 @@ describe Jasmine::Headless::FilesList do
it { should =~ paths }
end
describe '#search_paths' do
no_default_files!
let(:files_list) { described_class.new(:config => config) }
let(:config) { {
'src_dir' => src_dir,
'spec_dir' => spec_dir,
'asset_paths' => asset_paths
} }
let(:src_dir) { 'src dir' }
let(:spec_dir) { 'spec dir' }
let(:asset_paths) { [] }
let(:path) { 'path' }
before do
Jasmine::Headless::FilesList.stubs(:asset_paths).returns([])
end
let(:vendor_path) { Jasmine::Headless.root.join('vendor/assets/javascripts').to_s }
context 'no vendored gem paths' do
it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, File.expand_path(src_dir), File.expand_path(spec_dir) ]
end
end
context 'vendored gem paths' do
before do
Jasmine::Headless::FilesList.stubs(:asset_paths).returns([ path ])
end
it 'should add the vendor gem paths to the list' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, path, File.expand_path(src_dir), File.expand_path(spec_dir) ]
end
end
context 'multiple dirs' do
let(:dir_1) { 'dir 1' }
let(:dir_2) { 'dir 2' }
context 'src_dir is an array' do
let(:src_dir) { [ dir_1, dir_2 ] }
it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, File.expand_path(dir_1), File.expand_path(dir_2), File.expand_path(spec_dir) ]
end
end
context 'asset_paths has entries' do
let(:src_dir) { dir_1 }
let(:asset_paths) { [ dir_2 ] }
it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, File.expand_path(dir_1), File.expand_path(dir_2), File.expand_path(spec_dir) ]
end
end
end
end
describe '.asset_paths' do
include FakeFS::SpecHelpers
@ -147,7 +39,6 @@ describe Jasmine::Headless::FilesList do
before do
described_class.instance_variable_set(:@asset_paths, nil)
FileUtils.mkdir_p File.join(dir_two, 'vendor/assets/javascripts')
Gem::Specification.stubs(:_all).returns([gem_one, gem_two])
@ -158,94 +49,174 @@ describe Jasmine::Headless::FilesList do
end
end
describe '#files' do
let(:path_one) { 'one' }
let(:path_two) { 'two' }
let(:path_three) { 'three' }
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), :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) }
describe '#initialize' do
before do
files_list.stubs(:required_files).returns(Jasmine::Headless::UniqueAssetList.new([ file_one, file_two ]))
described_class.any_instance.stubs(:load_initial_assets)
end
subject { files_list.files }
describe '#spec_file_line_numbers' do
include FakeFS::SpecHelpers
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([])
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" }
end
it { should == [ file_one, file_two, file_three, file_four ] }
it 'should generate filenames and line number info' do
files_list.spec_file_line_numbers.should == {
'test.coffee' => { 'cat' => [ 1, 2 ] }
}
end
end
context 'with filter' do
describe '#search_paths' do
let(:files_list) { described_class.new(:config => config) }
let(:config) { {
'src_dir' => src_dir,
'spec_dir' => spec_dir,
'asset_paths' => asset_paths
} }
let(:src_dir) { 'src dir' }
let(:spec_dir) { 'spec dir' }
let(:asset_paths) { [] }
let(:path) { 'path' }
before do
files_list.stubs(:spec_filter).returns([ "#{spec_dir}/one", '**/tw*' ])
Jasmine::Headless::FilesList.stubs(:asset_paths).returns([])
end
it { should == [ file_one, file_two, file_four ] }
end
end
let(:vendor_path) { Jasmine::Headless.root.join('vendor/assets/javascripts').to_s }
describe '#add_files' do
let(:files_list) { described_class.new(:seed => 100) }
no_default_files!
let(:dir) { 'tmp' }
before do
FileUtils.mkdir_p dir
10.times do |index|
File.open(File.join(dir, "file-#{index}.js"), 'wb')
context 'no vendored gem paths' do
it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, File.expand_path(src_dir), File.expand_path(spec_dir) ]
end
end
File.open(File.join(dir, 'file.js.erb'), 'wb')
context 'vendored gem paths' do
before do
Jasmine::Headless::FilesList.stubs(:asset_paths).returns([ path ])
end
it 'should add the vendor gem paths to the list' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, path, File.expand_path(src_dir), File.expand_path(spec_dir) ]
end
end
context 'multiple dirs' do
let(:dir_1) { 'dir 1' }
let(:dir_2) { 'dir 2' }
context 'src_dir is an array' do
let(:src_dir) { [ dir_1, dir_2 ] }
it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, File.expand_path(dir_1), File.expand_path(dir_2), File.expand_path(spec_dir) ]
end
end
context 'asset_paths has entries' do
let(:src_dir) { dir_1 }
let(:asset_paths) { [ dir_2 ] }
it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, vendor_path, File.expand_path(dir_1), File.expand_path(dir_2), File.expand_path(spec_dir) ]
end
end
end
end
before do
files_list.send(:add_files, [ '*' ], 'spec_files', [ dir ])
describe '#files' do
let(:path_one) { 'one' }
let(:path_two) { 'two' }
let(:path_three) { 'three' }
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), :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(Jasmine::Headless::UniqueAssetList.new([ file_one, file_two ]))
end
subject { files_list.files }
it { should == [ path_one, path_two, path_three ] }
end
it 'should load spec files in a random order' do
files_list.files.collect { |name| name[%r{\d+}] }.should == %w{6 7 1 0 5 3 4 8 2 9}
describe '#filtered_files' do
let(:spec_dir) { 'spec' }
FileUtils.rm_rf dir
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
it { should == [ file_one, file_two, file_three, file_four ] }
end
context 'with filter' do
before do
files_list.stubs(:spec_filter).returns([ "#{spec_dir}/one", '**/tw*' ])
end
it { should == [ file_one, file_two, file_four ] }
end
end
it 'should not load an excluded format' do
files_list.files.any? { |file| file['.erb'] }.should be_false
describe '#add_files' do
let(:files_list) { described_class.new(:seed => 100) }
let(:dir) { 'tmp' }
before do
FileUtils.mkdir_p dir
10.times do |index|
File.open(File.join(dir, "file-#{index}.js"), 'wb')
end
File.open(File.join(dir, 'file.js.erb'), 'wb')
end
before do
files_list.send(:add_files, [ '*' ], 'spec_files', [ dir ])
end
it 'should load spec files in a random order' do
files_list.files.collect { |name| name[%r{\d+}] }.should == %w{6 7 1 0 5 3 4 8 2 9}
FileUtils.rm_rf dir
end
it 'should not load an excluded format' do
files_list.files.any? { |file| file['.erb'] }.should be_false
end
end
end
end

View File

@ -167,10 +167,13 @@ describe Jasmine::Headless::Runner do
let(:only) { 'only' }
let(:seed) { 12345 }
let(:jasmine_config) { 'jasmine config' }
let(:reporters) { [] }
before do
runner.stubs(:options).returns(options)
runner.stubs(:jasmine_config).returns(jasmine_config)
options.stubs(:reporters).returns(reporters)
end
it { should be_a_kind_of(Jasmine::Headless::FilesList) }
@ -179,6 +182,7 @@ describe Jasmine::Headless::Runner do
subject.options[:config].should == jasmine_config
subject.options[:only].should == only
subject.options[:seed].should == seed
subject.options[:reporters].should == reporters
end
end
end