Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Commit

Permalink
second part - cache spec file analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbintz committed Sep 1, 2011
1 parent fa27ee7 commit c523a1e
Show file tree
Hide file tree
Showing 29 changed files with 284 additions and 190 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
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
1 change: 1 addition & 0 deletions bin/jasmine-headless-webkit
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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
2 changes: 2 additions & 0 deletions lib/jasmine-headless-webkit.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Jasmine
module Headless
autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache'
autoload :SpecFileAnalyzer, 'jasmine/headless/spec_file_analyzer'
autoload :CacheableAction, 'jasmine/headless/cacheable_action'
end
end

Expand Down
20 changes: 2 additions & 18 deletions lib/jasmine/files_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ 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

def initialize(options = {})
@options = options
@files = DEFAULT_FILES.dup
Expand Down Expand Up @@ -56,7 +40,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 Down Expand Up @@ -105,7 +89,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
Original file line number Diff line number Diff line change
@@ -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

54 changes: 4 additions & 50 deletions lib/jasmine/headless/coffee_script_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,14 @@

module Jasmine
module Headless
class CoffeeScriptCache
class CoffeeScriptCache < CacheableAction
class << self
def enabled=(bool)
@enabled = bool
def cache_type
"coffee_script"
end

def enabled?
@enabled = true if @enabled == nil
@enabled
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 self.class.enabled?
if fresh?
File.read(cache_file)
else
result = compile
FileUtils.mkdir_p self.class.cache_dir
File.open(cache_file, 'wb') { |fh| fh.print result }
result
end
else
compile
end
end

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

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

def compile
def action
CoffeeScript.compile(File.read(file))
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jasmine/headless/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def jasmine_command(*targets)
end

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

files_list = Jasmine::FilesList.new(
:config => jasmine_config,
Expand Down
37 changes: 37 additions & 0 deletions lib/jasmine/headless/spec_file_analyzer.rb
Original file line number Diff line number Diff line change
@@ -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

1 change: 0 additions & 1 deletion spec/bin/jasmine-headless-webkit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
describe 'files' do
it 'should list all the files that will be found' do
files = %x{bin/jasmine-headless-webkit -l -j spec/jasmine/success/success.yml}
p files
$?.exitstatus.should == 0

files.lines.to_a.should include("./spec/jasmine/success/success.js\n")
Expand Down
44 changes: 0 additions & 44 deletions spec/lib/jasmine/files_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,50 +182,6 @@
end
end

describe '.get_spec_line_numbers' do
let(:line_numbers) do
described_class.get_spec_line_numbers(file)
end

context 'coffeescript' do
let(:file) do
<<-SPEC
describe 'test', ->
context 'yes', ->
it 'should do something', ->
"yes"
"PR.registerLangHandler(PR.createSimpleLexer([[\"com\",/^#[^\\n\\r]*/,null,\"#\"],[\"pln\",/^[\\t\\n\\r \\xa0]+/,null,\"\\t\\n\\r \xC2\\xa0\"],[\"str\",/^\"(?:[^\"\\\\]|\\\\[\\S\\s])*(?:\"|$)/,null,'\"']],[[\"kwd\",/^(?:ADS|AD|AUG|BZF|BZMF|CAE|CAF|CA|CCS|COM|CS|DAS|DCA|DCOM|DCS|DDOUBL|DIM|DOUBLE|DTCB|DTCF|DV|DXCH|EDRUPT|EXTEND|INCR|INDEX|NDX|INHINT|LXCH|MASK|MSK|MP|MSU|NOOP|OVSK|QXCH|RAND|READ|RELINT|RESUME|RETURN|ROR|RXOR|SQUARE|SU|TCR|TCAA|OVSK|TCF|TC|TS|WAND|WOR|WRITE|XCH|XLQ|XXALQ|ZL|ZQ|ADD|ADZ|SUB|SUZ|MPY|MPR|MPZ|DVP|COM|ABS|CLA|CLZ|LDQ|STO|STQ|ALS|LLS|LRS|TRA|TSQ|TMI|TOV|AXT|TIX|DLY|INP|OUT)\\s/,\n"
SPEC
end

it 'should get the line numbers' do
line_numbers['test'].should == [ 1 ]
line_numbers['yes'].should == [ 2 ]
line_numbers['should do something'].should == [ 3 ]
end
end

context 'javascript' do
let(:file) do
<<-SPEC
describe('test', function() {
context('yes', function() {
it('should do something', function() {
});
});
});
SPEC
end

it 'should get the line numbers' do
line_numbers['test'].should == [ 1 ]
line_numbers['yes'].should == [ 2 ]
line_numbers['should do something'].should == [ 3 ]
end
end
end

describe '#spec_file_line_numbers' do
include FakeFS::SpecHelpers

Expand Down
Loading

0 comments on commit c523a1e

Please sign in to comment.