Skip to content

Commit

Permalink
Don't interact with config_hash in specs
Browse files Browse the repository at this point in the history
Let's use the public interface for how to set config options in the
specs to replicate how people use it.
  • Loading branch information
tombruijn committed Sep 26, 2024
1 parent 8d64ac3 commit a0423c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
16 changes: 8 additions & 8 deletions spec/lib/appsignal/transmitter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
describe Appsignal::Transmitter do
let(:options) { {} }
let(:config) do
build_config(:options => { :hostname => "app1.local" }, :logger => Logger.new(log))
build_config(
:options => { :hostname => "app1.local" }.merge(options),
:logger => Logger.new(log)
)
end
let(:base_uri) { "action" }
let(:log) { StringIO.new }
Expand Down Expand Up @@ -89,9 +93,7 @@

context "with ca_file_path config option set" do
context "when file does not exist" do
before do
config.config_hash[:ca_file_path] = File.join(resources_dir, "cacert.pem")
end
let(:options) { { :ca_file_path => File.join(resources_dir, "cacert.pem") } }

it "ignores the config and logs a warning" do
expect(response).to be_kind_of(Net::HTTPResponse)
Expand All @@ -102,9 +104,7 @@
end

context "when not existing file" do
before do
config.config_hash[:ca_file_path] = File.join(tmp_dir, "ca_file_that_does_not_exist")
end
let(:options) { { :ca_file_path => File.join(tmp_dir, "ca_file_that_does_not_exist") } }

it "ignores the config and logs a warning" do
expect(response).to be_kind_of(Net::HTTPResponse)
Expand All @@ -116,8 +116,8 @@

context "when not readable file" do
let(:file) { File.join(tmp_dir, "ca_file") }
let(:options) { { :ca_file_path => file } }
before do
config.config_hash[:ca_file_path] = file
File.open(file, "w") { |f| f.chmod 0o000 }
end

Expand Down
29 changes: 16 additions & 13 deletions spec/lib/appsignal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,14 @@ def on_load
end

context "when config is loaded" do
before { Appsignal.configure(:production, :root_path => project_fixture_path) }
let(:options) { {} }
before do
configure(
:env => :production,
:root_path => project_fixture_path,
:options => options
)
end

it "should initialize logging" do
Appsignal.start
Expand All @@ -395,7 +402,7 @@ def on_load
Appsignal.config[:ignore_actions] << "my action"
end
expect_frozen_error do
Appsignal.config.config_hash[:ignore_actions] << "my action"
Appsignal.config[:ignore_actions] << "my action"
end
expect_frozen_error do
Appsignal.config.config_hash.merge!(:option => :value)
Expand All @@ -410,8 +417,8 @@ def expect_frozen_error(&block)
end

context "when allocation tracking has been enabled" do
let(:options) { { :enable_allocation_tracking => true } }
before do
Appsignal.config.config_hash[:enable_allocation_tracking] = true
capture_environment_metadata_report_calls
end

Expand All @@ -426,35 +433,31 @@ def expect_frozen_error(&block)
end

context "when allocation tracking has been disabled" do
let(:options) { { :enable_allocation_tracking => false } }
before do
Appsignal.config.config_hash[:enable_allocation_tracking] = false
capture_environment_metadata_report_calls
end

it "should not install the allocation event hook" do
it "doesn't install the allocation event hook" do
expect(Appsignal::Extension).not_to receive(:install_allocation_event_hook)
Appsignal.start
expect_not_environment_metadata("ruby_allocation_tracking_enabled")
end
end

context "when minutely metrics has been enabled" do
before do
Appsignal.config.config_hash[:enable_minutely_probes] = true
end
let(:options) { { :enable_minutely_probes => true } }

it "should start minutely" do
it "starts minutely probes" do
expect(Appsignal::Probes).to receive(:start)
Appsignal.start
end
end

context "when minutely metrics has been disabled" do
before do
Appsignal.config.config_hash[:enable_minutely_probes] = false
end
let(:options) { { :enable_minutely_probes => false } }

it "should not start minutely" do
it "does not start minutely probes" do
expect(Appsignal::Probes).to_not receive(:start)
Appsignal.start
end
Expand Down
8 changes: 6 additions & 2 deletions spec/support/helpers/config_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ def build_config(
end
module_function :build_config

def start_agent(env: "production", options: {}, internal_logger: nil)
def configure(env: :default, root_path: project_fixture_path, options: {})
env = "production" if env == :default
env ||= "production"
Appsignal.configure(env, :root_path => project_fixture_path) do |config|
Appsignal.configure(env, :root_path => root_path) do |config|
options.each do |option, value|
config.send("#{option}=", value)
end
end
end

def start_agent(env: :default, options: {}, internal_logger: nil)
configure(:env => env, :options => options)
Appsignal.start
Appsignal.internal_logger = internal_logger if internal_logger
end
Expand Down

0 comments on commit a0423c5

Please sign in to comment.