-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generator for initializer generation (#2286)
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require "rails/generators/base" | ||
|
||
class SentryGenerator < ::Rails::Generators::Base | ||
class_option :dsn, type: :string, desc: "Sentry DSN" | ||
|
||
def copy_initializer_file | ||
dsn = options[:dsn] ? "'#{options[:dsn]}'" : "ENV['SENTRY_DSN']" | ||
|
||
create_file "config/initializers/sentry.rb", <<~RUBY | ||
# frozen_string_literal: true | ||
Sentry.init do |config| | ||
config.breadcrumbs_logger = [:active_support_logger] | ||
config.dsn = #{dsn} | ||
config.enable_tracing = true | ||
end | ||
RUBY | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require "fileutils" | ||
require "rails/generators/test_case" | ||
require "generators/sentry_generator" | ||
|
||
RSpec.describe SentryGenerator do | ||
include ::Rails::Generators::Testing::Behaviour | ||
include FileUtils | ||
self.destination File.expand_path('../../tmp', __dir__) | ||
self.generator_class = described_class | ||
|
||
before do | ||
prepare_destination | ||
end | ||
|
||
it "creates a initializer file" do | ||
run_generator | ||
|
||
file = File.join(destination_root, "config/initializers/sentry.rb") | ||
expect(File).to exist(file) | ||
content = File.read(file) | ||
expect(content).to include(<<~RUBY) | ||
Sentry.init do |config| | ||
config.breadcrumbs_logger = [:active_support_logger] | ||
config.dsn = ENV['SENTRY_DSN'] | ||
config.enable_tracing = true | ||
end | ||
RUBY | ||
end | ||
|
||
context "with a DSN option" do | ||
it "creates a initializer file with the DSN" do | ||
run_generator %w[--dsn foobarbaz] | ||
|
||
file = File.join(destination_root, "config/initializers/sentry.rb") | ||
expect(File).to exist(file) | ||
content = File.read(file) | ||
expect(content).to include(<<~RUBY) | ||
Sentry.init do |config| | ||
config.breadcrumbs_logger = [:active_support_logger] | ||
config.dsn = 'foobarbaz' | ||
config.enable_tracing = true | ||
end | ||
RUBY | ||
end | ||
end | ||
end |