Freeze inner hashes in DEFAULTS and rewrite deep_merge to build fresh
hashes with {}.merge for uncopied inner maps. Previously, inner hashes
shared objects with the frozen-looking DEFAULTS, so mutating a loaded
config silently poisoned all later loads.
66 lines
2.0 KiB
Ruby
66 lines
2.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "test_helper"
|
|
require "tmpdir"
|
|
|
|
class ConfigTest < Minitest::Test
|
|
MISSING = "/nonexistent/volumen.toml"
|
|
|
|
def test_defaults_when_file_missing
|
|
config = Volumen::Config.load(path: MISSING)
|
|
assert_equal 9090, config.port
|
|
assert_equal "en", config.language
|
|
end
|
|
|
|
def test_overrides_apply
|
|
config = Volumen::Config.load(
|
|
path: MISSING,
|
|
overrides: { host: "127.0.0.1", port: 9000, content: "/srv/posts" }
|
|
)
|
|
assert_equal "127.0.0.1", config.host
|
|
assert_equal 9000, config.port
|
|
assert_equal "/srv/posts", config.content_dir
|
|
end
|
|
|
|
def test_does_not_mutate_defaults
|
|
Volumen::Config.load(path: MISSING, overrides: { host: "10.0.0.1" })
|
|
assert_equal "0.0.0.0", Volumen::Config.load(path: MISSING).host
|
|
end
|
|
|
|
def test_does_not_mutate_default_inner_hashes
|
|
config = Volumen::Config.load(path: MISSING)
|
|
config.site["title"] = "MUTATED"
|
|
fresh = Volumen::Config.load(path: MISSING)
|
|
assert_equal "My Blog", fresh.site["title"],
|
|
"mutating a loaded config must not poison DEFAULTS for later loads"
|
|
end
|
|
|
|
def test_loads_and_merges_file
|
|
Dir.mktmpdir do |dir|
|
|
path = File.join(dir, "config.toml")
|
|
File.write(path, "[site]\ntitle = \"Custom\"\n")
|
|
config = Volumen::Config.load(path: path)
|
|
assert_equal "Custom", config.site["title"]
|
|
assert_equal 9090, config.port
|
|
end
|
|
end
|
|
|
|
def test_generate_default_writes_template
|
|
Dir.mktmpdir do |dir|
|
|
path = File.join(dir, "sub", "config.toml")
|
|
assert_equal path, Volumen::Config.generate_default(path)
|
|
assert_path_exists path
|
|
assert_equal 9090, Volumen::Config.load(path: path).port
|
|
end
|
|
end
|
|
|
|
def test_generate_default_skips_existing
|
|
Dir.mktmpdir do |dir|
|
|
path = File.join(dir, "config.toml")
|
|
File.write(path, "[server]\nport = 9999\n")
|
|
assert_nil Volumen::Config.generate_default(path)
|
|
assert_equal 9999, Volumen::Config.load(path: path).port
|
|
end
|
|
end
|
|
end
|