Let tests assign Domus.config directly
The reader still defaults to Config.env, but tests set a temp-dir Config
instead of juggling DATABASE_URL and STORAGE_PATH.
Assisted-by: Claude Opus 4.8 via Claude Code
diff --git a/lib/domus/config.rb b/lib/domus/config.rb
index c3a7d6e..5bb1f86 100644
--- a/lib/domus/config.rb
+++ b/lib/domus/config.rb
@@ -21,12 +21,18 @@ module Domus
end
class << self
- # The process-wide config, resolved from the environment the first time
+ # The process-wide config, defaulting to the environment the first time
# it's read. Loaded code reads this single instance: Web's :static plugin
- # needs the storage path at load, and uploads are stored under it. Tests
- # point it at a temp dir by setting DATABASE_URL / STORAGE_PATH before the
- # config is first read.
+ # needs the storage path at load, and uploads are stored under it.
#: () -> Config
def config = @config ||= Config.env
+
+ # Override the config before it's first read. Tests assign a Config
+ # pointing at a temp dir; production leaves it unset and falls back to the
+ # environment.
+ #: (Config) -> void
+ def config=(config)
+ @config = config
+ end
end
end
diff --git a/sig/generated/domus/config.rbs b/sig/generated/domus/config.rbs
index 24aa58f..a87467a 100644
--- a/sig/generated/domus/config.rbs
+++ b/sig/generated/domus/config.rbs
@@ -23,11 +23,15 @@ module Domus
def uploads_root: () -> Pathname
end
- # The process-wide config, resolved from the environment the first time
+ # The process-wide config, defaulting to the environment the first time
# it's read. Loaded code reads this single instance: Web's :static plugin
- # needs the storage path at load, and uploads are stored under it. Tests
- # point it at a temp dir by setting DATABASE_URL / STORAGE_PATH before the
- # config is first read.
+ # needs the storage path at load, and uploads are stored under it.
# : () -> Config
def self.config: () -> Config
+
+ # Override the config before it's first read. Tests assign a Config
+ # pointing at a temp dir; production leaves it unset and falls back to the
+ # environment.
+ # : (Config) -> void
+ def self.config=: (Config) -> void
end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 85d0f23..8d23fdf 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -2,15 +2,18 @@ require "minitest/autorun"
require "sequel"
require "sequel/extensions/migration"
require "fileutils"
+require "pathname"
require "tmpdir"
+require "domus/config"
+
storage = Dir.mktmpdir("domus-test")
at_exit { FileUtils.rm_rf(storage) }
-# Drive config through the environment, the same way production does, before
-# anything reads Domus.config: an in-memory database and a temp storage dir.
-ENV["DATABASE_URL"] = ":memory:"
-ENV["STORAGE_PATH"] = storage
+# Point config at an in-memory database and a temp storage dir before anything
+# reads it (db.rb opens the connection at require time, Web's :static plugin
+# reads the storage path at load).
+Domus.config = Domus::Config.new(database_url: ":memory:", storage_path: Pathname(storage))
require "domus/db"
migrate_dir = File.expand_path("../db/migrate", __dir__)