more sprockets improvements

This commit is contained in:
John Bintz 2011-11-19 19:15:38 -05:00
parent e04d692d26
commit 61c8ed8828
16 changed files with 170 additions and 87 deletions

View File

@ -2,31 +2,9 @@
require 'rubygems' require 'rubygems'
def gem_dir $: << File.expand_path('../../lib', __FILE__)
File.expand_path('../..', __FILE__)
end
$:.unshift(File.join(gem_dir, 'lib'))
require 'jasmine-headless-webkit' require 'jasmine-headless-webkit'
require 'coffee-script'
require 'rainbow'
begin Jasmine::Headless::CommandLine.run!
options = Jasmine::Headless::Options.from_command_line
runner = Jasmine::Headless::Runner.new(options)
if options[:do_list]
files_list = Jasmine::Headless::FilesList.new(:config => runner.jasmine_config)
files_list.files.each { |file| puts file }
else
exit runner.run
end
rescue CoffeeScript::CompilationError
exit 1
rescue StandardError => e
$stderr.puts "[%s] %s (%s)" % [ "jasmine-headless-webkit".color(:red), e.message.color(:white), e.class.name.color(:yellow) ]
$stderr.puts e.backtrace.collect { |line| " #{line}" }.join("\n")
exit 1
end

View File

@ -20,9 +20,9 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.add_dependency 'jasmine-core', '~>1.1.beta' s.add_runtime_dependency 'jasmine-core', '~> 1.1'
s.add_dependency 'coffee-script', '>= 2.2' s.add_runtime_dependency 'coffee-script'
s.add_dependency 'rainbow' s.add_runtime_dependency 'rainbow'
s.add_dependency 'multi_json' s.add_runtime_dependency 'multi_json'
s.add_dependency 'sprockets', '>= 2.0' s.add_runtime_dependency 'sprockets', '~> 2'
end end

View File

@ -2,6 +2,8 @@ require 'pathname'
require 'sprockets' require 'sprockets'
module Jasmine::Headless module Jasmine::Headless
autoload :CommandLine, 'jasmine/headless/command_line'
autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache'
autoload :SpecFileAnalyzer, 'jasmine/headless/spec_file_analyzer' autoload :SpecFileAnalyzer, 'jasmine/headless/spec_file_analyzer'
autoload :CacheableAction, 'jasmine/headless/cacheable_action' autoload :CacheableAction, 'jasmine/headless/cacheable_action'

View File

@ -0,0 +1,29 @@
module Jasmine::Headless
class CommandLine
class << self
def run!
require 'coffee-script'
require 'rainbow'
begin
options = Options.from_command_line
runner = Runner.new(options)
if options[:do_list]
files_list = FilesList.new(:config => runner.jasmine_config)
files_list.files.each { |file| puts file }
else
exit runner.run
end
rescue CoffeeScript::CompilationError
exit 1
rescue StandardError => e
$stderr.puts "[%s] %s (%s)" % [ "jasmine-headless-webkit".color(:red), e.message.color(:white), e.class.name.color(:yellow) ]
$stderr.puts e.backtrace.collect { |line| " #{line}" }.join("\n")
exit 1
end
end
end
end
end

View File

@ -57,7 +57,14 @@ module Jasmine::Headless
end end
def search_paths def search_paths
@search_paths ||= [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ] + self.class.vendor_asset_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 += self.class.vendor_asset_paths
@search_paths
end end
def has_spec_outside_scope? def has_spec_outside_scope?
@ -111,14 +118,13 @@ module Jasmine::Headless
def find_dependency(file) def find_dependency(file)
search_paths.each do |dir| search_paths.each do |dir|
if file[EXTENSION_FILTER] Dir[File.join(dir, "#{file}*")].find_all { |path| File.file?(path) }.each do |path|
if File.file?(path = File.join(dir, file)) root = path.gsub(%r{^#{dir}/}, '')
return [ File.expand_path(path), File.expand_path(dir) ]
end ok = (root == file)
else ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[EXTENSION_FILTER] }
if path = Dir[File.join(dir, "#{file}.*")].first
return [ File.expand_path(path), File.expand_path(dir) ] return [ File.expand_path(path), File.expand_path(dir) ] if ok
end
end end
end end
@ -176,15 +182,16 @@ module Jasmine::Headless
def add_files(searches, type) def add_files(searches, type)
searches.each do |search| searches.each do |search|
dir = @config[SEARCH_ROOTS[type]] || Dir.pwd [ @config[SEARCH_ROOTS[type]] || Dir.pwd ].flatten.each do |dir|
dir = File.expand_path(dir) dir = File.expand_path(dir)
path = File.expand_path(File.join(dir, search)) path = File.expand_path(File.join(dir, search))
found_files = expanded_dir(path) - files found_files = expanded_dir(path) - files
found_files.each do |file| found_files.each do |file|
type == 'spec_files' ? add_spec_file(file) : add_file(file, dir) type == 'spec_files' ? add_spec_file(file) : add_file(file, dir)
end
end end
end end

View File

@ -43,6 +43,8 @@ module Jasmine
if Rails.respond_to?(:version) && Rails.version >= "3.1.0" if Rails.respond_to?(:version) && Rails.version >= "3.1.0"
desc 'Force generate static assets without an MD5 hash, all assets end with -test.<ext>' desc 'Force generate static assets without an MD5 hash, all assets end with -test.<ext>'
task 'assets:precompile:for_testing' => :environment do task 'assets:precompile:for_testing' => :environment do
$stderr.puts "This task is deprecated and will be removed after 2012-01-01"
Rails.application.assets.digest_class = Digest::JasmineTest Rails.application.assets.digest_class = Digest::JasmineTest
Rake::Task['assets:precompile'].invoke Rake::Task['assets:precompile'].invoke

View File

@ -50,6 +50,7 @@ module Jasmine::Headless
else else
if engine = Sprockets.engines(extension) if engine = Sprockets.engines(extension)
data = engine.new(path) { data || read }.render(self) data = engine.new(path) { data || read }.render(self)
data = %{<script type="text/javascript">#{data}</script>} if extension == '.jst'
process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data) process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data)
else else

