Skip to content

Commit

Permalink
Merge branch 'master' into qt-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbintz committed Sep 2, 2011
2 parents e5510f1 + b21573b commit f1318a5
Show file tree
Hide file tree
Showing 23 changed files with 422 additions and 214 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -14,3 +14,4 @@ gem 'rake', '0.8.7'
gem 'mocha', '0.9.12'
gem 'guard-jasmine-headless-webkit'
gem 'facter'

1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -9,6 +9,7 @@ RSpec::Core::RakeTask.new(:spec)

$: << File.expand_path('../lib', __FILE__)

require 'jasmine-headless-webkit'
require 'jasmine/headless/task'

Jasmine::Headless::Task.new
Expand Down
2 changes: 2 additions & 0 deletions bin/jasmine-headless-webkit
Expand Up @@ -8,6 +8,8 @@ def gem_dir
end

$:.unshift(File.join(gem_dir, 'lib'))
require 'jasmine-headless-webkit'

require 'jasmine/headless/errors'
require 'jasmine/headless/runner'
require 'jasmine/headless/options'
Expand Down
5 changes: 5 additions & 0 deletions ext/jasmine-webkit-specrunner/Makefile.dummy
@@ -0,0 +1,5 @@
build:
true
install:
true

1 change: 1 addition & 0 deletions ext/jasmine-webkit-specrunner/extconf.rb
Expand Up @@ -7,3 +7,4 @@
system %{make clean}
Qt::Qmake.make!('jasmine-headless-webkit', 'specrunner.pro')

FileUtils.cp File.expand_path('../Makefile.dummy', __FILE__), File.expand_path('../Makefile', __FILE__)
4 changes: 2 additions & 2 deletions jasmine-headless-webkit.gemspec
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "jasmine-headless-webkit/version"
require "jasmine/headless/version"

Gem::Specification.new do |s|
s.name = "jasmine-headless-webkit"
s.version = Jasmine::Headless::Webkit::VERSION
s.version = Jasmine::Headless::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["John Bintz", "Sencha Inc.", "Pivotal Labs"]
s.email = ["john@coswellproductions.com"]
Expand Down
14 changes: 12 additions & 2 deletions lib/jasmine-headless-webkit.rb
@@ -1,9 +1,19 @@
module Jasmine
autoload :FilesList, 'jasmine/files_list'
autoload :TemplateWriter, 'jasmine/template_writer'

module Headless
module Webkit
end
autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache'
autoload :SpecFileAnalyzer, 'jasmine/headless/spec_file_analyzer'
autoload :CacheableAction, 'jasmine/headless/cacheable_action'
autoload :VERSION, 'jasmine/headless/version'
autoload :Runner, 'jasmine/headless/runner'
autoload :Options, 'jasmine/headless/options'
autoload :Task, 'jasmine/headless/task'
end
end

require 'jasmine/headless/errors'

require 'jasmine/headless/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3

7 changes: 0 additions & 7 deletions lib/jasmine-headless-webkit/version.rb

This file was deleted.

79 changes: 23 additions & 56 deletions lib/jasmine/files_list.rb
@@ -1,5 +1,6 @@
require 'jasmine-core'
require 'iconv'
require 'time'

module Jasmine
class FilesList
Expand All @@ -11,21 +12,7 @@ class FilesList
File.expand_path('../../../jasmine/jasmine.headless-reporter.js', __FILE__)
]

