From f7d899b0f7e851af9f42b57f4895495a92331a4b Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 10 Jun 2011 11:02:26 -0400 Subject: [PATCH] write out two templates and execute one after the other if everything's cool --- bin/jasmine-headless-webkit | 11 ++-- ext/jasmine-webkit-specrunner/specrunner.cpp | 54 +++++++++++++------ lib/jasmine/cli.rb | 28 +--------- lib/jasmine/files_list.rb | 17 +++++- lib/jasmine/template_writer.rb | 50 +++++++++++++++++ spec/bin/jasmine-headless-webkit_spec.rb | 24 +++++++++ spec/jasmine/filtered_failure/failure_spec.js | 6 +++ .../filtered_failure/filtered_failure.yml | 10 ++++ spec/jasmine/filtered_failure/src.js | 0 spec/jasmine/filtered_failure/success_spec.js | 6 +++ .../filtered_success/filtered_success.yml | 10 ++++ spec/jasmine/filtered_success/src.js | 0 .../filtered_success/success_one_spec.js | 6 +++ .../filtered_success/success_two_spec.js | 6 +++ spec/lib/jasmine/files_list_spec.rb | 33 ++++++++---- spec/lib/jasmine/template_writer_spec.rb | 37 +++++++++++++ 16 files changed, 238 insertions(+), 60 deletions(-) create mode 100644 lib/jasmine/template_writer.rb create mode 100644 spec/jasmine/filtered_failure/failure_spec.js create mode 100644 spec/jasmine/filtered_failure/filtered_failure.yml create mode 100644 spec/jasmine/filtered_failure/src.js create mode 100644 spec/jasmine/filtered_failure/success_spec.js create mode 100644 spec/jasmine/filtered_success/filtered_success.yml create mode 100644 spec/jasmine/filtered_success/src.js create mode 100644 spec/jasmine/filtered_success/success_one_spec.js create mode 100644 spec/jasmine/filtered_success/success_two_spec.js create mode 100644 spec/lib/jasmine/template_writer_spec.rb diff --git a/bin/jasmine-headless-webkit b/bin/jasmine-headless-webkit index 57cacdc..574dcf2 100755 --- a/bin/jasmine-headless-webkit +++ b/bin/jasmine-headless-webkit @@ -18,6 +18,7 @@ require 'rainbow' require 'jasmine/cli' require 'jasmine/files_list' +require 'jasmine/template_writer' include Jasmine::CLI if !File.file?(File.join(gem_dir, RUNNER)) @@ -67,12 +68,12 @@ files_list = Jasmine::FilesList.new( :only => ARGV.dup ) -output = jasmine_html_template(files_list.files_to_html) - -File.open(target = "specrunner.#{$$}.html", 'w') { |fh| fh.print output } -system jasmine_command(options, target) +system jasmine_command(options, targets = Jasmine::TemplateWriter.write!(files_list)) status = $?.exitstatus -FileUtils.rm_f target if options[:remove_html_file] || (status == 0) + +if options[:remove_html_file] || (status == 0) + targets.each { |target| FileUtils.rm_f target } +end exit status diff --git a/ext/jasmine-webkit-specrunner/specrunner.cpp b/ext/jasmine-webkit-specrunner/specrunner.cpp index ff7a00e..cb58aac 100644 --- a/ext/jasmine-webkit-specrunner/specrunner.cpp +++ b/ext/jasmine-webkit-specrunner/specrunner.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #if QT_VERSION < QT_VERSION_CHECK(4, 7, 0) #error Use Qt 4.7 or later version @@ -64,9 +65,10 @@ class HeadlessSpecRunner: public QObject Q_OBJECT public: HeadlessSpecRunner(); - void load(const QString &spec); void setColors(bool colors); void reportFile(const QString &file); + void addFile(const QString &spec); + void go(); public slots: void log(const QString &msg); void specPassed(); @@ -78,6 +80,7 @@ private slots: void watch(bool ok); void errorLog(const QString &msg, int lineNumber, const QString &sourceID); void internalLog(const QString ¬e, const QString &msg); + void addJHW(); protected: bool hasElement(const char *select); void timerEvent(QTimerEvent *event); @@ -91,12 +94,14 @@ private: bool isFinished; bool didFail; bool consoleNotUsedThisRun; + QQueue runnerFiles; QString reportFilename; void red(); void green(); void yellow(); void clear(); + void loadSpec(); }; HeadlessSpecRunner::HeadlessSpecRunner() @@ -113,14 +118,30 @@ HeadlessSpecRunner::HeadlessSpecRunner() connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(watch(bool))); connect(&m_page, SIGNAL(consoleLog(QString, int, QString)), this, SLOT(errorLog(QString, int, QString))); connect(&m_page, SIGNAL(internalLog(QString, QString)), this, SLOT(internalLog(QString, QString))); + connect(m_page.mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJHW())); } -void HeadlessSpecRunner::load(const QString &spec) +void HeadlessSpecRunner::addFile(const QString &spec) +{ + runnerFiles.enqueue(spec); +} + +void HeadlessSpecRunner::go() { m_ticker.stop(); - m_page.mainFrame()->addToJavaScriptWindowObject("JHW", this); - m_page.mainFrame()->load(spec); m_page.setPreferredContentsSize(QSize(1024, 600)); + addJHW(); + loadSpec(); +} +void HeadlessSpecRunner::addJHW() +{ + m_page.mainFrame()->addToJavaScriptWindowObject("JHW", this); +} + +void HeadlessSpecRunner::loadSpec() +{ + m_page.mainFrame()->load(runnerFiles.dequeue()); + m_ticker.start(200, this); } void HeadlessSpecRunner::watch(bool ok) @@ -298,7 +319,11 @@ void HeadlessSpecRunner::timerEvent(QTimerEvent *event) } } - QApplication::instance()->exit(exitCode); + if ((exitCode == 0 && runnerFiles.count() == 0) || (exitCode != 0)) { + QApplication::instance()->exit(exitCode); + } else { + loadSpec(); + } } if (m_runs > 30) { @@ -311,7 +336,6 @@ void HeadlessSpecRunner::timerEvent(QTimerEvent *event) int main(int argc, char** argv) { - char *filename = NULL; char *reporter = NULL; char showColors = false; @@ -328,16 +352,9 @@ int main(int argc, char** argv) } } - bool filenameFound = false; - - for (index = optind; index < argc; index++) { - filename = argv[index]; - filenameFound = true; - } - - if (!filenameFound) { + if (optind == argc) { std::cerr << "Run Jasmine's SpecRunner headlessly" << std::endl << std::endl; - std::cerr << " specrunner [-c] SpecRunner.html" << std::endl; + std::cerr << " specrunner [-c] [-r ] specrunner.html ..." << std::endl; return 1; } @@ -345,7 +362,12 @@ int main(int argc, char** argv) HeadlessSpecRunner runner; runner.setColors(true); runner.reportFile(reporter); - runner.load(QString::fromLocal8Bit(filename)); + + for (index = optind; index < argc; index++) { + runner.addFile(QString::fromLocal8Bit(argv[index])); + } + runner.go(); + return app.exec(); } diff --git a/lib/jasmine/cli.rb b/lib/jasmine/cli.rb index 419e493..4b80429 100644 --- a/lib/jasmine/cli.rb +++ b/lib/jasmine/cli.rb @@ -30,42 +30,18 @@ module Jasmine end def jasmine_html_template(files) - <<-HTML - - - - Jasmine Test Runner - - #{files.join("\n")} - - - - - - - - HTML end def runner_path @runner_path ||= File.join(gem_dir, RUNNER) end - def jasmine_command(options, target) + def jasmine_command(options, targets) [ runner_path, options[:colors] ? '-c' : nil, options[:report] ? "-r #{options[:report]}" : nil, - target + *targets ].join(" ") end diff --git a/lib/jasmine/files_list.rb b/lib/jasmine/files_list.rb index 477add2..9ee7891 100644 --- a/lib/jasmine/files_list.rb +++ b/lib/jasmine/files_list.rb @@ -12,7 +12,8 @@ module Jasmine def initialize(options = {}) @options = options - @files = DEFAULT_FILES + @files = DEFAULT_FILES.dup + @filtered_files = @files.dup use_config! if config? end @@ -20,7 +21,20 @@ module Jasmine spec_filter.empty? || spec_filter.include?(file) end + def filtered? + files != filtered_files + end + def files_to_html + to_html(files) + end + + def filtered_files_to_html + to_html(filtered_files) + end + + private + def to_html(files) files.collect { |file| case File.extname(file) when '.js' @@ -40,7 +54,6 @@ module Jasmine } end - private def spec_filter @options[:only] || [] end diff --git a/lib/jasmine/template_writer.rb b/lib/jasmine/template_writer.rb new file mode 100644 index 0000000..e37c7bf --- /dev/null +++ b/lib/jasmine/template_writer.rb @@ -0,0 +1,50 @@ +require 'jasmine/files_list' + +module Jasmine + class TemplateWriter + class << self + def write!(files_list) + output = [ + [ "specrunner.#{$$}.html", files_list.files_to_html ] + ] + + output.unshift([ "specrunner.#{$$}.filter.html", files_list.filtered_files_to_html ]) if files_list.filtered? + + output.each do |name, files| + File.open(name, 'w') { |fh| fh.print template_for(files) } + end + + output.collect(&:first) + end + + private + def template_for(files) + <<-HTML + + + + Jasmine Test Runner + + #{files.join("\n")} + + + + + + + +HTML + end + end + end +end + diff --git a/spec/bin/jasmine-headless-webkit_spec.rb b/spec/bin/jasmine-headless-webkit_spec.rb index 85aeed2..b2f49bb 100644 --- a/spec/bin/jasmine-headless-webkit_spec.rb +++ b/spec/bin/jasmine-headless-webkit_spec.rb @@ -63,5 +63,29 @@ describe "jasmine-headless-webkit" do parts[2].should == "T" end end + + describe 'with filtered run' do + it "should fail and not run the second" do + system %{bin/jasmine-headless-webkit -j spec/jasmine/filtered_failure/filtered_failure.yml --report #{report} ./spec/jasmine/filtered_failure/failure_spec.js} + $?.exitstatus.should == 1 + + parts = File.read(report).strip.split('/') + parts.length.should == 4 + parts[0].should == "1" + parts[1].should == "1" + parts[2].should == "F" + end + + it "should succeed and run both" do + system %{bin/jasmine-headless-webkit -j spec/jasmine/filtered_success/filtered_success.yml --report #{report} ./spec/jasmine/filtered_success/success_one_spec.js} + $?.exitstatus.should == 0 + + parts = File.read(report).strip.split('/') + parts.length.should == 4 + parts[0].should == "2" + parts[1].should == "0" + parts[2].should == "F" + end + end end diff --git a/spec/jasmine/filtered_failure/failure_spec.js b/spec/jasmine/filtered_failure/failure_spec.js new file mode 100644 index 0000000..325c465 --- /dev/null +++ b/spec/jasmine/filtered_failure/failure_spec.js @@ -0,0 +1,6 @@ +describe('fail', function() { + it('should fail', function() { + expect(false).toEqual(true); + }); +}); + diff --git a/spec/jasmine/filtered_failure/filtered_failure.yml b/spec/jasmine/filtered_failure/filtered_failure.yml new file mode 100644 index 0000000..8529b01 --- /dev/null +++ b/spec/jasmine/filtered_failure/filtered_failure.yml @@ -0,0 +1,10 @@ +src_files: + - spec/jasmine/filtered_failure/src.js + +spec_files: + - spec/jasmine/filtered_failure/*_spec.js + +src_dir: . +spec_dir: . + + diff --git a/spec/jasmine/filtered_failure/src.js b/spec/jasmine/filtered_failure/src.js new file mode 100644 index 0000000..e69de29 diff --git a/spec/jasmine/filtered_failure/success_spec.js b/spec/jasmine/filtered_failure/success_spec.js new file mode 100644 index 0000000..0a73e8d --- /dev/null +++ b/spec/jasmine/filtered_failure/success_spec.js @@ -0,0 +1,6 @@ +describe('success', function() { + it('should succeed', function() { + expect(true).toEqual(true); + }); +}); + diff --git a/spec/jasmine/filtered_success/filtered_success.yml b/spec/jasmine/filtered_success/filtered_success.yml new file mode 100644 index 0000000..f9d3c01 --- /dev/null +++ b/spec/jasmine/filtered_success/filtered_success.yml @@ -0,0 +1,10 @@ +src_files: + - spec/jasmine/filtered_success/src.js + +spec_files: + - spec/jasmine/filtered_success/*_spec.js + +src_dir: . +spec_dir: . + + diff --git a/spec/jasmine/filtered_success/src.js b/spec/jasmine/filtered_success/src.js new file mode 100644 index 0000000..e69de29 diff --git a/spec/jasmine/filtered_success/success_one_spec.js b/spec/jasmine/filtered_success/success_one_spec.js new file mode 100644 index 0000000..0a73e8d --- /dev/null +++ b/spec/jasmine/filtered_success/success_one_spec.js @@ -0,0 +1,6 @@ +describe('success', function() { + it('should succeed', function() { + expect(true).toEqual(true); + }); +}); + diff --git a/spec/jasmine/filtered_success/success_two_spec.js b/spec/jasmine/filtered_success/success_two_spec.js new file mode 100644 index 0000000..8ac1080 --- /dev/null +++ b/spec/jasmine/filtered_success/success_two_spec.js @@ -0,0 +1,6 @@ +describe('also success', function() { + it('should succeed', function() { + expect(true).toEqual(true); + }); +}); + diff --git a/spec/lib/jasmine/files_list_spec.rb b/spec/lib/jasmine/files_list_spec.rb index 7efc90f..4d4211b 100644 --- a/spec/lib/jasmine/files_list_spec.rb +++ b/spec/lib/jasmine/files_list_spec.rb @@ -90,7 +90,7 @@ describe Jasmine::FilesList do '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') @@ -101,6 +101,7 @@ describe Jasmine::FilesList do it 'should return all files for files' do files_list.files.any? { |file| file['two_spec.js'] }.should be_true + files_list.filtered?.should be_true end it 'should return only filtered files for filtered_files' do @@ -113,9 +114,14 @@ describe Jasmine::FilesList do before do files_list.instance_variable_set(:@files, [ - 'test.js', - 'test.coffee', - 'test.css' + 'test.js', + 'test.coffee', + 'test.css' + ]) + + files_list.instance_variable_set(:@filtered_files, [ + 'test.js', + 'test.coffee' ]) File.open('test.coffee', 'w') @@ -126,16 +132,21 @@ describe Jasmine::FilesList do describe '#files_to_html' do it "should create the right HTML" do files_list.files_to_html.should == [ -%{}, -%{}, -%{} + %{}, + %{}, + %{} + ] + end + end + + describe '#filtered_files_to_html' do + it "should create the right HTML" do + files_list.filtered_files_to_html.should == [ + %{}, + %{} ] end end end - - describe '#filtered_files_to_html' do - - end end diff --git a/spec/lib/jasmine/template_writer_spec.rb b/spec/lib/jasmine/template_writer_spec.rb new file mode 100644 index 0000000..ad2a8f7 --- /dev/null +++ b/spec/lib/jasmine/template_writer_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' +require 'jasmine/template_writer' +require 'fakefs/spec_helpers' + +describe Jasmine::TemplateWriter do + describe '.write!' do + include FakeFS::SpecHelpers + + let(:files_list) { Jasmine::FilesList.new } + + before do + files_list.files << 'file.js' + files_list.filtered_files << 'file.js' + end + + context 'no filter' do + it 'should write one file' do + Jasmine::TemplateWriter.write!(files_list).should == [ + "specrunner.#{$$}.html" + ] + end + end + + context 'filtered files' do + before do + files_list.files << 'file2.js' + end + + it 'should write two files' do + Jasmine::TemplateWriter.write!(files_list).should == [ + "specrunner.#{$$}.filter.html", "specrunner.#{$$}.html" + ] + end + end + end +end +