class Transpec::DynamicAnalyzer

Constants

ANALYSIS_METHOD
ANALYSIS_MODULE
HELPER_TEMPLATE_FILE
RESULT_FILE
RUNTIME_DATA_ERROR_MESSAGE_KEY

Attributes

project[R]
rspec_command[R]
silent[R]
silent?[R]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 19
def initialize(options = {})
  @project = options[:project] || Project.new
  @rspec_command = options[:rspec_command] || default_rspec_command
  @silent = options[:silent] || false

  if block_given?
    in_copied_project do
      yield self
    end
  end
end

Public Instance Methods

analyze(paths = []) click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 39
def analyze(paths = [])
  in_copied_project do
    rewrite_specs(paths)
    put_analysis_helper
    modify_dot_rspec
    run_rspec(paths)
    load_analysis_result
  end
end
default_rspec_command() click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 31
def default_rspec_command
  if project.using_bundler?
    'bundle exec rspec'
  else
    'rspec'
  end
end

Private Instance Methods

helper_filename() click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 87
def helper_filename
  File.basename(HELPER_TEMPLATE_FILE, '.erb')
end
helper_source() click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 91
def helper_source
  erb_path = File.join(File.dirname(__FILE__), 'dynamic_analyzer', HELPER_TEMPLATE_FILE)
  erb = ERB.new(File.read(erb_path), nil)
  erb.result(binding)
end
in_copied_project() { || ... } click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 51
def in_copied_project(&block)
  return yield if @in_copied_project

  @in_copied_project = true

  Dir.mktmpdir do |tmpdir|
    copied_project_path = DirectoryCloner.copy_recursively(project.path, tmpdir)
    Dir.chdir(copied_project_path, &block)
  end
ensure
  @in_copied_project = false
end
load_analysis_result() click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 108
def load_analysis_result
  File.open(RESULT_FILE) do |file|
    RuntimeData.load(file)
  end
rescue Errno::ENOENT
  message = 'Failed running dynamic analysis. ' \
            'Transpec runs your specs in a copied project directory. ' \
            'If your project requires some special setup or commands to run specs, ' \
            'use -c/--rspec-command option.'
  raise AnalysisError, message
end
modify_dot_rspec() click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 101
def modify_dot_rspec
  filename = '.rspec'
  content = "--require ./#{helper_filename}\n"
  content << File.read(filename) if File.exist?(filename)
  File.write(filename, content)
end
put_analysis_helper() click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 97
def put_analysis_helper
  File.write(helper_filename, helper_source)
end
rewrite_specs(paths) click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 76
def rewrite_specs(paths)
  rewriter = Rewriter.new

  spec_suite = SpecSuite.new(project, paths)

  spec_suite.specs.each do |spec|
    next if spec.error
    rewriter.rewrite_file!(spec)
  end
end
run_rspec(paths) click to toggle source
# File lib/transpec/dynamic_analyzer.rb, line 64
def run_rspec(paths)
  project.with_bundler_clean_env do
    command = "#{rspec_command} #{paths.shelljoin}"

    if silent?
      `#{command} 2> /dev/null`
    else
      system(command)
    end
  end
end