Skip to content

Commit

Permalink
Add tests for route injecting task
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Aug 4, 2024
1 parent 4455010 commit d506835
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/alchemy/install/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module Alchemy
module Install
class Tasks < Thor
SENTINEL = /\.routes\.draw do(?:\s*\|map\|)?\s*$/

include Thor::Actions

no_tasks do
Expand All @@ -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)
Expand Down
77 changes: 77 additions & 0 deletions spec/libraries/alchemy/install/tasks_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d506835

Please sign in to comment.