Skip to content

Commit 0627714

Browse files
CopilotGrantBirki
andcommitted
fix: ensure app is built before accessing global components in integration test
Co-authored-by: GrantBirki <23362539+GrantBirki@users.noreply.github.com>
1 parent bd1f331 commit 0627714

File tree

10 files changed

+95
-92
lines changed

10 files changed

+95
-92
lines changed

lib/hooks/plugins/instruments/failbot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ def warning(message, context = {})
4040
end
4141
end
4242
end
43-
end
43+
end

lib/hooks/plugins/instruments/failbot_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ def capture(context = {})
6363
end
6464
end
6565
end
66-
end
66+
end

lib/hooks/plugins/instruments/stats.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def timing(metric_name, duration, tags = {})
4242
end
4343
end
4444
end
45-
end
45+
end

lib/hooks/plugins/instruments/stats_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ def measure(metric_name, tags = {})
6767
end
6868
end
6969
end
70-
end
70+
end

spec/integration/global_lifecycle_hooks_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ def report(error_or_message, context = {})
142142
end
143143
end
144144

145+
# Ensure the app is built and plugins are loaded before accessing global components
146+
app
147+
145148
original_stats = Hooks::Core::GlobalComponents.stats
146149
original_failbot = Hooks::Core::GlobalComponents.failbot
147150

spec/unit/lib/hooks/core/plugin_loader_instruments_spec.rb

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
})
3030
end
3131

32-
describe ".load_custom_instrument_plugins" do
33-
it "loads custom stats instrument plugins" do
34-
# Create a custom stats plugin file
35-
custom_stats_content = <<~RUBY
32+
describe ".load_custom_instrument_plugins" do
33+
it "loads custom stats instrument plugins" do
34+
# Create a custom stats plugin file
35+
custom_stats_content = <<~RUBY
3636
class CustomStats < Hooks::Plugins::Instruments::StatsBase
3737
def record(metric_name, value, tags = {})
3838
# Custom implementation
@@ -48,17 +48,17 @@ def timing(metric_name, duration, tags = {})
4848
end
4949
RUBY
5050

51-
File.write(File.join(test_plugin_dir, "custom_stats.rb"), custom_stats_content)
51+
File.write(File.join(test_plugin_dir, "custom_stats.rb"), custom_stats_content)
5252

53-
expect { described_class.send(:load_custom_instrument_plugins, test_plugin_dir) }.not_to raise_error
53+
expect { described_class.send(:load_custom_instrument_plugins, test_plugin_dir) }.not_to raise_error
5454

55-
# Verify the stats plugin was loaded
56-
expect(described_class.instrument_plugins[:stats]).to be_a(CustomStats)
57-
end
55+
# Verify the stats plugin was loaded
56+
expect(described_class.instrument_plugins[:stats]).to be_a(CustomStats)
57+
end
5858

59-
it "loads custom failbot instrument plugins" do
60-
# Create a custom failbot plugin file
61-
custom_failbot_content = <<~RUBY
59+
it "loads custom failbot instrument plugins" do
60+
# Create a custom failbot plugin file
61+
custom_failbot_content = <<~RUBY
6262
class CustomFailbot < Hooks::Plugins::Instruments::FailbotBase
6363
def report(error_or_message, context = {})
6464
# Custom implementation
@@ -74,114 +74,114 @@ def warning(message, context = {})
7474
end
7575
RUBY
7676

77-
File.write(File.join(test_plugin_dir, "custom_failbot.rb"), custom_failbot_content)
77+
File.write(File.join(test_plugin_dir, "custom_failbot.rb"), custom_failbot_content)
7878

79-
expect { described_class.send(:load_custom_instrument_plugins, test_plugin_dir) }.not_to raise_error
79+
expect { described_class.send(:load_custom_instrument_plugins, test_plugin_dir) }.not_to raise_error
8080

81-
# Verify the failbot plugin was loaded
82-
expect(described_class.instrument_plugins[:failbot]).to be_a(CustomFailbot)
83-
end
81+
# Verify the failbot plugin was loaded
82+
expect(described_class.instrument_plugins[:failbot]).to be_a(CustomFailbot)
83+
end
8484

