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
- ANALYSIS_METHOD =
'transpec_analyze'
- HELPER_FILE =
'transpec_analysis_helper.rb'
- RESULT_FILE =
'transpec_analysis_result.json'
- HELPER_SOURCE =
<<-END require 'pathname' module TranspecAnalysis @base_path = Dir.pwd def self.data @data ||= {} end at_exit do # Use JSON rather than Marshal so that: # * Unknown third-party class information won't be serialized. # (Such objects are stored as a string.) # * Singleton method information won't be serialized. # (With Marshal.load, `singleton can't be dumped (TypeError)` will be raised.) require 'json' path = File.join(@base_path, '#{RESULT_FILE}') File.open(path, 'w') do |file| JSON.dump(data, file) end end end def #{ANALYSIS_METHOD}(object, context, node_id, analysis_codes) node_data = {} analysis_codes.each do |key, (target_type, code)| target = case target_type when :object then object when :context then context end begin node_data[key] = target.instance_eval(code) rescue Exception end end TranspecAnalysis.data[node_id] = node_data object end END
Instance Attribute Summary collapse
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#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
- #copy_recursively(source_root, destination_root) ⇒ Object
- #default_rspec_command ⇒ Object
- #in_copied_project ⇒ Object
-
#initialize(options = {}) ⇒ DynamicAnalyzer
constructor
A new instance of DynamicAnalyzer.
- #run_rspec(paths) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ DynamicAnalyzer
Returns a new instance of DynamicAnalyzer.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/transpec/dynamic_analyzer.rb', line 67 def initialize( = {}) @project = [:project] || Project.new @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 ⇒ Object (readonly)
Returns the value of attribute project.
64 65 66 |
# File 'lib/transpec/dynamic_analyzer.rb', line 64 def project @project end |
#rspec_command ⇒ Object (readonly)
Returns the value of attribute rspec_command.
64 65 66 |
# File 'lib/transpec/dynamic_analyzer.rb', line 64 def rspec_command @rspec_command end |
#silent ⇒ Object (readonly) Also known as: silent?
Returns the value of attribute silent.
64 65 66 |
# File 'lib/transpec/dynamic_analyzer.rb', line 64 def silent @silent end |
Instance Method Details
#analyze(paths = []) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/transpec/dynamic_analyzer.rb', line 87 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| RuntimeData.load(file) end end end |
#copy_recursively(source_root, destination_root) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/transpec/dynamic_analyzer.rb', line 139 def copy_recursively(source_root, destination_root) source_root = File.(source_root) source_root_pathname = Pathname.new(source_root) destination_root = File.(destination_root) if File.directory?(destination_root) destination_root = File.join(destination_root, File.basename(source_root)) end Find.find(source_root) do |source_path| relative_path = Pathname.new(source_path).relative_path_from(source_root_pathname).to_s destination_path = File.join(destination_root, relative_path) copy(source_path, destination_path) end end |
#default_rspec_command ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/transpec/dynamic_analyzer.rb', line 79 def default_rspec_command if project.require_bundler? 'bundle exec rspec' else 'rspec' end end |
#in_copied_project ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/transpec/dynamic_analyzer.rb', line 109 def in_copied_project return yield if @in_copied_project @in_copied_project = true Dir.mktmpdir do |tmpdir| copy_recursively(project.path, tmpdir) copied_project_path = File.join(tmpdir, project.basename) Dir.chdir(copied_project_path) do yield end end ensure @in_copied_project = false end |
#run_rspec(paths) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/transpec/dynamic_analyzer.rb', line 125 def run_rspec(paths) project.with_bundler_clean_env do ENV['SPEC_OPTS'] = ['-r', "./#{HELPER_FILE}"].shelljoin command = "#{rspec_command} #{paths.shelljoin}" if silent? `#{command} 2> /dev/null` else system(command) end end end |