summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-01 23:41:55 +0200
committerPaul Buetow <paul@buetow.org>2026-03-01 23:41:55 +0200
commit9a6526a7022c1d1172c1be9b9b3545ed6e00c9e6 (patch)
tree5958efd55a2d11bbc7805b7bc539a53dc928e394
parent211e3650387bd299bfcc6d21b7230323e45217c2 (diff)
refactor: defer Config loading — add explicit Config.load! method
Loading config.toml at module eval time (during require) runs before the application's working directory is set, can slow down tests that don't use config at all, and gives no way to reload between calls. Move the load logic into Config.load! (mirrors the Options.parse! pattern added in the previous commit). The module body now just initialises @@config to {}. Call Config.load! alongside Options.parse! at the top of configure() so all entry points reload from the correct directory. Also qualify File as ::File inside the RCM module to avoid resolving it as RCM::File once the keyword files are loaded. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
-rw-r--r--lib/config.rb21
-rw-r--r--lib/dsl.rb7
2 files changed, 20 insertions, 8 deletions
diff --git a/lib/config.rb b/lib/config.rb
index 2a13ae0..fa43c4f 100644
--- a/lib/config.rb
+++ b/lib/config.rb
@@ -8,12 +8,23 @@ end
module RCM
# Configuration — config.toml is optional. If the toml gem is not installed
# or no config.toml exists, config() will raise a helpful error when called.
+ #
+ # Config is not loaded at module load time. Call Config.load! once at the
+ # application entry point (e.g. from configure) before calling config().
+ # Tests that don't use config() don't need config.toml at all.
module Config
- @@config = if TOML_AVAILABLE && File.exist?('config.toml')
- TOML.load_file('config.toml')
- else
- {}
- end
+ @@config = {}
+
+ # Load (or reload) config.toml from the current working directory.
+ # Falls back to an empty hash when the toml gem is unavailable or the
+ # file does not exist, so callers that never invoke config() are unaffected.
+ def self.load!
+ @@config = if TOML_AVAILABLE && ::File.exist?('config.toml')
+ TOML.load_file('config.toml')
+ else
+ {}
+ end
+ end
def config(key)
raise "No such config key: #{key}" unless @@config.key?(key)
diff --git a/lib/dsl.rb b/lib/dsl.rb
index 6185c98..e3f6ee2 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -71,10 +71,11 @@ module RCM
end
def configure(reset: false, &block)
- # Parse ARGV each time configure is called so that scripts which call
- # configure multiple times (or test suites that reset state) always
- # start with a fresh, consistent option set.
+ # Parse ARGV and load config.toml each time configure is called so that
+ # scripts and test suites that call configure multiple times always
+ # start from a consistent, freshly-loaded state.
RCM::Options.parse!
+ RCM::Config.load!
RCM::DSL.new(reset) do |rcm|
rcm.info('Configuring...')
rcm.instance_eval(&block)