diff --git a/bin/jasmine-headless-webkit b/bin/jasmine-headless-webkit index b8cc56c..9ac404e 100755 --- a/bin/jasmine-headless-webkit +++ b/bin/jasmine-headless-webkit @@ -2,8 +2,21 @@ require 'yaml' require 'fileutils' +require 'getoptlong' -data = YAML.load_file(ARGV[0] || 'spec/javascripts/support/jasmine.yml') +opts = GetoptLong.new( + [ '--colors', '-c', GetoptLong::NO_ARGUMENT ] +) + +colors = false +opts.each do |opt, arg| + case opt + when '--colors' + colors = true + end +end + +data = YAML.load_file(ARGV.shift || 'spec/javascripts/support/jasmine.yml') gem_dir = File.expand_path('../..', __FILE__) files = [ @@ -63,7 +76,7 @@ window.console = { log: function(data) { debug.log(JSON.stringify(data)); } }; HTML File.open(target = "specrunner.#{$$}.html", 'w') { |fh| fh.print output } -system %{#{File.join(gem_dir, 'ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner')} #{target}} +system %{#{File.join(gem_dir, 'ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner')} #{colors ? '-c' : ''} #{target}} status = $?.exitstatus FileUtils.rm_f target diff --git a/ext/jasmine-webkit-specrunner/specrunner.cpp b/ext/jasmine-webkit-specrunner/specrunner.cpp index 97f8b23..203320d 100644 --- a/ext/jasmine-webkit-specrunner/specrunner.cpp +++ b/ext/jasmine-webkit-specrunner/specrunner.cpp @@ -49,6 +49,7 @@ class HeadlessSpecRunner: public QObject public: HeadlessSpecRunner(); void load(const QString &spec); + void setColors(bool colors); public slots: void log(const QString &msg); void specLog(int indent, const QString &msg, const QString &clazz); @@ -64,6 +65,12 @@ private slots: int m_runs; bool hasErrors; bool usedConsole; + bool showColors; + + void red(); + void green(); + void yellow(); + void clear(); }; HeadlessSpecRunner::HeadlessSpecRunner() @@ -71,6 +78,7 @@ HeadlessSpecRunner::HeadlessSpecRunner() , m_runs(0) , hasErrors(false) , usedConsole(false) + , showColors(false) { m_page.settings()->enablePersistentStorage(); connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(watch(bool))); @@ -101,9 +109,36 @@ bool HeadlessSpecRunner::hasElement(const char *select) return !m_page.mainFrame()->findFirstElement(select).isNull(); } +void HeadlessSpecRunner::setColors(bool colors) +{ + showColors = colors; +} + +void HeadlessSpecRunner::red() +{ + if (showColors) std::cout << "\033[0;31m"; +} + +void HeadlessSpecRunner::green() +{ + if (showColors) std::cout << "\033[0;32m"; +} + +void HeadlessSpecRunner::yellow() +{ + if (showColors) std::cout << "\033[0;33m"; +} + +void HeadlessSpecRunner::clear() +{ + if (showColors) std::cout << "\033[m"; +} + void HeadlessSpecRunner::errorLog(const QString &msg, int lineNumber, const QString &sourceID) { - std::cout << "\033[0;31m" << "[error] " << "\033[m;"; + red(); + std::cout << "[error] "; + clear(); std::cout << qPrintable(sourceID) << ":" << lineNumber << " : " << qPrintable(msg); std::cout << std::endl; @@ -115,7 +150,9 @@ void HeadlessSpecRunner::errorLog(const QString &msg, int lineNumber, const QStr void HeadlessSpecRunner::log(const QString &msg) { usedConsole = true; - std::cout << "\033[0;32m" << "[console] " << "\033[m"; + green(); + std::cout << "[console] "; + clear(); std::cout << qPrintable(msg); std::cout << std::endl; } @@ -125,10 +162,12 @@ void HeadlessSpecRunner::specLog(int indent, const QString &msg, const QString & for (int i = 0; i < indent; ++i) std::cout << " "; if ( clazz.endsWith("fail") ) { - std::cout << "\033[0;31m" << qPrintable(msg) << "\033[m"; + red(); } else { - std::cout << "\033[0;33m" << qPrintable(msg) << "\033[m"; + green(); } + std::cout << qPrintable(msg); + clear(); std::cout << std::endl; } @@ -162,16 +201,21 @@ void HeadlessSpecRunner::timerEvent(QTimerEvent *event) if (hasElement(".runner.passed")) { QWebElement desc = m_page.mainFrame()->findFirstElement(".description"); - std::cout << "\033[0;32m" << "PASS: " << qPrintable(desc.toPlainText()) << "\033[m" << std::endl; + green(); + std::cout << "PASS: " << qPrintable(desc.toPlainText()); + clear(); + std::cout << std::endl; QApplication::instance()->exit(usedConsole ? 2 : 0); return; } if (hasElement(".runner.failed")) { QWebElement desc = m_page.mainFrame()->findFirstElement(".description"); - std::cout << "\033[0;31m" << "FAIL: " << qPrintable(desc.toPlainText()) << "\033[m" << std::endl; + red(); + std::cout << "FAIL: " << qPrintable(desc.toPlainText()); + clear(); + std::cout << std::endl; m_page.mainFrame()->evaluateJavaScript(DUMP_MSG); - //QDesktopServices::openUrl(m_page.mainFrame()->url()); QApplication::instance()->exit(1); return; } @@ -187,16 +231,37 @@ void HeadlessSpecRunner::timerEvent(QTimerEvent *event) int main(int argc, char** argv) { - if (argc != 2) { + bool showColors = false; + char *filename = NULL; + + int c, index; + + while ((c = getopt(argc, argv, "c")) != -1) { + switch(c) { + case 'c': + showColors = true; + break; + } + } + + bool filenameFound = false; + + for (index = optind; index < argc; index++) { + filename = argv[index]; + filenameFound = true; + } + + if (!filenameFound) { std::cerr << "Run Jasmine's SpecRunner headlessly" << std::endl << std::endl; - std::cerr << " specrunner SpecRunner.html" << std::endl; + std::cerr << " specrunner [-c] SpecRunner.html" << std::endl; return 1; } QApplication app(argc, argv); HeadlessSpecRunner runner; - runner.load(QString::fromLocal8Bit(argv[1])); + runner.setColors(showColors); + runner.load(QString::fromLocal8Bit(filename)); return app.exec(); }