Class: Transpec::DynamicAnalyzer
- Inherits:
-
Object
- Object
- Transpec::DynamicAnalyzer
show all
- 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.json'
- HELPER_SOURCE =
" require 'pathname'\n require 'json'\n\n module TranspecAnalysis\n @base_path = Dir.pwd\n\n def self.data\n @data ||= {}\n end\n\n def self.node_id(filename, begin_pos, end_pos)\n absolute_path = File.expand_path(filename)\n relative_path = Pathname.new(absolute_path).relative_path_from(base_pathname).to_s\n [relative_path, begin_pos, end_pos].join('_')\n end\n\n def self.base_pathname\n @base_pathname ||= Pathname.new(@base_path)\n end\n\n at_exit do\n # Use JSON rather than Marshal so that:\n # * Unknown third-party class information won't be serialized.\n # (Such objects are stored as a string.)\n # * Singleton method information won't be serialized.\n # (With Marshal.load, `singleton can't be dumped (TypeError)` will be raised.)\n path = File.join(@base_path, '\#{RESULT_FILE}')\n File.open(path, 'w') do |file|\n JSON.dump(data, file)\n end\n end\n end\n\n def \#{ANALYSIS_METHOD}(object, context, analysis_codes, filename, begin_pos, end_pos)\n pair_array = analysis_codes.map do |key, (target_type, code)|\n target = case target_type\n when :object then object\n when :context then context\n end\n\n eval_data = {}\n\n begin\n eval_data[:result] = target.instance_eval(code)\n rescue Exception => error\n eval_data[:error] = error\n end\n\n [key, eval_data]\n end\n\n object_data = Hash[pair_array]\n\n id = TranspecAnalysis.node_id(filename, begin_pos, end_pos)\n TranspecAnalysis.data[id] = object_data\n\n object\n end\n"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/transpec/dynamic_analyzer.rb', line 84
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
|
Instance Attribute Details
#project ⇒ Object
Returns the value of attribute project.
81
82
83
|
# File 'lib/transpec/dynamic_analyzer.rb', line 81
def project
@project
end
|
#rspec_command ⇒ Object
Returns the value of attribute rspec_command.
81
82
83
|
# File 'lib/transpec/dynamic_analyzer.rb', line 81
def rspec_command
@rspec_command
end
|
#silent ⇒ Object
Also known as:
silent?
Returns the value of attribute silent.
81
82
83
|
# File 'lib/transpec/dynamic_analyzer.rb', line 81
def silent
@silent
end
|
Instance Method Details
#analyze(paths = []) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/transpec/dynamic_analyzer.rb', line 104
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/transpec/dynamic_analyzer.rb', line 156
def copy_recursively(source_root, destination_root)
source_root = File.expand_path(source_root)
source_root_pathname = Pathname.new(source_root)
destination_root = File.expand_path(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
96
97
98
99
100
101
102
|
# File 'lib/transpec/dynamic_analyzer.rb', line 96
def default_rspec_command
if @project.require_bundler?
'bundle exec rspec'
else
'rspec'
end
end
|
#in_copied_project ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/transpec/dynamic_analyzer.rb', line 126
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
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/transpec/dynamic_analyzer.rb', line 142
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
|