class Teaspoon::Instrumentation

Public Class Methods

add?(response, env) click to toggle source
# File lib/teaspoon/instrumentation.rb, line 13
def self.add?(response, env)
  executable &&                                                     # we have an executable
    env["QUERY_STRING"].to_s =~ /instrument=(1|true)/ &&            # the instrument param was provided
    response[0] == 200 &&                                           # the status is 200 (304 maybe?)
    response[1]["Content-Type"].to_s == "application/javascript" && # the format is something that we care about
    response[2].respond_to?(:source) &&                             # it looks like an asset
    !ignored?(response[2])                                          # it is not ignored
end
add_to(response, env) click to toggle source
# File lib/teaspoon/instrumentation.rb, line 8
def self.add_to(response, env)
  return response unless add?(response, env)
  Teaspoon::Instrumentation.new(response).instrumented_response
end
executable() click to toggle source
# File lib/teaspoon/instrumentation.rb, line 22
def self.executable
  return @executable if @executable_checked
  @executable_checked = true
  @executable = which("istanbul")
end
new(response) click to toggle source
# File lib/teaspoon/instrumentation.rb, line 28
def initialize(response)
  @response = response
end

Protected Class Methods

ignored?(asset) click to toggle source
# File lib/teaspoon/instrumentation.rb, line 46
def self.ignored?(asset)
  Array(Teaspoon::Coverage.configuration.ignore).any? do |ignore|
    asset.filename.match(ignore)
  end
rescue Teaspoon::UnknownCoverage
  false
end

Public Instance Methods

instrumented_response() click to toggle source
# File lib/teaspoon/instrumentation.rb, line 32
def instrumented_response
  status, headers, asset = @response
  headers, asset = [headers.clone, asset.clone]

  result = add_instrumentation(asset)

  asset.instance_variable_set(:@source, result)
  asset.instance_variable_set(:@length, headers["Content-Length"] = result.bytesize.to_s)

  [status, headers, asset]
end

Protected Instance Methods

add_instrumentation(asset) click to toggle source
# File lib/teaspoon/instrumentation.rb, line 54
def add_instrumentation(asset)
  source_path = asset.filename
  Dir.mktmpdir do |temp_path|
    input_path = File.join(temp_path, File.basename(source_path)).sub(/\.js.+/, ".js")
    File.open(input_path, "w") { |f| f.write(asset.source) }
    instrument(input_path).gsub(input_path, source_path)
  end
end
instrument(input) click to toggle source
# File lib/teaspoon/instrumentation.rb, line 63
def instrument(input)
  result = %x{#{self.class.executable} instrument --embed-source #{input.shellescape}}
  return result if $?.exitstatus == 0
  raise Teaspoon::DependencyError.new("Unable to add instrumentation to #{File.basename(input)}.")
end