Skip to content

Commit

Permalink
The only source to be traced is the module called from target_files.
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Apr 22, 2024
1 parent 8809591 commit 26d2376
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 13 deletions.
3 changes: 1 addition & 2 deletions lib/diver_down/trace/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ def build_trace_point
pushed = false

unless source_name.nil?
source = @definition.find_or_build_source(source_name)

# If the call stack contains a call to a module to be traced
# `@ignored_call_stack` is not nil means the call stack contains a call to a module to be ignored
unless call_stack.empty?
Expand All @@ -91,6 +89,7 @@ def build_trace_point

if caller_location
pushed = true
source = @definition.find_or_build_source(source_name)

call_stack.push(
StackContext.new(
Expand Down
114 changes: 103 additions & 11 deletions spec/diver_down/trace/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,7 @@ def trace_fixture(path, module_set: {}, target_files: nil, ignored_method_ids: [

expect(definition.to_h).to match(fill_default(
title: 'title',
sources: [
{
source_name: 'AntipollutionModule::A',
dependencies: [],
}, {
source_name: 'AntipollutionModule::B',
dependencies: [],
}, {
source_name: 'AntipollutionModule::C',
},
]
sources: []
))
end

Expand Down Expand Up @@ -667,6 +657,108 @@ def trace_fixture(path, module_set: {}, target_files: nil, ignored_method_ids: [
))
end

it 'traces wrapped in the module to be target before starting the trace' do
dir = Dir.mktmpdir
File.write(File.join(dir, 'a.rb'), <<~RUBY)
class A
def self.call
yield
end
end
RUBY

File.write(File.join(dir, 'b.rb'), <<~RUBY)
class B
def self.call
C.call
end
end
RUBY

File.write(File.join(dir, 'c.rb'), <<~RUBY)
class C
def self.call
nil
end
end
RUBY

load(File.join(dir, 'a.rb'))
load(File.join(dir, 'b.rb'))
load(File.join(dir, 'c.rb'))

tracer = DiverDown::Trace::Tracer.new(
module_set: {
modules: [A, B, C],
},
target_files: [File.join(dir, 'a.rb'), File.join(dir, 'c.rb')]
)

definition = A.call do
tracer.trace(title: 'title') do
B.call
end
end

expect(definition.to_h).to match(fill_default(
title: 'title',
sources: [
{
source_name: 'C',
},
]
))
ensure
Object.send(:remove_const, :A) if defined?(A)
Object.send(:remove_const, :B) if defined?(B)
Object.send(:remove_const, :C) if defined?(C)
end

it 'traces modules when caller_location is not matched' do
dir = Dir.mktmpdir
File.write(File.join(dir, 'a.rb'), <<~RUBY)
class A
def self.call
B.call
end
end
RUBY

File.write(File.join(dir, 'b.rb'), <<~RUBY)
class B
def self.call
nil
end
end
RUBY

load(File.join(dir, 'a.rb'))
load(File.join(dir, 'b.rb'))

tracer = DiverDown::Trace::Tracer.new(
module_set: {
modules: [A, B],
},
target_files: [File.join(dir, 'b.rb')]
)

definition = tracer.trace(title: 'title') do
A.call
end

expect(definition.to_h).to match(fill_default(
title: 'title',
sources: [
{
source_name: 'B',
},
]
))
ensure
Object.send(:remove_const, :A) if defined?(A)
Object.send(:remove_const, :B) if defined?(B)
end

# For optimize
# it 'traces tracer_deep_stack.rb fast' do
# require 'benchmark'
Expand Down

0 comments on commit 26d2376

Please sign in to comment.