From d506835071a0c63c839244c8cbacf60d0959aba8 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sun, 4 Aug 2024 15:30:26 +0200 Subject: [PATCH] Add tests for route injecting task --- lib/alchemy/install/tasks.rb | 7 +- spec/libraries/alchemy/install/tasks_spec.rb | 77 ++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 spec/libraries/alchemy/install/tasks_spec.rb diff --git a/lib/alchemy/install/tasks.rb b/lib/alchemy/install/tasks.rb index d9327c936d..d2a07e4507 100644 --- a/lib/alchemy/install/tasks.rb +++ b/lib/alchemy/install/tasks.rb @@ -5,6 +5,8 @@ module Alchemy module Install class Tasks < Thor + SENTINEL = /\.routes\.draw do(?:\s*\|map\|)?\s*$/ + include Thor::Actions no_tasks do @@ -15,8 +17,9 @@ def inject_routes(auto_accept = false) unless auto_accept mountpoint = ask("- At which path do you want to mount Alchemy CMS at?", default: mountpoint) end - sentinel = /\.routes\.draw do(?:\s*\|map\|)?\s*$/ - inject_into_file "./config/routes.rb", "\n mount Alchemy::Engine => '#{mountpoint}'\n", {after: sentinel, verbose: true} + + inject_into_file "./config/routes.rb", "\n mount Alchemy::Engine => '#{mountpoint}'\n", + {after: SENTINEL, verbose: true} end def set_primary_language(code: "en", name: "English", auto_accept: false) diff --git a/spec/libraries/alchemy/install/tasks_spec.rb b/spec/libraries/alchemy/install/tasks_spec.rb new file mode 100644 index 0000000000..c423c9f2ec --- /dev/null +++ b/spec/libraries/alchemy/install/tasks_spec.rb @@ -0,0 +1,77 @@ +require "rails_helper" +require "alchemy/install/tasks" + +RSpec.describe Alchemy::Install::Tasks do + let(:tasks) { described_class.new } + + describe "#inject_routes" do + subject { tasks.inject_routes(auto_accept) } + + before do + allow(File).to receive(:read).and_return(routes_content) + allow(tasks).to receive(:inject_into_file) + end + + context "with auto_accept" do + let(:auto_accept) { true } + + context "when routes are already mounted" do + let(:routes_content) { "mount Alchemy::Engine" } + + it "does not inject the routes" do + subject + expect(tasks).not_to have_received(:inject_into_file) + end + end + + context "when routes are not mounted" do + let(:routes_content) { "get '/home', to: 'home#index'" } + + it "injects the routes" do + subject + expect(tasks).to have_received(:inject_into_file).with( + "./config/routes.rb", + "\n mount Alchemy::Engine => '/'\n", + { + after: Alchemy::Install::Tasks::SENTINEL, + verbose: true + } + ) + end + end + end + + context "without auto_accept" do + let(:auto_accept) { false } + + before do + allow(tasks).to receive(:ask).and_return("/cms") + end + + context "when routes are already mounted" do + let(:routes_content) { "mount Alchemy::Engine" } + + it "does not inject the routes" do + subject + expect(tasks).not_to have_received(:inject_into_file) + end + end + + context "when routes are not mounted" do + let(:routes_content) { "get '/home', to: 'home#index'" } + + it "injects mountpoint into the routes" do + subject + expect(tasks).to have_received(:inject_into_file).with( + "./config/routes.rb", + "\n mount Alchemy::Engine => '/cms'\n", + { + after: Alchemy::Install::Tasks::SENTINEL, + verbose: true + } + ) + end + end + end + end +end