85-
it "raises error for invalid inheritance" do
86-
# Create an invalid plugin file that doesn't inherit from base classes
87-
invalid_content = <<~RUBY
85+
it "raises error for invalid inheritance" do
86+
# Create an invalid plugin file that doesn't inherit from base classes
87+
invalid_content = <<~RUBY
8888
class InvalidInstrument
8989
def some_method
9090
# This doesn't inherit from the right base class
9191
end
9292
end
9393
RUBY
9494

95-
File.write(File.join(test_plugin_dir, "invalid_instrument.rb"), invalid_content)
95+
File.write(File.join(test_plugin_dir, "invalid_instrument.rb"), invalid_content)
9696

97-
expect do
98-
described_class.send(:load_custom_instrument_plugins, test_plugin_dir)
99-
end.to raise_error(StandardError, /must inherit from StatsBase or FailbotBase/)
100-
end
97+
expect do
98+
described_class.send(:load_custom_instrument_plugins, test_plugin_dir)
99+
end.to raise_error(StandardError, /must inherit from StatsBase or FailbotBase/)
100+
end
101101

102-
it "validates class names for security" do
103-
malicious_content = <<~RUBY
102+
it "validates class names for security" do
103+
malicious_content = <<~RUBY
104104
class File < Hooks::Plugins::Instruments::StatsBase
105105
def record(metric_name, value, tags = {})
106106
# Malicious implementation
107107
end
108108
end
109109
RUBY
110110

111-
File.write(File.join(test_plugin_dir, "file.rb"), malicious_content)
112-
113-
expect do
114-
described_class.send(:load_custom_instrument_plugins, test_plugin_dir)
115-
end.to raise_error(StandardError, /Invalid instrument plugin class name/)
116-
end
117-
end
118-
119-
describe ".get_instrument_plugin" do
120-
before do
121-
# Load default instruments
122-
described_class.send(:load_default_instruments)
123-
end
124-
125-
it "returns the stats instrument" do
126-
stats = described_class.get_instrument_plugin(:stats)
127-
expect(stats).to be_a(Hooks::Plugins::Instruments::Stats)
128-
end
111+
File.write(File.join(test_plugin_dir, "file.rb"), malicious_content)
129112

130-
it "returns the failbot instrument" do
131-
failbot = described_class.get_instrument_plugin(:failbot)
132-
expect(failbot).to be_a(Hooks::Plugins::Instruments::Failbot)
113+
expect do
114+
described_class.send(:load_custom_instrument_plugins, test_plugin_dir)
115+
end.to raise_error(StandardError, /Invalid instrument plugin class name/)
116+
end
133117
end
134118

135-
it "raises error for unknown instrument type" do
136-
expect do
137-
described_class.get_instrument_plugin(:unknown)
138-
end.to raise_error(StandardError, "Instrument plugin 'unknown' not found")
119+
describe ".get_instrument_plugin" do
120+
before do
121+
# Load default instruments
122+
described_class.send(:load_default_instruments)
123+
end
124+
125+
it "returns the stats instrument" do
126+
stats = described_class.get_instrument_plugin(:stats)
127+
expect(stats).to be_a(Hooks::Plugins::Instruments::Stats)
128+
end
129+
130+
it "returns the failbot instrument" do
131+
failbot = described_class.get_instrument_plugin(:failbot)
132+
expect(failbot).to be_a(Hooks::Plugins::Instruments::Failbot)
133+
end
134+
135+
it "raises error for unknown instrument type" do
136+
expect do
137+
described_class.get_instrument_plugin(:unknown)
138+
end.to raise_error(StandardError, "Instrument plugin 'unknown' not found")
139+
end
139140
end
140-
end
141141

142-
describe ".load_default_instruments" do
143-
it "loads default stats and failbot instances" do
144-
described_class.send(:load_default_instruments)
142+
describe ".load_default_instruments" do
143+
it "loads default stats and failbot instances" do
144+
described_class.send(:load_default_instruments)
145145

146-
expect(described_class.instrument_plugins[:stats]).to be_a(Hooks::Plugins::Instruments::Stats)
147-
expect(described_class.instrument_plugins[:failbot]).to be_a(Hooks::Plugins::Instruments::Failbot)
148-
end
146+
expect(described_class.instrument_plugins[:stats]).to be_a(Hooks::Plugins::Instruments::Stats)
147+
expect(described_class.instrument_plugins[:failbot]).to be_a(Hooks::Plugins::Instruments::Failbot)
148+
end
149149