View File

@ -87,6 +87,19 @@ describe Jasmine::Headless::FilesList do
it_should_behave_like :reading_data it_should_behave_like :reading_data
end end
context 'with multidimensional src dir' do
let(:config) { {
'src_dir' => [ src_dir ],
'spec_dir' => spec_dir,
'src_files' => [ [ 'js/first_file.js', 'js/*.js' ] ],
'spec_files' => [ '*_spec.js' ],
'helpers' => [ 'helper/*.js' ],
'stylesheets' => [ 'stylesheet/*.css' ]
} }
it_should_behave_like :reading_data
end
end end
context 'with filtered specs' do context 'with filtered specs' do
@ -246,11 +259,11 @@ describe Jasmine::Headless::FilesList do
let(:spec_dir) { 'spec dir' } let(:spec_dir) { 'spec dir' }
let(:path) { 'path' } let(:path) { 'path' }
context 'no vendored gem paths' do before do
before do Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([])
Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([]) end
end
context 'no vendored gem paths' do
it 'should take the src dir and spec dirs' do it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ] files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ]
end end
@ -265,6 +278,17 @@ describe Jasmine::Headless::FilesList 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, File.expand_path(src_dir), File.expand_path(spec_dir), path ]
end end
end end
context 'src_dir is an array' do
let(:dir_1) { 'dir 1' }
let(:dir_2) { 'dir 2' }
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, File.expand_path(dir_1), File.expand_path(dir_2), File.expand_path(spec_dir) ]
end
end
end end
describe '.vendor_asset_paths' do describe '.vendor_asset_paths' do
@ -293,31 +317,38 @@ describe Jasmine::Headless::FilesList do
include FakeFS::SpecHelpers include FakeFS::SpecHelpers
let(:dir) { File.expand_path('dir') } let(:dir) { File.expand_path('dir') }
let(:filename) { 'file' } let(:filename) { 'file' }
let(:file) { "#{filename}.js" } let(:file) { "#{filename}.js" }
before do before do
files_list.stubs(:search_paths).returns([ dir ])
FileUtils.mkdir_p dir FileUtils.mkdir_p dir
%w{file.sub.js file.js.coffee}.each do |file|
File.open(File.join(dir, file), 'wb')
end
files_list.stubs(:search_paths).returns([ dir ])
end end
context 'does not exist' do subject { files_list.find_dependency(search) }
it 'should not be found' do
files_list.find_dependency(file).should be_false context 'bad' do
end let(:search) { 'bad' }
it { should be_false }
end end
context 'exists' do context 'file' do
let(:path) { File.join(dir, file) } let(:search) { 'file' }
before do it { should == [ File.join(dir, 'file.js.coffee'), dir ] }
File.open(path, 'wb') end
end
it 'should be found' do context 'file.sub' do
files_list.find_dependency(filename).should == [ File.expand_path(path), dir ] let(:search) { 'file.sub' }
end
it { should == [ File.join(dir, 'file.sub.js'), dir ] }
end end
end end
end end

View File

