start basic JS instrumentation, so alpha it hurts

This commit is contained in:
John Bintz 2012-03-26 17:15:58 -04:00
parent d4b14f2036
commit e28703b047
6 changed files with 95 additions and 4 deletions

View File

@ -21,7 +21,11 @@ Flowerbox =
message = Flowerbox.messageQueue.shift()
Flowerbox.socket.send(JSON.stringify(message))
Flowerbox.done = true if url == 'results'
if url == 'results'
if __$instrument?
Flowerbox.socket.send(JSON.stringify(['instrument', __$instrument]))
Flowerbox.done = true
started: false
done: false

View File

@ -1,8 +1,9 @@
require 'flowerbox/reporter_list'
require 'flowerbox/instrumented_files_list'
module Flowerbox
class Configuration
attr_writer :reporters, :backtrace_filter
attr_writer :reporters, :backtrace_filter, :instrument_js
attr_accessor :port
attr_accessor :test_environment, :runner_environment, :bare_coffeescript
@ -27,6 +28,14 @@ module Flowerbox
@backtrace_filter ||= []
end
def instrument_files
@instrument_files ||= Flowerbox::InstrumentedFilesList.new
end
def instrument_js?
!@instrument_files.empty?
end
def test_with(what)
self.test_environment = Flowerbox::TestEnvironment.for(what)
end

View File

@ -0,0 +1,22 @@
require 'forwardable'
module Flowerbox
class InstrumentedFilesList
extend Forwardable
def_delegators :@files_list, :empty?
def initialize
@files_list = []
end
def <<(pattern)
@files_list << pattern
end
def include?(filename)
@files_list.any? { |pattern| filename[pattern] }
end
end
end

View File

@ -36,6 +36,9 @@ module Flowerbox
sprockets.find_asset(file.first, :bundle => false).body
end
def instrument(data)
end
def ensure_alive
while @count < MAX_COUNT && !finished?
@count += 1 if @timer_running
@ -92,8 +95,13 @@ module Flowerbox
end
@results
rescue ExecJS::RuntimeError => e
rescue => e
case e
when ExecJS::RuntimeError, ExecJS::ProgramError
handle_coffeescript_compilation_error(e)
else
raise e
end
end
def configured?

View File

@ -40,6 +40,12 @@ module Flowerbox
@environment = Sprockets::Environment.new
@environment.cache = cache
if Flowerbox.instrument_js?
require 'flowerbox/tilt/instrument_js'
@environment.register_postprocessor 'application/javascript', Flowerbox::Tilt::InstrumentJS
end
default_asset_paths.each { |path| @environment.append_path(path) }
@environment

View File

@ -0,0 +1,42 @@
require 'tilt'
module Flowerbox
module Tilt
class InstrumentJS < ::Tilt::Template
def prepare ; end
def evaluate(scope, locals, &block)
if Flowerbox.instrument_files.include?(file)
block_comment = false
lines = data.lines.to_a
output = [
'if (typeof __$instrument == "undefined") { __$instrument = {} }',
"__$instrument['#{file}'] = [];",
"__$instrument['#{file}'][#{lines.length - 1}] = 0;"
]
prior_comma_end = comma_end = false
lines.each_with_index do |line, index|
line.rstrip!
instrument = "__$instrument['#{file}'][#{index}] = 1;"
if line[%r{; *$}]
line = line + instrument
end
output << line
end
output.join("\n")
else
data
end
end
end
end
end