Skip to content

Commit

Permalink
Handle Hanami::AppLoadError
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Sep 20, 2023
1 parent 8768ec7 commit 4ec3e41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
12 changes: 8 additions & 4 deletions lib/hanami/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ def self.gem_loader
cookie_options.to_h.compact
}
setting :root_directory, constructor: -> (dir) {
if Hanami.respond_to?(:app) && Hanami.app
Hanami.app.root
else
Pathname(File.expand_path(dir || Dir.pwd)).realpath
begin
if Hanami.respond_to?(:app) && Hanami.app
return Hanami.app.root
end
rescue StandardError => exception
raise unless exception.is_a?(Hanami::AppLoadError)
end

Pathname(File.expand_path(dir || Dir.pwd)).realpath
}
setting :public_directory, default: Config::DEFAULT_PUBLIC_DIRECTORY
setting :before_callbacks, default: Utils::Callbacks::Chain.new, mutable: true
Expand Down
30 changes: 25 additions & 5 deletions spec/unit/hanami/action/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,37 @@

context "within a Hanami app" do
before do
app = double("Hanami.app", root: root_directory)

allow(Hanami).to receive(:respond_to?).with(:app, true).and_return(true)
allow(Hanami).to receive(:respond_to?).with(:app).and_return(true)
allow(Hanami).to receive(:app).and_return(app)
end

let(:app) { double("Hanami.app", root: root_directory) }
let(:root_directory) { Pathname.new(__dir__).join("spec") }

it "returns the Hanami app root" do
expect(config.root_directory).to eq(root_directory)
context "when app is loaded" do
before do
allow(Hanami).to receive(:app).and_return(app)
end

it "returns the Hanami app root" do
expect(config.root_directory).to eq(root_directory)
end
end

context "when app is not loaded" do
before do
unless defined?(Hanami::AppLoadError)
module Hanami
class AppLoadError < StandardError; end
end
end

expect(Hanami).to receive(:app).and_raise(Hanami::AppLoadError)
end

it "falls back to standalone gem logic" do
expect(config.root_directory).to eq(Pathname.new(Dir.pwd))
end
end
end
end
Expand Down

0 comments on commit 4ec3e41

Please sign in to comment.