From 7f707fbd54622917327aef0f9b306fcb53ad9238 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Mon, 30 Sep 2024 10:39:54 +0930 Subject: [PATCH] Ensure auth config is inserted before final end in trestle.rb, even when improperly indented --- .../trestle/auth/install/install_generator.rb | 2 +- spec/generators/install_generator_spec.rb | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/generators/trestle/auth/install/install_generator.rb b/lib/generators/trestle/auth/install/install_generator.rb index 750febd..d87e38a 100644 --- a/lib/generators/trestle/auth/install/install_generator.rb +++ b/lib/generators/trestle/auth/install/install_generator.rb @@ -18,7 +18,7 @@ def check_trestle_installed end def insert_configuration - inject_into_file "config/initializers/trestle.rb", before: /^end/ do + inject_into_file "config/initializers/trestle.rb", before: /end(?!.*end.*)/m do format_configuration(template_content(configuration_template)) end end diff --git a/spec/generators/install_generator_spec.rb b/spec/generators/install_generator_spec.rb index 4bb0511..e4f3066 100644 --- a/spec/generators/install_generator_spec.rb +++ b/spec/generators/install_generator_spec.rb @@ -132,4 +132,29 @@ end end end + + context "when the existing Trestle configuration is improperly indented" do + let(:configuration) do + <<~EOF + Trestle.configure do |config| + config.menu do + end + end + EOF + end + + describe "the generated files" do + before do + run_generator generator_params + end + + describe "the Trestle configuration" do + subject { file("config/initializers/trestle.rb") } + + it { is_expected.to have_correct_syntax } + it { is_expected.to contain "# == Authentication Options" } + it { is_expected.not_to contain /config.menu do\n\s*# == Authentication Options/ } + end + end + end end