# Copyright (c) 2010 Cory Ondrejka. All rights reserved. # See MIT.LICENSE for licensing details. module Jasnode class Spec def self.find_specs(root_dir) dir = File.join(root_dir, "**", "*_spec.js") files = Dir[dir] dir = File.join(root_dir, "**", "spec.js") files += Dir[dir] dir = File.join(root_dir, "**", "spec_*.js") files += Dir[dir] files.uniq end def self.find_jasmine dir = File.join(File.dirname(__FILE__),"..","..","jasmine","**","jasmine-*.js") Dir[dir] end def self.build_jasmine_test_file(jasmine, specs, verbose) spec_list = "" specs.each do |spec| spec_list += " process.compile(fs.readFileSync('#{spec}'), '#{spec}');\n" end <<-eos // This file is autogenerated by jasnode // It was inspired by node-jasmine (c) 2010 Adam Abrons and Misko Hevery http://getangular.com // Copyright (c) 2010 Cory Ondrejka. All rights reserved. // See MIT.LICENSE for licensing details. var fs = require('fs'); var sys = require('sys'); var filename = '#{jasmine}'; var src = fs.readFileSync(filename); // give jasmine values to bind global.window = { setTimeout: setTimeout, clearTimeout: clearTimeout, setInterval: setInterval, clearInterval: clearInterval }; var jasmine = process.compile(src + '\\njasmine;', filename); delete global.window; jasmine.run_specs = function() { var log = []; var columnCounter = 0; var verbose = #{verbose}; // process in specs collected by jasnode #{spec_list} // override jasmine reporter base class var jasmineEnv = jasmine.getEnv(); jasmineEnv.reporter = { log: function(str){ }, reportRunnerStarting: function(runner) { date = new Date(); sys.puts('Starting spec run at ' + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()); start = date.getTime(); }, reportSuiteResults: function(suite) { var spec_results = suite.results(); var path = []; while(suite) { path.unshift(suite.description); suite = suite.parentSuite; } var description = path.join(' -> '); spec_results.items_.forEach(function(spec){ if (spec.failedCount > 0 && spec.description) { log.push(description + '\\n'); log.push(' it ' + spec.description + '\\n'); spec.items_.forEach(function(result){ log.push(' ' + result.trace.stack + '\\n'); }); } }); }, reportSpecResults: function(spec) { sys.print(spec.results().failedCount ? "F" : "."); if (columnCounter++ < 50) return; columnCounter = 0; sys.print('\\n'); }, reportRunnerResults: function(runner) { elapsed = (Number(new Date) - start) / 1000; sys.puts('\\n'); if (verbose) log.forEach(function(log){ sys.puts(log); }); sys.puts('\\n'); sys.puts('Finished in ' + elapsed + ' seconds'); sys.puts(jasmine.results(runner)); process.exit(runner.results().failedCount); } }; jasmineEnv.execute(); }; // print out the results jasmine.results = function(runner){ var results = runner.results(); var suites = runner.suites(); var msg = ''; msg += "\\033[1m" + results.totalCount + ' example' + ((results.totalCount === 1) ? '' : 's') + '\\033[22m, '; if (0 == results.failedCount) msg += "\\033[32m" + results.failedCount + ' failure' + ((results.failedCount === 1) ? '' : 's') + '\\033[39m\\n'; else msg += "\\033[31m" + results.failedCount + ' failure' + ((results.failedCount === 1) ? '' : 's') + '\\033[39m\\n'; return msg; }; // create a busy wait to allow for async testing jasmine.async_spec_wait = function() { var wait = jasmine.async_spec_wait; wait.start = new Date().getTime(); wait.done = false; (function inner_wait(){ waits(10); runs(function() { if (wait.start + wait.timeout < new Date().getTime()) { expect('timeout waiting for spec').toBeNull(); } else if (wait.done) { wait.done = false; } else { inner_wait(); } }); })(); }; // timeout after 10 seconds of waiting jasmine.async_spec_wait.timeout = 10 * 1000; jasmine.async_spec_done = function() { jasmine.async_spec_wait.done = true; }; // make sure all the jasmine methods are available for ( var key in jasmine) { exports[key] = jasmine[key]; } eos end def self.spec(verbose, logfile) File.open(File.join("lib", "jasnode.js"), "w") {|f| f.write(Jasnode::Spec.build_jasmine_test_file(Jasnode::Spec.find_jasmine, Jasnode::Spec.find_specs(Dir.pwd), true)) } Dir.chdir("spec") output = "" if logfile output = %x[node runner.js > #{logfile}] else putput = puts %x[node runner.js] end Dir.chdir("..") output end end class Init def self.template(projectname) require "ftools" template_base = File.join(File.dirname(__FILE__), '..', '..', 'templates') File.makedirs("#{projectname}") FileUtils.cp_r("#{template_base}/.", "#{projectname}") end end end