150-
it "doesn't override custom instruments if already loaded" do
151-
# Create custom stats
152-
custom_stats_content = <<~RUBY
150+
it "doesn't override custom instruments if already loaded" do
151+
# Create custom stats
152+
custom_stats_content = <<~RUBY
153153
class MyCustomStats < Hooks::Plugins::Instruments::StatsBase
154154
def record(metric_name, value, tags = {})
155155
# Custom implementation
156156
end
157157
end
158158
RUBY
159159

160-
File.write(File.join(test_plugin_dir, "my_custom_stats.rb"), custom_stats_content)
161-
described_class.send(:load_custom_instrument_plugins, test_plugin_dir)
160+
File.write(File.join(test_plugin_dir, "my_custom_stats.rb"), custom_stats_content)
161+
described_class.send(:load_custom_instrument_plugins, test_plugin_dir)
162162

163-
# Load defaults
164-
described_class.send(:load_default_instruments)
163+
# Load defaults
164+
described_class.send(:load_default_instruments)
165165

166-
# Should still have custom stats, but default failbot
167-
expect(described_class.instrument_plugins[:stats]).to be_a(MyCustomStats)
168-
expect(described_class.instrument_plugins[:failbot]).to be_a(Hooks::Plugins::Instruments::Failbot)
166+
# Should still have custom stats, but default failbot
167+
expect(described_class.instrument_plugins[:stats]).to be_a(MyCustomStats)
168+
expect(described_class.instrument_plugins[:failbot]).to be_a(Hooks::Plugins::Instruments::Failbot)
169+
end
169170
end
170-
end
171171

172-
describe ".valid_instrument_class_name?" do
173-
it "accepts valid class names" do
174-
expect(described_class.send(:valid_instrument_class_name?, "CustomStats")).to be true
175-
expect(described_class.send(:valid_instrument_class_name?, "MyCustomFailbot")).to be true
176-
expect(described_class.send(:valid_instrument_class_name?, "DatadogStats")).to be true
172+
describe ".valid_instrument_class_name?" do
173+
it "accepts valid class names" do
174+
expect(described_class.send(:valid_instrument_class_name?, "CustomStats")).to be true
175+
expect(described_class.send(:valid_instrument_class_name?, "MyCustomFailbot")).to be true
176+
expect(described_class.send(:valid_instrument_class_name?, "DatadogStats")).to be true
177+
end
178+
179+
it "rejects invalid class names" do
180+
expect(described_class.send(:valid_instrument_class_name?, "")).to be false
181+
expect(described_class.send(:valid_instrument_class_name?, "lowercaseClass")).to be false
182+
expect(described_class.send(:valid_instrument_class_name?, "Class-With-Dashes")).to be false
183+
expect(described_class.send(:valid_instrument_class_name?, "File")).to be false
184+
end
177185
end
178-
179-
it "rejects invalid class names" do
180-
expect(described_class.send(:valid_instrument_class_name?, "")).to be false
181-
expect(described_class.send(:valid_instrument_class_name?, "lowercaseClass")).to be false
182-
expect(described_class.send(:valid_instrument_class_name?, "Class-With-Dashes")).to be false
183-
expect(described_class.send(:valid_instrument_class_name?, "File")).to be false
184-
end
185-
end
186186
end
187-
end
187+
end

spec/unit/lib/hooks/plugins/instruments/failbot_base_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@
5353
expect(failbot).to have_received(:report).with(error, { context: "test" })
5454
end
5555
end
56-
end
56+
end

spec/unit/lib/hooks/plugins/instruments/failbot_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@
4444
end.to raise_error(error)
4545
end
4646
end
47-
end
47+
end

spec/unit/lib/hooks/plugins/instruments/stats_base_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
expect(stats).to have_received(:timing).with("test_metric", kind_of(Numeric), { tag: "value" })
4040
end
4141
end
42-
end
42+
end

spec/unit/lib/hooks/plugins/instruments/stats_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
expect(result).to eq("block_result")
3535
end
3636
end
37-
end
37+
end

0 commit comments

Comments
 (0)