class << self
def get_spec_line_numbers(file)
line_numbers = {}

ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
file.lines.each_with_index.each { |line, index|
line = ic.iconv(line + ' ')[0..-2]
if description = line[%r{(describe|context|it)[( ]*(["'])(.*)\2}, 3]
(line_numbers[description] ||= []) << (index + 1)
end
}

line_numbers
end
end
PLEASE_WAIT_IM_WORKING_TIME = 2

def initialize(options = {})
@options = options
Expand Down Expand Up @@ -55,7 +42,7 @@ def filtered_files_to_html
def spec_file_line_numbers
@spec_file_line_numbers ||= Hash[@spec_files.collect { |file|
if File.exist?(file)
if !(lines = self.class.get_spec_line_numbers(File.read(file))).empty?
if !(lines = Jasmine::Headless::SpecFileAnalyzer.for(file)).empty?
[ file, lines ]
end
else
Expand All @@ -66,51 +53,31 @@ def spec_file_line_numbers

private
def to_html(files)
coffeescript_run = []
alert_time = Time.now + PLEASE_WAIT_IM_WORKING_TIME

files.collect { |file|
coffeescript_run << file if (ext = File.extname(file)) == '.coffee'

output = []
if (files.last == file or ext != '.coffee') and !coffeescript_run.empty?
output << ensure_coffeescript_run!(coffeescript_run)
if alert_time && alert_time < Time.now
puts "Rebuilding cache, please wait..."
alert_time = nil
end

if ext != '.coffee'
output << case File.extname(file)
when '.js'
%{<script type="text/javascript" src="#{file}"></script>}
when '.css'
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
case File.extname(file)
when '.coffee'
begin
%{<script type="text/javascript">#{Jasmine::Headless::CoffeeScriptCache.for(file)}</script>}
rescue CoffeeScript::CompilationError => ne
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), ne.message.to_s.color(:white) ]
raise ne
rescue StandardError => e
puts "[%s] Error in compiling one of the followng: %s" % [ 'coffeescript'.color(:red), files.join(' ').color(:yellow) ]
raise e
end
when '.js'
%{<script type="text/javascript" src="#{file}"></script>}
when '.css'
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
end

output
}.flatten.reject(&:empty?)
end

def ensure_coffeescript_run!(files)
data = StringIO.new
files.each { |file| data << File.read(file) << "\n" }
data.rewind

%{<script type="text/javascript">#{CoffeeScript.compile(data)}</script>}
rescue CoffeeScript::CompilationError => e
files.each do |file|
begin
CoffeeScript.compile(fh = File.open(file))
rescue CoffeeScript::CompilationError => ne
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), ne.message.to_s.color(:white) ]
raise ne
ensure
fh.close
end
end
rescue StandardError => e
puts "[%s] Error in compiling one of the followng: %s" % [ 'coffeescript'.color(:red), files.join(' ').color(:yellow) ]
raise e
ensure
files.clear
}.flatten.compact.reject(&:empty?)
end

def spec_filter
Expand All @@ -131,7 +98,7 @@ def use_config!
@files += found_files

if searches == 'spec_files'
@spec_files = @files + spec_filter
@spec_files += spec_filter
end

@filtered_files += (if searches == 'spec_files'
Expand Down
77 changes: 77 additions & 0 deletions lib/jasmine/headless/cacheable_action.rb
@@ -0,0 +1,77 @@
module Jasmine::Headless
class CacheableAction
class << self
def enabled=(bool)
@enabled = bool
end

def enabled?
@enabled = true if @enabled == nil
@enabled
end

def cache_type
raise ArgumentError.new("No cache type defined for #{self.name}") if @cache_type == nil
@cache_type
end

def cache_type=(type)
@cache_type = type
end

def cache_dir=(dir)
@cache_dir = dir
end

def cache_dir
@cache_dir ||= '.jhw-cache'
end

def for(file)
new(file).handle
end
end

attr_reader :file

def initialize(file)
@file = file
end

def handle
if CacheableAction.enabled?
if fresh?
unserialize(File.read(cache_file))
else
result = action
FileUtils.mkdir_p File.split(cache_file).first
File.open(cache_file, 'wb') { |fh| fh.print serialize(result) }
result
end
else
action
end
end

def cache_file
@cache_file ||= File.join(self.class.cache_dir, self.class.cache_type, Digest::SHA1.hexdigest(file))
end

def fresh?
File.exist?(cache_file) && (File.mtime(file) < File.mtime(cache_file))
end

def action
raise StandardError.new("Override action")
end

def serialize(data)
data
end

def unserialize(data)
data
end
end
end

20 changes: 20 additions & 0 deletions lib/jasmine/headless/coffee_script_cache.rb
@@ -0,0 +1,20 @@
require 'coffee_script'
require 'digest/sha1'
require 'fileutils'

module Jasmine
module Headless
class CoffeeScriptCache < CacheableAction
class << self
def cache_type
"coffee_script"
end
end

def action
CoffeeScript.compile(File.read(file))
end
end
end
end

7 changes: 7 additions & 0 deletions lib/jasmine/headless/options.rb
Expand Up @@ -15,6 +15,7 @@ class Options
:report => false,
:do_list => false,
:full_run => true,
:enable_cache => true,
:files => []
}

Expand Down Expand Up @@ -42,6 +43,10 @@ def process_option(*args)
@options[:colors] = true
when '--no-colors', '-nc'
@options[:colors] = false
when '--cache'
@options[:enable_cache] = true
when '--no-cache'
@options[:enable_cache] = false
when '--keep'
@options[:remove_html_file] = false
when '--report'
Expand All @@ -67,6 +72,8 @@ def process_command_line_args
command_line_args = GetoptLong.new(
[ '--colors', '-c', GetoptLong::NO_ARGUMENT ],
[ '--no-colors', GetoptLong::NO_ARGUMENT ],
[ '--cache', GetoptLong::NO_ARGUMENT ],
[ '--no-t stcache', GetoptLong::NO_ARGUMENT ],
[ '--keep', GetoptLong::NO_ARGUMENT ],
[ '--report', GetoptLong::REQUIRED_ARGUMENT ],
[ '--jasmine-config', '-j', GetoptLong::REQUIRED_ARGUMENT ],
Expand Down
2 changes: 0 additions & 2 deletions lib/jasmine/headless/railtie.rb
@@ -1,5 +1,3 @@
require 'jasmine/headless/task'

module Jasmine
module Headless
class Railtie < Rails::Railtie
Expand Down
8 changes: 2 additions & 6 deletions lib/jasmine/headless/runner.rb
@@ -1,14 +1,8 @@
require 'jasmine/headless/errors'
require 'jasmine/headless/options'

require 'fileutils'

require 'coffee-script'
require 'rainbow'

require 'jasmine/files_list'
require 'jasmine/template_writer'

require 'yaml'

module Jasmine
Expand Down Expand Up @@ -61,6 +55,8 @@ def jasmine_command(*targets)
end

def run
Jasmine::Headless::CacheableAction.enabled = @options[:enable_cache]

files_list = Jasmine::FilesList.new(
:config => jasmine_config,
:only => @options[:files]
Expand Down
37 changes: 37 additions & 0 deletions lib/jasmine/headless/spec_file_analyzer.rb
@@ -0,0 +1,37 @@
require 'iconv'
require 'multi_json'

module Jasmine::Headless
class SpecFileAnalyzer < CacheableAction
class << self
def cache_type
"spec_file_analysis"
end
end

def action
line_numbers = {}

ic = Iconv.new('UTF-8//IGNORE', 'US-ASCII')
data = ic.iconv(File.read(file) + ' ')[0..-2]
data.force_encoding('US-ASCII') if data.respond_to?(:force_encoding)

data.lines.each_with_index.each { |line, index|
if description = line[%r{(describe|context|it)[( ]*(["'])(.*)\2}, 3]
(line_numbers[description] ||= []) << (index + 1)
end
}

line_numbers
end

def serialize(data)
MultiJson.encode(data)
end

def unserialize(data)
MultiJson.decode(data)
end
end
end

2 changes: 0 additions & 2 deletions lib/jasmine/headless/task.rb
@@ -1,5 +1,3 @@
require 'jasmine/headless/runner'

module Digest
class JasmineTest
def self.file(file)
Expand Down

0 comments on commit f1318a5

Please sign in to comment.