Skip to content

Commit 544daff

Browse files
Merge pull request #29 from carlosdanielpohlod/fix/custom_trace_selector
fix: fix CustomTraceSelectorFilter to return true when a trace matches with the provided filters
2 parents 1f57419 + a1fd65c commit 544daff

File tree

5 files changed

+55
-76
lines changed

5 files changed

+55
-76
lines changed

lib/rails_tracepoint_stack/filter/custom_trace_selector_filter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module CustomTraceSelectorFilter
44
def is_a_trace_required_to_watch_by_the_custom_configs?(trace:)
55
return false unless RailsTracepointStack.configuration.file_path_to_filter_patterns.any?
66

7-
!filter_match_a_custom_pattern_to_be_not_ignored?(trace)
7+
filter_match_a_custom_pattern_to_be_not_ignored?(trace)
88
end
99

1010
private

spec/rails_tracepoint_stack/filter/custom_trace_selector_filter_spec.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@
1111
let(:trace) { instance_double(RailsTracepointStack::Trace, file_path: file_path) }
1212

1313
before do
14-
allow(RailsTracepointStack.configuration).to receive(:file_path_to_filter_patterns).and_return([/app\/controllers/])
14+
allow(RailsTracepointStack.configuration)
15+
.to receive(:file_path_to_filter_patterns)
16+
.and_return([/app\/controllers/])
1517
end
1618

17-
it { is_expected.to be_falsey }
19+
it { is_expected.to be_truthy }
1820
end
1921

2022
context "when the trace's file path does not match a custom pattern" do
2123
let(:file_path) { 'app/models/post.rb' }
2224
let(:trace) { instance_double(RailsTracepointStack::Trace, file_path: file_path) }
2325

2426
before do
25-
allow(RailsTracepointStack.configuration).to receive(:file_path_to_filter_patterns).and_return([/app\/controllers/])
27+
allow(RailsTracepointStack.configuration)
28+
.to receive(:file_path_to_filter_patterns)
29+
.and_return([/app\/controllers/])
2630
end
2731

28-
it { is_expected.to be_truthy }
32+
it { is_expected.to be_falsey }
2933
end
3034
end
3135
end

spec/rails_tracepoint_stack/tracer_spec.rb

+14-71
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ def dummy_method_with_params(param_1, param_2)
1313
RSpec.describe RailsTracepointStack::Tracer do
1414
let(:tracer) { RailsTracepointStack::Tracer.new }
1515

16-
context "when the log not should be ignored" do
16+
before do
17+
allow(RailsTracepointStack::Logger)
18+
.to receive(:log)
19+
20+
allow_any_instance_of(TracePoint)
21+
.to receive(:lineno).and_return(6)
22+
end
23+
24+
describe "when the log should not be ignored because not match any filter block" do
1725
before do
1826
allow(RailsTracepointStack::Filter::GemPath)
1927
.to receive(:full_gem_path)
@@ -23,71 +31,16 @@ def dummy_method_with_params(param_1, param_2)
2331
.to receive(:ruby_lib_path)
2432
.and_return('/path/to/ruby/lib')
2533

26-
allow(RailsTracepointStack::Logger).to receive(:log)
27-
2834
allow_any_instance_of(TracePoint)
2935
.to receive(:path)
3036
.and_return("/app/rails_tracepoint_stack/spec/tracer_spec.rb")
3137

32-
allow_any_instance_of(TracePoint).to receive(:lineno).and_return(6)
33-
3438
allow_any_instance_of(RailsTracepointStack::Configuration)
3539
.to receive(:log_format)
3640
.and_return(:text)
3741
end
3842

39-
context "when log format is text" do
40-
before do
41-
RailsTracepointStack.configure do |config|
42-
config.log_format = :text
43-
end
44-
end
45-
46-
it 'calls logger with correct log' do
47-
tracer.tracer.enable do
48-
Foo.new.dummy_method
49-
end
50-
51-
expect(RailsTracepointStack::Logger)
52-
.to have_received(:log)
53-
.with("called: Foo#dummy_method in /app/rails_tracepoint_stack/spec/tracer_spec.rb:6 with params: {}")
54-
end
55-
end
56-
57-
context "when log format is json" do
58-
before do
59-
RailsTracepointStack.configure do |config|
60-
config.log_format = :json
61-
end
62-
end
63-
# TODO: Extract this test to a proper place
64-
it 'calls logger with correct log with json log format' do
65-
allow_any_instance_of(RailsTracepointStack::Configuration)
66-
.to receive(:log_format)
67-
.and_return(:json)
68-
69-
tracer.tracer.enable do
70-
Foo.new.dummy_method
71-
end
72-
73-
expect(RailsTracepointStack::Logger)
74-
.to have_received(:log)
75-
.with("{\"class\":\"Foo\",\"method_name\":\"dummy_method\",\"path\":\"/app/rails_tracepoint_stack/spec/tracer_spec.rb\",\"line\":6,\"params\":{}}")
76-
end
77-
78-
# TODO: Extract this test to a proper place
79-
it 'calls logger with correct log with json log format' do
80-
allow_any_instance_of(RailsTracepointStack::Configuration).to receive(:log_format).and_return(:json)
81-
82-
tracer.tracer.enable do
83-
Foo.new.dummy_method_with_params("param_1_value", "param_2_value")
84-
end
85-
86-
expect(RailsTracepointStack::Logger)
87-
.to have_received(:log)
88-
.with("{\"class\":\"Foo\",\"method_name\":\"dummy_method_with_params\",\"path\":\"/app/rails_tracepoint_stack/spec/tracer_spec.rb\",\"line\":6,\"params\":{\"param_1\":\"param_1_value\",\"param_2\":\"param_2_value\"}}")
89-
end
90-
end
43+
include_examples "tracer success examples asserts"
9144
end
9245