@ -44,26 +44,37 @@ describe Jasmine::Headless::TestFile do
end 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 before do
Sprockets.stubs(:engines).with('.tilt').returns(klass) Sprockets.stubs(:engines).with('.tilt').returns(klass)
Sprockets.stubs(:engines).with('.jst').returns(other_klass)
end end
context '.tilt' do context '.tilt' do
let(:path) { 'path.tilt' } let(:path) { 'path.tilt' }
it { should == "#{path} made it #{content}" } it { should == %{#{path} made it #{content}} }
end end
context '.tilt.tilt' do context '.tilt.tilt' do
let(:path) { 'path.tilt.tilt' } let(:path) { 'path.tilt.tilt' }
it { should == "path.tilt made it #{path} made it #{content}" } it { should == %{path.tilt made it #{path} made it #{content}} }
end end
context '.js.tilt' do context '.jst.tilt' do
let(:path) { 'path.js.tilt' } let(:path) { 'path.jst.tilt' }
it { should == "#{path} made it #{content}" } it { should == %{<script type="text/javascript">#{path} made it #{content}</script>} }
end end
end end
end end

View File

@ -1,13 +1,16 @@
(function() {
window.HeadlessReporterResult = (function() { window.HeadlessReporterResult = (function() {
function HeadlessReporterResult(name, splitName) { function HeadlessReporterResult(name, splitName) {
this.name = name; this.name = name;
this.splitName = splitName; this.splitName = splitName;
this.results = []; this.results = [];
} }
HeadlessReporterResult.prototype.addResult = function(message) { HeadlessReporterResult.prototype.addResult = function(message) {
return this.results.push(message); return this.results.push(message);
}; };
HeadlessReporterResult.prototype.print = function() { HeadlessReporterResult.prototype.print = function() {
var bestChoice, output, result, _i, _len, _ref, _results; var bestChoice, output, result, _i, _len, _ref, _results;
output = this.name.foreground('red'); output = this.name.foreground('red');
@ -25,10 +28,15 @@
output += (" (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")").foreground('red').bright(); output += (" (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")").foreground('red').bright();
} }
JHW.stdout.puts(" " + output); JHW.stdout.puts(" " + output);
_results.push(result.line != null ? JHW.stdout.puts((" " + result.line).foreground('yellow')) : void 0); if (result.line != null) {
_results.push(JHW.stdout.puts((" " + result.line).foreground('yellow')));
} else {
_results.push(void 0);
}
} }
return _results; return _results;
}; };
HeadlessReporterResult.findSpecLine = function(splitName) { HeadlessReporterResult.findSpecLine = function(splitName) {
var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref; var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref;
bestChoice = { bestChoice = {
@ -49,9 +57,7 @@
for (_i = 0, _len = newLineNumberInfo.length; _i < _len; _i++) { for (_i = 0, _len = newLineNumberInfo.length; _i < _len; _i++) {
line = newLineNumberInfo[_i]; line = newLineNumberInfo[_i];
lastLine = line; lastLine = line;
if (line > lineNumber) { if (line > lineNumber) break;
break;
}
} }
lineNumber = lastLine; lineNumber = lastLine;
} }
@ -67,6 +73,7 @@
} }
return bestChoice; return bestChoice;
}; };
return HeadlessReporterResult; return HeadlessReporterResult;
})(); })();
}).call(this);

View File

@ -1,5 +1,6 @@
(function() { (function() {
var code, method, _ref; var code, method, _ref;
window.Intense = { window.Intense = {
colors: { colors: {
black: 0, black: 0,
@ -29,9 +30,11 @@
}, },
useColors: true useColors: true
}; };
_ref = Intense.methods; _ref = Intense.methods;
for (method in _ref) { for (method in _ref) {
code = _ref[method]; code = _ref[method];
String.prototype[method] = code; String.prototype[method] = code;
} }
}).call(this); }).call(this);

View File

@ -1,8 +1,10 @@
(function() { (function() {
var getSplitName, pauseAndRun; var getSplitName, pauseAndRun;
if (!(typeof jasmine !== "undefined" && jasmine !== null)) { if (!(typeof jasmine !== "undefined" && jasmine !== null)) {
throw new Error("jasmine not laoded!"); throw new Error("jasmine not laoded!");
} }
if (window.JHW) { if (window.JHW) {
getSplitName = function(parts) { getSplitName = function(parts) {
parts.push(String(this.description).replace(/[\n\r]/g, ' ')); parts.push(String(this.description).replace(/[\n\r]/g, ' '));
@ -98,4 +100,5 @@
}; };
} }
} }
}).call(this); }).call(this);

View File

