Class: Transpec::DynamicAnalyzer
- Inherits:
-
Object
- Object
- Transpec::DynamicAnalyzer
- Defined in:
- lib/transpec/dynamic_analyzer.rb,
lib/transpec/dynamic_analyzer/rewriter.rb,
lib/transpec/dynamic_analyzer/runtime_data.rb
Defined Under Namespace
Classes: Rewriter, RuntimeData
Constant Summary collapse
- EVAL_TARGET_TYPES =
[:object, :context]
- ANALYSIS_METHOD =
'transpec_analysis'
- HELPER_FILE =
'transpec_analysis_helper.rb'
- RESULT_FILE =
'transpec_analysis_result.dump'
- HELPER_SOURCE =
<<-END require 'ostruct' require 'pathname' module TranspecAnalysis @base_pathname = Pathname.pwd def self.data @data ||= {} end def self.node_id(filename, begin_pos, end_pos) absolute_path = File.expand_path(filename) relative_path = Pathname.new(absolute_path).relative_path_from(@base_pathname).to_s [relative_path, begin_pos, end_pos].join('_') end at_exit do File.open('#{RESULT_FILE}', 'w') do |file| Marshal.dump(data, file) end end end def #{ANALYSIS_METHOD}(object, context, analysis_codes, filename, begin_pos, end_pos) pair_array = analysis_codes.map do |key, (target_type, code)| target = case target_type when :object then object when :context then context end eval_data = OpenStruct.new begin eval_data.result = target.instance_eval(code) rescue Exception => error eval_data.error = error end [key, eval_data] end object_data = Hash[pair_array] id = TranspecAnalysis.node_id(filename, begin_pos, end_pos) TranspecAnalysis.data[id] = object_data object end END
Instance Attribute Summary collapse
-
#project_path ⇒ Object
readonly
Returns the value of attribute project_path.
-
#rspec_command ⇒ Object
readonly
Returns the value of attribute rspec_command.
-
#silent ⇒ Object
(also: #silent?)
readonly
Returns the value of attribute silent.
Instance Method Summary collapse
- #analyze(paths = []) ⇒ Object
- #default_rspec_command ⇒ Object
- #in_copied_project ⇒ Object
-
#initialize(options = {}) ⇒ DynamicAnalyzer
constructor
A new instance of DynamicAnalyzer.
- #run_rspec(paths) ⇒ Object
- #with_bundler_clean_env ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ DynamicAnalyzer
Returns a new instance of DynamicAnalyzer.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/transpec/dynamic_analyzer.rb', line 72 def initialize( = {}) @project_path = [:project_path] || Dir.pwd @rspec_command = [:rspec_command] || default_rspec_command @silent = [:silent] || false if block_given? in_copied_project do yield self end end end |
Instance Attribute Details
#project_path ⇒ Object (readonly)
Returns the value of attribute project_path.
69 70 71 |
# File 'lib/transpec/dynamic_analyzer.rb', line 69 def project_path @project_path end |
#rspec_command ⇒ Object (readonly)
Returns the value of attribute rspec_command.
69 70 71 |
# File 'lib/transpec/dynamic_analyzer.rb', line 69 def rspec_command @rspec_command end |
#silent ⇒ Object (readonly) Also known as: silent?
Returns the value of attribute silent.
69 70 71 |
# File 'lib/transpec/dynamic_analyzer.rb', line 69 def silent @silent end |
Instance Method Details
#analyze(paths = []) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/transpec/dynamic_analyzer.rb', line 92 def analyze(paths = []) in_copied_project do rewriter = Rewriter.new FileFinder.find(paths).each do |file_path| begin rewriter.rewrite_file!(file_path) rescue Parser::SyntaxError # rubocop:disable HandleExceptions # Syntax errors will be reported in CLI with Converter. end end File.write(HELPER_FILE, HELPER_SOURCE) run_rspec(paths) File.open(RESULT_FILE) do |file| hash = Marshal.load(file) RuntimeData.new(hash) end end end |
#default_rspec_command ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/transpec/dynamic_analyzer.rb', line 84 def default_rspec_command if File.exist?('Gemfile') 'bundle exec rspec' else 'rspec' end end |
#in_copied_project ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/transpec/dynamic_analyzer.rb', line 115 def in_copied_project return yield if @in_copied_project @in_copied_project = true Dir.mktmpdir do |tmpdir| FileUtils.cp_r(@project_path, tmpdir) @copied_project_path = File.join(tmpdir, File.basename(@project_path)) Dir.chdir(@copied_project_path) do yield end end ensure @in_copied_project = false end |
#run_rspec(paths) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/transpec/dynamic_analyzer.rb', line 131 def run_rspec(paths) with_bundler_clean_env do ENV['SPEC_OPTS'] = ['-r', "./#{HELPER_FILE}"].shelljoin command = "#{rspec_command} #{paths.shelljoin}" if silent? rspec_output = `#{command} 2> /dev/null` else system(command) end unless $CHILD_STATUS.exitstatus == 0 = 'Dynamic analysis failed!' if silent? << "\n" << rspec_output end fail end end end |
#with_bundler_clean_env ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/transpec/dynamic_analyzer.rb', line 154 def with_bundler_clean_env if defined?(Bundler) Bundler.with_clean_env do # Bundler.with_clean_env cleans environment variables # which are set after bundler is loaded. yield end else yield end end |