factor out file list management

This commit is contained in:
John Bintz 2011-06-10 10:04:21 -04:00
parent 7c8f4c0e4e
commit c32f13fbe1
8 changed files with 232 additions and 73 deletions

View File

@ -11,3 +11,4 @@ gem 'guard-shell'
gem 'guard-coffeescript'
gem 'growl'
gem 'rake', '0.8.7'
gem 'mocha'

View File

@ -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" }

View File

@ -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'
%{<script type="text/javascript" src="#{file}"></script>}
when '.coffee'
begin
%{<script type="text/javascript">#{CoffeeScript.compile(fh = File.open(file))}</script>}
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'
%{<link rel="stylesheet" href="#{file}" type="text/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)

View File

@ -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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

75
lib/jasmine/files_list.rb Normal file
View File

@ -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'
%{<script type="text/javascript" src="#{file}"></script>}
when '.coffee'
begin
%{<script type="text/javascript">#{CoffeeScript.compile(fh = File.open(file))}</script>}
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'
%{<link rel="stylesheet" href="#{file}" type="text/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

View File

@ -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

View File

@ -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 == [
%{<script type="text/javascript" src="test.js"></script>},
%{<script type="text/javascript">i compiled</script>},
%{<link rel="stylesheet" href="test.css" type="text/css" />}
]
end
end
end
describe '#filtered_files_to_html' do
end
end

View File

@ -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)