diff --git a/lib/hanami/action.rb b/lib/hanami/action.rb index af842582..73ef78e1 100644 --- a/lib/hanami/action.rb +++ b/lib/hanami/action.rb @@ -67,7 +67,11 @@ def self.gem_loader cookie_options.to_h.compact } setting :root_directory, constructor: -> (dir) { - Pathname(File.expand_path(dir || Dir.pwd)).realpath + if Hanami.respond_to?(:app) && Hanami.app + Hanami.app.root + else + Pathname(File.expand_path(dir || Dir.pwd)).realpath + end } setting :public_directory, default: Config::DEFAULT_PUBLIC_DIRECTORY setting :before_callbacks, default: Utils::Callbacks::Chain.new, mutable: true diff --git a/spec/unit/hanami/action/config_spec.rb b/spec/unit/hanami/action/config_spec.rb index 3d2b187f..743865e5 100644 --- a/spec/unit/hanami/action/config_spec.rb +++ b/spec/unit/hanami/action/config_spec.rb @@ -95,6 +95,22 @@ config.root_directory = "/non-existent" }.to raise_error(StandardError) end + + 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(:root_directory) { Pathname.new(__dir__).join("spec") } + + it "returns the Hanami app root" do + expect(config.root_directory).to eq(root_directory) + end + end end describe "public_directory" do