@ -1,19 +1,20 @@
(function() {
if (!(typeof jasmine !== "undefined" && jasmine !== null)) { if (!(typeof jasmine !== "undefined" && jasmine !== null)) {
throw new Error("jasmine not loaded!"); throw new Error("jasmine not loaded!");
} }
jasmine.HeadlessConsoleReporter = (function() { jasmine.HeadlessConsoleReporter = (function() {
function HeadlessConsoleReporter(callback) { function HeadlessConsoleReporter(callback) {
this.callback = callback != null ? callback : null; this.callback = callback != null ? callback : null;
this.results = []; this.results = [];
this.failedCount = 0; this.failedCount = 0;
this.length = 0; this.length = 0;
} }
HeadlessConsoleReporter.prototype.reportRunnerResults = function(runner) { HeadlessConsoleReporter.prototype.reportRunnerResults = function(runner) {
var output, result, resultLine, runtime, _i, _len, _ref; var output, result, resultLine, runtime, _i, _len, _ref;
if (this.hasError()) { if (this.hasError()) return;
return;
}
runtime = (new Date() - this.startTime) / 1000.0; runtime = (new Date() - this.startTime) / 1000.0;
JHW.stdout.print("\n"); JHW.stdout.print("\n");
resultLine = this._formatResultLine(runtime); resultLine = this._formatResultLine(runtime);
@ -30,20 +31,18 @@
result = _ref[_i]; result = _ref[_i];
result.print(); result.print();
} }
if (window.JHW) { if (window.JHW) window.onbeforeunload = null;
window.onbeforeunload = null;
}
return JHW.finishSuite(); return JHW.finishSuite();
}; };
HeadlessConsoleReporter.prototype.reportRunnerStarting = function(runner) { HeadlessConsoleReporter.prototype.reportRunnerStarting = function(runner) {
this.startTime = new Date(); this.startTime = new Date();
return JHW.stdout.puts("\nRunning Jasmine specs...".bright()); return JHW.stdout.puts("\nRunning Jasmine specs...".bright());
}; };
HeadlessConsoleReporter.prototype.reportSpecResults = function(spec) { HeadlessConsoleReporter.prototype.reportSpecResults = function(spec) {
var failureResult, foundLine, result, results, testCount, _i, _len, _ref; var failureResult, foundLine, result, results, testCount, _i, _len, _ref;
if (this.hasError()) { if (this.hasError()) return;
return;
}
JHW.ping(); JHW.ping();
results = spec.results(); results = spec.results();
this.length++; this.length++;
@ -70,16 +69,20 @@
return this.results.push(failureResult); return this.results.push(failureResult);
} }
}; };
HeadlessConsoleReporter.prototype.reportSpecStarting = function(spec) { HeadlessConsoleReporter.prototype.reportSpecStarting = function(spec) {
if (this.hasError()) { if (this.hasError()) {
spec.finish(); spec.finish();
return spec.suite.finish(); return spec.suite.finish();
} }
}; };
HeadlessConsoleReporter.prototype.reportSuiteResults = function(suite) {}; HeadlessConsoleReporter.prototype.reportSuiteResults = function(suite) {};
HeadlessConsoleReporter.prototype.hasError = function() { HeadlessConsoleReporter.prototype.hasError = function() {
return JHW._hasErrors; return JHW._hasErrors;
}; };
HeadlessConsoleReporter.prototype._formatResultLine = function(runtime) { HeadlessConsoleReporter.prototype._formatResultLine = function(runtime) {
var line; var line;
line = []; line = [];
@ -91,6 +94,7 @@
line.push((runtime === 1.0 ? "sec" : "secs") + '.'); line.push((runtime === 1.0 ? "sec" : "secs") + '.');
return line.join(' '); return line.join(' ');
}; };
return HeadlessConsoleReporter; return HeadlessConsoleReporter;
})(); })();
}).call(this);

View File

@ -1,5 +1,6 @@
(function() { (function() {
var createHandle, handle, _i, _len, _ref; var createHandle, handle, _i, _len, _ref;
if (window.JHW) { if (window.JHW) {
window.console = { window.console = {
log: function(data) { log: function(data) {
@ -40,9 +41,7 @@
e = e || window.event; e = e || window.event;
JHW.hasError(); JHW.hasError();
JHW.stdout.puts('The code tried to leave the test page. Check for unhandled form submits and link clicks.'); JHW.stdout.puts('The code tried to leave the test page. Check for unhandled form submits and link clicks.');
if (e) { if (e) e.returnValue = 'string';
e.returnValue = 'string';
}
return 'string'; return 'string';
}; };
window.confirm = function(message) { window.confirm = function(message) {
@ -84,6 +83,9 @@
return JHW.stdout.puts(msg); return JHW.stdout.puts(msg);
}; };
} }
window.CoffeeScriptToFilename = {}; window.CoffeeScriptToFilename = {};
window.CSTF = window.CoffeeScriptToFilename; window.CSTF = window.CoffeeScriptToFilename;
}).call(this); }).call(this);