9346
context "when the log should be ignored because is a gem dependency" do
@@ -104,8 +57,6 @@ def dummy_method_with_params(param_1, param_2)
10457
.to receive(:path)
10558
.and_return("/path/to/ruby/lib")
10659

107-
allow(RailsTracepointStack::Logger).to receive(:log)
108-
10960
RailsTracepointStack.configure do |config|
11061
config.log_external_sources = false
11162
end
@@ -134,8 +85,6 @@ def dummy_method_with_params(param_1, param_2)
13485
.to receive(:path)
13586
.and_return("/path/to/gem/some_file.rb")
13687

137-
allow(RailsTracepointStack::Logger).to receive(:log)
138-
13988
RailsTracepointStack.configure do |config|
14089
config.log_external_sources = false
14190
end
@@ -164,8 +113,6 @@ def dummy_method_with_params(param_1, param_2)
164113
.to receive(:path)
165114
.and_return("/another/path/to/gem/some_file.rb")
166115

167-
allow(RailsTracepointStack::Logger).to receive(:log)
168-
169116
RailsTracepointStack.configure do |config|
170117
config.log_external_sources = true
171118
end
@@ -194,8 +141,6 @@ def dummy_method_with_params(param_1, param_2)
194141
.to receive(:path)
195142
.and_return("/another/path/to/gem/some_file.rb")
196143

197-
allow(RailsTracepointStack::Logger).to receive(:log)
198-
199144
RailsTracepointStack.configure do |config|
200145
config.ignore_patterns = [/another\/path/]
201146
end
@@ -222,20 +167,18 @@ def dummy_method_with_params(param_1, param_2)
222167

223168
allow_any_instance_of(TracePoint)
224169
.to receive(:path)
225-
.and_return("/another/path/to/gem/some_file.rb")
226-
227-
allow(RailsTracepointStack::Logger).to receive(:log)
228-
170+
.and_return("/another/path/some_file.rb")
171+
229172
allow_any_instance_of(RailsTracepointStack::Trace)
230173
.to receive(:file_path)
231-
.and_return("/another/path/to/gem/some_file.rb")
174+
.and_return("/another/path/some_file.rb")
232175

233176
RailsTracepointStack.configure do |config|
234177
config.file_path_to_filter_patterns = [/another\/path/]
235178
end
236179
end
237180

238-
it 'calls logger' do
181+
it "calls logger" do
239182
tracer.tracer.enable do
240183
Foo.new.dummy_method
241184
end

spec/shared/tracer_examples.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
RSpec.shared_examples "tracer success examples asserts" do
2+
context "when log format is text" do
3+
it 'calls logger with correct log' do
4+
tracer.tracer.enable do
5+
Foo.new.dummy_method
6+
end
7+
8+
expect(RailsTracepointStack::Logger)
9+
.to have_received(:log)
10+
.with("called: Foo#dummy_method in /app/rails_tracepoint_stack/spec/tracer_spec.rb:6 with params: {}")
11+
end
12+
end
13+
14+
context "when log format is json" do
15+
before do
16+
allow(RailsTracepointStack.configuration)
17+
.to receive(:log_format)
18+
.and_return(:json)
19+
end
20+
# TODO: Extract this test to a proper place
21+
it 'calls logger with correct log with json log format' do
22+
tracer.tracer.enable do
23+
Foo.new.dummy_method
24+
end
25+
26+
expect(RailsTracepointStack::Logger)
27+
.to have_received(:log)
28+
.with("{\"class\":\"Foo\",\"method_name\":\"dummy_method\",\"path\":\"/app/rails_tracepoint_stack/spec/tracer_spec.rb\",\"line\":6,\"params\":{}}")
29+
end
30+
end
31+
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Dir[File.join(File.dirname(__FILE__), '../lib/**/*.rb')].sort.each { |file| require file }
2+
Dir[File.join(File.dirname(__FILE__), './shared/**/*.rb')].each { |f| require f }
23

34
def initialize_gem_configuration!
45
RailsTracepointStack.configuration = RailsTracepointStack::Configuration.new

0 commit comments

Comments
 (0)