From 61c8ed8828a88e6d434884c8e03798bab72afb7d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 19 Nov 2011 19:15:38 -0500 Subject: [PATCH] more sprockets improvements --- bin/jasmine-headless-webkit | 26 +------ jasmine-headless-webkit.gemspec | 10 +-- lib/jasmine/headless.rb | 2 + lib/jasmine/headless/command_line.rb | 29 ++++++++ lib/jasmine/headless/files_list.rb | 37 +++++----- lib/jasmine/headless/task.rb | 2 + lib/jasmine/headless/test_file.rb | 1 + .../assets/things/templates/that.jst.ejs | 1 + .../assets/things/templates/this.jst | 2 + spec/lib/jasmine/headless/files_list_spec.rb | 67 ++++++++++++++----- spec/lib/jasmine/headless/test_file_spec.rb | 21 ++++-- .../javascripts/headless_reporter_result.js | 19 ++++-- vendor/assets/javascripts/intense.js | 3 + .../assets/javascripts/jasmine-extensions.js | 3 + .../jasmine.HeadlessConsoleReporter.js | 26 ++++--- vendor/assets/javascripts/prolog.js | 8 ++- 16 files changed, 170 insertions(+), 87 deletions(-) create mode 100644 lib/jasmine/headless/command_line.rb diff --git a/bin/jasmine-headless-webkit b/bin/jasmine-headless-webkit index 3796599..530cdf1 100755 --- a/bin/jasmine-headless-webkit +++ b/bin/jasmine-headless-webkit @@ -2,31 +2,9 @@ require 'rubygems' -def gem_dir - File.expand_path('../..', __FILE__) -end - -$:.unshift(File.join(gem_dir, 'lib')) +$: << File.expand_path('../../lib', __FILE__) require 'jasmine-headless-webkit' -require 'coffee-script' -require 'rainbow' -begin - 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 +Jasmine::Headless::CommandLine.run! diff --git a/jasmine-headless-webkit.gemspec b/jasmine-headless-webkit.gemspec index e7be2ec..2a6c27e 100644 --- a/jasmine-headless-webkit.gemspec +++ b/jasmine-headless-webkit.gemspec @@ -20,9 +20,9 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - s.add_dependency 'jasmine-core', '~>1.1.beta' - s.add_dependency 'coffee-script', '>= 2.2' - s.add_dependency 'rainbow' - s.add_dependency 'multi_json' - s.add_dependency 'sprockets', '>= 2.0' + s.add_runtime_dependency 'jasmine-core', '~> 1.1' + s.add_runtime_dependency 'coffee-script' + s.add_runtime_dependency 'rainbow' + s.add_runtime_dependency 'multi_json' + s.add_runtime_dependency 'sprockets', '~> 2' end diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 45ddf9d..bb88225 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -2,6 +2,8 @@ require 'pathname' require 'sprockets' module Jasmine::Headless + autoload :CommandLine, 'jasmine/headless/command_line' + autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' autoload :SpecFileAnalyzer, 'jasmine/headless/spec_file_analyzer' autoload :CacheableAction, 'jasmine/headless/cacheable_action' diff --git a/lib/jasmine/headless/command_line.rb b/lib/jasmine/headless/command_line.rb new file mode 100644 index 0000000..a746247 --- /dev/null +++ b/lib/jasmine/headless/command_line.rb @@ -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 + diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index b1311db..4d45d51 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -57,7 +57,14 @@ module Jasmine::Headless end 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 def has_spec_outside_scope? @@ -111,14 +118,13 @@ module Jasmine::Headless def find_dependency(file) search_paths.each do |dir| - if file[EXTENSION_FILTER] - if File.file?(path = File.join(dir, file)) - return [ File.expand_path(path), File.expand_path(dir) ] - end - else - if path = Dir[File.join(dir, "#{file}.*")].first - return [ File.expand_path(path), File.expand_path(dir) ] - end + 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] } + + return [ File.expand_path(path), File.expand_path(dir) ] if ok end end @@ -176,15 +182,16 @@ module Jasmine::Headless def add_files(searches, type) searches.each do |search| - dir = @config[SEARCH_ROOTS[type]] || Dir.pwd - dir = File.expand_path(dir) + [ @config[SEARCH_ROOTS[type]] || Dir.pwd ].flatten.each do |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| - type == 'spec_files' ? add_spec_file(file) : add_file(file, dir) + found_files.each do |file| + type == 'spec_files' ? add_spec_file(file) : add_file(file, dir) + end end end diff --git a/lib/jasmine/headless/task.rb b/lib/jasmine/headless/task.rb index d6456a2..78eec46 100644 --- a/lib/jasmine/headless/task.rb +++ b/lib/jasmine/headless/task.rb @@ -43,6 +43,8 @@ module Jasmine if Rails.respond_to?(:version) && Rails.version >= "3.1.0" desc 'Force generate static assets without an MD5 hash, all assets end with -test.' 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 Rake::Task['assets:precompile'].invoke diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index 528acd4..03fbda6 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -50,6 +50,7 @@ module Jasmine::Headless else if engine = Sprockets.engines(extension) data = engine.new(path) { data || read }.render(self) + data = %{} if extension == '.jst' process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data) else diff --git a/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs b/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs index e69de29..30f1577 100644 --- a/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs +++ b/spec/jasmine/with_sprockets_includes/assets/things/templates/that.jst.ejs @@ -0,0 +1 @@ +'hello' diff --git a/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst b/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst index e69de29..9424e78 100644 --- a/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst +++ b/spec/jasmine/with_sprockets_includes/assets/things/templates/this.jst @@ -0,0 +1,2 @@ +'hello' + diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index 82f36a8..7efc57d 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -87,6 +87,19 @@ describe Jasmine::Headless::FilesList do it_should_behave_like :reading_data 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 context 'with filtered specs' do @@ -246,11 +259,11 @@ describe Jasmine::Headless::FilesList do let(:spec_dir) { 'spec dir' } let(:path) { 'path' } - context 'no vendored gem paths' do - before do - Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([]) - end + before do + Jasmine::Headless::FilesList.stubs(:vendor_asset_paths).returns([]) + end + context 'no vendored gem paths' 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) ] 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 ] 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 describe '.vendor_asset_paths' do @@ -293,31 +317,38 @@ describe Jasmine::Headless::FilesList do include FakeFS::SpecHelpers let(:dir) { File.expand_path('dir') } + let(:filename) { 'file' } let(:file) { "#{filename}.js" } before do - files_list.stubs(:search_paths).returns([ 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 - context 'does not exist' do - it 'should not be found' do - files_list.find_dependency(file).should be_false - end + subject { files_list.find_dependency(search) } + + context 'bad' do + let(:search) { 'bad' } + + it { should be_false } end - context 'exists' do - let(:path) { File.join(dir, file) } + context 'file' do + let(:search) { 'file' } - before do - File.open(path, 'wb') - end + it { should == [ File.join(dir, 'file.js.coffee'), dir ] } + end - it 'should be found' do - files_list.find_dependency(filename).should == [ File.expand_path(path), dir ] - end + context 'file.sub' do + let(:search) { 'file.sub' } + + it { should == [ File.join(dir, 'file.sub.js'), dir ] } end end end diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index b19ed21..6302fb2 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -44,26 +44,37 @@ describe Jasmine::Headless::TestFile do 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}" } + 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}" } + it { should == %{path.tilt made it #{path} made it #{content}} } end - context '.js.tilt' do - let(:path) { 'path.js.tilt' } + context '.jst.tilt' do + let(:path) { 'path.jst.tilt' } - it { should == "#{path} made it #{content}" } + it { should == %{} } end end end diff --git a/vendor/assets/javascripts/headless_reporter_result.js b/vendor/assets/javascripts/headless_reporter_result.js index 14ba347..92ec7b9 100644 --- a/vendor/assets/javascripts/headless_reporter_result.js +++ b/vendor/assets/javascripts/headless_reporter_result.js @@ -1,13 +1,16 @@ -(function() { + window.HeadlessReporterResult = (function() { + function HeadlessReporterResult(name, splitName) { this.name = name; this.splitName = splitName; this.results = []; } + HeadlessReporterResult.prototype.addResult = function(message) { return this.results.push(message); }; + HeadlessReporterResult.prototype.print = function() { var bestChoice, output, result, _i, _len, _ref, _results; output = this.name.foreground('red'); @@ -25,10 +28,15 @@ output += (" (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")").foreground('red').bright(); } 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; }; + HeadlessReporterResult.findSpecLine = function(splitName) { var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref; bestChoice = { @@ -49,9 +57,7 @@ for (_i = 0, _len = newLineNumberInfo.length; _i < _len; _i++) { line = newLineNumberInfo[_i]; lastLine = line; - if (line > lineNumber) { - break; - } + if (line > lineNumber) break; } lineNumber = lastLine; } @@ -67,6 +73,7 @@ } return bestChoice; }; + return HeadlessReporterResult; + })(); -}).call(this); diff --git a/vendor/assets/javascripts/intense.js b/vendor/assets/javascripts/intense.js index 2e5fffd..6d885a1 100644 --- a/vendor/assets/javascripts/intense.js +++ b/vendor/assets/javascripts/intense.js @@ -1,5 +1,6 @@ (function() { var code, method, _ref; + window.Intense = { colors: { black: 0, @@ -29,9 +30,11 @@ }, useColors: true }; + _ref = Intense.methods; for (method in _ref) { code = _ref[method]; String.prototype[method] = code; } + }).call(this); diff --git a/vendor/assets/javascripts/jasmine-extensions.js b/vendor/assets/javascripts/jasmine-extensions.js index 6464dd3..c9b4a93 100644 --- a/vendor/assets/javascripts/jasmine-extensions.js +++ b/vendor/assets/javascripts/jasmine-extensions.js @@ -1,8 +1,10 @@ (function() { var getSplitName, pauseAndRun; + if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not laoded!"); } + if (window.JHW) { getSplitName = function(parts) { parts.push(String(this.description).replace(/[\n\r]/g, ' ')); @@ -98,4 +100,5 @@ }; } } + }).call(this); diff --git a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js index be8f10b..ea1d365 100644 --- a/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js +++ b/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js @@ -1,19 +1,20 @@ -(function() { + if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not loaded!"); } + jasmine.HeadlessConsoleReporter = (function() { + function HeadlessConsoleReporter(callback) { this.callback = callback != null ? callback : null; this.results = []; this.failedCount = 0; this.length = 0; } + HeadlessConsoleReporter.prototype.reportRunnerResults = function(runner) { var output, result, resultLine, runtime, _i, _len, _ref; - if (this.hasError()) { - return; - } + if (this.hasError()) return; runtime = (new Date() - this.startTime) / 1000.0; JHW.stdout.print("\n"); resultLine = this._formatResultLine(runtime); @@ -30,20 +31,18 @@ result = _ref[_i]; result.print(); } - if (window.JHW) { - window.onbeforeunload = null; - } + if (window.JHW) window.onbeforeunload = null; return JHW.finishSuite(); }; + HeadlessConsoleReporter.prototype.reportRunnerStarting = function(runner) { this.startTime = new Date(); return JHW.stdout.puts("\nRunning Jasmine specs...".bright()); }; + HeadlessConsoleReporter.prototype.reportSpecResults = function(spec) { var failureResult, foundLine, result, results, testCount, _i, _len, _ref; - if (this.hasError()) { - return; - } + if (this.hasError()) return; JHW.ping(); results = spec.results(); this.length++; @@ -70,16 +69,20 @@ return this.results.push(failureResult); } }; + HeadlessConsoleReporter.prototype.reportSpecStarting = function(spec) { if (this.hasError()) { spec.finish(); return spec.suite.finish(); } }; + HeadlessConsoleReporter.prototype.reportSuiteResults = function(suite) {}; + HeadlessConsoleReporter.prototype.hasError = function() { return JHW._hasErrors; }; + HeadlessConsoleReporter.prototype._formatResultLine = function(runtime) { var line; line = []; @@ -91,6 +94,7 @@ line.push((runtime === 1.0 ? "sec" : "secs") + '.'); return line.join(' '); }; + return HeadlessConsoleReporter; + })(); -}).call(this); diff --git a/vendor/assets/javascripts/prolog.js b/vendor/assets/javascripts/prolog.js index 2fd358b..eb2b69f 100644 --- a/vendor/assets/javascripts/prolog.js +++ b/vendor/assets/javascripts/prolog.js @@ -1,5 +1,6 @@ (function() { var createHandle, handle, _i, _len, _ref; + if (window.JHW) { window.console = { log: function(data) { @@ -40,9 +41,7 @@ e = e || window.event; JHW.hasError(); JHW.stdout.puts('The code tried to leave the test page. Check for unhandled form submits and link clicks.'); - if (e) { - e.returnValue = 'string'; - } + if (e) e.returnValue = 'string'; return 'string'; }; window.confirm = function(message) { @@ -84,6 +83,9 @@ return JHW.stdout.puts(msg); }; } + window.CoffeeScriptToFilename = {}; + window.CSTF = window.CoffeeScriptToFilename; + }).call(this);