Create the contacts directory at boot
data/ is gitignored, so a fresh checkout had no contacts directory
and every addressbook request 500ed.
Assisted-by: GLM-5.3 via pi
diff --git a/config.ru b/config.ru
index 20261fe..e86a66d 100644
--- a/config.ru
+++ b/config.ru
@@ -4,4 +4,6 @@ require "pathname"
$LOAD_PATH.unshift(Pathname.new(__dir__) / "lib")
require "pro_tacts/web"
+ProTacts.ensure_data_directories
+
run ProTacts::Web.freeze.app
diff --git a/lib/pro_tacts.rb b/lib/pro_tacts.rb
index 31381b8..317deea 100644
--- a/lib/pro_tacts.rb
+++ b/lib/pro_tacts.rb
@@ -1,4 +1,6 @@
+require "fileutils"
+
require "pro_tacts/config"
module ProTacts
@@ -8,5 +10,12 @@ module ProTacts
end
attr_writer :config
+
+ # Called from config.ru rather than at require time, so loading the
+ # app stays side-effect free. A fresh checkout has no contacts dir;
+ # an empty address book beats a 500 on every request.
+ def ensure_data_directories
+ FileUtils.mkdir_p(config.contacts_dir)
+ end
end
end
diff --git a/test/pro_tacts/test_pro_tacts.rb b/test/pro_tacts/test_pro_tacts.rb
new file mode 100644
index 0000000..fcf002b
--- /dev/null
+++ b/test/pro_tacts/test_pro_tacts.rb
@@ -0,0 +1,32 @@
+require "test_helper"
+
+require "tmpdir"
+
+class ProTactsTest < Minitest::Test
+ def teardown
+ # Reset the swapped-in config so later tests see the default one.
+ ProTacts.config = nil
+ end
+
+ def test_ensure_data_directories_creates_a_nested_contacts_dir
+ Dir.mktmpdir do |tmp|
+ data_dir = File.join(tmp, "nested", "data")
+ ProTacts.config = ProTacts::Config.new("PRO_TACTS_DATA_DIR" => data_dir)
+
+ ProTacts.ensure_data_directories
+
+ assert_path_exists File.join(data_dir, "contacts")
+ end
+ end
+
+ def test_ensure_data_directories_is_idempotent
+ Dir.mktmpdir do |tmp|
+ data_dir = File.join(tmp, "data")
+ ProTacts.config = ProTacts::Config.new("PRO_TACTS_DATA_DIR" => data_dir)
+
+ 2.times { ProTacts.ensure_data_directories }
+
+ assert_path_exists File.join(data_dir, "contacts")
+ end
+ end
+end